var showLayerObjects = new Array();

function showLayerInit(){
	var blocks = document.getElementsByTagName("div");
	for(var i=0; i<blocks.length; i++) {
		var obj = blocks[i];
		if(obj.getAttribute("rel")){
			if(obj.getAttribute("rel").match(/showLayer\[([^\]]+)\]/)){
				var id = RegExp.$1;
				var isShow = true;
				if(getClass(obj).match(/showLayerDefaultHide/)) isShow = false;
				obj.handler = new showLayer(obj, isShow, id);
				showLayerObjects[id] = obj.handler;
			}
		}
	}
}

var showLayer = function(id){
	this.init.apply(this,arguments);
};

showLayer.prototype={
	_interval:30,
	_obj:null,
	_id:null,
	_padObj:null,
	_height : 0,
	_n : 1,
	_show : false,
	_resolution : 5,
	_direction : null,
	init:function(obj, isShow, id){
		this._obj = obj;
		this._id = id;
		this._height = this._obj.offsetHeight;
		this._obj.style.overflow = "hidden";
		this._show = isShow;
		if(isShow){
//			this._obj.style.display = "block";
//			this._obj.style.position = "relative";
//			this._obj.style.top = "0px";
//			this._obj.style.left = "0px";
		} else{
			//IE
			if(document.all){
//				var padObj = document.createElement('img');
//				this._obj.parentNode.insertBefore(padObj, this._obj);
//				padObj.style.src = "img/spacer.gif";
//				padObj.style.height = "1px";
//				this._padObj = padObj;
			}
		}
	},
	move:function(direction, isSkip){
		if(this._padObj) this._padObj.style.display = "none";
		this._obj.style.position = "relative";
		this._obj.style.top = "0px";
		this._obj.style.left = "0px";
		this._obj.style.display = "block";
		this._direction = direction;
		if(this._n >= 1){
			this._show = !this._show;
			this._n  = (isSkip)?1:0;
			this.callback();
			return this._show;
		}
	},
	moveSkipAnime:function(){
		this.move(1, true);
	},
	change:function(event){
		if(!this._show){
			this.show(event);
		} else{
			this.hide(event);
		}
	},
	show:function(event){
		var obj = getObj(this._id + "Button");
		if(obj){
			obj.style.backgroundImage = "url(../common/search_cate_on.gif)";
			obj.style.color = "#FFFFFF";
		} else{
			if(window.event){
				obj = window.event.srcElement;
			} else{
				obj = arguments.callee.caller.arguments[0].target;
			}
			var c = getClass(obj);
			setClass(obj, c.replace("close", "open"));
		}
		
		if(!this._show){
			return this.move(1);
		}
	},
	hide:function(event){
		var obj = getObj(this._id + "Button");
		if(obj){
			obj.style.backgroundImage = "url(../common/search_cate_off.gif)";
			obj.style.color = "#005BAC";
		} else{
			if(window.event){
				obj = window.event.srcElement;
			} else{
				obj = arguments.callee.caller.arguments[0].target;
			}
			var c = getClass(obj);
			setClass(obj, c.replace("open", "close"));
		}

		if(this._show){
			return this.move(-1);
//			this._obj.style.position = "relative";
//			this._obj.style.top = "0px";
//			this._obj.style.left = "0px";
//			this._obj.style.height = "0px";
//			this._obj.style.display = "none";
			this._show = !this._show;
			return false;
		}
	},
	callback:function(){
		f = this.event.update(this, this._obj);
		if(f) setTimeout(this.bind(this.callback), this._interval);
	},
	bind:function(method,arg){
		var _this=this;var _arg=(arg)?arg:[];
		return function(){
			method.apply(_this,_arg);
		}
	}
};

showLayer.prototype.event={
	update:function(t,o){
		var h;
		t._n = Math.min(1, (t._n * 1000 + (t._direction * 1000) / t._resolution) / 1000);

		if(t._show){
			h = Math.pow(t._n, 2) * t._height;
		} else{
			h = (1 - Math.pow(t._n, 2)) * t._height;
		}

		o.style.height = h + "px";

		if(t._n >= 1 || t._n <= -1){
			if(!t._show){
				o.style.display = "none";
			}
			else{
				o.style.position = "static";
				o.style.height = "";
				o.style.left = "1px";
				o.style.top = "1px";
			}
			t._n = 1;
			return false;
		} else{
			return true;
		}
	}
};

addEvent(window,"load", showLayerInit);

