Kotohaco = new Object;

Kotohaco.Naturum = {
	init : function() {
		this.panel_min_width = 180;

		var elements = ["category", "option", "genre", "maker", "price"];
		for (var i=0; i<elements.length; i++) {
			if (Kotohaco.Naturum.getValueFromCookieByKey(elements[i]) === "none") {
				Kotohaco.Naturum.m_hidePanel(elements[i]);
			}
		}

		Kotohaco.Naturum.setContainerHeight();
	},

	hidePanel : function(elemid) {
		Kotohaco.Naturum.m_hidePanel(elemid);
		Kotohaco.Naturum.shrinkPanel(elemid);
		Kotohaco.Naturum.setContainerHeight();
	},
	m_hidePanel : function(elemid) {
		tgt = document.getElementById(elemid);
		if (tgt === null) {
			return;
		}
		Kotohaco.Naturum.setCookie(elemid, "none");
		tgt.style.display = "none";

		icon1 = document.getElementById(elemid + "_icon1");
		if(icon1 !== null){
			icon1.style.display = "none";
		}
		icon2 = document.getElementById(elemid + "_icon2");
		if(icon2 !== null){
			icon2.style.display = "block";
		}
	},

	showPanel : function(elemid) {
		Kotohaco.Naturum.m_showPanel(elemid);
		Kotohaco.Naturum.extendPanel(elemid);
		Kotohaco.Naturum.setContainerHeight();
	},
	m_showPanel : function(elemid) {
		tgt = document.getElementById(elemid);
		if (tgt === null) {
			return;
		}
		Kotohaco.Naturum.setCookie(elemid, "block");
		tgt.style.display = "block";

		icon1 = document.getElementById(elemid + "_icon1");
		if(icon1 !== null){
			icon1.style.display = "block";
		}
		icon2 = document.getElementById(elemid + "_icon2");
		if(icon2 !== null){
			icon2.style.display = "none";
		}
	},

	morePanel : function(elemid) {
		Kotohaco.Naturum.m_morePanel(elemid);
		Kotohaco.Naturum.setContainerHeight();
	},
	m_morePanel : function(elemid) {
		tgt = document.getElementById(elemid + "_hidden");
		if(tgt === null){
			return;
		}
		tgt.style.display = "block";

		more = document.getElementById(elemid + "_more");
		if(more !== null){
			more.style.display = "none";
		}
		less = document.getElementById(elemid + "_less");
		if(less !== null){
			less.style.display = "block";
		}
	},

	lessPanel : function(elemid) {
		Kotohaco.Naturum.m_lessPanel(elemid);
		Kotohaco.Naturum.setContainerHeight();
	},
	m_lessPanel : function(elemid) {
		tgt = document.getElementById(elemid + "_hidden");
		if(tgt === null){
			return;
		}
		tgt.style.display = "none";

		more = document.getElementById(elemid + "_more");
		if(more !== null){
			more.style.display = "block";
		}
		less = document.getElementById(elemid + "_less");
		if(less !== null){
			less.style.display = "none";
		}
	},

	extendPanel : function(elemid) {
		tgt = document.getElementById(elemid);
		if (tgt === null) {
			return;
		}

		/* max width */
		w = this.panel_min_width;
		spans = tgt.getElementsByTagName('span');
		for(var i=0; i<spans.length; i++){
			classList = spans[i].className.split(' ');
			var depth = 0;
			var b = false;
			for(var j=0; j<classList.length; j++){
				if(classList[j]==='extendable'){
					b = true;
				}
				else if(classList[j].match(/^depth/)){
					depth = Number(classList[j].replace(/^depth/,""));
				}
			}
			if(b){
				w2 = spans[i].offsetWidth + 40 + (depth*6);
				w  = Math.max(w,w2);
			}
		}
		tgt.style.width = w + "px";

		tgt2 = document.getElementById(elemid + "_end");
		if (tgt2 !== null){
			tgt2.style.width = w + "px";
		}
		tgt3 = document.getElementById(elemid + "_head");
		if (tgt3 !== null){
			tgt3.style.width = w + "px";
		}
	},

	shrinkPanel : function(elemid) {
		tgt = document.getElementById(elemid);
		if (tgt === null) {
			return;
		}
		tgt.style.width = this.panel_min_width + "px";

		tgt2 = document.getElementById(elemid + "_end");
		if (tgt2 !== null){
			tgt2.style.width = this.panel_min_width + "px";
		}
		tgt3 = document.getElementById(elemid + "_head");
		if (tgt3 !== null){
			tgt3.style.width = this.panel_min_width + "px";
		}			
	},

	setCookie : function(key, val) {
		var cookiestr = key + "=" + escape(val);

		t = new Date();
		t.setTime(t.getTime() + (14 * 1000 * 60 * 60 * 24));
		cookiestr = cookiestr + ";expires=" + t.toGMTString();

		document.cookie = cookiestr;
	},

	getValueFromCookieByKey: function(key) {
		var cookieArray = document.cookie.split("; ");

		for(var i=0; i<cookieArray.length; i++){
			if (cookieArray[i].split("=")[0] === key){
				return cookieArray[i].split("=")[1];
			}
		}
		return null;
	},

	setContainerHeight: function(){
		tgt=document.getElementById('container');
		if (tgt === null){
			return;
		}

		obj1 = document.getElementById('search_interface');
		obj2 = document.getElementById('search_result');
		if (obj1 === null || obj2 === null){
			return;
		}

		tgt.style.height = Math.max(obj1.clientHeight, obj2.clientHeight)+"px";
	}
};
