jQuery(function($){
	$("body").addClass($.os.name);
	$(".categorymenu li:first-child").addClass("fl");
	var url = location.href;
	var cookiename = "koizumiCFsize";
	var setSize = new Array("100%", "116.7%", "133.4%");
	var resetSize = new Array("100%", "85.7%", "75%");
	var i=0;
	var chArea = $("#contents");
	var nochg = $(".dl, .np a, .np span");
	if($.cookie(cookiename)){ while(setSize.length > i){ if(setSize[i] == $.cookie(cookiename)){ break; }; i++; }}
	$.cookie(cookiename, setSize[i], {path:'/'});
	chArea.css("font-size",setSize[i]).css("line-height","166.7%");	
	nochg.css("font-size",resetSize[i]).css("line-height","166.7%");
		if($(".moji").length){
			var chImg = $(".moji a:eq("+(i)+") img");
			chImg.attr("src",chImg.attr("src").replace("_d.", "_s."));
		
			$(".moji a").click(function(index){
				var index = $(".moji a").index(this);
				$(".moji a img").each(function(){
					$(this).attr("src",$(this).attr("src").replace("_s.", "_d."));
				});
				sss = $(".moji a:eq("+(index)+") img");
				sss.attr("src",sss.attr("src").replace("_d.", "_s."));
				chArea.css("font-size",setSize[index]).css("line-height","166.7%");
				nochg.css("font-size",resetSize[index]).css("line-height","166.7%");
				$.cookie(cookiename, setSize[index], {path:'/'});
				if($("#mainvisual").length){
					koushinjouhou.location.reload();
				}
				return false;
			});
		}

	$("#menu li").each(function(){
		$(this).css("z-index",1);
		$(this).hover(
			function(){$(this).css("z-index",10)},
			function(){$(this).css("z-index",1)}
		);
	});
	
	if($("#mainvisual").length){
		/* オートムーブ */
		var $imgblock = $("#mainvisual .viewarea img")
		var $imgNum = $imgblock.length;
		var $plus,$minus;
		var array = []
		for( var al= 0; al < $imgNum; al++){
			if(al < Math.ceil($imgNum/2)){
				array[al] = al;
				$plus = al
			}else{
				array[al] = al - $imgNum;
				if(!$minus)$minus = array[al];
			}
		}
		var $haba = $imgblock.width();
		var $stopTime = 4000; var $moveTime = 1000; var $teaTime = $stopTime+$moveTime
		var flag = false;
		
		$imgblock.each(function(i){$(this).css("left",array[i]*$haba+"px");});
		function movie(obj, pX, way){
			obj.stop().animate({"left":pX+"px"},{duration:$moveTime,complete:function(){
				flag = false;
				posi = Number(obj.css("left").slice(0,-2));
				if(posi < $minus*$haba){if(way == "lt"){ test = array.pop(); array.unshift(test); obj.css("left",posi+$imgNum*$haba); }}
				if(posi > $plus*$haba){if(way == "gt"){ test = array.shift(); array.push(test); obj.css("left",posi-$imgNum*$haba); }}
			},step:function(){	flag = true;}});
		}
		
		function loopaction(){
			$imgblock.each(function(i){movie($(this), (array[i]-1)*$haba, "lt")});
			timerID = setTimeout(loopaction,$teaTime);
		}
		timerID = setTimeout(loopaction,$stopTime);
		
		/* ボタン操作 */
		$(".lt a").click(function() {
			if(!flag){
				clearTimeout(timerID);
				$imgblock.each(function(i){movie($(this), (array[i]+1)*$haba, "gt")});
				timerID = setTimeout(loopaction,$teaTime);
			}
		});
		$(".gt a").click(function() {
			if(!flag){
				clearTimeout(timerID);
				$imgblock.each(function(i){movie($(this), (array[i]-1)*$haba, "lt")});
				timerID = setTimeout(loopaction,$teaTime);
			}
		});
	}
	
	if(url.match("/product/")){
		$("a.pop").fancybox({
			'width'				: 750,
			'height'			: '75%',
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe'
		});
	}
	
	$("a").click(function(){
		if(this.href.match("imd-web.com") && $(this).attr("href") == "/"){
			location.href = "http://client.imd-web.com/nichinan/www/";
			return false;
		}else{
			if((this.href.match("file")) && (!$(this).attr("href").match(/html|pdf/)) && (!this.hash)){
				if($(this).attr("href") != "/"){
					$(this).attr("href",$(this).attr("href")+"index.html");
				}else{
					if(location.href.match("www/index.html")){ $(this).attr("href","./index.html"); }else{ $(this).attr("href","../index.html"); };
				}
			}
		}
	});
});


function wopen(u){
//	alert(navigator.userAgent);
	if(navigator.userAgent.indexOf("MSIE")>0 && getBrowserVersion() == 9){
		params = 'scrollbars=yes';
		w = 650;
	}else{
		params = 'scrollbars=no';
		w = 630;
	}
	win = window.open(u,'pop','width='+w+',height=660,'+params);
}
function getBrowserVersion() { // @return Number:
	return window.opera ? (opera.version().replace(/\d$/, "") - 0) // Opera10 shock
											: parseFloat((/(?:IE |fox\/|ome\/|ion\/)(\d+\.\d)/.
																		exec(navigator.userAgent) || [,0])[1]);
}


