﻿// JavaScript Document
var Sidebar = function(_boxEle,_itemEle,_bodyEle){
	
	var box = $(_boxEle);
	
	var matchHref = function(_obj){
		if(!_obj) return false;
		
		var localhref = window.location.href;
		var reBool = new Boolean(false);
		_obj.find('a').each(function(){
			var thisHref = $(this).attr('href').replace(/\.\.\//,'').toString();
			thisHref = thisHref.replace(/\?title=/,'/').toString();
			thisHref = thisHref.replace(/\&.*$/,'');
			if(localhref.match(thisHref)){
				reBool = true;
			}
		});
		return reBool;
	}
	
	box.find(_itemEle).each(function(){
		if($(this).find(_bodyEle).length > 0){
			var thisItem = $(this);
			var bodyArr = thisItem.find(_bodyEle);
			
			thisItem.find(_bodyEle).remove();
			thisItem.html('<span class="open itemTit">' + thisItem.html() + '</span>');
			thisItem.append(bodyArr);
			
			var itemBody = thisItem.find(_bodyEle);
			var itemTit = thisItem.find('.itemTit').eq(0);
			
			if(matchHref(thisItem) == true){
				itemBody.css('display','block');
				itemTit.removeClass('open');
				itemTit.addClass('close');
			}else{
				itemBody.css('display','none');
			}
			
			itemTit.css('cursor','pointer');
			
			itemTit.click(function(){
				if(itemBody.css('display') == 'none'){
					itemTit.removeClass('open');
					itemTit.addClass('close');
					itemBody.css('display','block');
				}else{
					itemTit.removeClass('close');
					itemTit.addClass('open');
					itemBody.css('display','none');
				}
			});
		}
	});	
}
