DocumentRoot = (typeof(DocumentRoot)=="undefined" || DocumentRoot == "/") ? "" : DocumentRoot; //Base for paths

/*----------------------------------
STORE CHOOSER OVERLAY. 
Handles overlay, cookies and redirect
-----------------------------------*/

var ThisStore= "us";

var Stores = [
{store: "eu", url: "http://eu.eroscillator.com"}, 
{store: "de", url: "http://de.eroscillator.com"}, 
{store: "us", url: "http://www.eroscillator.com"}
];

var OverlayHTML = "\
<h1>Please Choose your store</h1>\
<div class='Left'>\
	<p><a href=javascript:SetStore('us')><img src='" + DocumentRoot + "/images/global/store_chooser/north_america_lg.gif'></a></p>\
	<p><strong><a href=javascript:SetStore('us')>North American store</a></strong><br />\
	For users with 110v Electricity<br />\
	<span class='Small'>(USA, Canada, Japan, Columbia, Mexico and Puerto Rico)</span>\
	</p>\
</div>\
<div class='Right'>\
	<p><a href=javascript:SetStore('eu')><img src='" + DocumentRoot + "/images/global/store_chooser/europe_lg.gif'></a></p>\
	<p><strong><a href=javascript:SetStore('eu')>European store</a></strong><br />\
		For users with 220v Electricity <br />\
		<span class='Small'>\
		(everyone else)<br />\
		<img src='" + DocumentRoot + "/images/global/store_chooser/germany_sm.gif' style='margin: 3px 3px -3px 0;'>\
		<a href=javascript:SetStore('de')>Auch verf&uuml;gbar auf Deutsch</a></span>\
	</p>\
</div>\
<div class='Clear'>\
";

function StoreChooser (){
	if (ShowStoreChooserOverlay == true) { //ShowStoreChooserOverlay is set by the server
		DisplayOverlay ();	
	}
}

function Redirect (theStore){
	for (var i=0;i< Stores.length; i++) {
		if (Stores[i].store == theStore){
			location.href= Stores[i].url + location.pathname + "?st=" + theStore;
		}
	}	
}

function GetStoreQueryVal (){
	var query = window.location.search;
	if(query.length > 1) {
		queryArray = query.substring(1, query.length).split("&"); //Removes the ? and splits the querystring
		for(var i=0; i < queryArray.length; i++) {
			if (queryArray[i].split("=")[0] == "st") return queryArray[i].split("=")[1]; //if key = st then return value
		}
	}
	return null;	
}

function SetStore (theStore){
	SetStoreCookie(theStore);
	if (ThisStore == theStore) {
		HideOverlay ();
	}
	else {
		Redirect (theStore);
	}
}

function GetStoreCookie (){
	var CookieArray = document.cookie.split(';');
	for(var i=0;i < CookieArray.length;i++) {
		var theCookie = CookieArray[i];
		while (theCookie.charAt(0)==' ') theCookie = theCookie.substring(1,theCookie.length);
		if (theCookie.indexOf('st') == 0) return theCookie.split("=")[1];
	}
	return null;
}

function SetStoreCookie(theStore) {
	var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000));
	document.cookie = "st=" + theStore + "; expires=" + date.toGMTString() + "; path=/";
}

function DisplayOverlay (){
	document.getElementById("StoreOverlayBg").style.display = "block";
	document.getElementById("StoreOverlay").innerHTML = OverlayHTML;
	document.getElementById("StoreOverlay").style.display = "block";
	PositionLayer ()
	document.documentElement.style.overflow = "hidden";
	window.onresize = PositionLayer;
}

function PositionLayer (){
	theOverlay = document.getElementById("StoreOverlay");
	theOverlay.style.left = (document.body.clientWidth / 2) - (theOverlay.clientWidth / 2) + 'px';
}

function HideOverlay (){
	document.getElementById("StoreOverlay").style.display = "none";
	document.getElementById("StoreOverlayBg").style.display = "none";
	document.documentElement.style.overflow = "auto";
	window.onresize = "";
}

/*
	tooltips
*/
function initToolTip() {
	$('.ToolTipLink').hover(showTooltip, hideTooltip);
	$('.ToolTipLink').click(function () {return false;});
	$('body').append("<div id='toolTip'></div>");
	  $(document).mousemove(function(e){
		  window.mouseXPos = e.pageX;
		  window.mouseYPos = e.pageY;
	});	
}

function showTooltip () {
	var targetID = $(this).attr("href").replace(/.+\//, ''); // Remove absolute path on IE7
	
	if (showTooltip.clearTimer) {
		window.clearTimeout(showTooltip.clearTimer);
		delete showTooltip.clearTimer;
	}
	
	if (showTooltip.current && showTooltip.current == targetID) {
		return;
	}
	else if (showTooltip.showTimer) {
		window.clearTimeout(showTooltip.showTimer);
		delete showTooltip.showTimer;
	}
	
	var theToolTip = $('#toolTip');
	showTooltip.current = targetID;
	var HTML = $(targetID).html()

	theToolTip.html($(targetID).html()).show();
	
	// Position tooltip
	var viewPortTop = $(window).scrollTop()
	var viewPortBottom = viewPortTop + $(window).height();
	var viewPortLeft = $(window).scrollLeft()
	var viewPortRight = viewPortLeft + $(window).width();
	
	if (window.mouseXPos - viewPortLeft > viewPortRight - window.mouseXPos)  {
		theToolTip.css('left',  window.mouseXPos - 20 - theToolTip.outerWidth());
	}
	else {
		theToolTip.css('left',  window.mouseXPos + 20);
	}
	
	if (window.mouseYPos - viewPortTop > viewPortBottom - window.mouseYPos)  {
		theToolTip.css('top',  window.mouseYPos - 20  - theToolTip.outerHeight());
	}
	else {
		theToolTip.css('top',  window.mouseYPos + 20);
	}
}

function hideTooltip () {
	var hideDelay = 200;
	
	showTooltip.clearTimer = window.setTimeout( _hide, hideDelay);
	
	function _hide () {
		$('#toolTip').hide();
		delete showTooltip.current;
	}
}

$(document).ready(initToolTip);