/*  ================================================================================
	Smart Rollover
================================================================================  */
function smartRollover() {
	if(document.getElementsByTagName) {
		var images = document.getElementsByTagName("img");

		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() {
					this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));
				}
			}
		}
	}
}
if(window.addEventListener) { window.addEventListener("load", smartRollover, false); }
else if(window.attachEvent) { window.attachEvent("onload", smartRollover); }


/*  ================================================================================
	Scroll Up
================================================================================  */
(function(){
	var easing = 0.25;
	var interval = 20;
	var d = document;
	var targetX = 0;
	var targetY = 0;
	var targetHash = '';
	var scrolling = false;
	var splitHref = location.href.split('#');
	var currentHref_WOHash = splitHref[0];
	var incomingHash = splitHref[1];
	var prevX = null;
	var prevY = null;
	var timeevent;
	
	addEvent(window, 'load', init);
	if(window.location.hash){
		$hash = window.location.hash.substr(1);
	}
	function init(){
		setOnClickHandler();
		if(incomingHash){
			if(window.attachEvent && !window.opera){
				setTimeout(function(){scrollTo(0,0);setScroll('#'+incomingHash);},50);
			}else{
				scrollTo(0, 0);
				setScroll('#'+incomingHash);
			}
		}
	}
	
	function addEvent(eventTarget, eventName, func){
		if(eventTarget.addEventListener){
			eventTarget.addEventListener(eventName, func, false);
		}else if(window.attachEvent){
			eventTarget.attachEvent('on'+eventName, function(){func.apply(eventTarget);});
		}
	}
	
	function setOnClickHandler(){
		var links = d.links;
		for(var i=0; i<links.length; i++){
			var link = links[i];
			var splitLinkHref = link.href.split('#');
			if(currentHref_WOHash == splitLinkHref[0] && d.getElementById(splitLinkHref[1])){
				addEvent(link, 'click', startScroll);
			}
		}
	}
	
	function startScroll(event){
		if(event){
			event.preventDefault();
		}else if(window.event){ // IE
			window.event.returnValue = false;
		}
		setScroll(this.hash);
	}
	
	function setScroll(hash){
		var targetEle = d.getElementById(hash.substr(1));
		if(!targetEle)return;
		var ele = targetEle;
		var x = 0;
		var y = 0;
		while(ele){
			x += ele.offsetLeft;
			y += ele.offsetTop;
			ele = ele.offsetParent;
		}
		var maxScroll = getScrollMaxXY();
		targetX = Math.min(x, maxScroll.x);
		targetY = Math.min(y, maxScroll.y);
		targetHash = hash;
			if(!scrolling){
			scrolling = true;
			scroll();
		}
	}
	
	function scroll(){
		var currentX = d.documentElement.scrollLeft||d.body.scrollLeft;
		var currentY = d.documentElement.scrollTop||d.body.scrollTop;
		var vx = (targetX - currentX) * easing;
		var vy = (targetY - currentY) * easing;
		var nextX = currentX + vx;
		var nextY = currentY + vy;
		if((Math.abs(vx) < 1 && Math.abs(vy) < 1) || (prevX === currentX && prevY === currentY)){
			scrollTo(targetX, targetY);
			scrolling = false;
			location.hash = targetHash;
			prevX = prevY = null;
			return;
		}else{
			scrollTo(parseInt(nextX), parseInt(nextY));
			prevX = currentX;
			prevY = currentY;
			timeevent = setTimeout(function(){scroll()},interval);
		}
	}
		
	$(function(){
		$("body").mousewheel(function(event, delta) {
			clearTimeout(timeevent);
				scrolling = false;
				return;
		});
	});
	
	function getDocumentSize(){
		return {width:Math.max(document.body.scrollWidth, document.documentElement.scrollWidth), height:Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)};
	}
	
	function getWindowSize(){
		var result = {};
		if(window.innerWidth){
			var box = d.createElement('div');
			with(box.style){
				position = 'absolute';
				top = '0px';
				left = '0px';
				width = '100%';
				height = '100%';
				margin = '0px';
				padding = '0px';
				border = 'none';
				visibility = 'hidden';
			}
			d.body.appendChild(box);
			var width = box.offsetWidth;
			var height = box.offsetHeight;
			d.body.removeChild(box);
			result = {width:width, height:height};
		}else{
			result = {width:d.documentElement.clientWidth || d.body.clientWidth, height:d.documentElement.clientHeight || d.body.clientHeight};
		}
		return result;
	}
	
	function getScrollMaxXY() {
		if(window.scrollMaxX && window.scrollMaxY){
			return {x:window.scrollMaxX, y:window.scrollMaxY};
		}
		var documentSize = getDocumentSize();
		var windowSize = getWindowSize();
		return {x:documentSize.width - windowSize.width, y:documentSize.height - windowSize.height};
	}

}());


