/*
	ブランドページ・リストページ・アイテムページ共通
	2009/11/13
*/
window.onload = function() {
	document.oncontextmenu = function(){
		return false;
	}
}

window.onunload = function() {
	winClose();
}

var winbkt;
function Win(win,bar,w,h) {
	if (winbkt && !winbkt.closed) winbkt.close();
winbkt=window.open(win,"winVbs","left=5,top=5,toolbar=no,location=0,directories=0,status=0,menubar=no,scrollbars=" + bar + ",resizable=no,Width=" + w + ",Height=" + h);
	winbkt.focus();
}
function winClose() {
	if (winbkt && !winbkt.closed) winbkt.close();
}

/*
 * chgImg(), on(), off() メソッドは、jQuery化に伴い不要となっているはずだが、
 * 万が一これを使用するページ存在する場合の備えて、当分の間 存置する。
 */
function chgImg() {
	if (document.images) {
		document[chgImg.arguments[0]].src = chgImg.arguments[1];
	}
	return false;
}
function on(id) {
	if (document.all) {
		document.all.item(id).style.borderColor = borderOnCol;
	} else if (document.getElementById) {
		document.getElementById(id).style.borderColor = borderOnCol;
	}
}
function off(id) {
	if (document.all) {
		document.all.item(id).style.borderColor = borderOffCol;
	} else if (document.getElementById) {
		document.getElementById(id).style.borderColor = borderOffCol;
	}
}
jQuery(function($){
	// ============= スマートフォン用ページへの誘導 =================
	if ( (navigator.userAgent.indexOf('iPhone') > 0 && navigator.userAgent.indexOf('iPad') === -1) ||
		 navigator.userAgent.indexOf('iPod') > 0 || navigator.userAgent.indexOf('Android') > 0) {

		var href = document.URL;
		href = href.replace('.jp/','.jp/sp/');
		href = href.replace('.html','.php');
		if ( href.match('amp_japan/amp_talent') ) {
			href = 'http://www.a-l-e.jp/sp/talent/';
		}
		// 外部サイトから来た場合、直ちにスマートフォンサイトに飛ばす。
		var ref = document.referrer;
		if ( ref != '' && !ref.match('a-l-e.jp|astland.co.jp') ) {
			// ブックマークからきた場合を除き、外部サイトから来た場合、直ちにスマートフォンサイトに飛ばす。
			location.href = href;
//			$('body').prepend('<a href="' + href + '" id="smartPhone"><img src="/img/smartPhone.png"></a>');
		} else {
			// ページ上部にスマートフォンサイトへのリンク画像を表示する。
			$('body').prepend('<a href="' + href + '" id="smartPhone"><img src="/img/smartPhone.png"></a>');
		}
	}

	// ============= リストページのborderの切替 =================
	$('img.bdL').mouseover(function(){
		$(this).css('border-color',borderOnCol);
	}).mouseout(function(){
		$(this).css('border-color',borderOffCol);
	});

	// ================= 商品検索 =================
	$('#sh input[name=shQ]').focus(function(){
		if ( $(this).val() == '商品検索' ) { $(this).val(''); }		
	});

	$('#sh img').click(function(){
		var shQ = $('input[name=shQ]').val();
		var request = '';
		if ( shQ == '商品検索' || shQ == '' ) {
			//　return false;　なるべく検索してもらうようにコメントアウト
			request = '';
		} else {
			request = '?mode=search&shQ=' + shQ + '&backURL=' + $('input[name=backURL]').val();
		}
		location.href = 'http://www.a-l-e.jp/order/sh/' + request;
	});

	// ================= アイテムページの商品画像の切替 =================
	var imgData = [];
	$('img[name^=img]').each(function(){
		imgData[ $(this).attr('name') ] = $(this).attr('src'); 
	});
	var btnAlt		= '';// 商品画像の変更ボタンのalt属性値
	var imgFile		= '';// 商品画像のファイル名
	var imgPath		= '';// 商品画像のパス
	var imgTag		= '';// 商品画像のimgタグ
	var src 		= '';// 商品画像のsrc属性値
	var clickAlt	= '';// クリックされた商品画像の変更ボタンのalt属性値
		
	$('img.bd').click(function(){
		$('img.bd').css('border-color', borderOffCol);
		clickAlt = $(this).attr('alt');
		$('img.bd[alt=' + clickAlt + ']').css('border-color', borderOnCol);
		btnAlt	= $(this).attr('alt').split('-');
							// btnAlt[0] ... 商品画像用のimgタグのname属性 
							// btnAlt[1] ... 商品画像の番号
							// btnAlt[2] ... ケース画像の場合は case
		imgFile	= imgData[ btnAlt[0] ].substr(0,imgData[ btnAlt[0] ].length - 6);
		imgPath = imgFile.match(/^\/.*\//);
		imgTag	= $('img[name=' + btnAlt[0] + ']');

		if (btnAlt[2] == 'case') {
			src = imgPath + 'case_' + btnAlt[1] + '.jpg';
		} else {
			src = imgFile + btnAlt[1] + '.jpg';
		}
		
		imgTag.fadeOut(600, function(){
			if ( imgTag.hasClass('img') ) {
				imgTag.after('<img src="/img/blank.gif" class="img" id="loader">')
			} else {// 旧アイテムページ対策（ ex. GLENNの古いアイテムなど ）
				imgTag.after(	'<img src="/img/blank.gif" id="loader" width="' + imgTag.width() +
											'" height="' + imgTag.outerHeight(true) + '">')
			}
			if ( $.browser.msie === true ) {// IEではload()が正常に作動しないから。
																			// 特に2番目の画像が再表示されない。キャッシュの問題のようだ。
																			// この問題の解決のヒントは http://api.jquery.com/load-event/
				imgTag.attr('src', src).fadeIn(600);
				$('#loader').remove();
			} else {
				imgTag.attr('src', src).load(function(){
					imgTag.fadeIn(600);
					$('#loader').remove();
				});
			}
		});
	}).mouseover(function(){
		if ( clickAlt != $(this).attr('alt')) { $(this).css('border-color', borderOverCol); }
	}).mouseout(function(){
		if ( clickAlt != $(this).attr('alt')) { $(this).css('border-color', borderOffCol); }
	});

});
