﻿// make that the namespace "NURUN.widget" is created.
NURUN.namespace("NURUN.widget");
NURUN.namespace("NURUN.helper");
/**
* @namespace All widgets must be under this namespace 
*/
NURUN.widget = NURUN.widget;
NURUN.helper = NURUN.helper;


/**
* 
*
* @param {HTMLElement} (elm) HTMLElement that you want to add functionnalities
* @author Sebastien Hiticas
* @requires YAHOO.util.Event, YAHOO.util.Dom
* @class 
*/
NURUN.widget.LinkToPanel = function(elm, link, userConfig){

    function __show(e, obj)
    {
        obj.popup.show()
        YAHOO.util.Event.preventDefault(e);
    }
    
    //default config
    var cfg = new NURUN.util.Param(
                                    {
                                        width:"661px", 
                                        fixedcenter: true, 
                                        constraintoviewport: true, 
                                        close:true, 
                                        underlay:"none",
                                        effect:YAHOO.widget.ContainerEffect.FADE,
                                        duration:0.5,
                                        visible:false, 
                                        draggable:false,
                                        modal:true
                                    }
                                       
                                  );
    //user config
    cfg.addParams(userConfig);
    
    cfg = {
                width:                  cfg.getParam("width"), 
                fixedcenter:            cfg.getParam("fixedcenter"), 
                constraintoviewport:    cfg.getParam("constraintoviewport"), 
                close:                  cfg.getParam("close"), 
                underlay:               cfg.getParam("underlay"),
                effect:                 {effect:cfg.getParam("effect"), duration:cfg.getParam("duration")},
                visible:                cfg.getParam("visible"), 
                draggable:              cfg.getParam("draggable"),
                modal:                  cfg.getParam("modal"),
                zIndex:15000
            };
    
    //add "yui-skin-sam" class to the body
    if(!YAHOO.util.Dom.hasClass(document.body, "yui-skin-sam")) YAHOO.util.Dom.addClass(document.body, "yui-skin-sam");
    
    myPopup = new YAHOO.widget.Panel(elm, cfg );

    myPopup.render(document.body);
        
    YAHOO.util.Event.addListener(link, "click", __show, {popup:myPopup});
    
}

/**
* 
*
* @param {HTMLElement} (elm) HTMLElement that you want to add functionnalities
* @author Sebastien Hiticas
* @requires NURUN.widget.LinkToPanel, NURUN.util.Param, YAHOO.util.Dom
* @class 
*/
NURUN.helper.BatchLinkToPanel = function(elm, userConfig){
    
    var result = new Array();
    
    //user config
    var cfg = new NURUN.util.Param({links: "link", popups: "popup", hide: "hide"});
    cfg.addParams(userConfig);
    
    //get all "links"
    var links = YAHOO.util.Dom.getElementsByClassName(cfg.getParam("links"));
    
    //get all popups
    var popups = YAHOO.util.Dom.getElementsByClassName(cfg.getParam("popups"));
    
    //add listener to each link
    for(var i=0; i<links.length; i++)
    {
        myPopup = new NURUN.widget.LinkToPanel(popups[i], links[i]);
        
        result[i] = myPopup;
    }
    
    return result;
}

// Register module with YUI (for use with YUI Loader)
if (typeof YAHOO !== "undefined" && YAHOO.register) {
    YAHOO.register("modalpopup", NURUN.widget.ModalPopUp, {version: "1", build: "1"}); 
    YAHOO.register("batchlinktopanel", NURUN.helper.BatchLinkToPanel, {version: "1", build: "1"}); 
}
