/*!
* JS Redirection Mobile
*
* Developed by
* Sebastiano Armeli-Battana (@sebarmeli) - http://www.sebastianoarmelibattana.com
* Dual licensed under the MIT or GPL Version 3 licenses.
*/
if (!window.SA) {window.SA = {};}
SA.redirection_mobile = function(configuration) {
	var addTimeToDate = function(msec) {
	var exdate = new Date();
	exdate.setTime(exdate.getTime() + msec);
	return exdate;
};
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);
		}
	}
};
var agent = navigator.userAgent.toLowerCase(),
FALSE = "false",
TRUE = "true",
config = configuration || {},
redirection_param = config.noredirection_param || "noredirection",
mobile_prefix = config.mobile_prefix || "m",
mobile_url = config.mobile_url,
mobile_protocol = config.mobile_scheme ?
config.mobile_scheme + ":" :
document.location.protocol,
host = document.location.host,
queryValue = getQueryValue(redirection_param),
mobile_host = mobile_url ||
(mobile_prefix + "." +
(!!host.match(/^www\./i) ?
host.substring(4) :
host)),
cookie_hours = config.cookie_hours || 1,
keep_path = config.keep_path || false,
keep_query = config.keep_query || false,
tablet_host = config.tablet_url || mobile_host,
isUAMobile =!!(agent.match(/(iPhone|iPod|blackberry|android 0.5|htc|lg|midp|mmp|mobile|nokia|opera mini|palm|pocket|psp|sgh|smartphone|symbian|treo mini|Playstation Portable|SonyEricsson|Samsung|MobileExplorer|PalmSource|Benq|Windows Phone|Windows Mobile|IEMobile|Windows CE|Nintendo Wii)/i));
if (document.referrer.indexOf(mobile_host) >= 0 || queryValue === TRUE ) {
	if (window.sessionStorage) {
		window.sessionStorage.setItem(redirection_param, TRUE);
	} else {
		document.cookie = redirection_param + "=" + TRUE + ";expires="+
		addTimeToDate(3600*1000*cookie_hours).toUTCString();
	}
}
var isSessionStorage = (window.sessionStorage) ?
(window.sessionStorage.getItem(redirection_param) === TRUE) :
false,
isCookieSet = document.cookie ?
(document.cookie.indexOf(redirection_param) >= 0) :
false;
if (!!(agent.match(/(iPad|SCH-I800|xoom|kindle)/i))) {
	var isUATablet = (config.tablet_redirection === TRUE || !!config.tablet_url) ? true : false;
	isUAMobile = false;
}
if ((isUATablet || isUAMobile) && !(isCookieSet || isSessionStorage)) {
	if (config.beforeredirection_callback) {
		if (!config.beforeredirection_callback.call(this)) {
		return;
	}
}
var path_query = "";
if(keep_path) {
	path_query += document.location.pathname;
}
if (keep_query) {
	path_query += document.location.search;
}
if (isUATablet){
	document.location.href = mobile_protocol + "//" + tablet_host + path_query;
} else if (isUAMobile) {
	document.location.href = mobile_protocol + "//" + mobile_host + path_query;
}
}
}; 
