/*------------------------------------------------------------
 *ページ読み込み時の処理
/*------------------------------------------------------------*/
//init.js
var init = {
	// imageのプリローダー
	preloader: {
		loadedImages: [],
		load: function (url){
			var img = this.loadedImages;
			var l = img.length;
			img[l] = new Image();
			img[l].src = url;
		}
	},
	// URIを解析したオブジェクトを返すfunction
	URI: function(s){
		this.originalPath = s;
		
		//絶対パスを取得
			this.getAbsolutePath = function(path){
				
				if (!path.match(/^(mailto:)|(javascript:)/)) {
					var img = new Image();
					img.src = path;
					path = img.src;
					img.src = '#';
				}
				return path;
			};
	
		this.absolutePath = this.getAbsolutePath(s);
		
		//location絶対パス最後の文字
		var _loc = location.href;
		var _lastloc = _loc.charAt( _loc.length - 1 )
		//alert('最後の文字 : '+ _lastloc)
		if(_lastloc == '/'){
			_loc = _loc +'index.html';
		}
		//同じ文書にリンクしているかどうか
		this.isSelfLink = (this.absolutePath == _loc/*location.href*/);

		
		//絶対パスを分解
			var fields = {'schema' : 2, 'username' : 5, 'password' : 6, 'host' : 7, 'path' : 9, 'query' : 10, 'fragment' : 11};
			var r = /^((\w+):)?(\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/.exec(this.absolutePath);
			for (var field in fields) {
				this[field] = r[fields[field]]; 
			}
		
	}
	
};





$(function(){

	//現在のページへのリンク
	$('a[@href]').each(function(){
		var href = new init.URI(this.getAttribute('href'));
		if (href.isSelfLink && !href.fragment) {
			$(this).addClass('current');
			//img要素が含まれていたら現在用画像（_cr）に設定
			$(this).find('img').each(function(){
				//ロールオーバーが設定されていたら削除
				$(this).removeClass('xOver');
				$(this).unbind('mouseover');
				$(this).unbind('mouseout');
				this.currentSrc = this.getAttribute('src').replace(/(\.gif|\.jpg|\.png)/, "_cr$1");
				$(this).attr('src',this.currentSrc);
			});
		}
	});
	//class="xCr"はロールオーバーを設定（src属性を'_cr'付きのものに差し替える）
	$('.xCr').each(function(){
		this.originalSrc = $(this).attr('src');
		this.xcurrentSrc = this.originalSrc.replace(/(\.gif|\.jpg|\.png)/, "_cr$1");
		$(this).attr('src',this.xcurrentSrc);
	})
	
	//class="xOver"はロールオーバーを設定（src属性を'_2'付きのものに差し替える）
	$('.xOver').each(function(){
		this.originalSrc = $(this).attr('src');
		this.rolloverSrc = this.originalSrc.replace(/(\.gif|\.jpg|\.png)/, "_2$1");
		init.preloader.load(this.rolloverSrc);
	}).hover(function(){
		$(this).attr('src',this.rolloverSrc);
	},function(){
		$(this).attr('src',this.originalSrc);
	});
});
