var isMobileDevice = false;
function gomobile(){
    var path = '/media/mobile/' + getPath();
    sessionStorage.desktop = false;
    location.href = location.protocol + '//' + location.host + path;
}
function getPath(){
	var chapter, subpath, loc = location.pathname, list = [
		{chapter: 'veranstaltung', exp: /veranstaltung\/(?:[0-9]{6})?/},
		{chapter: 'abo', exp: /abonnements\/([0-9]*)?/},
		{chapter: 'nicht-versaumen', exp: /nicht-versaumen\/([0-9]*)?/},
		{chapter: 'im-fokus', exp: /im-fokus\/([0-9]*)?/},
		{chapter: 'news', exp: /news\/(?:[0-9]*)?/},
		{chapter: 'faq', exp: /faq\//},
		{chapter: 'kartenkauf', exp: /kartenkauf\//},
		{chapter: 'barrierefreiheit', exp: /barrierefreiheit\//},
		{chapter: 'sehbehinderte', exp: /orientierungshilfe-sehbehinderte\//},
		{chapter: 'gehbehinderte', exp: /orientierungshilfe-gehbehinderte\//},
		{chapter: 'anfahrt', exp: /anfahrt-unterkunft\//},
		{chapter: 'impressum', exp: /impressum\//}
	];
	for(var i in list){
		var exp = list[i], result = list[i].exp.exec(loc);
		if(result){
			chapter = {chapter: exp.chapter, result: result};
			break;
		}
	}
	if(!chapter) return '';
	switch(chapter.chapter){
		case "veranstaltung":
			subpath = '#' + cutTrailingDash(chapter.result[0]);
			break;
		case "abo":
			var id = cutTrailingDash(chapter.result[1]);
			subpath = '#reihen/abos' + (id ? '/' + id : '');
			break;
		case "nicht-versaumen":
			var id = cutTrailingDash(chapter.result[1]);
			subpath = '#reihen/nichtversaeumen' + (id ? '/' + id : '');
			break;
		case "im-fokus":
			var id = cutTrailingDash(chapter.result[1]);
			subpath = '#reihen/zooms' + (id ? '/' + id : '');
			break;
		case "news":
			subpath = '#' + cutTrailingDash(chapter.result[0]);
			break;
		case "faq":
			subpath = '#service/' + cutTrailingDash(chapter.result[0]);
			break;
		case "kartenkauf":
			subpath = '#service/' + cutTrailingDash(chapter.result[0]);
			break;
		case "barrierefreiheit":
			subpath = '#service/' + cutTrailingDash(chapter.result[0]);
			break;
		case "sehbehinderte":
			subpath = '#service/barrierefreiheit' + chapter.chapter;
			break;
		case "gehbehinderte":
			subpath = '#service/barrierefreiheit' + chapter.chapter;
			break;
		case "anfahrt":
			subpath = '#service/' + chapter.chapter;
			break;
		case "impressum":
			subpath = '#more/' + chapter.chapter;
			break;
		default:
			subpath = '';
	}
	return subpath;
}
function cutTrailingDash(str){
	return (/\/$/.exec(str) ? str.substr(0, str.length-1) : str);
}
function main() {    
    //will be redirected?
    if(sessionStorage.desktop == "true" || location.search.indexOf('mlink') > -1){
	isMobileDevice = true;
	return;
    }
    if(!WebKitDetect.isWebKit()) return;
    
    if(WebKitDetect.isMobile()){
	gomobile();
    }else{
	var screenWidth = screen.height > screen.width ? screen.width : screen.height;
	if(screenWidth < 481){
	    gomobile();
	}
    }
}

main();

