/*!
* JS Redirection Mobile
*
* Copyright (c) 2011 Sebastiano Armeli-Battana (http://sebarmeli.com)
* MIT Licensed: http://www.opensource.org/licenses/mit-license.php
* @link http://github.com/sebarmeli/JS-Redirection-Mobile-Site/
*/

/* 
* This version is exactly the same as redirection_mobile.js, but it's an anonymous self-executing
* function, and it's using the default values for 'redirection_paramName' ("mobile_redirect"), 'mobile_prefix'("m"),
* 'mobile_url'("") and 'cookie_hour'(1). In this way you can drop this file on your web server with no configuration
* 
* @author Sebastiano Armeli-Battana
* @version 0.6 
* 
*/

/*globals window, redirection_mobile, document, navigator, location */
(function(window, document, navigator) {

		// Helper function for adding time to the current date
	var addTimeToDate = function(msec) {

		// Get the current date
		var exdate = new Date();

		// Add time to the date
		exdate.setTime(exdate.getTime() + msec);

		//Return the new Date
		return exdate;

	};

	// Helper function for getting a value from a parameter in the querystring
	var getQueryValue = function(param) {

		if (!param) {
			return;
		}

		var querystring = document.location.search,
			queryStringArray = querystring && querystring.substring(1).split("&"),
			i = 0,
			length = queryStringArray.length;
		
		for (; i < length; i++) {
			var token = queryStringArray[i],
				firstPart = token && token.substring(0, token.indexOf("="));
			if (firstPart === param ) {
				return token.substring(token.indexOf("=") + 1, token.length);
			}
		}

	};

	// Retrieve the User Agent of the browser
	var agent = navigator.userAgent.toLowerCase(),

		// Constants
		FALSE = "false",

		// parameter to pass in the URL to avoid the redirection
		redirection_param = "mobile_redirect",

		//fail to redirect to mobile site, due to 404 or 500 error
		redirection_error="redirection_error",
		
		// prefix appended to the hostname
		mobile_prefix = "m",

		// new url for the mobile site domain 
		mobile_url = "",

		// protocol for the mobile site domain 
		mobile_protocol = document.location.protocol,

		// URL host of incoming request
		host = document.location.host,
		
		//Absolute URL of incoming request
		location = document.location.href.split("//")[1],
	
		// value for the parameter passed in the URL to avoid the redirection
		queryValue = getQueryValue(redirection_param),
		errorValue = getQueryValue(redirection_error);
		// Compose the mobile hostname considering "mobile_url" or "mobile_prefix" + hostname
		mobile_host = "";
		if(location.indexOf(host + "/home") >= 0){
			//if the redirect happen on home page, take off the "/home", because a few of dealer sites having their own mobile sites do not have "home" page 
			mobile_host = mobile_url ||
			(mobile_prefix + "." + 
				(!!host.match(/^www\./i) ?
						host.substring(4) : 
							host));
		}else{
			mobile_host = mobile_url ||
			(mobile_prefix + "." + 
				(!!location.match(/^www\./i) ?
						location.substring(4) : 
							location));
		}
		

		// Expiry hours for cookie
		cookie_hours = 1,

		// Check if the UA is a mobile one (iphone, ipod, ipad, android, blackberry)
		isUAMobile =!!(agent.match(/(iPhone|iPod|iPad|blackberry|android|htc|kindle|lg|midp|mmp|mobile|nokia|opera mini|palm|pocket|psp|sgh|smartphone|symbian|treo mini|Playstation Portable|SonyEricsson|Samsung|MobileExplorer|PalmSource|Benq|Windows Phone)/i));

	// Check if the referrer was a mobile page (probably the user clicked "Go to full site") or in the 
	// querystring there is a parameter to avoid the redirection such as "?mobile_redirect=false"
	// (in that case we need to set a variable in the sessionStorage or in the cookie)
	if (document.referrer.indexOf(mobile_host) >= 0 || queryValue === FALSE ) {

		if (window.sessionStorage) {
			window.sessionStorage.setItem(redirection_param, FALSE);
		} else {
			document.cookie = redirection_param + "=" + FALSE + ";expires="+
												addTimeToDate(3600*1000*cookie_hours).toUTCString();
		}
	}

	// Check if the sessionStorage contain the parameter
	var isSessionStorage = (window.sessionStorage) ? 
							(window.sessionStorage.getItem(redirection_param) === FALSE) :
								false,
		
		// Check if the Cookie has been set up
		isCookieSet = document.cookie ? 
						(document.cookie.indexOf(redirection_param) >= 0) :
							false;
							
	// Check that User Agent is mobile, cookie is not set or value in the sessionStorage not present
	if (isUAMobile && !(isCookieSet || isSessionStorage) && errorValue != 'true') {
			//create cookie variable for google to track original reference rather than holden site
			document.cookie="originalReferrer"+"="+escape(document.referrer)+";domain="+host;
			//"dealerlocator" redirect
			mobile_host = mobile_host.replace("dealerlocator", "locate-a-dealer");
			//"latestoffers" redirect, only redirect if the request going to root of latestoffers
			//for particular offer, the url will not be rewritten
			if(mobile_host.indexOf("latestoffers") == mobile_host.length - 12){
				mobile_host = mobile_host.replace("latestoffers", "latest-offers");
			}
			var new_mobile_host = "";
			if(mobile_host.indexOf("/vehicles") > 0){
				var parts = mobile_host.split("/");
				var vehicleRoot = false;
				var captivaRoolt = false;
				if(mobile_host.indexOf("/vehicles") > 0){
					for(var i=0; i<parts.length; i++){
						if(!vehicleRoot){
							new_mobile_host += parts[i] + "/";
							if(parts[i] == 'vehicles'){
								vehicleRoot = true;
							}
						}else if(!captivaRoolt){
							new_mobile_host += parts[i] + "/";
							captivaRoolt=true;
						}else {
							new_mobile_host += parts[i];
							break;
						}
					}
				}else{
					for(var i=0; i<parts.length; i++){
						if(!vehicleRoot){
							new_mobile_host += parts[i] + "/";
							if(parts[i] == 'vehicles'){
								vehicleRoot = true;
							}
						}else{
							new_mobile_host += parts[i];
							break;
						}
					}
				}
				mobile_host = new_mobile_host;
			}
			document.location.href = mobile_protocol + "//" + mobile_host;
	} 
}(window, document, navigator));
