// verify namespace
if (typeof WIREDRIVE != 'object'){
    var WIREDRIVE = function() { 
        return {}; 
    }();
}

WIREDRIVE.popbox = function(){
    
    var markupGallery = function(p,my){
    
        var that = {};
        my = my || {};
        
        // set defaults for gallery config object
        p.containerid = p.containerid || 'gallery';
        p.itemclass = p.itemclass || 'previewSection';
        p.popupmarkupclass = p.popupmarkupclass || 'datasource';
        p.width = p.width || 640;
        
        // boolean test if an element is the popup markup container
        my.isDatasource = function(e){
            if (YAHOO.util.Dom.hasClass(e,p.popupmarkupclass)){
                return true;
            } else {
                return false;
            }
        };
        
        // set the popup panels content based on item rank
        my.setPopupContent = function(rank){
            rank = parseInt(rank);
            var data = YAHOO.util.Dom.getChildrenBy(my.items[rank], my.isDatasource);
            my.popupwindow.setHeader('<span id="btn_prev" class="container_prev">&nbsp;</span><span id="btn_next" class="container_next">&nbsp;</span>');
            my.popupwindow.setBody(data[0].innerHTML + '<hr class="clearing"/>');
            my.popupwindow.render(document.body);
            
            // configure the interactive elements of the popup    
              
            var nextrank = rank >= my.items.length - 1 ? 0 : rank + 1;
            var prevrank = rank <= 0 ? my.items.length - 1 : rank - 1;
            
            YAHOO.util.Event.on("btn_next","click", function(){
                my.setPopupContent(nextrank);
            },this,true);
            
            YAHOO.util.Event.on("btn_prev","click", function(){
                my.setPopupContent(prevrank);
            },this,true);
            
        };

        
        // creates the YUI popup panel ( but doesn't show it )        
        my.popupwindow = new YAHOO.widget.Panel("poppanel",
            { 
                width: p.width + "px", 
                fixedcenter:true, 
                close:true, 
                draggable:false,
                zindex:4,
                modal:true,
                visible:false,
                effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}
            } 
        );
        
        // set the array of gallery items
        my.items = YAHOO.util.Dom.getChildrenBy(YAHOO.util.Dom.get(p.containerid), my.isDataEl);
 
        // trigger the popup when one of the gallery items is clicked
        my.itemclick = function(e) {
            YAHOO.util.Event.preventDefault(e);
            var element = this;
            var rank = element.getAttribute('rank');
            my.setPopupContent(rank);
            my.popupwindow.show();
            
        };

        // sets a listener on the gallery items 
        YAHOO.util.Event.addListener(my.items, "click", my.itemclick); 

        // set the BG cover to hide the popover when clicked
        YAHOO.util.Event.on("poppanel_mask","click", function(){my.popupwindow.hide();},this,true);
        
        // TODO:  enable gallery object controls
        that.next = function(){};
        that.previous = function(){};
        that.show = function(){};
        that.hide = function(){};
        that.destroy = function(){};
                
        return that;
    };

    return {
        createGallery : function(p){
            var gallery = markupGallery(p);
            return gallery;
        }
    }
}();


