/* ■■ common.js ■■ */
/* common.jsは汎用関数をまとめたJavaScript集です。
-----------------------------------------------------------------------------
■目次
	01.onLoad向け処理
		01.IEのロールオーバーで起こる背景画像再読み込み問題対応
	02.ページ向け全般
		01.ページトップへするするスクロール(ReturnToTop)
		02.画像ロールオーバー(RollOver)
	03.form制御
		01.初期文字消去(ResetValue)
		02.初期文字挿入(SetValue)
		03.検索初期文字チェック(CheckDefault)
	04.商品ページ用
		01.画像差し替え(PhotoChange)
----------------------------------------------------------------------------- */

// ■■ 01.onLoad向け処理

// ■□ 01-01.IEのロールオーバーで起こる背景画像再読み込み問題対応
// CSSのa:hoverなどで背景画像の切り替えを行っている際にIEで起こる問題の対応策。
// 効能は気休め程度。
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}


// ■■ 02.ページ向け全般

// ■□ 02-01.ページトップへするするスクロール(ReturnToTop)
// ページの先頭へなめらか動作でスクロールする関数。
// 使用例）
// <a href="#Header" onClick="ReturnToTop(); return false;">ページTOPへ</a>
function ReturnToTop() {
	var x1 = x2 = x3 = 0;
	var y1 = y2 = y3 = 0;
	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}
	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;
	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));
	window.scrollTo(Math.floor(x / 1.25), Math.floor(y / 1.25));
	if (x > 0 || y > 0) {
		window.setTimeout("ReturnToTop()", 25);
	}
}

// ■□ 02-02.画像ロールオーバー(RollOver)
// Standards Compliant Rollover Script
// Author : Daniel Nolan
// http://www.bleedingego.co.uk/webdev.php
//
// 使用例）
// imgタグに.rolloverを指定すると、ファイル名の末尾に
// _roとついた画像に自動ロールオーバー。
function RollOver() {
	if (!document.getElementById) return

	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className == 'rollover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_ro'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);

			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;

			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}

			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_ro'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}


// ■■ 03.form制御

// ■□ 03-01.初期文字消去(ResetValue)
// デフォルトで入力していたvalueを消去する。
// 使用例）
// <input type="text" value="初期文字" onFocus="ResetValue(this,'初期文字')" />
function ResetValue(pos,dflt){
	if(pos.value == dflt){
		pos.value = "";
		pos.style.color = "#000000";
	}
}

// ■□ 03-02.初期文字挿入(SetValue)
// 空だった場合、デフォルトで入力していたvalueへ戻す。
// 使用例）
// <input type="text" value="初期文字" onBlur="SetValue(this,'初期文字')" />
function SetValue(pos,dflt){
	if(pos.value == ''){
		pos.value = dflt;
		pos.style.color = "#666666";
	}
}

// ■□ 03-03.検索初期文字チェック(CheckDefault)
// form送信前に、デフォルトで入力していたvalueだった場合に動作キャンセルをする。
// 使用例）
// <form onSubmit="return CheckDefault('使用したいinputのid','初期文字');">
function CheckDefault(pos,dflt){
	if(document.getElementById(pos).value == dflt || document.getElementById(pos).value == ""){
		document.getElementById(pos).select() ; return false;
	}
}

//
// 現在表示中のページをお気に入りに追加
// (IEのみ対応)
//
function registFavorite() {
	var url = location.href;
        var title = document.title;

	var bi = new BrowserInfo();
	if (bi.ie) {
		window.external.AddFavorite(url, title);
	} else if (bi.firefox) {
		window.sidebar.addPanel(title, url, "");
	} else {
		alert("このブラウザでは対応しておりません");
	}
}

//
// イメージ差し替え(画像リサイズなし)
//
function changeImgSrc(src, dest) {
	var tmp_s = src.src;
	src.src = dest.src;
	dest.src = tmp_s;
}

function changeImg(src, srcMaxWidth, srcMaxHeight, dest, destMaxWidth, destMaxHeight) {
	var destS = src.src;
	var destI = new Image();
	destI.src = src.src;
	var array = getImageResizeSize(destI.width, destI.height, destMaxWidth, destMaxHeight);
	var destW = array[0];
	var destH = array[1];

	var srcS = dest.src;
	var srcI = new Image();
	srcI.src = dest.src;
	var array = getImageResizeSize(srcI.width, srcI.height, srcMaxWidth, srcMaxHeight);
	var srcW = array[0];
	var srcH = array[1];

	src.src = srcS;
	src.width = srcW;
	src.height = srcH;

	dest.src = destS;
	dest.width = destW;
	dest.height = destH;
}

function getImageResizeSize(width, height, targetWidth, targetHeight) {
	var newWidth = width;
	var newHeight = height;
	if (width > targetHeight) {
		if (height > targetHeight) {
			if (height <= width) {
				newWidth = targetWidth;
				newHeight = Math.floor(height / (width / targetWidth));
			} else {
				newWidth = Math.floor(width / (height / targetHeight));
				newHeight = targetHeight;
			}
		} else {
			newWidth = targetWidth;
			newHeight = Math.floor(height / (width / targetWidth));
		}
	} else if (height > targetHeight) {
		newWidth = Math.floor(width / (height / targetHeight));
		newHeight = targetHeight;
	}
	return new Array(newWidth, newHeight);
}

