/**
    Some javascript specific to the category page. Generally,
    this means doing OnLoad stuff, and assigining event handlers
    to various components.
    ~aoliver
*/

dojo.require("dojo.widget.ContentPane")

/**
* Some page-scope variables
*/
PAGE_MODES = {grid:"grid", detail:"detail"};
currentPage = 1;
currentMode = PAGE_MODES.grid;
products = new Array();

/** 
* Initializes the category page
*/
_init_poppers = function(){
    dojo.debug("entering init()");
    initPoppers();
    dojo.debug("leaving init()");
};

   
/**
* Initialize the popper widgets. This lets us delay the building of dojo widgets until
* the mouseover occurs
*/
function initPoppers(){
        dojo.debug('entering initPoppers');
        
        var which_popper = getPopper();
        var popper_flavors = [
                "ag.widget.Popper", 
                "ag.widget.PrintablesPopper",
                "ag.widget.InvitationsPopper",
                "ag.widget.DownloadsPopper",
                "ag.widget.FlashPopper",
                "ag.widget.MomPopper"
        ];
        for(i in popper_flavors){
            dojo.require(popper_flavors[i]);
        }
       
        for (i in products){
            productNumber = products[i];
            productNode = dojo.byId(productNumber+'');
            if(smallThumbNode = dojo.byId('small-thumb-' + productNumber)) {
                dojo.event.connectOnce(smallThumbNode,"onmouseover", function(evt){createPopper(evt.target.id, which_popper)});
            }
        }
    }

/**
* look for `popper` var on the page. If it exists, build up and return
  correct string for the intended popper widget.
*/
function getPopper()
{
    //popper can be something like "printables" or "downloads"
    //the outcome will be "PrintablesPopper" or "DownloadsPopper";
    if (typeof(popper) == "undefined")
    	popper = '';
    	
    try
    {
        //capitalize first character
        which_popper = popper.charAt(0).toUpperCase();
        //tack on rest of string
        which_popper += popper.substring(1).toLowerCase();
    }
    catch(e){ which_popper = ''; }
    //make it official
    which_popper += "Popper";
    return which_popper;
}

/**
* Create a Popper widget on the fly
*  NOTE: this is intended to be used only on category-like pages
*        and DEPENDS UPON thumbbuilder.py generating the proper markup/classes.
*/
function createPopper(smallThumbId, which_popper){
    productNumber = smallThumbId.split('-')[2];
    if( dojo.widget.byId("productw-" + productNumber) != null){
        w = dojo.widget.byId("productw-" + productNumber);
        return;
    }
    else {
        which_popper = _changePopper(productNumber, which_popper);
        dojo.debug('creating widget: '+which_popper);
        w = dojo.widget.createWidget(which_popper, {
                                                id:"productw-" + productNumber,
                                                productNumber:productNumber,
                                                connectId:smallThumbId, 
                                                dataId:productNumber,
                                                fromNode:true,
                                                imghost:imghost,
                                                toggle:"fade"
                                                });
    }
    w._onHover(); //Fire the hover event so the popup appears
}

/**
* Maybe we have to make a different popper (other than what the page expected?)
* Same caveats as createPopper above
*/
function _changePopper(node_id, which_popper){
    var mainNode = dojo.byId(node_id);
    var flavors = dojo.html.getElementsByClass('popper_flavor', mainNode, 'dd');
    if(flavors.length){
        return flavors[0].innerHTML+'Popper';
    }
    return which_popper;
}

/**
* Get an array of <li> nodes with class "product".
*/
function getProductNodes(){
    return dojo.html.getElementsByClass("product", null, "li");
}

function getPopperWidgets(){
    which_popper = getPopper()
    return dojo.widget.byType(which_popper);
}

/**
* Destroy all Popper widgets on the page. Most often used
* when switching to another page of thumbs. This saves resources, and
* also gives dojo a clean slate when coming back to this page of results.
*/
function destroyPoppers(){
    dojo.debug("starting destroy Poppers");
    poppers = getPopperWidgets();
    for (i in poppers){
        popper = poppers[i];
        //parents cause problems for destroying things ~aoliver
        popper.parent = null;
        popper.destroy();
    }
    dojo.debug("ending destroy Poppers");
}

/*
* Initializes the autocomplete feature for quicksend
*/
function init_recipient_autocomplete(inputId){
    var toemail = new TextAreaComplete(inputId);
}


//Ultimately, add the init function to the page load
dojo.addOnLoad(_init_poppers);
