(function($){
  $.fn.goodMenu = function(options){
    // merge default and runtime options
    var o = $.extend($.fn.goodMenu.def, options||{});
    var items;
    var hilight;
    var hovering = false;

    this.each(function(){
      var _this = $(this);

      // target must be a UL structure
      if(!_this.is('ul')){ return this; }

      items = $('> li', this);
      buildMenu(_this, o);

      return this;
    });

    function _hilight(options){
      var o = {};
      
      if(typeof options.defaultHilight == 'string'){
        // assume a sting is to be used as a jq selector
        var s = $(options.defaultHilight);
        var ign = options.ignoreItems || '.good-ui_menu_no-hilight';
        if(s.length > 0){
            o.height = s.height();
            o.width = Math.ceil((options.hilightPadWidth ? s.width()-options.hilightPadWidth : s.width()))+1;
            o.top = Math.floor(s.position().top);
            o.left = !s.is(ign) ? Math.floor((options.hilightPadWidth ? s.position().left+(options.hilightPadWidth/2) : s.position().left))-2 : (o.width+10)*-3;           
        }
      } else {
        o = $.extend(o, options.defaultHilight||{});
      }

      return o;
    }

    function buildMenu(ul, o){
      ul.wrap('<div id="good-ui_menu"></div>');
	    var menu = ul.parent('#good-ui_menu').css({'position':'relative', height: ul.outerHeight(), overflow: "hidden"});
     
      var p = _hilight(o);
      
      var hilight = $('<div id="good-ui_menu-hilight"></div>').css({
          height: p.height,
          width: p.width,
          position: 'absolute',
          top: p.top,
          left: p.left,
          padding: '0 1px'
      });
      
      if($.browser.msie && parseInt($.browser.version) < 7){
    	  hilight.css('overflow', 'hidden');
      }
      
      var hilightFill = $('<div class="fill"></div>').css({
    		  "background-color": "#000",
    		  "opacity": 0.2,
    		  "position": "absolute",
    		  "height": '70%',
    		  "width": '100%',
    		  "top": 5,
    		  "left": 0,
    		  overflow: "visible"
		  }).appendTo(hilight);
		
  		var hilightStroke = $('<div class="stroke"></div>').append($('<div></div>').css({
  		  "border-left": "3px solid #0f0",
        "border-right": "3px solid #0f0", 
        height: '50%', 
        position: 'static'
      })).css({
        "opacity": 0.5,
        "position": "absolute",
        margin: '0 0px',
        "height": '80%',
  	    "width": '100%',
  		  "top": 10,
  		  "left": 0
      }).appendTo(hilight);
      
      menu.append(hilight)
        
  	  items.each(function(i){
        var $this = $(this);
        $this.addClass('good-ui_menu_item').css('z-index', 3);
        
        if(!$this.is(o.ignoreItems)){
          var c = $('> a', $this);
          $this.hover(function(){
            //onmouseover
            var t = $(this).is('a') ? $(this).parent('li') : $(this);
    			  var target = {
              left: Math.floor((o.hilightPadWidth ? t.position().left + (o.hilightPadWidth/2) : t.position().left))-2,
              height: t.height(),
              width: Math.ceil((o.hilightPadWidth ? t.width() - o.hilightPadWidth : t.width()))+1
            };
            
            hilight.stop(true).animate(target, 'slow', 'swing').css('overflow', 'visible');       
          },
          function(){
            hilight.stop(true).animate(p, 300, 'swing').css('overflow', 'visible');
          });
        }
      });
    }
  };
  
  $.fn.goodMenu.def = {
    ignoreItems: "#menu-home, .ignore",
    defaultHilight: {top: 0, left: -210, height: 84, width: 100},
  	hilightPadWidth: 10
  };
})(jQuery);