YAHOO.namespace("shipwire.planitems");
YAHOO.shipwire.planitems = new function()
{
    this.updateObj = "";
    this.reqDelayID = -1;
    this.visible = false;
    this.overlay = null;
    this.openDelay = null;
    
    this.init = function(e)
    {
        var oSelf = YAHOO.shipwire.planitems;

        //if it's in a bubble, get the overlay ready
        if (document.getElementById("sw_planitems").parentNode.className=="sw_dialog_bubble")
        {
            document.getElementById("sw_planitems").style.display = "block";
            oSelf.overlay = new YAHOO.widget.Panel(document.getElementById("sw_planitems").parentNode, { 
                close: false,
                fixedcenter:false,
                visible:false,
                draggable:false,
                underlay:"none",
                constraintoviewport: true,
                iframe: true
            });
            YAHOO.util.Dom.removeClass("sw_planitems","hide-scrollbars");
            oSelf.overlay.header = YAHOO.util.Dom.getElementsByClassName("hd","div",document.getElementById("sw_planitems"))[0];
            oSelf.overlay.body = YAHOO.util.Dom.getElementsByClassName("bd","div",document.getElementById("sw_planitems"))[0];
            oSelf.overlay.footer = YAHOO.util.Dom.getElementsByClassName("ft","div",document.getElementById("sw_planitems"))[0];
            oSelf.overlay.render(document.body);
            oSelf.overlay.hideEvent.subscribe(oSelf.onHide);
            YAHOO.util.Event.addListener("sw_planitems_close","click",function(e){oSelf.overlay.hide();YAHOO.util.Event.preventDefault(e);});
            YAHOO.util.Event.onContentReady("sw_planitems_c",function(e){document.getElementById("sw_planitems_c").style.visibility = "hidden";});
        }
                
        var tabView = new YAHOO.widget.TabView('sw_planitems');
        
        YAHOO.util.Event.addListener("sw_example_order_link","click",oSelf.toggleExamples);
        YAHOO.util.Event.addListener("sw_trigger_planitems","click",oSelf.startShowTimer);
    };
    
    this.inputChange = function(e)
    {
        var oSelf = YAHOO.shipwire.planitems;
        
        oSelf.clearValues();
        
        var reqDelayID =
            setTimeout(function(){oSelf.updateTables(e);},500);

        if(oSelf.reqDelayID != -1) {
            clearTimeout(oSelf.reqDelayID);
        }

        oSelf.reqDelayID = reqDelayID;
    }
    
    this.startShowTimer = function(e)
    {
        var elTarget = this;
        var timer = 0;
        var oSelf = YAHOO.shipwire.planitems;
        var wait = function()
        {
            oSelf.showOverlay(e,elTarget);
        }
        oSelf.openDelay = setTimeout(wait,timer);
    };
    
    this.stopShowTimer = function(e)
    {
        var oSelf = YAHOO.shipwire.planitems;
        clearTimeout(oSelf.openDelay);
    };
    
    this.showOverlay = function(e,elTarget)
    {
        var oSelf = YAHOO.shipwire.planitems;
        
        if (oSelf.visible)
        {
            return;
        }
        else
        {
            oSelf.visible = true;
        }
        YAHOO.shipwire.util.transDlg.show();
        
        if (!oSelf.overlay)
        {
            oSelf.init();
        }
        
        var config = {defaultX: "right", defaultY: "top", offsetX: [20,10], offsetY: 0};
        var xy = YAHOO.shipwire.util.fitOverlayToViewport(document.getElementById("sw_planitems").parentNode,elTarget,config);
        oSelf.overlay.cfg.setProperty("xy",xy);
        var viewportXY = [ YAHOO.util.Dom.getViewportWidth(), YAHOO.util.Dom.getViewportHeight() ];
        var scrollXY = [ YAHOO.util.Dom.getDocumentScrollLeft(), YAHOO.util.Dom.getDocumentScrollTop() ];
    
        if(scrollXY[1]>xy[1])
        {
            window.scroll(scrollXY[0],xy[1]-15);
        }
        if (oSelf.overlay) {
            oSelf.overlay.show();
        }
        YAHOO.shipwire.util.transDlg.hide();
    };
    
    this.onHide = function(e)
    {
        var oSelf = YAHOO.shipwire.planitems;
        oSelf.visible = false;
    };
    
    this.toggleExamples = function(e) 
    {
        var examples = document.getElementById('sw_example_orders');
        
        if (examples.className == 'hidden') {
            YAHOO.util.Dom.removeClass(examples, 'hidden');
        } else {
            YAHOO.util.Dom.addClass(examples, 'hidden');
        }

        if (e) {
            YAHOO.util.Event.preventDefault(e);
        }
    };
    
};

YAHOO.util.Event.onContentReady("sw_planitems",YAHOO.shipwire.planitems.init);
