/* Author: Steve Ambielli
	Main JS file for all functions used in BERTL
*/

jQuery.noConflict();
jQuery(document).ready(function() {		
		
		jQuery.preloadImages = function() {
			for (var i = 0; i<arguments.length; i++) {
				img = new Image();
				img.src = arguments[i];
				}
		}
		jQuery.preloadImages (
			"/images/bg-nav-first-on.png",
			"/images/bg-nav-last-on.png",
			"/images/bg-nav-on.png"
		);
								
		jQuery("#nav ul li:last-child").addClass('last');
		jQuery("#nav ul li ul li:last-child").removeClass('last');
		
		//show hidden drop down nav
		jQuery('#nav ul li').hover(
			function() { 
				jQuery('ul', this).addClass('hover');
				jQuery('ul', this).css('display','block');
				jQuery('#slideshow').css('z-index','-1');
			},
			function() { 
				jQuery('ul', this).removeClass('hover');
				jQuery('ul', this).css('display','none');
				jQuery('#slideshow').css('z-index','0');
		});
		
		//add borders to each side of the buttons
		jQuery.fn.exists = function(){return jQuery(this).length>0;}
		if (jQuery('input.button').exists()) {
			jQuery('<span class="button-right">&nbsp;</span>').insertAfter('input.button');
		}
		
		//align column heights
		equalHeight(jQuery(".column"));
		//equalHeight(jQuery(".hp-height"));
		
		//init session dialog
		initializeSessionTimeout();
});

function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = jQuery(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}


//session timeout message
var idleTime = 1725000; // number of miliseconds until the user is considered idle    1725000
var sessionTimeoutCountdownId = 'dialog-countdown';  
var redirectAfter = 60; // number of seconds to wait before redirecting the user  
var redirectTo = '/logout.do?action=consumer'; // URL to relocate the user to once they have timed out  
var keepAliveURL = '/sessionExtend.jsp'; // URL to call to keep the session alive  
var expiredMessage = 'Your session has expired.  You are being logged out for security reasons.'; // message to show user when the countdown reaches 0  
var running = false; // var to check if the countdown is running  
var timer; // reference to the setInterval timer so it can be stopped

/**
 * Logout function, will prompt user if anything is in the shopping cart
 * @param logoutUrl
 * @param checkoutUrl
 * @param shoppingCartTotal
 */
function logout(logoutUrl, checkoutUrl, shoppingCartTotal) {
	
	//shopping cart is null or total is 0
	if(shoppingCartTotal == null || shoppingCartTotal == 0) {
		//proceed with check out
		window.location = logoutUrl; 
		return;
	}
	
	jQuery("#shoppingCartWarning").dialog({
		autoOpen: false,
		modal: true,
		width: 460,  
    	minHeight: 50,
		draggable: false,
		resizable: false,
		open: function() {  
         // scrollbar fix for IE  
			jQuery('body').css('overflow','hidden');  
     	},  
    	close: function() {  
        // reset overflow  
    		jQuery('body').css('overflow','auto');  
    	},  
		buttons: {
			'Checkout': function(){
				//proceed with check out
				window.location = checkoutUrl; 
			},
			'Log Out': function(){
				// fire whatever the configured onTimeout callback is.
				window.location = logoutUrl; 
			}
		}
	});
	
	//open dialog box
   	jQuery("#shoppingCartWarning").dialog('open');
}

function initializeSessionTimeout() {  
	  	// setup the dialog 
	  	jQuery("#sessionTimeoutWarning").dialog({
			autoOpen: false,
			modal: true,
			width: 460,  
        	minHeight: 50,
			draggable: false,
			resizable: false,
			open: function() {  
             // scrollbar fix for IE  
				jQuery('body').css('overflow','hidden');  
         	},  
        	close: function() {  
            // reset overflow  
        		jQuery('body').css('overflow','auto');  
        	},  
			buttons: {
				'Yes, Keep Working': function(){
					// stop the timer  
	             	clearInterval(timer);  
	               	// stop countdown  
	               	running = false;   
	               
	               	// ajax call to keep the server-side session alive  
	               	jQuery.ajax({  
	                	url: keepAliveURL, 
	                	async: false 
	              	});  
					// Just close the dialog. We pass a reference to this
					// button during the init of the script, so it'll automatically
					// resume once clicked
	               	jQuery(this).dialog('close');
				},
				'No, Logoff': function(){
					// fire whatever the configured onTimeout callback is.
					window.location = redirectTo; 
				}
			}
		});
	  
		

    // start the idle timer  
	  	jQuery.idleTimer(idleTime);  

    // bind to idleTimer's idle.idleTimer event  
	  	jQuery(document).bind("idle.idleTimer", function(){  

			// if the user is idle and a countdown isn't already running  
       	if(jQuery.data(document,'idleTimer') === 'idle' && !running){  
       		var counter = redirectAfter;  
            	running = true;   

            	// intialisze timer  
            	jQuery('#'+sessionTimeoutCountdownId).html(redirectAfter);  
		
           	// open dialog  
            jQuery(window).focus();  //set window focus
           	jQuery("#sessionTimeoutWarning").dialog('open');

             	// create a timer that runs every second  
             	timer = setInterval(function(){  
               	counter -= 1;  
                	// if the counter is 0, redirect the user  
                	if(counter === 0) {  
                		jQuery("#sessionTimeoutWarning").html(expiredMessage); 
                		jQuery("#sessionTimeoutWarning").dialog('disable');  
                     window.location = redirectTo;  
                 } else {  
              	   jQuery('#'+sessionTimeoutCountdownId).html(counter);  
                 };  
             }, 1000);  
         };  
     });  
 }
