HTTP_GET_VARS=new Array();	// Funktion für auslesen der Header-Variablen
strGET=document.location.search.substr(1,document.location.search.length);
if(strGET!=''){
    gArr=strGET.split('&');
    for(i=0;i<gArr.length;++i){
        v='';vArr=gArr[i].split('=');
        if(vArr.length>1){v=vArr[1];}
        HTTP_GET_VARS[unescape(vArr[0])]=unescape(v);
    }
}
function GET(v){	
	if(!HTTP_GET_VARS[v]){return 'undefined';}
	return HTTP_GET_VARS[v];
}

function MobileSwitch(){

	var baseMobileURL = "http://m.20min.ch";
	var oldURL = document.location.href;
	
	mobileParameter = GET('redirect');	// Prüfen ob redirect=no und falls ja, classic cookie setzen
	if(mobileParameter == "no") {
		SetMobileCookie("classic", 31536000);
	}
	
	if(GetMobileCookie()=='desktop'){	// Falls Desktop Cookie, Abbruch
		return;
	} else if (GetMobileCookie()=='classic'){ 	// Falls Benutzer klassische Seite mit Mobile-Device sehen möchte, Abbruch.
		printHeader()
		return;
	} else {	// Wenn kein Cookie vorhanden, User-Agent prüfen
	
		/* Dirty Soundshack Hack, remove per End 2011 */
		if (oldURL=='http://www.20min.ch/soundshack/' || oldURL=='http://www.20min.ch/ro/soundshack/'){
			return;
		}
	
		var userBrowser = navigator.userAgent.toLowerCase();
		var mobileDevice = false;
		var devices = ["240x320", "android", "avantgo", "blackberry", "benq", "ericsson", "iphone", "ipod", "lg", "mda", "midp", "mot-", "motorola", "netfront", "nokia", "opera mini", "palm", "panasonic", "philips", "portalmmm", "sagem" , "samsung", "sgh-", "sharp", "sie-", "sony", "sonyericsson" , "symbian", "vodafone", "windows ce", "windows phone"];
		
		$.each(devices, function(key, deviceName) { 
			if (userBrowser.indexOf(deviceName) != -1){
				mobileDevice = true;				
				newURL = onlineToMobileURL(oldURL, baseMobileURL);				
				if(oldURL != newURL){
					document.location.href = newURL;
				}
				return false;
			}
		});
		
		if(!mobileDevice){		// Falls kein Mobile-Useragent, desktop cookie setzen.
			SetMobileCookie('desktop', 31536000);
		}
	}

}
MobileSwitch();



function printHeader(){
	if(location.href.indexOf('/ro/') == -1){	
		$(window).load(function() {
	  		$(document.getElementById('site-wrapper')).before('<div id="mobileHint"><strong>Mobile-User?</strong> <strong><a href="http://m.20min.ch" onclick="changeCookie();">Wechseln Sie zur mobile Version von 20 Minuten Online.</a></strong></div>');		  
		});
	} else {
		$(window).load(function() {
	  		$(document.getElementById('site-wrapper')).before('<div id="mobileHint"><strong>Mobile-User?</strong> <strong><a href="http://m.20min.ch/index.jsp?lang=fr" onclick="changeCookie();">Prenez la version mobile de 20 Minutes Online.</a></strong></div>');		  
		});
	}
}


function GetMobileCookie () { 	// Auslesen des Mobile Cookie
  var name = "MobileCookie";
  var arg = name + "="; 
  var alen = arg.length; 
  var clen = document.cookie.length; 
  var i = 0; 
  while (i < clen) { 
    var j = i + alen; 
    if (document.cookie.substring(i, j) == arg) 
      return getCookieVal (j); 
      i = document.cookie.indexOf(" ", i) + 1; 
      
      if (i == 0) break; 
      } 

    return null;
}
function getCookieVal (offset) { 	// Auslesen des Mobile Cookie Values
  var endstr = document.cookie.indexOf (";", offset); 
  if (endstr == -1) 
    endstr = document.cookie.length; 
    return unescape(document.cookie.substring(offset, endstr));
}
function SetMobileCookie (Wert, Verfall) {		// Setzen des Mobile Cookie
  var jetzt = new Date();
  var Auszeit = new Date(jetzt.getTime() + Verfall);
  document.cookie = "MobileCookie" + "=" + Wert + "; expires=" + Auszeit.toGMTString() + "; path=/;";
}
function changeCookie(){	// Zurücksetzen des Cookies
	SetMobileCookie("mobile", 0);
}

