if(YAHOO.util){
  /**
   * create shortcuts into YAHOO lib, allows for easer coding/readability.
   * some of the shortcuts: yuiDom, yuiEvent, yuiAnim, yuiEasing...
   * Same as var yuiDom = YAHOO.util.Dom;
   */
    for(var prop in YAHOO.util){
        window["yui"+ prop] = YAHOO.util[prop];
    }
}

var mm = YAHOO.namespace("mm");

mm.Error = {
    errorArray:new Array(),
    panel:null,

    init:function(){
        errorPanelConfig = {
            fixedcenter: true,
            constraintoviewport:true,
            close: true,
            draggable: false,
            zindex:4,
            modal: true,
            visible: false,
            buttons : [ { text:"OK", handler:this.closeError } ]
        };

        this.panel = new YAHOO.widget.SimpleDialog("errorPanel", errorPanelConfig);
        this.panel.setHeader("Sorry, An Error Has Occurred");
        this.panel.setBody("");
        this.panel.render(document.body);
    },

    populate:function(){
        this.panel.setBody("");
        var tempBlock = "";
        for(var i=0; i<this.errorArray.length; i++){
            tempBlock += "<p>" + this.errorArray[i] + "</p>";
        }
        this.panel.setBody(tempBlock);
    },

    add:function(newError){
        //todo, remove the jsExtensions dependency here
        if(mm.Error.indexOf(this.errorArray,newError) < 0){
            this.errorArray.push(newError);
        }
    },

    indexOf : function(arrayToCheck, k){
        var len = arrayToCheck.length;
        for(i=0;i<len;i++){
          if(arrayToCheck[i] == k){
            return i;
          }
        }
        return -1;
    },

    removeAll:function(){
        this.errorArray.splice(0, this.errorArray.length);
    },

    closeError:function(){
        var self = mm.Error;
        self.panel.hide();
    },

    generateBridge:function(){
        this.populate();
        this.panel.show();
    },

    initBridge:function(){
        var self = mm.Error;
        self.init();
    }
};

yuiEvent.onDOMReady(mm.Error.initBridge);

mm.Save = {
    panel:null,
    savePanelConfig:{
        fixedcenter: true,
        close: false,
        draggable: false,
        zindex:4,
        modal: true,
        visible: false
    },

    init:function(){
        this.panel = new YAHOO.widget.SimpleDialog("savePanel", this.savePanelConfig);
        this.panel.setHeader("Saving, please wait...");

        var savingPanel = document.createElement("div");

            var uploadImage = document.createElement("img");
                uploadImage.setAttribute("src", "/images/ajax_load.gif");
                uploadImage.setAttribute("alt", "");

            savingPanel.appendChild(uploadImage);

            var bottomCurve = document.createElement("div");
                bottomCurve.className = "bottomBar";
                bottomCurve.appendChild(document.createElement("div"));

            savingPanel.appendChild(bottomCurve);

        this.panel.setBody(savingPanel);
        this.panel.render(document.body);
    },

    initBridge:function(){
        var self = mm.Save;
        self.init();
    }
};

mm.Upload = {
    panel:null,
    panelManager:[],
    form:null,
    selectedPanelConfig:null,
    uploadPanelConfig : {
        fixedcenter:true,
        constraintoviewport:true,
        underlay:"shadow",
        close:true,
        visible:false,
        modal:true,
        draggable:false,
        zIndex:10
    },
    panelTypeConfig : {
        avatar : {
            id : "avatarUpload",
            header : "Upload your Avitar",
            body : "Please select the avatar image that you want to use.  Your image should be 200x200 and a .gif, .jpg, or .png.",
            button : "Upload"
        },
        image : {
            id : "imageUpload",
            header : "Upload your Image",
            body : "Please select an image that you want to use for this article.  Your image should be 320x180 and a .gif, .jpg, or .png.",
            button : "Upload"
        }
    },

    setTextConfig:function(configId){
        this.selectedPanelConfig = this.panelTypeConfig[configId];
    },

    create:function(){
        if(! this.panelManager[this.selectedPanelConfig.id]){
            // create this panel type for the first time and add to the manager
            this.panel = new YAHOO.widget.Panel(this.selectedPanelConfig.id, this.uploadPanelConfig);
            this.panel.setHeader(this.selectedPanelConfig.header);

            var uploadForm = document.createElement("form");
                uploadForm.setAttribute("id", "uploadImageForm");
                uploadForm.setAttribute("name", "uploadImageForm");
                var uploadCopy = document.createElement("p");
                    var copyNode = document.createTextNode(this.selectedPanelConfig.body);
                    uploadCopy.appendChild(copyNode);
                uploadForm.appendChild(uploadCopy);
                var uploadImage = document.createElement("input");
                    uploadImage.setAttribute("type", "file");
                    uploadImage.setAttribute("name", "uploadedImage");
                uploadForm.appendChild(uploadImage);

                var uploadTriggerContainer = document.createElement("div");
                    uploadTriggerContainer.className = "buttonLink";

                var uploadTrigger = document.createElement("a");
                    uploadTrigger.href = "javascript:makeUploadRequestBridge()";
                    var textNode = document.createTextNode(this.selectedPanelConfig.button);
                    uploadTrigger.appendChild(textNode);

                    uploadTriggerContainer.appendChild(uploadTrigger);


                uploadForm.appendChild(uploadTriggerContainer);

                var clearDiv = document.createElement("div");
                    clearDiv.setAttribute("class", "clearDiv")
                    uploadForm.appendChild(clearDiv)

                var bottomCurve = document.createElement("div");
                    bottomCurve.className = "";
                    bottomCurve.appendChild(document.createElement("div"));


                uploadForm.appendChild(bottomCurve);

                this.form = uploadForm;
            this.panel.setBody(uploadForm);
            this.panel.render(document.body);
            this.addPanelToManager(this.selectedPanelConfig.id, this.panel);
       }
       else{
            this.panel = this.panelManager[this.selectedPanelConfig.id];
        }
    },

    addPanelToManager:function(key, value){
        this.panelManager[key] = value;
    },

    resetForm:function(){
      this.form.reset();
    }
};