
function mobileCookie()
{
	//valeur du param desktop
	var desktop = getUrlParam('desktop');
	
	//si égal à 1, on créer un cookie "non-mobile"
	if(desktop == 1) {
		//cookie permanent comme quoi le user a fait un choix mobile/non-mobile
		Set_Cookie('bvMobileChoix', 1, 365, '/', '.branchez-vous.com', 0 );
		//cookie variable pour changer le choix
		Set_Cookie('bvNoMobile', 1, 365, '/', '.branchez-vous.com', 0 );
	}
}

function mobileAlert(param,id) 
{
	if(param == '') return false;
	
	var domain = document.domain;
	var curLocation = document.location.toString();
	
	//excluded domains	
	switch(domain) {
		case 'voyagez.branchez-vous.com' :
			var noIpad = true;
			break;
	}
	
	//excluded directories :
		//femmeweb nouveau
		if(domain == 'femmeweb.branchez-vous.com' && curLocation.match(/nouveau\//i)) return false;
		//auto, exception page d'accueil + blogueauto
		if(domain == 'auto.branchez-vous.com' && curLocation != 'http://' + domain + '/') return false;	
	
	//cookie init
	mobileCookie();
	
	var aff = false;
	var fontSize = 40;
	var baseurl = 'http://m.branchez-vous.com/';
	var auto = false;
	
	if((navigator.userAgent.match(/(iPhone|iPod)/i))){
		var mot = 'iPhone';
		var url = baseurl + 'html.php';				
		var aff = true;
		auto = true;
	} else if((navigator.userAgent.match(/(ipad|iPad)/i))){
		var mot = 'iPad';
		var url = 'http://www.branchez-vous.com/ipad/';	
		var aff = (noIpad == true) ? false : true;
	} else if((navigator.userAgent.match(/(blackberry9530|android|BlackBerry 9800)/i))){ //blackberry storm
		var mot = 'mobile';
		var url = baseurl + 'html.php';
		var aff = true;
	} else if((navigator.userAgent.match(/(mobi|blackberry|opera mini|nokia|sony|mot|ericsson)/i))){
		var mot = 'mobile';
		var fontSize = 16;
		var url = baseurl + 'xml.php';		
		var aff = true;
		// forcer la redirection
		//document.location.href = url + '?s=' + param + '&id=' + id;
	}	
	
	//redirection auto toujours bloquée quand user a  fait un choix pour les cookies
	var choix = Get_Cookie('bvMobileChoix');
	//if(choix == '1') auto = false;
	
	//construire l'URL
	url = url + '?';
	if(param.length>0)	url = url + '&s=' + param;
	if(id>0)			url = url + '&id=' + id;
		
	//noredirect ?
	var desktop = getUrlParam('desktop');
	
	//cookie?
	var cookie = Get_Cookie('bvNoMobile');
	if(cookie == 1) var url = url + '&delCookie=1';

	if(aff && auto && desktop != 1 && (choix != 1 || (choix == 1 && cookie != 1)) ) {
		//redirection automatique
		document.location.href = url;
		//alert('choix=' + choix + '; cookie=' + cookie);
		
	} else if(aff && desktop != 1) {
		document.write('<style type="text/css">');
		document.write('#mobile { font-family:Arial,Verdana; width:100%; height: 60px; padding:20px 0; line-height:60px;vertical-align:middle;background-color:#333; text-align:center; border-bottom:3px solid #000;}');
		document.write('#mobile A { text-shadow:#000 -1px -1px 0; font-size: '+fontSize+'pt; width:100%; height: 60px; line-height:60px;vertical-align:middle;height:100%; display:block; color:#999; text-decoration:none; z-index:100;}');
		document.write('#mobile A STRONG { color:#fff; }');
		document.write('</style>');
		document.write('<div id="mobile">');
		document.write('<a href="' + url + '">Acc&eacute;dez &agrave; la version <strong>' + mot + ' &gt;</strong></a>');		
		document.write('<!-- location: ' + document.location + ' -->');
		document.write('</div>');
	}
}


/*
 * trouver la valeur d'un paramêtre dans l'URL
 *
 * @param	VARCHAR		Nom du paramêtre
 * @return	VARCHAR		Valeur du param.
*/
function getUrlParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return false;
  else
    return results[1];
}


/*
Script Name: Javascript Cookie Script
Author: Public Domain, with some modifications
Script Source URI: http://techpatterns.com/downloads/javascript_cookies.php
Version 1.1.2
Last Update: 5 November 2009
*/
function Get_Cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	var i = '';	
	for ( i = 0; i < a_all_cookies.length; i++ ) {
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name ) {
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )			
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );			
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) 	return null;
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the current script below will set it for x number of days, 
	//to make it sfor hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires ) expires = expires * 1000 * 60 * 60 * 24;	
	var expires_date = new Date( today.getTime() + (expires) );	
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) 
{
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
