$(document).ready(function(){
			var goto = window.location.href;
			var wless = false;
			if(goto.indexOf('/mobile/')!=-1){
				wless = true;
			}
			var smallScreen = false;
			if((screen.width<=800 && screen.height<=600) || (screen.width<=600 && screen.height<=800)){smallScreen = true;}
			if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/Android/i))) {smallScreen = true;}
			
			$('li#mobileswitchitem').hide();	
			
			if(smallScreen&&!wless){
				$('li#mobileswitchitem').show();	
			}
			
			
			if($.cookie('isMobile')){
				if($.cookie('isMobile')=='true'){
					if(!wless){doRedirect(wless,goto);}
				}else{
					if(wless){doRedirect(wless,goto);}
				}
			}
			/*
			else{
				if(smallScreen){
					$.cookie('isMobile', 'true', { path: '/', domain: '.hants.gov.uk', expires: 365 });
					if(!wless){doRedirect(wless,goto);}
				}else{
					$.cookie('isMobile', 'false', { path: '/', domain: '.hants.gov.uk', expires: 365 });
					if(wless){doRedirect(wless,goto);}
				}
			}
			*/
			$('#mobileswitchlink').click(function(){
				if(wless){
					$.cookie('isMobile', 'false', { path: '/', domain: '.hants.gov.uk', expires: 365 });
					doRedirect(wless,goto);
				}else{
					$.cookie('isMobile', 'true', { path: '/', domain: '.hants.gov.uk', expires: 365 });
					doRedirect(wless,goto);
				}
				return false;
			});
			
			
			function doRedirect(isWless,gotoUrl){
				if(gotoUrl=='http://www.hants.gov.uk'||gotoUrl=='http://www.hants.gov.uk/'||gotoUrl=='http://www.hants.gov.uk/index.html'){
					gotoUrl = 'http://www3.hants.gov.uk/mobile/mhome';
				}
				if(isWless){
					gotoUrl = gotoUrl.replace('/mobile/','/');
				}else{
					var gotoParts = gotoUrl.split('.gov.uk');
					gotoUrl = gotoParts[0]+'.gov.uk/mobile'+gotoParts[1];	
				}
				window.location = gotoUrl;
				return false;
			}	 					
		});
