/* cls_customergroupsession.js
 *
 * @category   CLS
 * @package    CustomerGroupSession
 * @author     Jordan Phillips <jordan@classyllama.com>
 * @copyright  Copyright (c) 2010 Classy Llama Studios, LLC
 */

var interval;
var countdown;
var groupSessionTimeout;

function startTimer() {
	if (countdown == 30) {
		displayTimeoutWarning();
	} else if (countdown == 0) {
		stopTimer();
		jQuery("#sessionTimeoutWarning").text("You are being logged out...");
	    setLocation(customerLogoutUrl);
	}

	interval = self.setTimeout("startTimer()", 1000);
	jQuery("#sessionTimer").html(countdown);

	countdown--;
}

function stopTimer() {
	clearTimeout(interval);
}

function resetTimer() {
	clearTimeout(interval);
	countdown = groupSessionTimeout;
	startTimer();
}

function displayTimeoutWarning() {
	jQuery("#sessionTimeoutLink").trigger('click');
}

function hideTimeoutWarning() {
    jQuery.fancybox.close();
}

$(document).observe('dom:loaded', function() {

	jQuery("#sessionTimeoutLink").fancybox({
		'autoDimensions': true,
		'centerOnScroll': true,
		'modal'			: true,
		'overlayColor'	: '#000',
		'overlayOpacity': 0.5,
		'padding'		: 2,
		'scrolling'		: 'no',
		'titleShow'		: false,
		'transitionOut'	: 'none'
	});

	//Don't start the timer unless the user is logged in
	if (groupSessionTimeout != -1) {
		resetTimer();

		jQuery("div[id!=fancybox-overlay]").click(function() {
        	hideTimeoutWarning();
			resetTimer();
		});

		//Close the box when the Close link is clicked
		jQuery("#sessionContinue").click(function() {
        	hideTimeoutWarning();
			resetTimer();
		});


		jQuery("body").mousemove(function() {
			if (jQuery("#fancybox-overlay").css("display") == "none"){
				resetTimer();
			}
		});
	}
});
