/* Hover behavior class - allows changing of CSS class for element when mouse pointer is moved over it */

Type.registerNamespace('Takeout.UI');

Takeout.UI.HoverBehavior = function (element) {
	Takeout.UI.HoverBehavior.initializeBase(this, [element]);
	
	this._hoverCssClass = null;
	this._normalCssClass = null;
}

Takeout.UI.HoverBehavior.prototype = {
	initialize : function() {
		Takeout.UI.HoverBehavior.callBaseMethod(this, 'initialize');
		
		$addHandlers(this.get_element(),
			{
				mouseout : this._onMouseout,
				mouseover : this._onMouseover
			}, this);
	},
	
	dispose : function() {                                
        $clearHandlers(this.get_element());
        Takeout.UI.HoverBehavior.callBaseMethod(this, 'dispose');
    },
	
	_onMouseout : function() {
		Sys.UI.DomElement.removeCssClass(this._element, this._hoverCssClass);
		Sys.UI.DomElement.addCssClass(this._element, this._normalCssClass);
	},
	
	_onMouseover : function() {
		Sys.UI.DomElement.removeCssClass(this._element, this._normalCssClass);
		Sys.UI.DomElement.addCssClass(this._element, this._hoverCssClass);
	},
	
	get_hoverCssClass : function() {         
        return this._hoverCssClass;          
    },                                       
                                             
    set_hoverCssClass : function(value) {    
        this._hoverCssClass = value;         
    },                                       
                                             
    get_normalCssClass : function() {         
        return this._normalCssClass;          
    },                                       
                                             
    set_normalCssClass : function(value) {    
        this._normalCssClass = value;         
    } 
}

Takeout.UI.HoverBehavior.registerClass('Takeout.UI.HoverBehavior', Sys.UI.Behavior);

/* Dropdown behavior class - allows block element to appear when parent block element is under mouse pointer */

Takeout.UI.DropDownBehavior = function (element) {
	Takeout.UI.DropDownBehavior.initializeBase(this, [element]);
	
	this._parentElement = null;
	this._timer = null;
}
// static fields that store last opened submenu object and its parent. I need to hide it immediately if another submenu is opened
Takeout.UI.DropDownBehavior._lastVisibleSubMenu = null;
Takeout.UI.DropDownBehavior._lastVisibleSubMenuParent = null;

Takeout.UI.DropDownBehavior.prototype = {
	initialize : function() {
		Takeout.UI.DropDownBehavior.callBaseMethod(this, 'initialize');
		
		$addHandlers(this._parentElement,
			{
				mouseout : this._onMouseout,
				mouseover : this._onMouseover
			}, this);
			
		$addHandlers(this.get_element(),
			{
				mouseout : this._onMouseout,
				mouseover : this._onMouseoverSubMenu
			}, this);
	},
	
	dispose : function() {
        $clearHandlers(this.get_element());
		$clearHandlers(this._parentElement);
        Takeout.UI.DropDownBehavior.callBaseMethod(this, 'dispose');
    }, 
	
	_onMouseoverSubMenu : function() {
		if (this._timer) // clear timeout - will be set again with mouseout event
		{
			clearTimeout(this._timer);
		}
	},
	
	_onMouseover : function() {
		if (this._timer) // clear timeout - will be set again with mouseout event
		{
			clearTimeout(this._timer);
		}
		// if another submenu is already opened - hide it and set this submenu as current
		if ((null != Takeout.UI.DropDownBehavior._lastVisibleSubMenu) && 
			(this.get_element() != Takeout.UI.DropDownBehavior._lastVisibleSubMenu))
		{
			Takeout.UI.DropDownBehavior._lastVisibleSubMenu.style.display = "none";
			Takeout.UI.DropDownBehavior._lastVisibleSubMenu = null;
			Sys.UI.DomElement.removeCssClass(Takeout.UI.DropDownBehavior._lastVisibleSubMenuParent, "item_hover");
			Takeout.UI.DropDownBehavior._lastVisibleSubMenuParent = null;
		}
		
		// show submenu
		Takeout.UI.DropDownBehavior._lastVisibleSubMenu = this.get_element();
		Takeout.UI.DropDownBehavior._lastVisibleSubMenuParent = this._parentElement;
		Takeout.UI.DropDownBehavior._lastVisibleSubMenu.style.display = "block";
		Sys.UI.DomElement.addCssClass(Takeout.UI.DropDownBehavior._lastVisibleSubMenuParent, "item_hover");
		
		// set corrent location of submenu
		var parentLocation = Sys.UI.DomElement.getLocation(this._parentElement);
		Sys.UI.DomElement.setLocation(this.get_element(), parentLocation.x, parentLocation.y + this._parentElement.clientHeight);
	},
	
	_onMouseout : function() {
		this._setTimeout();
	},
	
	_setTimeout : function() {
		if (this._timer)
		{
			clearTimeout(this._timer);
		}
		// register function that will hide current submenu and remove item_hover css class from parent element
		this._timer = setTimeout("$get('" + this.get_element().id + "').style.display='none';Sys.UI.DomElement.removeCssClass($get('" + this._parentElement.id + "'), 'item_hover');", 100);
	},
	
	get_parentElement : function() {         
        return this._parentElement;          
    },                                       
                                             
    set_parentElement : function(value) {    
        this._parentElement = value;         
    } 
}

Takeout.UI.DropDownBehavior.registerClass('Takeout.UI.DropDownBehavior', Sys.UI.Behavior);
