//This global variable represents the interval between individual modal views
var interval = 10000;

//This global variable represents the start time that I use to compare the time of the mouse leaving to see if it has exceeded the interval
var timer1;

//This global variable is just a boolean flag I use to prevent functions from running if the modal is already visible
var visible = false;

$(document).ready(function() {
	//IMPORTANT: first make sure the modal exists
	if ($('#TBK-NC-Modal').length>0) {
		//Get the current time to compare to the time when a user's mouse leaves the document
		var time1 = new Date();
		timer1 = time1.getTime();

		$(document).mouseleave(function() {
		
			//Get the time when the user's mouse left the doc to compare to the start time
			var time2 = new Date();
			var timer2 = time2.getTime();

			if ((timer2 - timer1) >= interval) {
				
				//check to make sure the modal isn't already visible
				if (!visible) {
					//make sure this ID matches up with the one at the top of this function!
					$('#TBK-NC-Modal').modal({ onOpen: modalOpen, onClose: simplemodal_close });
					visible = true;
				}
			}
		});
	}
});

/**
* When the open event is called, this function will be used to 'open'
* the overlay, container and data portions of the modal dialog.
*
* onOpen callbacks need to handle 'opening' the overlay, container
* and data.
*/
function modalOpen(dialog) {
    dialog.overlay.fadeIn('fast', function() {
        dialog.container.fadeIn('fast', function() {
            dialog.data.hide().slideDown('fast');
        });
    });
}

/**
* When the close event is called, this function will be used to 'close'
* the overlay, container and data portions of the modal dialog.
*
* The SimpleModal close function will still perform some actions that
* don't need to be handled here.
*
* onClose callbacks need to handle 'closing' the overlay, container
* and data.
*/
function simplemodal_close(dialog) {
    dialog.data.fadeOut('fast', function() {
        dialog.container.hide('fast', function() {
            dialog.overlay.slideUp('fast', function() {
                $.modal.close();
            });
        });
    });
    
    //Reset the start time to the time when the user closed the modal
    var now = new Date();
    timer1 = now.getTime();
    
    //Set the visibility flag to false
    visible = false;
}
