/*
 * yuga.js 0.4.2 - 優雅な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-09-11
 *
 * jQuery 1.2.1
 * ThickBox 3.1
 * Interface 1.2 (Effects scroll)
 */


/* yuga.js内で使っているfunction群 */
var yuga = {
	// 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);
		//同じ文書にリンクしているかどうか
		this.isSelfLink = (this.absolutePath == 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(){
	
	//class="btn"はロールオーバーを設定（src属性を_on付きのものに差し替える）
	$('#gNav img').each(function(){
		this.originalSrc = $(this).attr('src');
		this.rolloverSrc = this.originalSrc.replace(/(\.gif|\.jpg|\.png)/, "_on$1");
		yuga.preloader.load(this.rolloverSrc);
	});
	//通常ロールオーバー
	$('#gNav img').not($('.btngroup .btn')).hover(function(){
		$(this).attr('src',this.rolloverSrc);
	},function(){
		$(this).attr('src',this.originalSrc);
	});
	//グループ化されたロールオーバー
	$('.btngroup').hover(function(){
		$(this).find('.btn').each(function(){
			$(this).attr('src',this.rolloverSrc);
		});
	},function(){
		$(this).find('.btn').each(function(){
			$(this).attr('src',this.originalSrc);
		});
	});
	
	//現在のページへのリンク
	$('a[@href]').each(function(){
		var href = new yuga.URI(this.getAttribute('href'));
		if (href.isSelfLink && !href.fragment) {
			$(this).addClass('active');
		}
	});

	//グローバルナビのロールオーバーをオン状態に
	$('.topBody #gNav .toppageNav img,.jarBody #gNav .jarNav img,.refugeeBody #gNav .refugeeNav img,.eventBody #gNav .eventNav img,.kifuBody #gNav .kifuNav img,.contactBody #gNav .contactNav img').each(function(){
		this.currentSrc = this.getAttribute('src').replace(/(\.gif|\.jpg|\.png)/, "_on$1");
		$(this).attr('src',this.currentSrc).hover(function(){
			$(this).attr('src',this.currentSrc);
		},function(){
			$(this).attr('src',this.currentSrc);
		});
	});


	//奇数、偶数を自動追加
	$('#hoge ul').each(function(){
		$(this).find('li:odd').addClass('even');
		$(this).find('li:even').addClass('odd');
	});
	$('#hoge table').each(function(){
		$(this).find('tr:odd').addClass('even');
		$(this).find('tr:even').addClass('odd');
	});


});




//フッターの最初の要素にクラス追加
$(function(){
	$('.infoList dt:first-child').addClass("first");
});


//入力フィールドをクリックするとデフォルトのテキストが消える
var isInitialized = false; function initialize(object) {
	if(!isInitialized) {
		object.value = "";
		object.style.color = "#333"; isInitialized = true;
	}
}


//外部リンクは別ウインドウを設定
$(document).ready( function () {
	$('a[@href^="http"]').not('[@href^="http://www.refugee.or.jp/"]').click(function(){
	window.open(this.href, '');
	return false;
	});
	$('a[@href^="http"]').not('[@href^="http://www.refugee.or.jp/"]').addClass("exLink");
});


