/*
 * cmsmenu The jQuery menu plugin
 *
 * Copyright (c) 2011 Martin Sadera
 * http://www.sadera.cz
 * Developed for EDACMS Content Management System - http://edacms.e-d-a.info
 * Licensed under MIT
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Launch  : 02/2011
 * Version : 1.0
 * Released: 3.2.2011 - 8:41
 */
(function($){
    $.fn.extend({
        cmsmenu: function(options) { 
            var defaults = {
                hoverClass: 'active',
                slidePixels: -10,
                slideSpeed: 200,
                isHorizontal: true
            };
             
            var options = $.extend(defaults, options);
            var o = options;
            var obj = $(this);
            obj.addClass('cmsmenu');
            //set default top
            $('>ul>li',obj).each(function() {
              $(this).find(">ul").css("opacity",0);
              /* horizontal navigation default positioning */
              if (!o.isHorizontal) {
                $(this).find(">ul").css("left",$(this).width());
                $(this).find(">ul").css("top",-o.slidePixels);
                } else {
              /* vertical navigation default positioning */
                $(this).find(">ul").css("left",0);
                $(this).find(">ul").css("top",$(this).height()-o.slidePixels);
                }
              });
            
            
            $('>ul>li',obj).hover(
                function() {
                  $(this).addClass(o.hoverClass);
		  /* horizontal navigation positioning */
                  if (!o.isHorizontal) {
                    $(this).find(">ul").stop(true,true).show().animate({opacity: 1.0, top: 0},o.slideSpeed,
                    function() {
                        $(this).find(">ul").hide();

                    });
                  /* vertical navigation positioning */
                  } else {
                    $(this).find(">ul").stop(true,true).show().animate({opacity: 1.0, top: $(this).height()},o.slideSpeed,
                    function() {});
                  }
                }, 
                function() {
                  $(this).removeClass(o.hoverClass);
                  /* horizontal navigation positioning */
                  if (!o.isHorizontal) {
		    $(this).find(">ul").stop(true,true).animate({opacity: 0,top: -o.slidePixels},o.slideSpeed,
                    function() {
                      $(this).hide();
                      $(this).find(">ul").hide();
                    });
                  /* vertical navigation positioning */
                  } else {
                    $(this).find(">ul").stop(true,true).animate({opacity: 0,top: $(this).height()-o.slidePixels},o.slideSpeed,
                    function() {
                      $(this).hide();
                      $(this).find(">ul").hide();
                    });
                  }
                });
            
            /* second and next levels of menu */
            $('ul ul li',obj).each(function () {
               $(this).hover(
                function() {
		              $(this).addClass(o.hoverClass);
                  $(this).find(">ul").css('left',$(this).width()).css('top',-o.slidePixels).css('opacity','0');
                  $(this).find(">ul").stop(true,true).show().animate({opacity: 1.0, top: 0},o.slideSpeed,
                  function() {
                    
                  });
                }, 
                function() {
                  $(this).removeClass(o.hoverClass);
		              $(this).find(">ul").stop(true,true).animate({opacity: 0,top: -o.slidePixels},o.slideSpeed,
                  function() {
                    $(this).hide();
                  });
                });
              });
        }
    });
})(jQuery);
