/*
 */

if (typeof (gadgets) == "undefined")
  gadgets = {};

gadgets.lp = gadgets.lp || {};

gadgets.lp.menu = function() {

  //define language packs
        var language = {};
        language["en"] =
                [
                    "Help",
                    "About",
                    "More gadgets",
                    "Help",
                    "About this gadget",
                    "Visit LabPixies.com"
                ];
        language["he"] = [
            "עזרה", "אודות", "גאג'טים נוספים", "אודות גאג'ט זה", "בקרו באתרינו"
        ];
        language["iw"] = language["he"];

        var targetUrl = "http://www.labpixies.com";
        var menuImageDir = "http://cdn.labpixies.com/infra/images/menu/";
        var addToToolbarUrl =
                "http://cdn.labpixies.com/gadget_page.php?inst_on_toolbar=1&id=";
        var openTimer = 0;
        var closeTimer = 0;
        var iconImage = null;
        var isOpen = false;
        var menuContainer, menuButton, iconContainer, logo, menu;
        var textDirection = "ltr";
        var lang = "en";

        var gadget_menu_element;
        var additionalParams;

        try {
          jQuery(document).ready( function() {
            reportCommScore();
          });
        } catch (eewe) {
        }
        /**
         *Elm must be an html element
         */
        function init(elementId, moreParams) {

          gadget_menu_element = $lp("#" + elementId);
          if (!gadget_menu_element)
            return;
          additionalParams = moreParams;

          // get settings from the gadget
          if (!additionalParams)
            additionalParams = {};
          textDirection = additionalParams.textDirection || "ltr";
          lang = additionalParams.lang || "en";
          lang = language[lang] ? lang : "en";

          addTopMenu();
        }

        //report to commsocre
        function reportCommScore() {

          try {
            var img = new Image(1, 1);
            var r1 = new String(Math.random());
            r1 = r1.substring(2, 11);
            img.onload = function() {
              return;
            };
            img.src =
                    "http://static.labpixies.com/commscore/?rnd=" + r1 +
                            "&CXNID=1000012." + _gadgetID + "NXC";
          } catch (ee) {
          }
        }

        //return true if the menu should be disply on top
        function isTopMenu() {
          try {
            if (gadgets.views.getCurrentView().getName() == "canvas")
              return false;
          } catch (ee) {
          }

          return true;

        }

        function getCachedUrl(url) {
          if (typeof (_IG_GetCachedUrl) == "function") {
            return _IG_GetCachedUrl(url);
          }
          return url;
        }

        function addTopMenu() {

          //create and add the main container for the menu
          menuContainer =
                  $lp("<div style='height:23px;padding:0px 5px 0px 5px;'></div>");
          menuContainer.css("background-image", "url(" +
                  getCachedUrl(menuImageDir + "menu_middle.gif") + ")");
          gadget_menu_element.append(menuContainer);

          // create the image icon
          menuButton =
                  $lp(
                          "<img width='39' height='14' border='0' src='" +
                                  getCachedUrl(menuImageDir + "menu_off.gif") +
                                  "' style='position:relative; top:4px; cursor:pointer; float:right;'></img>")
                          .hover(
                                  function(e) {
                                    $lp(this).attr(
                                            "src",
                                            getCachedUrl(menuImageDir +
                                                    "menu_on.gif"));
                                    noCloseDelayed();
                                  },
                                  function(e) {
                                    closeDelayed();
                                    noOpenDelayed();
                                    if (!isOpen)
                                      $lp(this).attr(
                                              "src",
                                              getCachedUrl(menuImageDir +
                                                      "menu_off.gif"));
                                  }).click( function(e) {
                            if (isOpen)
                              closeMenu();
                            else
                              openMenu();
                          });
          menuContainer.append(menuButton);

          // create the icon container
          iconContainer =
                  $lp("<div style='float:right; height:14px; overflow:hidden; margin:5px 3px 4px 0px;'></div>");
          menuContainer.append(iconContainer);

          logo =
                  $lp(
                          "<img width='65' height='23' border='0' src='" +
                                  getCachedUrl(menuImageDir + "pixies_off.gif") +
                                  "' style='cursor:pointer; float:left;' />")
                          .hover(
                                  function(e) {
                                    $lp(this).attr(
                                            "src",
                                            getCachedUrl(menuImageDir +
                                                    "pixies_on.gif"))
                                  },
                                  function(e) {
                                    $lp(this).attr(
                                            "src",
                                            getCachedUrl(menuImageDir +
                                                    "pixies_off.gif"))
                                  }).click(
                                  function(e) {
                                    window.open(targetUrl + "?gadget=" +
                                            _gadgetID +
                                            "&source=menu&menu_item=logo",
                                            "_blank");
                                  });
          menuContainer.append(logo);

          // add the menu
          menu =
                  $lp("<div style='display:none; padding:0px; margin:0px; position:absolute; z-index:101; width:70px; overflow:hidden; border:1px solid #0096d0; border-bottom:0px'></div>");
          menuContainer.append(menu);

          // add the items to the menu
          addItem(language[lang][2], language[lang][5], menuImageDir +
                  "generic", function() {
            window.open("http://www.labpixies.com/?gadget=" + _gadgetID +
                    "&source=menu&menu_item=more", "_blank");
          });

          addItem(language[lang][1], language[lang][4], menuImageDir + "about",
                  function() {
                    window.open("http://www.labpixies.com/gadget_page.php?id=" +
                            _gadgetID + "&gadget=" + _gadgetID +
                            "&source=menu&menu_item=about", "_blank");
                  });

          addItem(language[lang][0], language[lang][3], menuImageDir +
                  "generic", function() {
            window.open("http://www.labpixies.com/support/?gadget=" +
                    _gadgetID + "&source=menu&menu_item=help", "_blank");
          });
        }

        function addItem(lbl, longLbl, icon, callback) {

          var itm;
          var d = textDirection == "ltr" ? "left" : "right";
          itm =
                  $lp("<div align='" + d + "'>" + lbl + "</div>").click(
                          function() {
                            closeMenu();
                            callback();
                          });
          itm.css( {
              "font" : "normal 10px arial, Helvetica, sans-serif",
              "line-height" : "16px",
              "color" : "#0096d0",
              "background-color" : "#ffffff",
              "border-bottom" : "1px solid #0096d0",
              "width" : "68px",
              "height" : "16px",
              "padding" : "0px 2px 0px 2px",
              'cursor' : "pointer",
              'direction' : textDirection
          }).hover( function(e) {
            noCloseDelayed();
            $lp(this).css( {
                "color" : "#ffffff",
                "background-color" : "#0096d0"
            })
          }, function(e) {
            closeDelayed();
            $lp(this).css( {
                "color" : "#0096d0",
                "background-color" : "#ffffff"
            })
          });
          menu.append(itm);
        }

        /**
        will open the menu unless the timer will be canceld by the noOpenDelayed
         */
        function openDelayed() {
          openTimer = setTimeout(openMenu, 300);
        }

        /**
        canceld by the OpenDelayed
         */
        function noOpenDelayed() {
          clearTimeout(openTimer);
        }

        /**
        	actualy open the manu
         */
        function openMenu() {
          if (!isOpen) {
            var off = menuButton.offset();
            menu.css("top", off.top + 15).css("left", off.left - 34).show();
            isOpen = true;

            try {
              if (_IG_Analytics) {

                _IG_Analytics("UA-345375-1", "/openMenu");
              }
            } catch (ee) {
            }
          }
        }

        /**
        will close the menu unless the timer will be canceld by the noOpenDelayed
         */
        function closeDelayed() {
          closeTimer = setTimeout(closeMenu, 300);
        }

        /**
        canceld by the OpenDelayed
         */
        function noCloseDelayed() {
          clearTimeout(closeTimer);
        }

        function closeMenu() {
          if (isOpen) {
            $lp(menu).hide();
            menuButton.attr("src", getCachedUrl(menuImageDir + "menu_off.gif"));
            isOpen = false;
          }
        }

        function AddIcon(imageUrl, title, clickCallback) {

          var icon =
                  $lp(
                          "<img src='" + imageUrl + "' title='" + title +
                                  "' style='margin:0px 2px; cursor:pointer;' />")
                          .click(clickCallback);
          iconContainer.append(icon);
        }

        return /** @scope gadgets.lp.menu*/
        {

            deploy : function(div_element_id, additionalParams) {
              init(div_element_id, additionalParams);
            },

            //this function is depricated
            addMenuItem : function(lbl, callback) {
              addItem(lbl, lbl,
                      "http://cdn.labpixies.com/infra/images/menu/generic",
                      callback);
            },

            addLongMenuItem : function(lbl, longLbl, icon, callback) {
              addItem(lbl, longLbl, icon, callback);
            },

            addIconItem : function(imageUrl, title, clickCallback) {
              AddIcon(imageUrl, title, clickCallback);
            }
        };
      }();