//
// ブラウザ判定
//
function BrowserInfo(userAgent) {

	/*****************************************
	 * Geckoエンジンバージョンチェッカー
	 * (返値 int 合致しない場合は-1)
	 *****************************************/
	this.geckoVersionChecker = function(){
		var num = this.ua.match(new RegExp("Gecko/20[0-9]{6}"));
		return ( num == null ) ? -1 : parseInt(String(num).replace("Gecko/",""));
	}

	/*****************************************
	 * WebKitバージョンチェッカー
	 * (返値 float 合致しない場合は-1)
	 *****************************************/
	this.webkitVersionChecker = function(){
		var num = this.ua.match(new RegExp("WebKit/[0-9]{1,4}(\.[0-9]{1,2})?"));
		return ( num == null ) ? -1 : parseFloat(String(num).replace("Firefox/",""));
	}

	/*****************************************
	 * IEバージョンチェッカー
	 * (返値 float 合致しない場合は-1)
	 *****************************************/
	this.ieVersionChecker = function(){
		var ienum = this.ua.match(new RegExp("MSIE [0-9]{1,2}\.[0-9]{1,3}"));
		return ( ienum == null ) ? -1 : parseFloat(String(ienum).replace("MSIE ",""));
	}


	/*****************************************
	 * Firefoxバージョンチェッカー
	 * (返値 float 合致しない場合は-1)
	 *****************************************/
	this.firefoxVersionChecker = function(){
		var num = this.ua.match(new RegExp("Firefox/[0-9]{1,2}\.[0-9]{1,2}"));
		return ( num == null ) ? -1 : parseFloat(String(num).replace("Firefox/",""));
	}

	/*****************************************
	 * Operaバージョンチェッカー
	 * (返値 float 合致しない場合は-1)
	 *****************************************/
	this.operaVersionChecker = function(){
		var num = this.ua.match(new RegExp("Opera[/ ][0-9]{1,2}\.[0-9]{1,2}"));
		return ( num == null ) ? -1 : parseFloat(String(num).substr(6));
	}

	/*****************************************
	 * Safariバージョンチェッカー
	 * (返値 float 合致しない場合は-1)
	 *****************************************/
	this.safariVersionChecker = function(){
		var num = this.ua.match(new RegExp("Safari/[0-9]{1,4}\.[0-9]{1,2}"));
		return ( num == null ) ? -1 : parseFloat(String(num).replace("Safari/",""));
	}

	/*****************************************
	 * Netscapeバージョンチェッカー
	 * (返値 float 合致しない場合は-1)
	 *****************************************/
	this.netscapeVersionChecker = function(){
		var num = this.ua.match(new RegExp("Netscape[0-9]?/[0-9]{1,2}\.[0-9]{1,3}"));
		return ( num == null ) ? -1 : parseFloat(String(num).replace(new RegExp("Netscape[0-9]?/"),""));
	}

	/*****************************************
	 * Mozillaバージョンチェッカー
	 * (返値 float 合致しない場合は-1)
	 *****************************************/
	this.mozillaVersionChecker = function(){
		var num = this.ua.match(new RegExp("Mozilla/[0-9]{1,2}\.[0-9]{1,2}"));
		return ( num == null ) ? -1 : parseFloat(String(num).replace("Mozilla/",""));
	}

	this.ua = (userAgent) ? userAgent : navigator.userAgent;

	this.geckoVersion = this.geckoVersionChecker();
	this.gecko = (this.geckoVersion > 0 );

	this.webkitVersion = this.webkitVersionChecker();
	this.webkit = (this.webkitVersion > 0 );

	this.ieVersion = this.ieVersionChecker();
	this.ieMVersion = Math.floor(this.ieVersion);
	this.ie = ( this.ieVersion >= 3 );
	this.macie = ( this.ua.match("Mac_PowerPC") != null );

	this.firefoxVersion = this.firefoxVersionChecker();
	this.firefoxMVersion = Math.floor(this.firefoxVersion);
	this.firefox = (this.firefoxVersion > 0 );

	this.safariVersion = this.safariVersionChecker();
	this.safariMVersion = Math.floor(this.safariVersion);
	this.safari = (this.safariVersion > 85 );

	this.operaVersion = this.operaVersionChecker();
	this.operaMVersion = Math.floor(this.operaVersion);
	this.opera = (this.operaVersion > 1 );

	this.netscapeVersion = this.netscapeVersionChecker();
	this.netscapeMVersion = Math.floor(this.netscapeVersion);
	this.netscape = (this.netscapeVersion > 1 );

	this.mozillaVersion = this.mozillaVersionChecker();
	this.mozilla = ( !this.firefox && !this.opera && !this.ie && !this.netscape && this.mozillaVersion > 0 );

	this.toString = function(){
		return ("[ua:"+this.ua+"  netscapeVersion:"+this.netscapeVersion+"  operaVersion:" + this.operaVersion + "  webkitVersion:"+ this.webkitVersion +"  safariVersion:"+this.safariVersion+"  ieVersion:"+ this.ieVersion +"  macie:" + this.macie  + "  geckoVersion:" +this.geckoVersion + " firefoxVersion:" +this.firefoxVersion +"]" );
	}
}


// ■□ 04-01.画像差し替え(PhotoChange)
// 商品ページのサムネイル写真をクリックした際に、メインの写真を差し替える関数。
// 使用例）
// <a href="{大きな写真のパス}" onclick="PhotoChange('pos'); return false;">～～</a>
function PhotoChange(pos) {
	var mainLink = document.getElementById("MainPhotoLink");
	var mainPhoto = document.getElementById("MainPhoto");
	var photoURL = "img_1-1/" + StoreID + "_" + pos;
	mainLink.href = photoURL + "_large.jpg";
	mainPhoto.src = photoURL + ".jpg";
}

