/* configuration settings */
$.refresh = 2500;
$.txtExpiry = 'Session Timer: ';
$.txtCleared = '<div id="cleared">Your cart has expired...<br/><br/>Please reload the page in order to continue.</div>';

/* globals */
if($.browser.msie){$.ie='_ie';}else{$.ie='';}
var count = $.remaining			/* count of timer ticks */
var idleTime = count*1000;	/* timeout in miliseconds	*/

/* on document ready */
$(function(){
	
	/* check for mobile devices */
	if(navigator.userAgent.indexOf('Android') != -1 || navigator.userAgent.indexOf('iPhone') != -1){mobileLayout();}
	
	/* initialize the idle timer if it's enabled: */
	if($.timeout && ($.remaining!=0)){$.timer = setInterval(countdown,1000);}
	
});

function countdown() {
	
	/* update the display */
	$("a#cart_timeout").html((new Date).clearTime().addSeconds(count).toString('mm:ss'));
	$("a#cart_timeout2").html($.txtExpiry+(new Date).clearTime().addSeconds(count).toString('mm:ss'));

	/* decrement the counter and color the text */
	if(count > 0){
		if(count==(idleTime/1000)){$("a#cart_timeout2").css({'color':'Green'});}
		if(count==(idleTime/2000)){$("a#cart_timeout2").css({'color':'Orange'});}
		if(count==(idleTime/6000)){$("a#cart_timeout2").css({'color':'Red'});}
		count -= 1;
	}
	else {
		/* the cart has expired */
		
		/* stop timers */
		clearInterval($.tick);
		clearInterval($.free);
		clearInterval($.timer);

		$.ajax({
			url: "seatplan_ajax.php",
			data: "mode=terminate"+$.ie,
			success:
				function(){
					if($('div.seatplan').length > 0){freeSeats();}
					if($('div#easyTooltip').length > 0){$('div#easyTooltip').fadeOut(0);}
					$('div#id_sc').html('<div id="id_sc"><div id="id_sc2"></div><div class="infoBoxNavigation">0 items</div></div>');
					$('li#ticket_count').html('<a href="/shopping_cart.php"><span>0 TICKET(S)</span></a>');
					$('div#mainContent').prepend($.txtCleared);
					$('a#cart_timeout').empty();
					
					/* remove trigger from the seats */
					$('.s').unbind('click');
				}
		});
	}
}
function mobileLayout(){
	
	/* enlarge buttons */
	$('div#sub-menu').children('a').css({'height':'20px','paddingTop':'12px','paddingBottom':'8px'});
	$('div#sub-menuSelect').children('a').css({'height':'20px','paddingTop':'12px','paddingBottom':'8px'});
	$('div#sub-menuGroup').children('a').css({'height':'20px','paddingTop':'12px','paddingBottom':'8px'});
	
}
