/********************************************************************************************/
/*                                Redirection pour les sites mobiles                        */
/* Auteur : Costa C. <ccosta@e-magineurs.com                                                */
/* Date : 08/04/2011                                                                        */
/* Version : 0.4                                                                            */
/* Compagnie : E-Magineurs                                                                  */
/********************************************************************************************/

//----------------------------------
// Variables à modifier
//----------------------------------

var QUESTION = "Voulez-vous visiter la version mobile du site web de Chartres ?";
var MOBISITE = "http://mobi.chartres.fr";


//----------------------------------
// Initialisation des fonctions
// au chargement de la page
//----------------------------------
if (window.attachEvent){
	window.attachEvent("onload", enterFunctions);
}
if (window.addEventListener){
	window.addEventListener("load", enterFunctions, false);
}

//----------------------------------
// Fonction appelée au chargement
//----------------------------------
function enterFunctions(){
	whereDoYouGo();
}


/*----------------------------------------------------------------------------*/
/*                  		 		 	FONCTION WHEREDOYOUGO			                      */
/*----------------------------------------------------------------------------*/
function whereDoYouGo(){
	if(readSession("mobile")==null){
		writeSession("mobile", "yes");
	}

	if(/mobile/i.test(window.location.search)){
		writeSession("mobile", "no");
	}

	if((/mobile/i.test(navigator.userAgent))
	|| (/MIDP/i.test(navigator.userAgent))
	){
		if(readSession("mobile")!="no"){
			if (confirm(QUESTION)) { // Clic sur OK
		  	window.location.href=MOBISITE;
			}else{
				writeSession("mobile", "no");
			}
		}
	}
}


/*----------------------------------------------------------------------------*/
/*                  		 		 	FONCTION WRITESESSION			                      */
/*----------------------------------------------------------------------------*/

function writeSession(Nom, Valeur){
	var argc = writeSession.arguments.length;
	var argv = writeSession.arguments;

	var Expire			= (argc > 2) ? argv[2] : null;
	var Repertoire	= (argc > 3) ? argv[3] : null;
	var Domaine			= (argc > 4) ? argv[4] : null;
	var Securise		= (argc > 5) ? argv[5] : false;

	document.cookie = Nom+"="+escape(Valeur)+
	((Expire==null) ? "" : ("; expires="+Expire.toGMTString()))+
	((Repertoire==null) ? "" : ("; path="+Repertoire))+
	((Domaine==null) ? "" : ("; domain="+Domaine))+
	((Securise==true) ? "; secure" : "");
}

/*----------------------------------------------------------------------------*/
/*                  		 				FONCTION READSESSION			                    */
/*----------------------------------------------------------------------------*/

function readSession(Nom){
	var Argument	= Nom+"=";
	var ArgLen		= Argument.length;
	var CookLen		= document.cookie.length;
	var Counter		= 0;

	while (Counter<CookLen){
		var Offset = Counter + ArgLen;
		if(document.cookie.substring(Counter, Offset)==Argument){
			return getSessionVal(Offset);
		}
		Counter = document.cookie.indexOf(" ",Counter) + 1;
		if(Counter==0) break;
	}
	return null;
}

/*----------------------------------------------------------------------------*/
/*                  		 			FONCTION GETSESSIONVAL			                    */
/*----------------------------------------------------------------------------*/

function getSessionVal(Offset){
	var EndStr = document.cookie.indexOf (";", Offset);
	if(EndStr == -1){
		EndStr = document.cookie.length;
	}
	return unescape(document.cookie.substring(Offset, EndStr));
}

/*----------------------------------------------------------------------------*/
/*                  		 			FONCTION GETEXPIRESESSION                       */
/*----------------------------------------------------------------------------*/

function getExpireSession(butWhen){
	var LaDate = new Date();
	var heureEnCours = LaDate.getHours();

	if(butWhen == 'Tomorrow'){
		LaDate.setSeconds(0);
		LaDate.setMinutes(0);
		LaDate.setHours(heureEnCours + (24 - heureEnCours) + 2);
	}
	return(LaDate);
}
