/*
 * yuga.js 0.2.0β - 優雅なWeb制作のためのJS
 *
 * Copyright (c) 2007 Kyosuke Nakamura (kyosuke.jp)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Since:     2006-10-30
 * Modified:  2007-03-20
 
 */
 
/*  ------------------------------------------------------------------


		kakehashi jquery extend version 1.0.2
		for jQuery 1.2.1
		
		Modified: 07.10/01
		Copyright tenpo kakehashi
		Licensed under the MIT License:
		
リンクに絶対urlが含まれているときは、自サイトであってもすべて外部リンクになるため、notを使うことで回避する
.not('[@href^="http://localhost/"]')


---------------------------------------------------------------------*/



/*  プリローダー
-----------------------------------*/
var preloader = {
	loadedImages: [],
	load: function (url){
		var img = this.loadedImages;
		var l = img.length;
		img[l] = new Image();
		img[l].src = url;
	}
};

/*  ナビゲーションの設定
-----------------------------------*/
var nav = {
	//グローバルナビ
	//ターゲットとなる現在地の<dd>,<li>を配列として設定
	//<dd>,<li>の順番を記述する
	gNavName: {
		product: 1,
		nurikae: 2,
		eco: 3,
		hydrotect: 4,
		cases: 5,
		introduce: 6,
		faq: 7
	},
	
	//ローカルナビ用
	localNavName: {
	}
}


$(function(){
	
	/**
	* IEだけbodyの背景がずれるので修正(未完成: リサイズイベントに対応できていない）
	*/
	/*var uai = new UAIdentifier();
	if (uai.ie < 7) {
		alert($("html").width());
		if ($("html").width() % 2 != 0) {
			$("body").css( "margin-left", "-1px" );
		}
	}*/


	//bodyのclassを保存
	var bodyClass = document.body.className.split(" ");
	
	//URLからパスを抜き出す
	/*
	var _pathArr = location.pathname.split("/");
	_pathArr.shift();
	for (var i = 0,  j = _pathArr.length ; i < j; i++) {
		if(i == j-1) {
			if (_pathArr[i] == "index.html" || _pathArr[i] == "") {
				_pathArr[i] = "top";
			} else {
				_pathArr[i] = _pathArr[i].slice(0,-5);
			}
		}
		//alert("_pathArr[" + i + "]" + _pathArr[i]);
	}
	//var targetGNav, targetLNav, targetL3rdNav;
	*/
	
	//-------------------------------------------------★★<ul>や<dl>を設定する
	//targetGNav = "dl#gNav dd:nth-child(" + nav.gNavName[bodyClass[0]] + ") a";
	//targetGNav = "ul#gNav li:nth-child(" + nav.gNavName[bodyClass[0]] + ") a";
	
	//bodyのclassを保存
	var bodyClass = document.body.className.split(" ");
	
	//-------------------------------------------------★★<ul>や<dl>を設定する
	//targetGNav = "dl#gNav dd:nth-child(" + nav.gNavName[bodyClass[0]] + ") a";
	targetGNav = "ul#gNav li:nth-child(" + nav.gNavName[bodyClass[0]] + ") a";
	
	/* 現在地を示す関数。bodyClass[0]と一致するかどうかで判定 
	-----------------------------------*/
	if(nav.gNavName[bodyClass[0]] != undefined) {
		$(targetGNav).addClass("current");
	}
	

	//class="swap"はロールオーバーを設定（src属性を_on付きのものに差し替える）
	$('.swap').each(function(){
		this.originalSrc = this.src;
		var srcLen = this.originalSrc.lastIndexOf('.');
		var ftype = this.originalSrc.substring(srcLen, this.originalSrc.length);
		var fname = this.originalSrc.substring(0, srcLen);
		this.rolloverSrc = fname + "_on" + ftype;		
		this.currentSrc = fname + "_cr" + ftype;
		preloader.load(this.rolloverSrc);
	}).hover(function(){
		this.src = this.rolloverSrc;
	},function(){
		this.src = this.originalSrc;
	});
	
	
	//currentが付いている.swapは現在地を示すように変更（src属性を_on付きのものに差し替える）
	//.loadより.eachの方が正しく動作する
	$('a.current .swap').each(function(){
		this.src = this.rolloverSrc;
		preloader.load(this.rolloverSrc);
	}).unbind('mouseover').unbind('mouseout');
	
 
	//ページ内リンクはするするアニメーション
	//$("body").ScrollToAnchors(500,'easeout')
	$('a[href^=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body')
				.animate({scrollTop: targetOffset}, 500);
				return false;
			}
		}
	});

});

/* ==========================================================================================================

	 関数
 
============================================================================================================*/

/*  偶数と奇数クラスを追加する関数
-----------------------------------*/
function setOddEven (elem) {
	var oddText = elem + ":even";
	var evenText = elem + ":odd";
	$(oddText).addClass('odd');
	$(evenText).addClass('even');
}

/*  IE6以下のみ対応していない:hoverの代わりに.hoverクラス付与関数
-----------------------------------*/
function setHover(elem) {
	$(elem).hover(function() {
		$(this).addClass("hover");},function() {
		$(this).removeClass("hover");
	});
}


/*  ループをクリアー
	@param
		parent			親要素の
		targetChild		子要素で
		colNum			何番目の子要素に
		addClassName	クラス名を付与して
		appChild		クリアする
-----------------------------------*/
function floatLoopClear(parent, targetChild, colNum, addClassName, appChild) {
	//親要素が存在するか
	if($(parent).length) {
		var targetPath = parent + '>'+ targetChild + ':nth-child(' + colNum + 'n)';
		$(targetPath).addClass(addClassName).after(appChild);
	}
}