(function($){
var c = $.cookie('mobileMode');
var isSp = window.navigator.userAgent.match(/(?:Android|BlackBerry|iPhone|iPod|Opera\sMini|Opera\sMobile|Opera\sSymbian|WebOS)/i);
var w = (window.outerWidth) ? window.outerWidth : screen.width;
var h = (window.outerHeight) ? window.outerHeight : screen.height;
if(window.devicePixelRatio){
	w /= window.devicePixelRatio;
	h /= window.devicePixelRatio;
}
var path = [
	(window.location.pathname) ? window.location.pathname : '/',
	(window.location.search) ? window.location.search : '',
	(window.location.hash) ? window.location.hash : '',
].join('');

if(isSp){
	if(isEmpty(c)){
		if(!path.match(/^\/sp\//i) && Math.min(w,h) <= 480){
			$.cookie('mobileMode', 'on', {path: '/'});
			window.location.href = '/sp' + path;
		}else if(path.match(/^\/sp\//i) && Math.min(w,h) > 480){
			$.cookie('mobileMode', 'off', {path: '/'});
			window.location.href = path.replace(/^\/sp/i, '');
		}
	}
	if(c == 'on' && !path.match(/^\/sp\//i)){
		window.location.href = '/sp' + path;
	}else if(c == 'off' && path.match(/^\/sp\//i)){
		window.location.href = path.replace(/^\/sp/i, '');
	}
}

$(function(){
	$('a.toPC').live('click', function(){
		$.cookie('mobileMode', 'off', {path: '/'});
		return move(this);
	});
	$('a.toSP').live('click', function(){
		$.cookie('mobileMode', 'on', {path: '/'});
		return move(this);
	});
});

function move(o){
	var href = $(o).attr('href');
	if(!isEmpty(href)){
		window.location.href = href;
		return false;
	}
	return true;
}

function isEmpty(v){
	try{
		return !(v && v != null);
	}catch(e){
		return true;
	}
}
})(jQuery);
