﻿/*
 * Menu v0.1.2 - jQuery menu widget
 * Copyright (c) 2008 Tamás Paksa
 *
 * Dual licensed under the MIT and GPL licenses:
 * 	http://www.opensource.org/licenses/mit-license.php
 * 	http://www.gnu.org/licenses/gpl.html
 */

;(function($){

  $.fn.menu_vertical = function(options){
    $(this).addClass('menu_vertical');
    return $(this).each(function(){
      $(this).html($(this).html().replace(/<\/li>[\r\n\t ]*/mgi, '<\/li>'));
      var o = $.extend({},$.fn.menu_vertical.defaults,options);
      $("li>a", this).mouseover(function(){
        if (o.onTitleID != '')
            $('#'+o.onTitleID).text($(this).attr('title') + " ");
      });
      $("li:has(ul)", this).click(function(){
        if ($(this).children("ul").is(":hidden")) {
          $(this).nextAll("li").find("ul").animate(o.hideAnimation,o.speed);
          $(this).prevAll("li").find("ul").animate(o.hideAnimation,o.speed);
          $(this).children("ul").find("ul").hide();
          $(this).children("ul").animate(o.showAnimation,o.speed);
        } else {
          $(this).children("ul").animate(o.hideAnimation,o.speed);
        }
        return false;
      })
      $("li:has(ul)>a", this).append(document.createTextNode(o.appendText));//.next("ul").hide();
      $("li:has(ul)", this).children("ul").not(":has(.active)").hide();
      $("li:has(.active)", this).addClass('active');
    });
  };
  
  $.fn.menu_horizontal = function(options){
    $(this).addClass('menu_horizontal');
    return $(this).each(function(){
      $(this).html($(this).html().replace(/<\/li>[\r\n\t ]*/mgi, '<\/li>'));
      var $$ = $(this);
      var menuitem = false;
      var o = $.extend({},$.fn.menu_horizontal.defaults,options);
      $("li>a", this).mouseover(function(){
        if (o.onTitleID != '')
            $('#'+o.onTitleID).text($(this).attr('title') + " ");
      });
      $("li", this).mouseover(function(){
        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'));
        }
        if ($(this).children("ul").is(":hidden")) {
          $(this).children("ul").animate(o.showAnimation,o.speed);
        }
      });
      $("li", this).mouseout(function(){
        var _this = $(this);
        clearTimeout($$.sfTimer);
        menuitem = true;
        $$.sfTimer = setTimeout(function(){
          if (menuitem === true) {
            $('ul', $$).animate(o.hideAnimation,o.speed);
          } else {
          }
        },o.delay);	
      });
      $("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_vertical2 = function(options){
    $(this).addClass('menu_vertical2');
    return $(this).each(function(){
      $(this).html($(this).html().replace(/<\/li>[\r\n\t ]*/mgi, '<\/li>'));
      var $$ = $(this);
      var menuitem = false;
      var o = $.extend({},$.fn.menu_vertical2.defaults,options);
      $("li>a", this).mouseover(function(){
        if (o.onTitleID != '')
            $('#'+o.onTitleID).text($(this).attr('title') + " ");
      });
      $("li", this).mouseover(function(){
        menuitem = $(this);
        $(this).nextAll("li").find("ul").animate(o.hideAnimation,o.speed);
        $(this).prevAll("li").find("ul").animate(o.hideAnimation,o.speed);
        $(this).children("ul").css('top', $(this).attr('offsetTop'));
        $(this).children("ul").css('left', $(this).attr('offsetLeft') + $(this).attr('offsetWidth'));
        if ($(this).children("ul").is(":hidden")) {
          $(this).children("ul").animate(o.showAnimation,o.speed);
        }
      });
      $("li", this).mouseout(function(){
        var _this = $(this);
        clearTimeout($$.sfTimer);
        menuitem = true;
        $$.sfTimer = setTimeout(function(){
          if (menuitem === true) {
            $('ul', $$).animate(o.hideAnimation,o.speed);
          } else {
          }
        },o.delay);	
      });
      $("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_vertical.defaults = {
		delay		: 500,
		appendText	: '...',
		showAnimation	: {height: 'show'},
		hideAnimation	: {height: 'hide'},
		speed		: 'normal',
    onTitleID: '',
		onInit		: function(){}, // callback functions
		onBeforeShow: function(){},
		onShow		: function(){},
		onHide		: function(){}
	};
	$.fn.menu_horizontal.defaults = {
		delay		: 500,
		appendText	: '...',
		showAnimation	: {opacity: 'show'},
		hideAnimation	: {opacity: 'hide'},
		speed		: 'fast',
    onTitleID: '',
		onInit		: function(){}, // callback functions
		onBeforeShow: function(){},
		onShow		: function(){},
		onHide		: function(){}
	};
	$.fn.menu_vertical2.defaults = {
		delay		: 500,
		appendText	: '...',
		showAnimation	: {opacity: 'show'},
		hideAnimation	: {opacity: 'hide'},
		speed		: 'fast',
    onTitleID: '',
		onInit		: function(){}, // callback functions
		onBeforeShow: function(){},
		onShow		: function(){},
		onHide		: function(){}
	};
  
})(jQuery);

