
// Control Main site to Mobile site redirects
// Homeshopping pages redirect to the exact mobile equivalent
// CSS pages all redirect to the mobile CSS homepage

	var siteSelectedCookiePos = document.cookie.indexOf( 'mainsiteselected' );
	if ( siteSelectedCookiePos == -1 ) {
		var dl = top.location.href;
		var dlqs = top.location.search; // Query string params to preserve from CSS URL's

		var brand = (dl.indexOf('24studio') > -1) ? '24studio' : '24ace';
		var subdomain = (dl.indexOf('homeshopping') > -1) ? 'homeshopping.' : '';
		var newURL=''; 
		var agent=navigator.userAgent.toLowerCase();
		var is_iphone = (agent.indexOf('iphone')!=-1);
		var is_android = (agent.indexOf('android') != -1 && agent.indexOf('mobile') != -1);
		var is_blackberry = (agent.indexOf('blackberry')!=-1);
		

		// removed screen.width < 600 check for now!
		if( (dl.indexOf('://m.24') == -1) && (dl.indexOf('://m.homeshopping.24') == -1) && (is_android || is_iphone || is_blackberry) ){
			var myString = new String(dl.split(".co.uk/")[1]);

			if (subdomain == '') {
				newURL = 'http://m.' +brand +'.co.uk/mobile/index.jsp' +dlqs;
			} else {
				newURL = 'http://m.' +subdomain+brand +'.co.uk/'+myString;
			}
			if (document.cookie.indexOf('cssurn=') > -1)
			{
				if (document.location.pathname.indexOf('index.jsp') > -1 || document.location.pathname == '/')
				{
					newURL = 'http://m.homeshopping.' +brand +'.co.uk/' +dlqs;
				}
			}
		}
		
		// Set session cookie only if visitor came from the mobile site and is on the main site (i.e not on the mobile site)
		// otherwise allow multiple redirects from main (here) to mobile
		if (document.referrer.indexOf('://m.') != -1 && dl.indexOf('://m.') == -1) {
      document.cookie = "mainsiteselected=true; path=/; domain=."+brand+".co.uk";
		}
		else if (newURL != '')  {
			top.location.href = newURL;
		}
	}

//////////////
