/*
 * jcommon.js 1.0
 * Copyright (c) 2007 C.M.A. Co.,Ltd.
 *
 * Last Added: 2008-02-25
 *
 */



var ary = location.pathname.split('/');


var l=0;var isLinkAry=new Array;for(i=0;i<=ary.length-1;i++){isLinkAry[i]=ary.slice(i,i+1);isLinkAry[i]=isLinkAry[i].join('/');isLinkAry[i]=isLinkAry[i].replace(/index.*$/,"")}for(k=i+1;k<=i+ary.length;k++){isLinkAry[k]=ary.slice(0,ary.length-l);isLinkAry[k]=isLinkAry[k].join('/');isLinkAry[k]=isLinkAry[k].replace(/^\//,"");isLinkAry[k]=isLinkAry[k].replace(/index.*$/,"");l++}isLinkAry[0]=isLinkAry[0].replace(/\#.*$/,"");var jcommon={preloader:{loadedImages:[],load:function(url){var img=this.loadedImages;var l=img.length;img[l]=new Image();img[l].src=url}},URI:function(path){path=path.replace(/^https[^a-z]*/,"");path=path.replace(/^http[^a-z]*/,"");path=path.replace(document.domain,"");path=path.replace(/^[^a-z]*/,"");path=path.replace(/index.*$/,"");path=path.replace(/\/$/,"");this.absolutePath=path;this.len=isLinkAry.length;this.isSelfLink=false;while(this.len--){this.isSelfLink=this.isSelfLink||(this.absolutePath==isLinkAry[this.len])}}};

$(function(){

	//リンク画像はロールオーバーを設定
	$('a .btn').add('#depNav li a img').add('#subContent li a img').add('#subNav li a img').add('#depNav li a img').add('#depInNav li a img').each(function(){
		this.originalSrc = $(this).attr('src');
		this.rolloverSrc = this.originalSrc.replace(/(\.gif|\.jpg|\.png)/, "_on$1");
		jcommon.preloader.load(this.rolloverSrc);
	}).hover(function(){
		$(this).attr('src',this.rolloverSrc);
	},function(){
		$(this).attr('src',this.originalSrc);
	});

	//現在のページへのリンク
	 $('#subContent li a').add('#subNav li a:not([href="http://www.rap.ac.jp/"])').add('#depNav li a').add('#depInNav li a').each(function(){
		var href = new jcommon.URI($(this).attr('href'));
		if (href.isSelfLink) {
			$(this).addClass('current');
			$(this).find('img').each(function(){
				$(this).unbind('mouseenter');
				$(this).unbind('mouseleave');
				this.currentSrc = this.getAttribute('src').replace(/(\.gif|\.jpg|\.png)/, "_on$1");
				$(this).attr('src',this.currentSrc);
			});
		}
	});

	//外部リンクは別ウインドウを設定
	//除外が必要ない場合は:notから"])まで削除
	//追加はカンマで続ける ex. :not([href^="除外したいURL"],[href^="除外したいURL2"])
	$('a[href^="http://"]:not([href^="http://www.rad.ac.jp/"],[href^="http://design.rad.ac.jp/"],[href^="http://beauty.rad.ac.jp/"],[href^="https://www.rap.ac.jp/"])').click(function(){
		window.open(this.href, '_blank');
		return false;
	}).addClass('externalLink');

	//別ウィンドウを設定
	//追加はカンマで続ける ex. $('a[href$=".pdf"],a[href$=".asx"]')
	$('a[href$=".pdf"],a[href$=".wmv"]').click(function(){
		window.open(this.href, '_blank');
		return false;
	});
	
	// 直リンク画像はFaceboxで表示
	$('a[href$=".gif"]').add('a[href$=".jpg"]').add('a[href$=".jpeg"]').add('a[href$=".png"]').add('a[href$=".JPG"]').add('a[href$=".JPEG"]').add('a[href$=".png"]').facebox();

	//ulのliにoddとevenを追加
	$('ul').each(function(){
		$(this).find('li:odd').addClass('even');
		$(this).find('li:even').addClass('odd');
	});
	
	//n段組み
	$('.col3 li:nth-child(3n)').add('.col4 li:nth-child(4n)').addClass('clearMargin');


	//:first-child, :last-childをクラスとして追加
	$(':first-child').addClass('firstChild');
	$(':last-child').addClass('lastChild');

	//タブ ver1.0.1 
	$('.tabs').each(function(){
		var url = document.URL.match(/#.*/);
		$('.tabPage').hide();
		if(url == null){
			var tabDefault = $('.tabon a').attr('href').split('#');
			$('.tabon a').parent().addClass('tabon');
			$('#'+tabDefault[tabDefault.length-1]).show();
		}else{
			window.scrollTo(0,0);
			$('#tabControl li').removeClass();
			$('#tabControl li a[@href^="'+url+'"]').parent().addClass('tabon');
			$(''+url).show();
		}
		$('#tabControl li a').click(function() {
			splitID = $(this).attr('href').split('#');
			targetID = '#'+splitID[splitID.length-1];
			$('.tabPage').hide();
			$('#tabControl li').removeClass();
			$('#tabControl li a[@href^="'+targetID+'"]').parent().addClass('tabon');
			$(targetID).show();
			return false;
		});
	});


});
