var Dom = YAHOO.util.Dom,
    Event = YAHOO.util.Event;


YAHOO.util.Event.onDOMReady(function() {
  var containerClass = "wspContentRenderer"; // Class of container that contains modified font.
      var defaultFontSize = 12;                  // Default font size. Is used if no any font that apllied to container.
      var minFontSize = 8;                       // Min size of font.
      var maxFontSize = 22;                      // Max size of font.
      var fontSizeStep = 2;                      // Font change step.
      var fontSizeSuffix = "px";                 // Can takes next values "px", "pt", "%".

      var Dom = YAHOO.util.Dom,
          Event = YAHOO.util.Event;


  /* Username / password hints below */

  /**
   * Displays hint for text Input element
   * @param el {Object|String} Identifier oof element or HTML element.
   * @param text {Object}  text that must be displayed
   */
  var attachHintText = function (el, text) {
      var oElement = el;

      if ((oElement.value == "") || (oElement.value == text)) {
          Dom.addClass(oElement, "hint-text");
          oElement.value = text;


          Event.addListener(oElement, "focus", function() {
              if (Dom.hasClass(oElement, "hint-text")) {
                  Dom.removeClass(oElement, "hint-text");
                  oElement.value = "";
              }
          }, this, true);

          Event.addListener(oElement, "blur", function() {
              if (oElement.value == "") {
                  Dom.addClass(oElement, "hint-text");
                  oElement.value = text;
              }
          }, this, true);
      }
  }

  /**
   * Displays hint for password Input element
   * @param el {Object|String} Identifier oof element or HTML element.
   * @param text {Object}  text that must be displayed
   */
  var attachHintPassword = function(el, text) {
      var oElement = el;

      if (oElement.value == "") {
          var oProxyElement = document.createElement("input");
          oProxyElement.className = oElement.className;
          oProxyElement.value = text;
          oElement.parentNode.replaceChild(oProxyElement, oElement);
          Dom.addClass(oProxyElement, "hint-password");

          Event.addListener(oProxyElement, "focus", function() {
              oProxyElement.parentNode.replaceChild(oElement, oProxyElement);

              // Solution for IE focus() bug.
              setTimeout(function() { oElement.focus(); }, 10);
          }, this, true);

          Event.addListener(oElement, "blur", function() {
              if (oElement.value == "") {
                  oElement.parentNode.replaceChild(oProxyElement, oElement);
              }
          }, this, true);
      }
  }


  /**
   * Attachs hint to input element.
   * @param {Object|String} el
   * @param {Object} text
   */
  var attachHint = function(el, text) {
      var oElement = null;
      if (YAHOO.lang.isString(el)) {
          oElement = Dom.get(el);
      } else if (YAHOO.lang.isObject(el)) {
          oElement = id;
      }
      if (!oElement) {
          return;
      }

      oElement.readOnly = false;
      oElement.disabled = false;

      if (oElement.type == "password") {
          attachHintPassword(oElement, text);
      } else {
          attachHintText(oElement, text);
      }
  }

  attachHint("signin_username", "Enter username");
  attachHint("signin_password", "Enter password");


  /* Font size + - code below */

  var oContainer = YAHOO.util.Dom.getElementsByClassName(containerClass)[0];

  /**
   * Increases Container's font size.
   */
    var increaseFont = function () {
        var currentFontSize = parseInt(YAHOO.util.Dom.getStyle(oContainer, "font-size"));
    if (!isNaN(currentFontSize)) {
      currentFontSize += fontSizeStep;
      if (currentFontSize > maxFontSize) {
        currentFontSize = maxFontSize;
      }
    } else {
      currentFontSize = defaultFontSize;
    }

    YAHOO.util.Dom.setStyle(oContainer, "font-size", currentFontSize + fontSizeSuffix);
    }

  /**
   * Decreases Container's font size.
   */
    var decreaseFont = function () {
          var currentFontSize = parseInt(YAHOO.util.Dom.getStyle(oContainer, "font-size"));
          if (!isNaN(currentFontSize)) {
              currentFontSize -= fontSizeStep;
              if (currentFontSize < minFontSize) {
                  currentFontSize = minFontSize;
              }
          } else {
              currentFontSize = defaultFontSize;
          }

          YAHOO.util.Dom.setStyle(oContainer, "font-size", currentFontSize + fontSizeSuffix);
    }

    // Attach event handler to "A+" button.
    YAHOO.util.Event.addListener("special-font-increase", "click", increaseFont);

  // Attach event handler to "A-" button.
  YAHOO.util.Event.addListener("special-font-decrease", "click", decreaseFont);
});




