
/***************************************************
	ASAHI WOODTECH Javascript
****************************************************/
var currentCID;
var currentChildID;
var thisURL = location.href;
self.name="MainWindow";

$(function(){
	if(thisURL.indexOf('#') >= 0){
		var lastpoint = thisURL.lastIndexOf('#');
		var resStr = thisURL.substr(lastpoint,(thisURL.length - lastpoint));
	}
	thisURL = thisURL.replace(resStr,"");
	initRollOverImages();
	fontSizeInit();
	elevatorMenu();
	smoothScrollInit();
	initAccordionNavi();
});


function smoothScrollInit(){
	var content = document.getElementById("mainContent");
	var links = content.getElementsByTagName("a");
	var len = links.length;
	var i;
	var tmp;
	for(i=0;i<len;i++){
		tmp = hrefPearse(links[i].href);
		if(tmp){
			setMove(tmp,links[i]);
		}
	}
}

function setMove(tmp,obj){
	if(tmp){
		$(obj).click(function(){
			$.scrollTo(tmp,400);
			return false;
		});
	}
}

function hrefPearse(str){
	var len = str.length;
	var res;
	var last;
	
	if(str.indexOf(thisURL+'#') >= 0){
		last = str.lastIndexOf('#');
		res = str.substr(last,(len - last));
	}else{
		res = false;
	}
	return res;
}

/*FontSize
****************************************************
*/

function setStyle(classname){
	switch(classname){
		case"fontL":
			$("#global-footer-copyright").css("top","1.5em");
			$(".siteHome .container .content .main .section dl dt ").css("width","130px");		
		break;
		case"fontM":
			$("#global-footer-copyright").css("top","4px");
			$(".siteHome .container .content .main .section dl dt ").css("width","110px");
		break;
	}
}


function fontSizeInit(){
	var currentstyle = readCookie('fontStyle');
	if(!currentstyle){
		currentstyle = "fontM";
	}
	if (currentstyle){
		switchFont(currentstyle);
			switch(currentstyle){
			case"fontL":
			current_img("sizeL");
			break;
			case"fontM":
			current_img("sizeM");
			break;
		}
	};
	$("#sizeL").click(function(){
		switchFont("fontL");
		if($(this).attr("className")!="active"){
			$(this).addClass("active");
			$("#sizeM").removeClass("active");
			$("#sizeM").attr("src",$("#sizeM").attr("src").replace("_on.", "."));
		}
		return false;
	});
	$("#sizeM").click(function(){
		switchFont("fontM");
		if($(this).attr("className")!="active"){
			$(this).addClass("active");
			$("#sizeL").removeClass("active");
			$("#sizeL").attr("src",$("#sizeL").attr("src").replace("_on.", "."));
		}
		return false;
	});
}


function switchFont(className){
	$("body").removeClass("fontM");
	$("body").removeClass("fontL");
	$("body").addClass(className);
	setStyle(className);
	createCookie('fontStyle', className, 365);
}


/*
*	cookie script http://www.quirksmode.org/js/cookies.html
*/
function createCookie(name,value,days){
   if (days){
   var date = new Date();
   date.setTime(date.getTime()+(days*24*60*60*1000));
   var expires = "; expires="+date.toGMTString();
   }
   else var expires = "";
   document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name){
   var nameEQ = name + "=";
   var ca = document.cookie.split(';');
   for(var i=0;i < ca.length;i++)
   {
   var c = ca[i];
   while (c.charAt(0)==' ') c = c.substring(1,c.length);
   if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   }
   return null;
}

function current_img(str){
	var id="#"+str;
	var originalSrc = $(id).attr('src');
	if(originalSrc){
		var onSrc = originalSrc.replace(/(\.gif|\.jpg|\.png)/, "_on$1");
		$(id).attr('src',onSrc);
		$(id).addClass("active");
	}
}


/**
 ロールオーバー
******************************************************/

function initRollOverImages() {
  var image_cache = new Object();
  $("img.rover,input.rover").each(function(i) {
    var imgsrc = this.src;
    var dot = this.src.lastIndexOf('.');
    var imgsrc_ro = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
    image_cache[this.src] = new Image();
    image_cache[this.src].src = imgsrc_ro;
    $(this).hover(
    function() {
		if($(this).attr('class').indexOf("active")<0){
			this.src = imgsrc_ro;
		}
	},
    function() {
		if($(this).attr('class').indexOf("active")<0){
			this.src = imgsrc;
		} 
	});
  });
}

function Menu_active(id){
	
	var target_img = "#"+id;	
	$(target_img).each(function(i) {								
								
		var dot = this.src.lastIndexOf('.');
    	var imgsrc_ro = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
		this.src = imgsrc_ro;
		$(this).hover(
		  function() { this.src = imgsrc_ro; },
		  function() { this.src = imgsrc_ro; });
		
	});
	
}

function windowOpen(url,w,h,s){
	if(s){
		sb=1;
	}else{
		sb = 0;
	}
	var newWindow = window.open(url,"win01","width="+w+",height="+h+",scrollbars="+sb);
	newWindow.focus();
}

function printWindow(obj){
	windowOpen(obj.href,720,"auto",1);
	return false;
}


function localMenu_active(id){
	var target_img = "li#"+id; 
	$(target_img).each(function(i) {
		$(this).addClass("on");
	});
	var target_img = "dd#"+id; 
	$(target_img).each(function(i) {
		$(this).addClass("on");
	});
}


/*アコーディオン
*****************************************************************
*/

function initAccordionNavi(){
	
	$("#category-accordion dt.parent-category").each(function(i){
		if(i<1){
			$(this).css('cursor','pointer');
			$(this).click(function(){
				$("#category-accordion dt.parent-category").each(function(){
					$(this).find("img").attr("src",$(this).find("img").attr("src").replace("_on.jpg",".jpg"));
					$(this).find("img").removeClass("active");
					$(this).next('dd:visible').slideUp(400);
					$(this).next().find('dl').css("display","block");
				});
				$(this).find("img").attr("src",$(this).find("img").attr("src").replace(".jpg","_on.jpg"));
				$(this).find("img").addClass("active");
				$(this).next('dd:hidden').slideDown(400);
				
			});
		}
	});
	$("#category-accordion dt.parent-category").each(function(){
		$(this).next().css("display","none");
	});

	$("#category-accordion dl.children>dt").each(function(){
		$(this).next().hide();
		$(this).css('cursor','pointer');
	});
	
	accordionActive();
}


function accordionActive(){
	if(currentCID){
		$(currentCID+" img").attr("src",$(currentCID+" img").attr("src").replace(".jpg","_on.jpg"));
		$(currentCID+" img").addClass("active");
		$(currentCID).next().show();
		$(currentCID).next().find('ul').css("display","block");
		$(currentCID).next().find('dl').css("display","block");
	}
	if(currentChildID){
		$(currentChildID+" img").attr("src",$(currentChildID+" img").attr("src").replace(".gif","_on.gif"));
		$(currentChildID+" img").addClass("active");
		$(currentChildID).next().show();
	}
}

