/*
 *	Dealer website top bar
 *
 *	The anonymous jQuery function will add a toolbar to the top of the page when a user visits from a link on a dealer website.
 *	There are also a number of utility functions to aid in the handling of cookies. 
 */

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

$(function(){
	// check both the cookies and http referrer property
	var dealerWebsiteCookie = readCookie('JLR_fromDealerWebsite');
	var dealerWebsiteAddressCookie = readCookie('JLR_fromDealerAddress');
	var dealerWebsiteName = readCookie('JLR_fromDealerName');
	var dealerWebsiteReferrer = document.referrer.indexOf('dealer.jaguar');
	
	// if either of these returns positively, then we display the bar
	if (dealerWebsiteCookie == 'true' || dealerWebsiteReferrer != -1) {
		// check that the bar hasn't been closed forcibly. If it has, don't display it again
		if (dealerWebsiteCookie != 'false') {
			
			$('div.wrapper:eq(0)').before('<div id="toolbar-holder-block">'+
				'<p>'+sbDwsToGwsLabel+'</p>'+
				'<ul>'+
					'<li class="close"><a href="#">'+sbHideLabel+'</a></li>'+
					'<li class="return"><a href="#">'+sbReturnLabel+'</a></li>'+
					'<li class="find"><a href="'+sbDealerLocatorPath+'">'+sbFindAnotherDealerLabel+'</a></li>'+
				'</ul>'+
			'</div>');
			
			// check the dealer website name. If there is no data, then insert a default value
			dealerWebsiteName = (dealerWebsiteName == null)? "Jaguar Dealer" : dealerWebsiteName;
			// insert the dealer name into the toolbar
			$('#toolbar-holder-block span.dealer-name').html(dealerWebsiteName);
			// show toolbar
			$('#toolbar-holder-block').show();
			
			// only do the following if we have come to the page from a dealer, rather than just being in-session
			if (dealerWebsiteReferrer != -1) {
				// create cookies
				createCookie('JLR_fromDealerWebsite', 'true');
				createCookie('JLR_fromDealerWebsiteAddress', ''+document.referrer);
			}
			
			// set up actions for hide link
			$('#toolbar-holder-block li.close a').click(function(){
				$('#toolbar-holder-block').slideUp();
				createCookie('JLR_fromDealerWebsite', 'false');
				eraseCookie('JLR_fromDealerWebsiteAddress');
				return false;
			});
			
			// set up actions for return link
			$('#toolbar-holder-block li.return a').click(function(){
				eraseCookie('JLR_fromDealerWebsite');
				window.location = readCookie('JLR_fromDealerWebsiteAddress');
				return false;
			});
			
			// set up actions for find another dealer link
			$('#toolbar-holder-block li.find a').click(function(){
				eraseCookie('JLR_fromDealerWebsite');
			});
		}
	}
});
