
///////////////// jQUERY /////////////////////////////////
$(document).ready(function(){
    $(function() {
        addBehaviors();
        addEventHandlers();
    });
});

function addBehaviors() {
    if (jQuery.ui) { 
        $('#list').dialog({
            autoOpen: false,
            modal: true,
            title: "Join our Mailing List"
        })
    }
    
}

function addEventHandlers() {
    /*** mailing list submit button ***/
    if (jQuery.ui) { 
            $('#btnMailingList').click(function() {
            //get variables
            var diag = $('#list');
            var email = $('#txtMailingList').val();
            var path = $(this).attr('class');

            //open dialog
            diag.dialog('open');

            //make ajax call
            $.ajax({
                type: 'POST',
                data: { txtMailingList: email},
                url: path + "mailinglist/mailinglistAjax.php",
                beforeSend: function() {
                    //add 'saving' text or notify user to enter email
                    if (email == "") {
                        diag.html("Please enter an email address.");
                        return false;
                    }
                    else {
                        diag.html("<span class='center'>Saving...</span>");
                    }                
                },
                success: function(data) {
                    //load ajax response in dialog
                    diag.html(data);
                },
                error: function() {
                    //notify user of error
                    diag.html("Unable to load data");
                }
            })
        });
    }

}// end main function



///////////////// CONFIRM SUBMIT //////////////////////////////////////////
function confirmSubmit(type) {
    var agree = confirm("Are you sure you wish to " + type + "?");
    if (agree) {
            return true;
    }
    else {
            return false;
    }
}

//////////////// SET FOCUS ON DESIGNATED ELEMENT ////////////////////////
function setFocus(elementID) {
    if (elementID != "") {
        $("#" + elementID).focus();
    }
}

///////////////// MAINTAIN SCROLL POSITION /////////////////////////////
function saveScrollCoordinates() {
	var top = document.body.scrollTop;
	if (top == 0) {
            if (window.pageYOffset) {
                top = window.pageYOffset;
            }
            else {
                top = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
            }
	}
	$('#scrollY').val(top);
}

function scrollToCoordinates() {
	var y = $('#scrollY').val();
	window.scrollTo(0, y);
}




