
	// Check for the "pass" parameter. If present, continue loading page.
	if (/mobileRedirect=pass/.test(document.location.href)) {
		// Do nothing, continue loading page
	}
	// Check for the "off" parameter. If present, set the cookie that prevents the check for mobile devices and continue loading page.
	else if (/mobileRedirect=off/.test(document.location.href)) {
		setCookie("mobileRedirect", "off", 100);
	}
	// For all url:s, check for the mobileRedirect cookie. If not present, test the device.
	else {
		if (!cookieExists("mobileRedirect")) 
		
			if (!isIEDesktopBrowser(navigator.userAgent)) { 	// there is an error in the function checkForMobileDevice on desktop IE, so first check if the user is browsing from an IE desktop computer. If so, do NOT perform the check.
				checkForMobileDevice();
			}
	}
	
	// -----------------------------------------------
	//        FUNCTIONS 
	// -----------------------------------------------
	
	
	function checkForMobileDevice() {
	
		// Check for iPad device. If found, continue to website
		// <![CDATA[  
		var is_iPad = (/ipad/i.test(navigator.userAgent.toLowerCase()));  
		var is_NativeAndroidBrowser = checkAndroidNativeBrowser();
		
		if (!is_iPad) {  

		// create an iframe
			var iframe = document.createElement('iframe'), realScreen;

			// hide iframe, but not by using display:none
			iframe.style.height = '0px';
			iframe.style.width = '0px';
			iframe.style.border = 'none';
			iframe.style.position = 'absolute';
			iframe.style.left = '-99999px';

			// insert the iframe into our document
			document.documentElement.appendChild(iframe);

			// put a crazy meta tag in there to prevent scaling
			iframe.contentWindow.document.documentElement.innerHTML = 
			  '<head><meta name="viewport"' + 
			  ' content="width=device-width, initial-scale=1, maximum-scale=1" />' +
			  '</head><body></body>';

			// get screen property of frame's window object
			realScreen = iframe.contentWindow.screen;

			// if the screen is tiny
			// or the "real" screen is smaller than the "virtual" screen
			// then redirect
			if ((realScreen.width <= 480) || (realScreen.height <= 480) || 
				(realScreen.width < screen.width) || (realScreen.height < screen.height) || is_NativeAndroidBrowser) {  
				
				var url = document.location.href; // The complete referring url
				var murl = getMobileUrl(document.location.href); // The complete mobile url
				
				// Is the url "http://lt.se" OR "http://lt.se/" OR if it is a valid article url: Redirect to selection page on "http://m.na.se"
				if ( (document.location.href == "http://nynashamnsposten.se") || 
					(document.location.href == "http://nynashamnsposten.se/") || 
					isArticleUrl(document.location.href)) {
					// Go to the mobile site selection page
					location.replace("http://m.nynashamnsposten.se/default.aspx?pageid=629&url=" + url + "&murl=" + murl);
				}				
			}

			// remove our iframe if we're still here
			document.documentElement.removeChild(iframe);
		}  
		// ]]></	
	
	}
	
	function checkAndroidNativeBrowser() {
		if ((/android/i.test(navigator.userAgent)) && (/mobile/i.test(navigator.userAgent))) return true;
		else return false;
	}

	function isIEDesktopBrowser(userAgentStr) {
		var pat1 = /MSIE/;		// This string must be present in userAgent...
		var pat2 = /IEMobile/;	// ...but not this one.

		return ( pat1.test(userAgentStr) && !pat2.test(userAgentStr) );
	}	
	
	
	function isArticleUrl(webUrl) {
		var pattern1 = /[0-9].[0-9]/;
		var pattern2 = /blog|tidsfordriv|losnummer/g;
		
		return ( pattern1.test(webUrl) && !pattern2.test(webUrl) );
	}
	
	
	function getMobileUrl(webUrl) {
		var mobileUrl = "http://m.nynashamnsposten.se/Default.aspx?link=";
		var tmpStr, i=0;
			
		tmpStr = webUrl.substring(webUrl.lastIndexOf("/")+1, webUrl.length);  // Cut everything before "1.1315282..."
		mobileUrl = mobileUrl + tmpStr[0] + "."; // Add "1." to mobileUrl
		tmpStr = tmpStr.substring(2); // Cut "1.", leaving "1315282..."
		
		// Append the numbers 1, 3, 1, 5... to the mobileUrl
		while (/[0-9]/.test(tmpStr[i])) {
			mobileUrl = mobileUrl + tmpStr[i];
			i++;
		}
		
		return mobileUrl;
	}
		
		
	function setCookie(c_name,value,exdays)
	{
		var exdate=new Date();
		var cDomain = "nynashamnsposten.se";
		exdate.setDate(exdate.getDate() + exdays);
		var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()) + "; domain=" + cDomain;
		document.cookie=c_name + "=" + c_value;
	}
	
	
	function cookieExists(cName) {
		var retVal;
		if (document.cookie.indexOf(cName) == -1) retVal = false;
		else retVal = true;
		return retVal;
	}
	
