/**
 * goPIXit - slide menu
 * @author Mihai Gabriel Muntenas
 * @contact mihai.muntenas@{gmail,yahoo}.com
 * 
 * @version 1.0
 */

var SlideElement = function (element,triggerElement,width)
{	
	this.el = (typeof element == 'string' ? document.getElementById(element) : element);
	this.trigger = (typeof triggerElement == 'string' ? document.getElementById(triggerElement) : triggerElement);
	
	this.el.__is_sliding = false;
	this.el.__slide_step = 10;
	this.el.__is_collapsed = parseInt(this.el.offsetTop) < 0;
	
	this.trigger.targetEl = this.el;
	this.trigger.style.width = width ? width: this.trigger.parentNode.offsetWidth+'px';
	//this.trigger.style.left = this.trigger.parentNode.offsetLeft +'px';
	
	this.trigger.onmouseover = function(e){
		if (!e) var e = window.event;
		if (this.targetEl.__is_collapsed) this.targetEl.__slide_down();
	};
	
	this.trigger.onmouseout = function(e){
		if (!e) var e = window.event;
		var relTarg = e.relatedTarget || e.toElement;
		if (!this.targetEl.__is_collapsed && !this.__has_child(relTarg)) this.targetEl.__slide_up();
	};
	
	this.el.__do_slide = function(direction){
		if (direction == 'up'){
			if (parseInt(this.style.marginTop) > - parseInt(this.offsetHeight)){
				this.style.marginTop = (parseInt(this.style.marginTop) - this.__slide_step) + 'px';
			} else {
				this.style.marginTop = '-' + this.offsetHeight;
				this.__is_sliding = false;
				this.__is_collapsed = true;
				window.clearInterval(this.__slide_interval_id);
			}			
		} else {
			if (parseInt(this.style.marginTop) < 0 ){
				this.style.marginTop = (parseInt(this.style.marginTop) + this.__slide_step) + 'px';
			} else {
				this.style.marginTop = '0px';
				this.__is_sliding = false;
				this.__is_collapsed = false;
				window.clearInterval(this.__slide_interval_id);
			}
		}
	}
	
	this.el.__slide_up = function (){
		if (this.__is_sliding || this.__is_collapsed) return;
		this.__slide_interval_id = window.setInterval('document.getElementById(\'' + this.id + '\').__do_slide(\'up\')',50);
		this.__is_sliding = true;
	}
	
	this.el.__slide_down = function (){
		if (this.__is_sliding || !this.__is_collapsed) return;
		this.__slide_interval_id = window.setInterval('document.getElementById(\'' + this.id + '\').__do_slide(\'down\')',50);
		this.__is_sliding = true;
	}
	
	this.el.__has_child = this.trigger.__has_child = function(node){
	    if (node==null) return false;
		while(node.parentNode != null){
			if (node.parentNode == this) return true;
			node = node.parentNode;
		}
		return false;
	}
}