function onlineToMobileURL(onlineURL, baseMobileURL){

	var mobileURL = onlineURL;
	
	var lang = 'de';
	var roIndex = onlineURL.indexOf('/ro/');
	if(roIndex != -1){
		lang = 'fr';
		onlineURL = onlineURL.replace(/(.*?)\/ro\/(.*?$)/, '$1/$2');
	}
	
  var MAPPING = {
		//MAPPING GLOBAL
		'news' : 'front',
		'soundshack' : 'soundshack',
		'life' : 'life',
		
		// MAPPING DE				
		'news/schweiz' : 'schweiz',
		'news/zuerich' : 'schweiz',
		'news/bern' : 'schweiz',
		'news/basel' : 'schweiz',
		'news/mittelland' : 'schweiz',
		'news/zentralschweiz' : 'schweiz',
		'news/ostschweiz' : 'schweiz',
		'news/ausland' : 'ausland',
		'finance' : 'wirtschaft',
		'news/kreuz_und_quer' : 'panorama',
		'sport' : 'sport',
		'digital' : 'digital',
		'people' : 'people',
		'wissen' : 'wissen',
		
		//MAPPING FR	
		'news/suisse' : 'suisse',
		'news/geneve' : 'suisse',
		'news/romandie': 'suisse',
		'news/monde' : 'monde',	
		'news/economie' : 'economie',	
		'news/faits_divers' : 'faits%20divers',
		'news/insolite' : 'insolite',
		'sports' : 'sports',
		'entertainment' : 'entertainment',
		'multimedia' : 'hi-tech',
		'musique' : 'musique'
	};
	
	//main page... just return the mobileUrl + language and skip the regex hocus pocus
	if (onlineURL == 'http://'+document.domain+'/'){
		return baseMobileURL+'/'+lang;
	}

	// remove '/dossier/' from url
	onlineURL = onlineURL.replace(/(.*?)\/dossier\/.*\/(.*?$)/, '$1/$2');

	if(onlineURL.indexOf('videotv') != -1){
		// case videoportal	
		var videoId = onlineURL.match(/vid=([0-9]+)/)[1];
		mobileURL = 'http://podcast.20min-tv.ch/podcast/20min/'+videoId+'.mp4';
	}else	if(onlineURL.indexOf('diashow') != -1){
			// case diashow	
			var showId = onlineURL.match(/showid=([0-9]+)/)[1];
			mobileURL = baseMobileURL + '/' + lang + '/diashow/'  + showId;
	}else{
		channelExpr = new RegExp(document.domain+"\/(news\/.*?|.*?)\/"); // search for channelname. Matches: www.20min.ch/(news/channelname|channelname)/*
		storyExpr = /.*?([0-9]+$)/i; // search for storyID. Matches: *(storyid)
		var matched = [];
		// XXX: assuming that all (and only) stories ends with a number
		var isStory = onlineURL.match(/[0-9]$/); 
		if(isStory){
			// case story
			// extract channelname and storyid from url
			var expr = new RegExp(channelExpr.source + storyExpr.source);
			matched = expr.exec(onlineURL);
			if(matched.length == 3 && MAPPING[matched[1]]){
				mobileURL = baseMobileURL + '/' + lang + '/' + MAPPING[matched[1]] + '/' + matched[2];
			}
		}else{
			// case channel	
			matched = channelExpr.exec(onlineURL);
			if(matched.length == 2 && MAPPING[matched[1]]){
				mobileURL = baseMobileURL + '/' + lang + '/' + MAPPING[matched[1]];
			}
		}
	}
	return mobileURL;
}










