



get = function(id) {
    return YAHOO.util.Dom.get(id);
}

// IE6 calls onContentReady too early, resulting in JS errors.
// Use this custom method instead of the standard YUI method to avoid this problem.
YAHOO.webui.onContentReady = function (content_id, event_handler) {
    if(navigator.userAgent.indexOf('MSIE ') != -1 && navigator.platform == 'Win32') {
        YAHOO.util.Event.addListener(window, "load", event_handler);
    }
    else {
        YAHOO.util.Event.onContentReady(content_id, event_handler);
    }
}


YAHOO.widget.MenuItem.prototype.IMG_ROOT = "/images/";

YAHOO.webui.TinyMenu = function (menu_id, menu_data) {
    var ns = YAHOO.namespace("webui."+menu_id);
    ns.menu_id = menu_id;
    ns.menu_data = menu_data;
    ns.init = function(p_oEvent) {
        ns.beforeRender = function (p_sType, psArgs, poMenu) {
            for(var i=0; i<menu_data.length; i++) {
                  var menu = ns.widget.getItem(i)
                  if(menu){
                      menu.cfg.setProperty("submenu",
                          { id:ns.menu_id+"_"+i, itemdata: menu_data[i] });
//                      menu.cfg.setProperty("onclick",
//                          {fn: YAHOO.util.Event.stopEvent, obj: this, scope: this});
                  }
            }
            YAHOO.util.Event.on("nav_menu", "click", topMenuClickHandler);
        }
        ns.widget = new YAHOO.widget.MenuBar(ns.menu_id,
            {'lazyload': true,
             'zindex': 1000,
             'constraintoviewport': true,
             'submenualignment': ['tr','br'],
             'autosubmenudisplay': true,
             'showdelay': 0,
             'hidedelay': 850,
             'submenuhidedelay': 850});
        ns.widget.beforeRenderEvent.subscribe(ns.beforeRender, ns.widget, true);
        ns.widget.render();
        ns.widget.show();
    }
    YAHOO.webui.onContentReady(ns.menu_id, ns.init);
    return ns;
}

    function topMenuClickHandler(e) {
       // ===================================================
       // function to block click event for top menu items...
       // see bug 33542 for details...
       // ===================================================
       var elTarget = YAHOO.util.Event.getTarget(e);
       if(elTarget.className && (elTarget.className.indexOf("hassubmenu")>-1
         || elTarget.className.indexOf("submenuindicator")>-1)) {
           YAHOO.util.Event.stopEvent(e);
           YAHOO.util.Event.preventDefault(e);
           YAHOO.log("prevented CLICK event");
       }
    }
             

YAHOO.webui.NavBar = function (namespace_id, menu_id, menu_data) {
    var ns = YAHOO.namespace(namespace_id);
    ns.menu_data = menu_data;
    ns.init = function(p_oEvent) {
        ns.widget = new YAHOO.widget.MenuBar(menu_id,
            {'lazyload': true,
             'zindex': 9001,
             'constraintoviewport': true,
             'autosubmenudisplay': true,
             'submenuhidedelay': 800,
             'hidedelay': 800,
             'showdelay': 0});
        ns.widget.beforeRenderEvent.subscribe(ns.beforeRender, ns.widget, true);
        ns.widget.render();
        ns.widget.show();
    }
    ns.beforeRender = function(p_sType, psArgs, poMenu) {
        alert("Override this method");
    }
    YAHOO.webui.onContentReady(menu_id, ns.init);
    return ns;
}
               
