// JavaScript Document



function init(){
	//alert("called init");
	
	//document.getElementByIdなどの関数が使えるかチェック
	if( !document.getElementsByTagName ) return false;
	if( !document.getElementById ) return false;
	
	
	//IDが存在するかチェック
	if( !document.getElementById("g-img") ) return false;
	if( !document.getElementById("w-img") ) return false;
	
	if( !document.getElementById("g-layer") ) return false;
	if( !document.getElementById("w-layer") ) return false;
	
	if( !document.getElementById("g-fabric") ) return false;
	if( !document.getElementById("w-fabric") ) return false;
	
	
	smartRollover("grove");
	smartRollover("wreath");
	
}


function chDetail( brand, type){
	
	//押されたボタンによってフェードアウト開始
	switch( brand ){
		case "grove":
		$("span", "#g-img").fadeOut(500);
		$("span", "#g-layer").fadeOut(500);
		$("span", "#g-fabric").fadeOut(500, function(){ infoFadeIn(); });
		break;
		
		case "wreath":
		$("span", "#w-img").fadeOut(500);
		$("span", "#w-layer").fadeOut(500);
		$("span", "#w-fabric").fadeOut(500, function(){ infoFadeIn(); });
		break;
	}
	
	//フェードイン関数
	function infoFadeIn(){
	
		var image = "";
		var layerTxt = "";
		var fabricTxt = "";
		
		switch( brand ){
			case "grove":
				switch( type ){
					//grove-plush
					case "plush":
					image = '<a href="../../imgdir/product/mattress/main/grove-plush.jpg" title="Grove グローブ"><img src="../../imgdir/product/mattress/thumb/grove-plush_thumb.jpg" alt="Grove グローブ" width="185" height="98" /></a>';
					layerTxt = "6層";
					fabricTxt = "表布（レーヨン49％ポリエステル51％）<br />STファイバー/STコンボリューテッドフォーム（1.5インチ）<br /> STフォーム（ファーム 0.5インチ）/STフォーム（スーパーソフト 1インチ）/<br />STフォーム（ファーム 0.5インチ）/メッシュ";
					break;
					
					//grove-firm
					case "firm":
					image = '<a href="../../imgdir/product/mattress/main/grove-firm.jpg" title="Grove グローブ"><img src="../../imgdir/product/mattress/thumb/grove-firm_thumb.jpg" alt="Grove グローブ" width="185" height="98" /></a>';
					layerTxt = "4層";
					fabricTxt = "表布（レーヨン49％、ポリエステル51％）<br />STファイバー/STフォーム（ファーム 1インチ）/<br />STフォーム（ファーム 2インチ）/ファイバーパッド";
					break;
				}
				//document.getElementsByClassName("gallery")[1].innerHTML = "<span>"+ image +"</span>";
				//var img = document.getElementById("g-img");
				//alert(document.getElementById("g-img").firstChild.className);

				//画像を消す
				document.getElementById("g-img").firstChild.removeChild(document.getElementById("g-img").firstChild.firstChild);
				//<span>作成
				var element = document.createElement('span');
				//<span>の中身を入れる
				element.innerHTML = image;
				document.getElementById("g-img").firstChild.appendChild(element);
				//alert($("#g-img"));
				
				document.getElementById("g-layer").innerHTML = "<span>"+ layerTxt +"</span>";
				document.getElementById("g-fabric").innerHTML = "<span>"+ fabricTxt +"</span>";
				
				//フェードイン開始
				$("span", "#g-img").fadeIn(500, function(){ aaa(); });
				$("span", "#g-layer").fadeIn(500);
				$("span", "#g-fabric").fadeIn(500);
				//$("span", "#g-fabric").fadeIn(500, function(){ alert(""); });
				
			break;
			
			
			case "wreath":
				switch( type ){
					//wreath-plush
					case "plush":
					image = '<a href="../../imgdir/product/mattress/main/wreath-plush.jpg" title="Wreath リース"><img src="../../imgdir/product/mattress/thumb/wreath-plush_thumb.jpg" alt="Wreath リース" width="185" height="101" /></a>';
					layerTxt = "4層";
					fabricTxt = "表布(レーヨン61％、ポリプロピレン39％)<br />STファイバー/STコンボリューテッドフォーム（1.5インチ）/<br />STフォーム（ファーム 1インチ）/メッシュュ";
					break;
					
					//wreath-firm
					case "firm":
					image = '<a href="../../imgdir/product/mattress/main/wreath-firm.jpg" title="Wreath リース"><img src="../../imgdir/product/mattress/thumb/wreath-firm_thumb.jpg" alt="Wreath リース" width="185" height="101" /></a>';
					layerTxt = "4層";
					fabricTxt = "表布(レーヨン61％、ポリプロピレン39％)<br />STファイバー/STフォーム（ファーム 1インチ）/<br />STフォーム（ファーム 0.5インチ）/ファイバーパッド";
					break;
				}
				//document.getElementById("w-img").innerHTML = "<span>"+ image +"</span>";
				//画像を消す
				document.getElementById("w-img").firstChild.removeChild(document.getElementById("w-img").firstChild.firstChild);
				//<span>作成
				var element = document.createElement('span');
				//<span>の中身を入れる
				element.innerHTML = image;
				document.getElementById("w-img").firstChild.appendChild(element);
				
				document.getElementById("w-layer").innerHTML = "<span>"+ layerTxt +"</span>";
				document.getElementById("w-fabric").innerHTML = "<span>"+ fabricTxt +"</span>";
				
				//フェードイン開始
				$("#w-img").fadeIn(1500, function(){ aaa(); });
				$("span", "#w-layer").fadeIn(1500);
				$("span", "#w-fabric").fadeIn(1500);
			break;
		}
	}//end of function
	
	
	function aaa(){
			$('.gallery a').lightBox();
	}
	
}














function smartRollover( idName ) {
	//alert("called smartRollover");
	var target = document.getElementById(idName);

	var images = target.getElementsByTagName("img");
	var oldActive;

	for(var i=0; i < images.length; i++) {
		if(images[i].getAttribute("src").match("_off."))
		{
			images[i].onmouseover = function() {
				this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));
			}
			
			images[i].onmouseout = function() {
				if( oldActive ){
					if( this !== oldActive ){
						this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));
					}
				} else {
					this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));
				}
			}
			images[i].onclick = function() {
				this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));
				if( oldActive ) oldActive.setAttribute("src", oldActive.getAttribute("src").replace("_on.", "_off."));
				oldActive = this;
			}
		}
	}
}





function addLoadEvent(func) {
	var oldonload = window.onload;
	if ( typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
		}
	}
}


addLoadEvent( init );
