window.Cookie = new function() {
	var base = this;
	base.prefix = '';
	base.suffix = '';
	base.ext = {
		prefix: function(p) { base.prefix = p; },
		suffix: function(s) { base.suffix = s; },
		get: function(name) {
			var nameEQ = base.prefix + name + base.suffix + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length,c.length));
			}
			return null;
		},
		set: function(name, value, days) {
			var expires = "";
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				expires = "; expires="+date.toGMTString();
			}
			document.cookie = base.prefix + name + base.suffix + "=" + encodeURIComponent(value) + "; path=/";
		},
		unset: function(name) {
			base.ext.set(name,"",-1);
		} 
	}
	return base.ext;
};

function MobileRedirect() {
	var cn = 'SuppressMobileRedirect';
	//var q = (document.referrer.indexOf('mobil.leo.local') > -1) ? 'Wieder zur Mobilen Seite?' : 'Zur Mobilen Seite?' ;
	if( Cookie.get(cn) != '2') {
		//console.log('http://mobil.DOMAIN' + location.pathname + location.search + location.hash);
		location.href = 'http://'+location.host+'/mobile';
		Cookie.set(cn, '1');
	}
}


//regex & idea from zeit.de - THX!
window.myagent = navigator.userAgent||navigator.vendor||window.opera;
window.mygrep = /android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile|o2|opera mini|palm( os)?|plucker|pocket|pre\/|psp|smartphone|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce; (iemobile|ppc)|xiino/i;

window.is_iPad = navigator.userAgent.match(/iPad/i) !== null;
window.is_galaxyTab = navigator.userAgent.match(/GT-P1000/i) !== null;

if(is_iPad || is_galaxyTab) {
//hier können wir dann irgendwann mal was fürs iPad laden, Navi wieder fixen usw ;)
} else {
	try {
		if(window.mygrep.test(window.myagent)) {
		//if(true) {
			MobileRedirect();
			var oldonload = window.onload;
			window.onload = function (e) {
				MobileRedirect(e);
			    if (oldonload) oldonload(e)
			};
		}
	} catch(e) {
		console.info(e);
	}
}
