/**
 * GlobalNav -- Implemented as a jQuery plugin
 * 
 * @version 1.0
 * @author LBi - http://www.lbi.com/en
 * @requires jQuery Core 1.3.1 - http://www.jquery.com/
*/
/*jslint bitwise: true, eqeqeq: true, passfail: false, nomen: false, plusplus: false, undef: true, evil: true */
/*global window, document, navigator, $, jQuery, LBI */
/**
 * See (http://jquery.com/) 
 * @name $
 * @class
 * See the jQuery Library  (http://jquery.com/) for full details. This just
 * documents the function and classes that are added to jQuery by this plug-in. 
 */
/**
 * See (http://jquery.com/)  
 * @name $.fn
 * @class
 * See the jQuery Library  (http://jquery.com/) for full details. This just
 * documents the function and classes that are added to jQuery by this plug-in. 
 */
(function($) {
  // Object to hold the menu options: this is the extended object 
  var options = [], menuWidth = 0;
  /**
   * Instatiate the M&S primary navigation menu
   * @name $.fn.mandsmenu
   * @constructor
   * @param {Object} opts the options passed in when calling the object
   * @return the jQuery wrapper
   */
  $.fn.mandsmenu = function(opts) {
    var $menu = $(this);
    options = $.extend(/** @lends $ */{
      /**
       * menu delay speed
       * @type Number
       */
      delaySpeed: 300,
      /**
       * Menu fadein duration in ms
       * @type Number
       */
      fadeInDuration: 300,
      /**
       * Menu fadeout duration in ms
       * @type Number
       */
      fadeOutDuration: 50,
      /**
       * Make iframe shims transparent (needed if your 
           dropdowns have transparent or semi-transparent
           areas i.e. rounded corners)
       * @type Boolean
       */
      menuShimTransparency: false,
      /**
       * Number of Items in menu
       * @type Number
       */
      primaryNavItemsLength: null
    }, opts || {});
    //remove background (item devider) of 2nd-to-last menu item
    options.primaryNavItemsLength = $menu.children().size();
    $menu.children().eq(options.primaryNavItemsLength - 2).css('background-image', 'none');
    
    // work out and set the required width of the Offers list item
    $menu.children().not('#offers').each( function(i) {
      menuWidth += $(this).outerWidth();
    });   
    $menu.find('li#offers').width(760-(menuWidth+2));
    
    // Workaround for ie6 z-index bug -- search "iframe shim" for info
    // Using version because there's no jQuery.support property for
    var isIE6 = (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6);
    if (isIE6) {
      var iframeShim = $(document.createElement("iframe")).addClass("shim").attr("src", "javascript:false;");
      if (options.menuShimTransparency) {
        iframeShim.css({ 
          "filter": "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)" 
        });
      }
    }
    
    // Only get items with an id beginning with "menu": this means it has a sub menu
    $menu.find('li[id^="menu"]').each(function(i, item) {
      var t;
      
      if($(item).hasClass('current')) {
        $(item).prev().addClass('prev');
      }

      var container = $(item).find('.shopByGroupContainer');
      if (isIE6) {
        // Shim dimensions should match the div it's sitting behind
        var shim = iframeShim.clone(true).
              width(container.outerWidth(true)).
              height(container.outerHeight(true)).
              appendTo(item);
      }

      $(item).hover(
        function(e) {
          // Anonymous method to set highlighted state on tab, 
          // do fadein effect on flyout, etc.
          var $hoveritem = $(this);

          $hoveritem.
            addClass('hovered').
            prev().
            addClass('prev');
            
          if(!$hoveritem.hasClass('current')) {
            $('li.current').removeClass('current').addClass('currentOut');            
            $('li.currentOut').prev().removeClass('prev');  
          }
          
          t = window.setTimeout( function() {
	    if ($hoveritem.find('.shopByGroupContainer').size()>0) {
              if ($hoveritem.find('.shopByGroupContainer').queue().length<=1) {               
                $hoveritem.find('.shopByGroupContainer').fadeIn(options.fadeInDuration);
                if (isIE6) {
                  // the fade effects are actually binary on the shims :-/
                  shim.fadeIn(options.fadeInDuration);
                }
              }
	    }
          }, options.delaySpeed);
        },      
        function(e) {
          // Anonymous method to remove highlight and do fadeout effect
          var $hoveritem = $(this);
          if (!$hoveritem.hasClass('current')) {
            $hoveritem.removeClass('hovered').prev().removeClass('prev');         
          } else {
            $hoveritem.removeClass('hovered');  
          }
          $('li.currentOut').removeClass('currentOut').addClass('current').prev().addClass('prev');
          $hoveritem.find('.shopByGroupContainer').fadeOut(options.fadeOutDuration);
          if (isIE6) {
            shim.fadeOut(options.fadeOutDuration);
          }
          window.clearTimeout(t);
        }
      );
    });

    $menu.find('.shopByGroupContainer').css('display', 'none'); 
    
    return this;
  };
})(jQuery);

function showDropDownListForMenuOnLoad(){
    var menuItems = jQuery('#menu').children();
    var mouseX=0;
    var mouseY=0;
    if(typeof(event) != 'undefined'){
        mouseX = event.clientX + document.body.scrollLeft;
        mouseY = event.clientY + document.body.scrollTop;
    }

    menuItems.each(function(){
        var thisItem = jQuery(this);
        var top = thisItem.offset().top;
        var bottom = thisItem.height() + top;
        var left = thisItem.offset().left;
        var right = thisItem.width() + left;

        if(mouseX>=left&&mouseX<=right&&mouseY>=top&&mouseY<=bottom){
            thisItem.trigger('hover');
        }
    });
}

jQuery(document).ready( function(e) {
  jQuery('#menu').mandsmenu();
  showDropDownListForMenuOnLoad();
});

