/**
* @Plugin URL 	: http://www.pharmeon.nl
* @author		: Amir Swaleh
* @Description  : Plugin to spice up your menu 
* @Version 		: 1.6.1
* @published	: 19/11/2009
* @updated		: 15/02/2010
  Updated The Toggle. When Clicked Any Open Element Will Be Hidden
  Updated The Animation Sequence. It was looping like madd :( 
*
* @Installation : 
* Inlcude this in your html 

<script>
    //Prevent Jquery Conflicts
    //Replace $ with the defined jQuery sign - by Amir Swaleh
    var jQuery = jQuery.noConflict();
    jQuery(document).ready(function(){ 
     	 jQuery('#mainnav').HoverMnu({slideDownSpeed: 500, slideUpSpeed: 400, menu_id: "#mainnav"});
	}); 	   
</script>

*/

//You need an anonymous function to wrap around your function to avoid conflict
(function(jQuery){
		  
	//Attach this new method to jQuery
 	jQuery.fn.extend({ 
 		
 		//This is where you write your plugin's name
 		HoverMnu: function(options, functionHandle) {
				
			// Set Default Settings First
			var defaults = {
				slideDownSpeed: 300,
				slideUpSpeed: 340,
				menu_id: "#mainnav",
				mouseEvent: "hover",
				animationtype: "Show"
			};		
			
			var options = jQuery.extend({}, jQuery.fn.HoverMnu.defaults, options);

			//Iterate over the current set of matched elements
    		return this.each(function() {
				var o = options;
				var obj = jQuery(this);
				var items = jQuery("li", obj);
				
				// Hide All The Empty elements.
				// If a field is empty. Please Hide the parent

				jQuery("" + o.menu_id + " ul.submenu").each(function() {
					if (jQuery(this).children("li").text() == "") {
						jQuery(this).addClass("Hidden");
						jQuery(this).hide();
						return false;
					}
				});
	
				// Add a class to ul with ul in it 
				jQuery("" + o.menu_id + " li.submenu:has(li)").addClass("class").parents();
				
				
				jQuery("" + o.menu_id + " li.class a.lefttabs_notselected").each(function(){
					jQuery(this).addClass("noclick");
				});
				jQuery("" + o.menu_id + " li.class a.lefttabs_selected").each(function(){
					jQuery(this).addClass("noclick");
				});

				// Check for Event Type
				if ( o.mouseEvent == "Hover") {
					// Check For Animation Type
					if ( o.animationtype == 'Slide' ) {
					// Attach hover en hover out event to the LI 
						jQuery("" + o.menu_id + " li.class").hover(function(){
						jQuery(this).children('ul').filter(':not(:animated)').stop().slideDown(o.slideDownSpeed);
					}, function(){
						jQuery(this).children('ul').slideUp(o.slideUpSpeed);
					});
					} else if ( o.animationtype == 'Fade' ) {
						// Attach hover en hover out event to the LI 
						jQuery("" + o.menu_id + " li.class").hover(function(){
						jQuery(this).children('ul').filter(':not(:animated)').stop().fadeIn(o.slideDownSpeed);
					}, function(){
						jQuery(this).children('ul').fadeOut(o.slideUpSpeed);
					});
					} else if ( o.animationtype == 'Show' ) {
						jQuery("" + o.menu_id + " li.class").hover(function(){
							jQuery(this).children('ul').filter(':not(:animated)').stop().animate({width: 'toggle'});													
						}, function(){
							jQuery(this).children('ul').animate({width: 'toggle'});
						});
						//alert('Animation Isnt Set');
					} else {
						// This is the default animation type. It just Shows/Hides the sub Menu
						// If No Animationtype is defined Please use this
						
						jQuery("" + o.menu_id + " li.submenu").hover(function() {
							jQuery(this).addClass('iehover');
						}, function() {
							jQuery(this).removeClass('iehover');
						});
						
						jQuery("" + o.menu_id + " li.class").hover(function(){							
							jQuery(this).children('ul').filter(':not(:animated)').stop().show();													
							}, function(){
								jQuery(this).children('ul').hide();
							});
						//alert('Animation Isnt Set');
					};	
				} else {
					if( o.animationtype == 'Slide') {
						jQuery("" + o.menu_id + " li.class a.noclick") .attr("href", "javascript:void(0)");
						jQuery("" + o.menu_id + " li.selected.class ul").show();
						
						jQuery("" + o.menu_id + " li.class").click(function(event){	
							var visibleSiblings = jQuery("ul.submenu").is(":visible");						
							// Check If An Element Is Open
							// Then Close It
							if (visibleSiblings){
								jQuery("ul.submenu").filter(":not(:animated)").stop().slideUp(o.slideUpSpeed);
							}			
							jQuery(this).children("ul.submenu").filter(":not(:animated)").stop().slideDown(o.slideDownSpeed);
						});
					
					} else {
						if( o.animationtype == 'Show') {
							jQuery("" + o.menu_id + " li.class a.noclick") .attr("href", "javascript:void(0)");
							jQuery("" + o.menu_id + " li.selected.class ul").show();
							
							jQuery("" + o.menu_id + " li.class ").click(function(event){
								var visibleSiblings = jQuery("ul.submenu").is(":visible");							
								// Check If An Element Is Open
								// Then Close It
								if (visibleSiblings){
									jQuery("ul.submenu").filter(":not(:animated)").stop().hide(o.slideDownSpeed);
								}
								jQuery(this).children("ul.submenu").filter(":not(:animated)").stop().slideDown();
							});
						}
					};
				};
				
				

    		});
    	}
	});
	
//pass jQuery to the function, 
//So that we will able to use any valid Javascript variable name 
})(jQuery);


