// Check to see if the user has been informed that the Clarendon web
// site is "location aware". Mod. 16-08-2010


function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+"; path=/");
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function checkCookie(){
	locationAware = getCookie('locationAware');
	if (window.console) console.log(locationAware);
	if (locationAware != 'aware') {
		setCookie('locationAware', 'aware', 60);
		showLocations();
	}
}

function positionOverlay() {
	// Width of the element: 700px;
	var docWidth = $(window).width();
	var docHeight = $(window).height();
	var overlayWidth = 700;
	var overlayHeight = 453;

	// Set background to take up everything
	$('.location-overlay').css({
		'width' : docWidth,
		'height'  : docHeight
	});

	// Center overlay
	$('.location-select').css({
		'left' : (docWidth / 2) - (overlayWidth / 2),
		'top'  : (docHeight / 2) - (overlayHeight / 2)
	});
}

$(".location-swap li span").live('click', function(){
	var stateLocation = $(this).text();
	$("#change-location-img").attr('src', 'images/ajax-loader-small.gif');
	
	$.get(
		"http://www.clarendon.com.au/main/set_location/" + stateLocation,
		{},
		function(msg){
			$("#current_location").html('Your Location <strong>' + msg + '</strong>');
			$("#change-location-img").attr('src', 'images/down.png');
			window.location.href = window.location.href;
		}
	);
});


function showLocations() {
	var currentLocation = $('#current_location strong').text();
	// create the overlay
	$('body').append('<div class="location-overlay" onclick="closeLocationOverlay();"></div>');
	$('body').append('\
	<div class="location-select" style="display: none; color: #ccc;">\
		<div class="location-close" onclick="closeLocationOverlay();"></div>\
		<p>We have selected<br />\
		<span class="felt-tip">' + currentLocation + '</span><br />\
		as your location.</p>\
		<p>If this isn\'t where you\'re looking for please choose from the options below.</p>\
		<ul class="location-swap">\
			<li><span href="#">NSW</span></li>\
			<li><span href="#">VIC</span></li>\
			<li><span href="#">QLD</span></li>\
		</ul>\
		<p>In case you ever need to change this setting simply click the "Change Location" button at the top left of the web site:</p>\
		<p>\
		<img src="./images/location/change-location.jpg" />\
		</p>\
		<p><small>Please note that you are required to enable cookies\
		for this web site.</small></p>\
	</div>\
	');

	positionOverlay();
	Cufon.replace('.location-select .felt-tip');
	$('.location-select').fadeIn(500);

}



$(window).resize( function () { positionOverlay(); });

function closeLocationOverlay() {
	$('.location-overlay').remove();
	$('.location-select').remove();
}

$(function () {

	checkCookie();

});
