// JavaScript Document
//
// Redirect or cookie a user 

/**** INSTRUCTIONS ****

// 1. Copy this file to the base HTML directory.

// 2. ON FULL SITE PAGES IN <head>:

<script src="/redirector.js" type="text/javascript">
</script>
<script type="text/javascript">
detectMobileBrowser('path/to/mobile_page.htm');
</script>

// 3a. ON MOBILE PAGES IN <head>:

<script src="/redirector.js" type="text/javascript">
</script>

// 3b. AND IN BODY

<body onLoad="clearAbortMobileRedirect();">

// 3c. LINK FROM MOBILE TO FULL VERSION:

<a href="#" onClick="fullSiteRedirect('path/to/full_page.htm')">View on full site</a>

*/

function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x == c_name) {
			return unescape(y);
		}
	}
}

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


function detectMobileBrowser(redirPath) {
	// first check if override cookie set
	var amrVal = getCookie("abortMobileRedirect");
	if ((amrVal != null) && (amrVal != "")) {
		// Abort Mobile Redirect
	} else {
		// redirect to mobile site if screen is smaller than 1000 px, 
		// maight catch netbooks too, but hence the back2full link
		if ((screen.width < 1000) && (screen.height < 1000)) {
			window.location = redirPath;
		}
	}
}

function clearAbortMobileRedirect() {
	setCookie("abortMobileRedirect","",-10);
}


function fullSiteRedirect(redirPath) {
	setCookie("abortMobileRedirect","TRUE",180);
	window.location = redirPath;
}

