var basicSet = {

	// 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){

			/*var img;

			img = new Image();

			img.src = path;

			path = img.src;

			img.src = '#';*/
			var anc = document.createElement("a");
			anc.href = path;
			path = anc.href;
			delete anc;

			return path;

		};

	

		this.absolutePath = this.getAbsolutePath(s);

	

		//絶対パスを分解

		var a, d, f, fn, fq, ff;

		a = this.absolutePath.split('://');

		this.schema = a[0];

		d = a[1].split('/');

		this.host = d.shift();

		f = d.pop();

		this.dirs = d;

		this.file = f.split('?')[0].split('#')[0];

		fn = this.file.split('.');

		this.fileExtension = (fn.length == 1) ? '' : fn.pop();

		this.fileName = fn.join('.');

		fq = f.split('?');

		this.query = (fq[1]) ? fq[1].split('#')[0] : '';

		ff = f.split('#');

		this.fragment = (ff[1]) ? ff[1].split('?')[0] : '';	



	

		//同じ文書にリンクしているかどうか

		this.linkPath = this.absolutePath.split('#')[0].split('index.')[0];

		this.pagePath = location.href.split('#')[0].split('index.')[0];

		

		//キャンパスツアーアーカイブ、特別授業アーカイブ用

		this.pagePath = this.pagePath.split('campustour-archive.')[0].split('openclass-archive.')[0];

		this.isSelfLink = (this.linkPath == this.pagePath);

		

		//console.log(this.linkPath + ',' + this.pagePath);

	}

};



$(function(){

	//グローバルナビのカレント表示

	$('div#globalNavi ul li a[href]').each(function(){

		var image_cache = new Object();

		var href = new basicSet.URI(this.getAttribute('href'));

		$('img',this).each(function(){

			var imgsrc = this.src;

			var dot = this.src.lastIndexOf('.');

			var imgsrc_on = this.src.substr(0, dot) + '-on' + this.src.substr(dot, 4);

			image_cache[this.src] = new Image();

			image_cache[this.src].src = imgsrc_on;

			

			//同じカテゴリーかどうか

			var pageCategory = location.href.split('/')[3];

			var linkCategory;

			if(href.dirs[0]){

				linkCategory = href.dirs[0];

			}else{

				linkCategory = href.file;

			}

			//塾大連携に対応

			if(pageCategory=='program'){pageCategory = 'curriculum';}

			if (linkCategory == pageCategory) {

				this.src = imgsrc_on;

			} else {

				$(this).hover(

					function() { this.src = imgsrc_on; },

					function() { this.src = imgsrc; });

			}

			//console.log(pageCategory + ',' + linkCategory);

		});

	});

		

	$('div#localNavi dl dl dd').each(function(){

		$(this).css('display','none');

	});

	//現在のページへのリンク

	$('div#localNavi dl dl dl dl a[href]').each(function(){

		var href = new basicSet.URI(this.getAttribute('href'));

		if (href.isSelfLink && !href.fragment) {

			$(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode).addClass('current');

			$(this.parentNode.parentNode.parentNode.parentNode.parentNode).addClass('current');

			$(this.parentNode.parentNode.parentNode).addClass('current');

			$(this.parentNode).addClass('current');

		}

	});

	$('div#localNavi dl dl dl a[href]').each(function(){

		var href = new basicSet.URI(this.getAttribute('href'));

		if (href.isSelfLink && !href.fragment) {

			$(this.parentNode.parentNode.parentNode.parentNode.parentNode).addClass('current');

			$(this.parentNode.parentNode.parentNode).addClass('current');

			$(this.parentNode).addClass('current');

		}

	});

	$('div#localNavi dl dl a[href]').each(function(){

		var href = new basicSet.URI(this.getAttribute('href'));

		if (href.isSelfLink && !href.fragment) {

			$(this.parentNode.parentNode.parentNode).addClass('current');

			$(this.parentNode).addClass('current');

		}

	});

	$('div#localNavi dl a[href]').each(function(){

		var href = new basicSet.URI(this.getAttribute('href'));

		if (href.isSelfLink && !href.fragment) {

			$(this.parentNode).addClass('current');

		}

	});

	$('div#localNavi dl dd.current dl dd').each(function(){

		$(this).css('display','block');

	});

});
