/*
 * Menu v0.1.3 - jQuery menu widget
 * Copyright (c) 2010 Tamás Paksa
 */

;(function($){

  $.fn.menu_horizontal = function(options){
    $(this).addClass('menu_horizontal');
    return $(this).each(function(){
      var $$ = $(this);
      var menuitem = false;
      var o = $.extend({},$.fn.menu_horizontal.defaults,options);
      
      $("li>a", this).bind('mouseover', function(){
        if (o.onTitleID != '')
            $('#'+o.onTitleID).text($(this).attr('title') + " ");
      });
      
      $("li", this).bind('mouseover click', function(e){
        var $this = $(this);
        clearTimeout($$.showTimer);
        menuitem = $this;
        $$.showTimer = setTimeout(function(){
          if (menuitem == $this) {
            $this.nextAll("li").find("ul").animate(o.hideAnimation,o.speed);
            $this.prevAll("li").find("ul").animate(o.hideAnimation,o.speed);
            if ($this.closest('ul').html() != $$.html()) { //ha nem a gyökér elemről van szó
              $this.children("ul").css('top', $this.attr('offsetTop'));
              $this.children("ul").css('left', $this.attr('offsetLeft') + $this.attr('offsetWidth'));
            } else {
              if (o.setWidth) {
                $this.children("ul").width($this.width());
              }
            }
            if ($this.children("ul").is(":hidden")) {
              $this.children("ul").animate(o.showAnimation,o.speed);
            }
          }
        }, (e.type == 'click') && o.click ? 0 : o.hideDelay);
      });
      
      if (o.click) {
        $('li:has(ul)>a').bind('click', function (e) {
          e.preventDefault();
        });
      }

      
      $("li", this).bind('mouseout', function(){
        var _this = $(this);
        clearTimeout($$.hideTimer);
        menuitem = true;
        $$.hideTimer = setTimeout(function(){
          if (menuitem === true) {
            $('ul', $$).animate(o.hideAnimation,o.speed);
          } else {
          }
        }, o.hideDelay);  
      });
      
      $("li:has(ul)>a", this).append(document.createTextNode(o.appendText));
      $("li:has(ul)", this).children("ul").hide();
      $("", this).append('<li class="clear" />');
      $("li:has(.active)", this).addClass('active');
    });
  };

  $.fn.menu_horizontal.defaults = {
    click         : false,
    hideDelay     : 500,
    showDelay     : 200,
    appendText    : '...',
    showAnimation : {opacity: 'show'},
    hideAnimation : {opacity: 'hide'},
    speed         : 'fast',
    setWidth      : false,
    onTitleID     : '',
    onInit        : function(){}, // callback functions
    onBeforeShow  : function(){},
    onShow        : function(){},
    onHide        : function(){}
  };

})(jQuery);

