/*
	Programmer: Lukasz Czerwinski
	CodeCanyon: http://codecanyon.net/user/Lukasz_Czerwinski
	
	If this script you like, please put a comment on codecanyon.
	
*/

(function($){
$.fn.horizontalMenu = function(settings){
	//Global variables 
	var el, items;
	//Default settings
	settings = jQuery.extend({
		Speed			: 220,			//Speed animations 
		autohide		: true,			//(true) hide active items
		timeHide 		: 1000,			//seconds to hide when mouse is outside
		effectShow		: "slide",		//Default show effect
		effectHide		: "slide", 		//Default hide effect
		method			: "mouseover"	//The method of showing and hiding
	}, settings);
	//Basic element
	el = $(this);
	//Items with subitems
	items = el.children("ul").parent("li").children("a");
	el.find(">ul").addClass("menuLits");
	//Create new jQ function to show
	$.fn.effectShow = function (speed, callback) {
		switch(settings.effectShow) {
			//Slid effect
			case "slide":
				return $(this).slideDown(speed, callback);
				break;
			//Fade effect
			case "fade":
				return $(this).fadeIn(speed, callback);
				break;
		}
	};
	//Create new jQ function to hide
	$.fn.effectHide = function (speed, callback) {
		switch(settings.effectHide) {
			//Slide effect
			case "slide":
				return $(this).slideUp(speed, callback);
				break;
			//Fade effect	
			case "fade":
				return $(this).fadeOut(speed, callback);
				break;
		}
	};
	
	//Function to hide
	function HideItems (els) {
		//Hide actives
			els.parent().parent().find(".active").parent("li").children("ul").effectHide(settings.Speed/1.2, function(){
				//Replace class to "inactive"
				$(this).parent("li").children("a").removeClass().addClass("inactive");	
			}); 
	}
	
	//Add class
	items.addClass("inactive");
	//Hide and show the subitem
	function _item (){
		var thisEl = $(this);
		
		if(settings.autohide) {
			HideItems(thisEl);
		}
		
		if(settings.timeHide) {
			el.find(">ul").delay("150").bind("mouseleave", function() {
					setTimeout(function(){
						HideItems(thisEl);
					}, settings.timeHide);
			}); 	
		}
		
		if (thisEl.hasClass("inactive")) {
			//Replace class to "active"
			thisEl.removeClass().addClass("active");
			//Show subitems
			thisEl.parent("li").children("ul").effectShow(settings.Speed);
		} else {
				//Replace class to "inactive"
				thisEl.removeClass().addClass("inactive");
				//Show subitems
				thisEl.parent("li").children("ul").effectHide(settings.Speed);
		}
		return false;
	}
	
	//Mouseover or Click
	items.unbind(settings.method).bind(settings.method, _item); 
}
})(jQuery); 

$(document).ready(function (){ 
  //Usage
  $(".menu ul li").horizontalMenu({
  	timeHide: 100
  });  
}); 