YAHOO.webui.buildModalDialog = function (namespace_id, dialog_id, ok, cancel) {
    return YAHOO.webui.LargeDialog(namespace_id, dialog_id, ok, cancel, true, false);
}
YAHOO.webui.buildDragDialog = function (namespace_id, dialog_id, ok, cancel) {
    return YAHOO.webui.LargeDialog(namespace_id, dialog_id, ok, cancel, false, true);
}
YAHOO.webui.LargeDialog = function (namespace_id, dialog_id,
                                    ok, cancel, modal, draggable) {
    // Construct a namespace for this dialog (and return it later.)
    var ns = YAHOO.namespace(namespace_id);
    ns.init = function () {
        ns.element = get(dialog_id);
        ns.widget = new YAHOO.widget.SimpleDialog(dialog_id, {visible:false,
           fixedcenter:true, zIndex:200, modal:modal, constraintoviewport:false, draggable:draggable});
        var buttons = [];
        if (ok) buttons[0] = { text:ok, handler:ns.handleOK, isDefault:true };
        if (cancel) buttons[buttons.length] = {text:cancel, handler:ns.handleCancel};
        ns.okButtonText = ok;
        ns.buttons = buttons;
        ns.widget.cfg.queueProperty("buttons", buttons);
        var listeners = new YAHOO.util.KeyListener(document,
            { keys : 27 },
            {fn:ns.handleCancel, scope:ns.widget, correctScope:true});
        ns.widget.cfg.queueProperty("keylisteners", listeners);
        ns.widget.render(document.body);
    }
    YAHOO.webui.onContentReady(dialog_id, ns.init);

    // Override these methods after calling buildModalDialog.
    ns.show = function(e) {
        ns.widget.show();
    }
    ns.hide = function(e) {
        ns.widget.hide();
    }
    ns.handleCancel = function(e) {
        ns.widget.hide();
    }
    ns.handleOK = function(e) {
        alert("Override this method!");
        ns.widget.hide();
    }
        // quick fix
        // now it works fine now only with one validation field...

    ns.updateOK = function() {

        // this function should be used in the future...
        // it should check if there are active errors
        // and if there are any style OK button with error style
        // there should be an array of active errors...
    }

    ns.showOKError = function(bool){
        if(bool){
            ns.styleOK("error");
        }
        else{
            ns.styleOK("default");
        }
    }

    ns.styleOK = function(className){
        var okButton = ns.getOKButton();
        if(okButton){
            okButton.className = className;
        }
    }

    ns.getOKButton = function(){
        if(ns.okButton == undefined || ns.okButton == null){
//            ns.okButton = ns.getButtonByText(ns.okButtonText);
            ns.okButton = ns.getDefaultButton();
        }
        return ns.okButton;
    }
    ns.getButtonByText = function(str){
        var searchedStr = new String(str);
        var bts=ns.widget.cfg.getProperty("buttons");
        for(var i = 0; i < bts.length; i++){
            var b = bts[i];
            if(b){
                //if(b.text.toString().toLowerCase() == str.toString().toLowerCase() ){
                var txt = new String(b.text);
                if(txt.toLowerCase() == searchedStr.toLowerCase() ) {
                    var htmlButton = b.htmlButton;
                    if(htmlButton){
                        return htmlButton
                    }
                }
            }
        }
        return null;
    }

    ns.getDefaultButton = function(){
        var bts=ns.widget.cfg.getProperty("buttons");
        for(var i = 0; i < bts.length; i++){
            var b = bts[i];
            if(b){
                if(b.isDefault ) {
                    var htmlButton = b.htmlButton;
                    if(htmlButton){
                        return htmlButton
                    }
                }
            }
        }
        return null;
    }
    ns.enableOK = function(){
        var okButton = ns.getOKButton()
        if(okButton){
              okButton.disabled = false;
              ns.styleOK("default");
         }
    }
    ns.disableOK = function(){
        var okButton = ns.getOKButton();
        if(okButton){
             okButton.disabled = true;
             ns.styleOK("disabled");
         }
    }
    return ns;
}
          
