function getCurrentURI() {
	$uri = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search;
	return $uri;
}

function detectDeviceEx() {
	var ua = navigator.userAgent;

	var deviceType = {
		ipod: ua.match(/(iPod)/),
		ipad: ua.match(/(iPad)/),
		iphone: ua.match(/(iPhone)/),
		blackberry: ua.match(/BlackBerry/),
		android: ua.match(/Android/)
	};
	
	var deviceScreen = {
		mobile: ((deviceType.blackberry || deviceType.android) && ua.match(/mobile/i)) || deviceType.ipod || deviceType.iphone || ua.match(/Opera Mini/),
		tablet: ((deviceType.blackberry || deviceType.android) && !ua.match(/mobile/i) && !ua.match(/Opera Mini/)) || deviceType.ipad,
		width: screen.width // not accurate for android :(
	};
	
	var device = {
		type: deviceType,
		screen: deviceScreen
	};
	
	return device;
}

var deviceInfo = detectDeviceEx();

function redirectMobile() {
	if(deviceInfo.type.ipad || deviceInfo.type.iphone || deviceInfo.type.ipod) {
		if (!$.cookie('iss') && !$.cookie('dvf')) {
			window.location.replace("/~m/iphone_app_info?backurl=" + encodeURIComponent(getCurrentURI()));
			return;
		}
	}
	if(deviceInfo.screen.mobile /*&& (deviceInfo.screen.width < 800)*/) {
		var uri = $_hh.mobile_version + window.location.pathname + window.location.search;
		if($.cookie('dvf') != '1'){
			//Redirecting to mobile design
			window.location.href = uri;
		}
	}
}


redirectMobile();

$(document).ready(function(){
	if($.cookie('iphone-app-adv') || deviceInfo.type.android){
		if($('.iphoneAppC').length){
			$('.iphoneAppC').hide();
		}
	}

	var uri = $_hh.mobile_version + window.location.pathname + window.location.search;
	$('#design-switcher a').bind('click', function(e){
		e.preventDefault();
		$.cookie("dvm", 1, {path: "/"});
		window.location.href = uri;
	});
});
