
var urlPattern =  [
	'/kaiserwasser_hotel_wien',
	'/johnf_hotel_berlin',
	'/velvet_hotel_berlin',
	'/rubin_hotel_hamburg',
	'/moserverdino_hotel_klagenfurt',
	'/nike_hotel_linz',
	'/camino_hotel_stuttgart',
	'/boltzmann_hotel_wien',
	'/wimberger_hotel_wien',
	'/allegra_hotel_zagreb',
	'arcotel',
	't3tirol',
	'buswells',
	'motel.co.uk',
	'camdencourthotel',
	'slieverussell.ie',
	'hotel-paris.cz',
	'brooklodge.com',
	'hotelhastalprague.com',
	'lougherneresort.com',
	'hotel-tronchet.com',
	'bewleyshotels.com/ballsbridge',
	'bewleyshotels.com/dublin_airport',
	'bewleyshotels.com/leeds',
	'bewleyshotels.com/leopardstown',
	'bewleyshotels.com/manchester_airport',
	'bewleyshotels.com/newlands_cross',
	'bewleyshotels.com',
	'chiswick-moran-hotel',
	'red-cow-moran-hotel',
	'silver-springs-moran-hotel',
	'crown-moran-hotel',
	'moranhotels.com',
	'glenviewhotel.com'
	
];
var baPath =  [
	'arcotelhotelkaiserwasser',
	'arcotelhoteljohnf',
	'arcotelhotelvelvet',
	'arcotelhotelrubin',
	'arcotelhotelmoserverdino',
	'arcotelhotelnike',
	'arcotelhotelcamino',
	'arcotelhotelboltzmann',
	'arcotelhotelwimberger',
	'arcotelhotelallegra',
	'arcotelhotels',
	'hotelt3tirol',
	'buswellshotel',
	'belmorecourtmotel',
	'camdencourthotel',
	'slieverussell',
	'hotelparis',
	'brooklodge',
	'hotelhastalprague',
	'lougherneresort',
	'hoteltronchet',	
	'bewleysballsbridge',
	'bewleysdublinairport',
	'bewleysleeds',
	'bewleyleopardstown',
	'bewleymanchesterairport',
	'bewleynewlandscross',
	'bewleyshotels',
	'moranhotelschiswick',
	'moranhotelsredcow',
	'moranhotelssilversprings',
	'moranhotelscrown',
	'moranhotels',
	'glenviewhotel'
];

var is_mobile = isMobile();
var mobileURL = getMobileURL();
var paramOrCookiePresent = checkForParamVal('sf','true');
if(paramOrCookiePresent) {
	Set_Cookie('sf', 'true');
}
paramOrCookiePresent = Get_Cookie('sf') == 'true' ? true : paramOrCookiePresent;
paramOrCookiePresent = Get_Cookie('SF') == 'true' ? true : paramOrCookiePresent;
//alert(' is_mobile: ' + is_mobile +  ' paramOrCookiePresent: ' + paramOrCookiePresent + ' mobileURL: ' + mobileURL);
setOnLoad();
if(is_mobile && !paramOrCookiePresent && mobileURL!='') {
	if(pageTracker == undefined) {
		var pageTracker = false;
	}
	if(_gaq == undefined) {
		var _gaq = false;
	}

	if(location.search) {
		mobileURL = mobileURL + location.search;
	}
	if(pageTracker) {
		try {
			pageTracker._initData();
			mobileURL = pageTracker._getLinkerUrl(mobileURL);
		} catch(e){} finally{}
	} else 
		if(_gaq) {
			mobileURL = _gaq.push(['_getLinkerUrl', mobileURL]);
			location.href=mobileURL;
			//alert('async google: ' + mobileURL);
	}
	location.href=mobileURL;
}
function showLinkToMobile(){
	if (is_mobile && mobileURL!='') {  
		return true;
	}
	return false;
}

function setOnLoad() {
	var oldOnload = window.onload;
	
	//safariLog('typeof(window.onload): ' + typeof(window.onload));
	
	if (typeof(window.onload) != "function") {
		window.onload = writeMobileURL;
	} else {
		window.onload = function() {
			//safariLog('setting on load with existing onload mobileURL: ' + mobileURL);
			oldOnload();
			writeMobileURL;
		}
	}
}

function writeMobileURL(){
	var elem = document.getElementById('link_to_ba_mobile');
	if(elem) {
		if (is_mobile && mobileURL!='') {
			elem.style.display ="block";
			elem.onclick = toMobileSite;
			//safariLog('mobileURL: ' + mobileURL);
		} else {
			//safariLog('display none: ');
			elem.style.display ="none";		
		}
	}
}
function toMobileSite(){
	//safariLog('toMobileSite: ' + mobileURL);
	Delete_Cookie("sf");
	location.href=mobileURL;
	return false;
}
function getMobileURL(){
	var s = top.location.href;
	for ( var i=0, len=urlPattern.length; i<len; ++i ){
		if(s.indexOf(urlPattern[i])>=0) {
			//alert(mS[property]);
			return 'http://m.bookassist.com/' +  baPath[i];
		}
	}
    return '';
}
function isMobile(){
	var agent=navigator.userAgent.toLowerCase();
	var is_iphone = (agent.indexOf('iphone')!=-1);
	var is_ipod = (agent.indexOf('ipod')!=-1);
	var is_android = (agent.indexOf('android')!=-1);
	var is_mobile = (is_iphone || is_ipod || is_android);
	//safariLog('is_mobile: ' + is_mobile);
	return is_mobile;
}
function checkForParamVal(paramName, paramVal){
	var s = '';
	if(top.location) {
		s = top.location.href;
		//alert('s: ' + s + ' regex: ' + '[&\?]'+paramName+'=([^&]*)');
		var re = new RegExp('[&\?]'+paramName+'=([^&]*)');
		var found = s.match(re);
		if(found) {
			if(found.length>1) {
				return true;
			}
		}
	}
	return false;
}
function Set_Cookie(name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	/*
		if the expires variable is set, make the correct
		expires time, the current script below will set
		it for x number of days, to make it for hours,
		delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ ) {
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );

		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		//if(window.console){
		//	window.console.log('cookie_name: ' + cookie_name );
		//}
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			//if(window.console){
			//	window.console.log('cookie_value: ' + cookie_value );
			//}
			
			
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) {
		return null;
	}
}
// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function safariLog(text){
	if(window.console){
		window.console.log(text);
	}
}
	