YAHOO.webui.buildBubbleDialog = function (namespace_id, dialog_id, parentDivName) {
    return YAHOO.webui.BubbleDialog(namespace_id, dialog_id, false, 'ok', 'cancel', false, false, true, true, parentDivName);
}
YAHOO.webui.BubbleDialog = function (namespace_id, dialog_id, show_buttons,
                                    ok, cancel, fixedcenter, modal,
                                    constraintoviewport, draggable, parentDivName) {
    // Construct a namespace for this dialog (and return it later.)
    var ns = YAHOO.namespace(namespace_id);
    ns.init = function () {
        ns.element = get(dialog_id);
        ns.widget = new YAHOO.widget.Panel("win", { width:"220px", fixedcenter: false, constraintoviewport: true, close:true, visible:false, draggable:true, modal:false } );
        var listeners = new YAHOO.util.KeyListener(document,
            { keys : 27 },
            {fn:ns.handleCancel, scope:ns.widget, correctScope:true});
        ns.widget.cfg.queueProperty("keylisteners", listeners);
		var parentDiv = null;
		if(!parentDivName){
			parentDivName = "bubbleParentDiv";
		}
		if(parentDivName){
			parentDiv = document.getElementById(parentDivName);			
		}
		if(parentDiv){
	        ns.widget.render(parentDiv);			
		}
		else{
	        ns.widget.render(document.body);			
		}
    }
    YAHOO.util.Event.addListener(window, "load", ns.init);

    // Override these methods after calling buildModalDialog.
    ns.show = function(e) {
        ns.widget.show();
    }
    ns.hide = function(e) {
        ns.widget.hide();
    }
    ns.handleCancel = function(e) {
        ns.widget.hide();
    }
    return ns;
}
         
YAHOO.webui.SmallDialog = function (namespace_id, dialog_id,
                                    title, message, ok, cancel, onOk, onCancel,
                                    modal, draggable) {
    // Construct a namespace for this dialog (and return it later.)
    var ns = YAHOO.namespace(namespace_id);
    ns.onOk = onOk;
    ns.onCancel = onCancel;
    ns.element = get(dialog_id);
    ns.widget = new YAHOO.widget.Dialog(dialog_id, {visible:false,
        width:'350px', zIndex:200, fixedcenter:true, modal:modal,
        constraintoviewport:true, draggable:draggable});
    ns.show = function(e) {
        ns.widget.show();
    }
    ns.hide = function(e) {
        ns.widget.hide();
    }
    ns.handleCancel = function(e) {
        if (ns.onCancel)
            eval(ns.onCancel);
        ns.widget.hide();
    }
    ns.handleOK = function(e) {
        eval(ns.onOk);
        ns.widget.hide();
    }
    if (title)
        ns.widget.setHeader(title);
    if (message)
        ns.widget.setBody(message);

    var buttons = [];
    if (ok) buttons[0] = { text:ok, handler:ns.handleOK, isDefault:true };
    if (cancel) buttons[buttons.length] = {text:cancel, handler:ns.handleCancel};
    ns.widget.cfg.queueProperty("buttons", buttons);
    var listeners = new Array(
        new YAHOO.util.KeyListener(document,
          {keys:27}, {fn:ns.handleCancel, scope:ns.widget, correctScope:true}),
        new YAHOO.util.KeyListener(document,
          {keys:13}, {fn:ns.handleOK, scope:ns.widget, correctScope:true}))
    ns.widget.cfg.queueProperty("keylisteners", listeners);
    ns.widget.render(document.body);

    return ns;
}
         

// Custom js functions for Query Info Dialog Widget
function toggleDialogBD(dlg, hide) {
    // function hides or shows bd container for the dlg dialog object
    var display_text = "";
    if (hide) display_text = "none";
    for (var i=0; i<dlg.element.childNodes.length; i++) {
        if (dlg.element.childNodes[i].className == 'bd') {
            dlg.element.childNodes[i].style.display = display_text;
        }
    }
}

function queryInfoHandleOk(dlg) {
    // Custom function perform close action
    dlg.handleOK = function() {
            toggleDialogBD(dlg, true);
            dlg.hide();
        }

    // Custom function perform close for cancel action
    dlg.handleCancel = function() {
            toggleDialogBD(dlg, true);
            dlg.hide();
        }
}

function showQueryInfoDialog(dlg, header) {

    // There is a problem with the width getting set in Opera
    // so this code will set it manually if the client is Opera.
    if (window.opera) {
        if (dlg.element.style.width == '0px') {
            dlg.element.style.width = '644px';
        }
    }

    //show bd div before displaying
    toggleDialogBD(dlg, false);
    dlg.widget.setHeader(header);
    dlg.show();

    // On the first showing of the dialog, the centering code
    // is not running properly so we run it manually here just
    // for Opera.
    if (window.opera) {
        dlg.widget.center();
    }
}


function jslog (msg, reset) {
    // do no thing
}