// Attach Event handler to Forgot password link to show "Send password" panel.
YAHOO.util.Event.onDOMReady(
    function(e) {
        var oForgotPasswordEl = Dom.get("forgot-password");
        var oForgotPasswordPanelEl = Dom.get("forgot-password-panel");
        var oCancelButtonEl = Dom.get("forgot-password-panel-cancel");
        var oSendButtonEl = Dom.get("forgot-password-panel-send");
        if (oForgotPasswordEl && oForgotPasswordPanelEl){
            Event.purgeElement(oForgotPasswordEl, "click");
            Event.addListener(oForgotPasswordEl, "click", function(e){
               Event.stopEvent(e);
               // Hide "forgot password" link.
               Dom.addClass(oForgotPasswordEl, "hidden");
               // Hide Profile form.
               Dom.addClass("profile_form", "hidden");              
               // Show Email submit panel.
               Dom.removeClass(oForgotPasswordPanelEl, "hidden"); 
            });

            if (oCancelButtonEl) {
                // Cancel button click event handler.             
                Event.purgeElement(oCancelButtonEl, "click");
                Event.addListener(oCancelButtonEl, "click", function(e){
                    Event.stopEvent(e);
                    // Show "forgot password" link.
                    Dom.removeClass(oForgotPasswordEl, "hidden");
                    // Show Profile form.
                    Dom.removeClass("profile_form", "hidden");
                    // Hide Email submit panel.
                    Dom.addClass(oForgotPasswordPanelEl, "hidden");
                });
            }
            
            if (oSendButtonEl) {
                // Send button click event handler.
                Event.purgeElement(oSendButtonEl, "click");
                Event.addListener(oSendButtonEl, "click", function(e){
                    Event.stopEvent(e);
                    var callback = {
                        success: function (o) {
                            var obj = eval("(" + o.responseText + ")");

                            if (obj.error) {
                                
                            } else {
                                Dom.get("password_email").value = "";

                                // Show "forgot password" link.
                                Dom.removeClass(oForgotPasswordEl, "hidden");
                                // Show Profile form.
                                Dom.removeClass("profile_form", "hidden");
                                // Hide Email submit panel.
                                Dom.addClass(oForgotPasswordPanelEl, "hidden");
                            }

                            alert(obj.message);                            
                        },
                        failure: function (o) {
                            alert("Server connection error");
                        },
                        cache:false
                    }
                    // argument formId can be the id or name attribute value of the
                    // HTML form, or an HTML form object.
                    var oForm = Dom.get('forgot-password-form');
                    YAHOO.util.Connect.setForm(oForm);
                    YAHOO.util.Connect.asyncRequest('POST', oForm.action, callback);
                });
            }
        }
    }
);


// Add keyboard functionality to top menu.
YAHOO.util.Event.onDOMReady (
    function(e) {
        
        var getActiveItem = function() {
            var aItems = oMenuBar.getItems();
            var oResultItem = null;
            for (var i = 0; i < aItems.length; i++) {
                var oLabel = YAHOO.util.Dom.getElementsByClassName("yuimenubaritemlabel", "a", aItems[i].element)[0];
                if (YAHOO.util.Dom.hasClass(oLabel, "selected")) {
                    oResultItem = aItems[i];
                    break;    
                }
            }
            return oResultItem;
        }
        
        var getActiveItemIndex = function() {
            var aItems = oMenuBar.getItems();
            var nResultIndex = -1;
            for (var i = 0; i < aItems.length; i++) {
                var oLabel = YAHOO.util.Dom.getElementsByClassName("yuimenubaritemlabel", "a", aItems[i].element)[0];
                if (YAHOO.util.Dom.hasClass(oLabel, "selected")) {
                    nResultIndex = i;
                    break;    
                }
            }
            return nResultIndex;
        }
      
        var removeSelection = function(index) {
            var oItem = oMenuBar.getItems()[index];
            var oLabel = YAHOO.util.Dom.getElementsByClassName("yuimenubaritemlabel", "a", oItem.element)[0];
            YAHOO.util.Dom.removeClass(oLabel, "selected");
        }  

        var addSelection = function(index) {
            var oItem = oMenuBar.getItems()[index];
            var oLabel = YAHOO.util.Dom.getElementsByClassName("yuimenubaritemlabel", "a", oItem.element)[0];
            if (!YAHOO.util.Dom.hasClass(oLabel, "selected")) {
                YAHOO.util.Dom.addClass(oLabel, "selected");
            }
        }  
      
        // Add global key event listeners to control menu.
        var onChangeSection = function(e, args) {
            var newTabIndex = oldTabIndex = getActiveItemIndex();
            if (oldTabIndex > -1) {
                if (args[0] == YAHOO.util.KeyListener.KEY.LEFT) {
                    newTabIndex--;
                    if (newTabIndex < 0) {
                        newTabIndex = oMenuBar.getItems().length - 1;
                    } 
                } else if (args[0] == YAHOO.util.KeyListener.KEY.RIGHT) {
                    newTabIndex++;
                    if (newTabIndex == oMenuBar.getItems().length) {
                        newTabIndex = 0;
                    }
                }
                
                // If Tab index changed then navigate to new Tab.
                if (newTabIndex != oldTabIndex) {
                    removeSelection(oldTabIndex);
                    oMenuBar.getItems()[oldTabIndex].blur();
                    addSelection(newTabIndex);
                    oMenuBar.getItems()[newTabIndex].focus();
                    
                    var oSubmenu =  oMenuBar.getItems()[oldTabIndex].cfg.getProperty("submenu");
                    if (oSubmenu) {
                        if (oSubmenu.cfg.getProperty("visible")) {
                            oSubmenu.hide();
                        }    
                    }
                }
            }
        }
        
        var onChangePage = function(e, eObj) {
            var oSelectedMenuItem = getActiveItem();
            if (oSelectedMenuItem) {
                var oSubmenu =  oSelectedMenuItem.cfg.getProperty("submenu");
                if (oSubmenu) {
                    if (!oSubmenu.cfg.getProperty("visible")) {
                        oSubmenu.show();
                    }    
                }
            }            
        }
        
        var oChangeSectionListener = new YAHOO.util.KeyListener(
            document, 
            { keys: [YAHOO.util.KeyListener.KEY.LEFT, YAHOO.util.KeyListener.KEY.RIGHT] }, 
            { fn: onChangeSection, scope: this, correctScope: false}
        ); 
        
        oChangeSectionListener.enable();    
    
        var oChangePageListener = new YAHOO.util.KeyListener(
            document, 
            {keys: [YAHOO.util.KeyListener.KEY.UP, YAHOO.util.KeyListener.KEY.DOWN]},
            { fn: onChangePage, scope: this, correctScope: false} 
        ); 
        
        oChangePageListener.enable();        
    }
)

