/*
World Hotels Mobile redirect
----------------------------------------------------------------------------------------------------------
Include this file in your pages and call the javascript function mobileRedirect as instructed below.

<script src="redirect.js" type="text/javascript"></script>

USAGE:
mobileRedirect();

OR USAGE WITH JQUERY
$(document).ready(function()
{
	mobileRedirect();
});
*/

// START Mobile redirect ---------------------------------------------------------------------------------
var regDomain = 'www.pbsr.com';
var mobDomain = 'm.pbsr.com';
var allDomain = 'pbsr.com';

var myDomain = document.domain;
var myPage = document.URL;
var mobileCk = readCookie('ismobile');

var ismobile = false;
var isipad = false;

function mobileRedirect()
{
	if (navigator.userAgent.match(/iPod/i) != null ||
		navigator.userAgent.match(/iPhone/i) != null || 
		navigator.userAgent.match(/Android/i) != null ||
		navigator.userAgent.match(/webOS/i) != null || 
		navigator.userAgent.match(/IEMobile/i) != null ||
		navigator.userAgent.match(/BlackBerry/i) != null)
	{
		ismobile = true;
	}
	else if (navigator.userAgent.match(/iPad/i) != null)
	{
		isipad = true;
	}
	
	if (ismobile == true || isipad == true)
	{
		if (myPage.match(/ismobile=false/i) != null)
		{
			setCookie(false);
		}
		else if (myDomain.indexOf(regDomain) >= 0 && mobileCk == null || myDomain.indexOf(regDomain) >= 0 && mobileCk == 'true')
		{
			var newPage = myPage.replace(new RegExp(regDomain, 'gi'), mobDomain);
			setCookie(true);
			
			window.location = newPage;
		}
		else if (myDomain.indexOf(mobDomain) >= 0)
		{
			setCookie(true);
		}
	}
}

function setCookie(val)
{
	var myDate = new Date();
	myDate.setDate(myDate.getDate()+5);
	
	document.cookie = 'ismobile=' + val + ';expires=' + myDate + ';path=/;domain=' + allDomain;
}

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;
}

// END Mobile redirect ---------------------------------------------------------------------------------
