/*
 * yuga.js 0.1.0 - 優雅なWeb制作のためのJS
 *
 * Copyright (c) 2006 Kyosuke Nakamura (kyosuke.jp)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Since:     2006-10-30
 * Modified:  2006-11-15
 */

/*
 * [使用方法] XHTMLのhead要素内で次のように読み込みます。
 
<link rel="stylesheet" href="css/thickbox.css" type="text/css" media="screen" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/interface.js"></script>
<script type="text/javascript" src="js/thickbox.js"></script>
<script type="text/javascript" src="js/yuga.js" charset="utf-8"></script>

 */

var yuga = {
	preloader: {
		loadedImages: [],
		load: function (url){
			var img = this.loadedImages;
			var l = img.length;
			img[l] = new Image();
			img[l].src = url;
		}
	},
	uri: {
		dirName: function(uri){
			var ary = uri.split('/');
			ary.pop();
			return ary.join('/');
		},
		path: function (uri){
			return uri.split('#')[0];
		},
		anchorName: function (uri){
			return uri.split('#')[1];
		},
  	//グローバル用
		isSelfLink: function(href){
			if (this.dirName(href).indexOf("www") !== this.dirName(href).length-3){    //サイトパスによって変更
				return ((this.path(href) == this.path(location.href)) || (this.path(href) == this.dirName(location.href)+'/') || (this.dirName(href) == this.dirName(location.href)) || (this.dirName(href) == this.dirName(this.dirName(location.href))));
			}
		},
  	//追加：サイドナビゲーション用
		isSelfLink2: function(href){
			if (this.dirName(href).indexOf("www") !== this.dirName(href).length-3){    //サイトパスによって変更
				return ((this.path(href) == this.path(location.href)));
			}
		}
	}
};

$(function(){
	
	//リンク画像はロールオーバーを設定
	$('dl#gnavi dd ul li a img').each(function(){
		this.originalSrc = $(this).src();
		this.rolloverSrc = this.originalSrc.replace(/(\.gif|\.jpg|\.png)/, "_o$1");
		yuga.preloader.load(this.rolloverSrc);
	}).hover(function(){
		$(this).src(this.rolloverSrc);
	},function(){
		$(this).src(this.originalSrc);
	});

	//現在のページへのリンク（グローバル用）
	$('dl#gnavi dd ul li a').each(function(){
		if (yuga.uri.isSelfLink($(this).href()) && (this.href !=='http://www.e-fresco.co.jp/index.html') && !yuga.uri.anchorName($(this).href())) {
			$(this).addClass('current');
			//img要素が含まれていたら現在用画像に設定
			$(this).find('img').each(function(){
				//ロールオーバーが設定されていたら削除
				$(this).unbind('mouseover');
				$(this).unbind('mouseout');
				this.currentSrc = this.getAttribute('src').replace(/(\.gif|\.jpg|\.png)/, "_c$1");
				$(this).src(this.currentSrc);
			});
		}
	});

 	//追加：サイドナビゲーション用
	$('dl.sideMenu dd ul li a').each(function(){
		if (yuga.uri.isSelfLink2($(this).href()) && (this.href !=='http://www.e-fresco.co.jp/index.html') && !yuga.uri.anchorName($(this).href())) {
			$(this).addClass('current');
			//img要素が含まれていたら現在用画像に設定
			$(this).find('img').each(function(){
				//ロールオーバーが設定されていたら削除
				$(this).unbind('mouseover');
				$(this).unbind('mouseout');
				this.currentSrc = this.getAttribute('src').replace(/(\.gif|\.jpg|\.png)/, "_c$1");
				$(this).src(this.currentSrc);
			});
		}
	});

});