$(function() {
	// スマホ振分け
	if ($.cookie('is_pc_view') === null) {
		if (isSmartPhone()) {
			location.href = 'http://www.tamagawaro.com/sp/';
		}
	}

	// ロールオーバーイベントの設定
	$("img.swap").hover(
		function() {
			// ロールオーバーアクション
			$(this).attr("src", $(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_hover$2"));
		},
		function() {
			// ロールアウトアクション
			var extention = getExtension($(this).attr("src"));
			var filename = $(this).attr("src").replace(/_hover/, "");
			$(this).attr("src", filename);
		}
	).each(
		function(){
			// プリロード
			$("<img>").attr("src", $(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_hover$2"));
		}
	).click(function () {
		// スワップ画像のクリックアクションでオーバー画像の解除
		var extention = getExtension($(this).attr("src"));
		var filename = $(this).attr("src").replace(/_hover/, "");
		$(this).attr("src", filename);
	});
	
	// スマホ以外で表示なら切替え用スイッチを非表示
	if (!isSmartPhone()) {
		$("#switchSmartPhone").remove();
	}
	
});


// ファイル名から拡張子を取得
function getExtension(filename) {
	var result = "";
	var fileTypes = filename.split(".");
	if (fileTypes.length > 0) {
		result = fileTypes[fileTypes.length - 1];
	}
	return result;
}

// スマホ判定
function isSmartPhone() {
	return (navigator.userAgent.indexOf('iPhone') > 0 && navigator.userAgent.indexOf('iPad') == -1) || navigator.userAgent.indexOf('iPod') > 0 || navigator.userAgent.indexOf('Android') > 0;
}

// PC表示用cookie削除
function switchSmpView () {
	$.cookie('is_pc_view', null);
	$.cookie('is_pc_view', '', {path: '/', domein:'www.tamagawaro.com', expires: -1 });
	location.href = 'http://www.tamagawaro.com/sp/';
}

