var countdown;

$(function() { 
	$('#tclink').click(function() {
		window.open('http://www.jaeger-lecoultre.com/html/termsAndConditions/termsConditions_en.html','','width=450, height=450, scrollbars=yes');
		return false;
	});
	$('.termsLink').click(function() {
		var popup = window.open('terms.html', 'popupTerms','width=500,height=500,left='+(window.screen.availWidth-500)/2+', top='+(window.screen.availHeight-500)/2+',resizable=1,toolbar=1,scrollbars=1,location=0,status=0,menubar=1');
		return false;
	});
	updateTimer(); // timestamp of end date/time, assuming CET is 1 hour ahead
	countdown = setInterval('updateTimer()',500);
});

var datEnding = 1303401600; // also need to change this in include file.

function updateTimer() {
	
	/**
	 * This bit will need revisiting if it's hosted outside the UK
	 * It compensates for the user being in a different timezone to the server
	 */
	
	var datToday = new Date();
	var tzOffset = (datToday.getTimezoneOffset()/60);	// timezone offset
	var datEnd = new Date(datEnding * 1000);
	var difference = datEnd.getTime() - datToday.getTime();
	
	datSeconds = parseInt(difference / 1000) +  tzOffset; //- 3600;	// add timezone offset, and remove 1 hour.
	
	var datDays = 0;
	var datHours = 0;
	var datMinutes = 0;
	var datString = '';
	
	var hasDays = false;
	var hasHours = false;
	var hasMinutes = false;

	var prevDat = datSeconds;
	if(datSeconds >= 86400) {
		datDays = Math.floor(datSeconds / 86400);
		datSeconds -= (86400 * datDays);
		datString += datDays + 'd ';
		hasDays = true;
	}
	if(datSeconds >= 3600) {
		datHours = Math.floor(datSeconds / 3600);
		datSeconds -= (3600 * datHours);
		datString += datHours + 'h ';
		hasHours = true;
	} else {
		if(hasDays) {
			datString += '0h ';
		}
	}
	if(datSeconds >= 60) {
		datMinutes = Math.floor(datSeconds / 60);
		datSeconds -= (60 * datMinutes);
		datString += datMinutes + 'm ';
		hasMinutes = true;
	} else {
		if(hasDays || hasHours) {
			datString += '0m ';
		}
	}
	
	datString += Math.floor(datSeconds) + 's';

	if(Math.floor(datSeconds) > 0 || hasDays || hasHours || hasMinutes) {
		$('#mnu_time').text(datString);
	} else {
		$('#mnu_remaining').hide();
		clearInterval(countdown);
	}
}
