﻿// make that the namespace "NURUN.widget" is created.
NURUN.namespace("NURUN.widget");

/**
* @namespace All widgets must be under this namespace 
*/
NURUN.widget = NURUN.widget;



/**
* Add functionnality to a specific html structure to simulate a dropdown list
*
* @param {HTMLElement} (elm) HTMLElement that you want to add functionnalities
* @author Patrick Meunier
* @requires YAHOO.util.Event, YAHOO.util.Dom
* @class 
*/
NURUN.widget.LinksDropDown = function(elm, userConfig){
	this.cfg = new NURUN.util.Param();
    this.cfg.addParams(userConfig);
    
	//get the links block (<ul>)
	var linksBlock = YAHOO.util.Dom.getChildrenBy(elm, __getLinksBlock);
	
	linksBlock = linksBlock[0];
	
	linksBlock.style.display = "none";
	linksBlock.style.zIndex = "999999";


	YAHOO.util.Event.addListener(elm, "click", __container_onclick);
	YAHOO.util.Event.addListener(document, "click", __close);
	
	if(this.cfg.getParam("linksOnClick") != null ){
	    
	    var aElement = linksBlock.getElementsByTagName("a");
	    
	    for(var i=0; i < aElement.length; i++)
	    {
	        YAHOO.util.Event.addListener(aElement[i], "click", this.cfg.getParam("linksOnClick"));
	    }
	}
	

	/**
	* @public
	* @description Open link list block
	* @function 
	*/
	this.open = __open;

	/**
	* @public
	* @description Close link list block
	* @function 
	*/
	this.close = __close;	
	

    // private methods
	function __open(e){
		linksBlock.style.display = "block";
	}

	
	function __close(){
		linksBlock.style.display = "none";
	}

	function __container_onclick(e){
		if(linksBlock.style.display === "none"){
			__open();
		}
		else{
			__close();
		}

		YAHOO.util.Event.stopPropagation(e || event);
	}


	function __getLinksBlock(elm){
		return elm.tagName.toLowerCase() == "ul";
	}

}

// Register module with YUI (for use with YUI Loader)
if (typeof YAHOO !== "undefined" && YAHOO.register) {
    YAHOO.register("linksdropdown", NURUN.widget.LinksDropDown, {version: "1", build: "1"}); 
}
