/**
 * @author Martin 
 */

// "cm" is the prefix of all public methods in this class

(function($){
	//Config vars
	var commonPages			=	"commonPages";
	var	easeIn					=	300;	
	var	easeOut					=	250;	
	
	//Interacts only with "li" elements in the
	//already opened menu - public fn
	$.cmCommonPagesMenu = function(){
		$("li.mainCat").mouseenter(function(){
			if($(this).find("ul").length >= 1){
				if(typeof document.body.style.maxHeight === "undefined") {
						$(this).find("ul").show();
		        $(this).attr("style","margin-bottom: -2px;");
						$(this).addClass("hovered");
				}else{
					$(this).find("ul").show();
					$(this).addClass("hovered");
				}
			}
		});
		$("li.mainCat").mouseleave(function(){
			if ($(this).find("ul").length >= 1) {
		  	if (typeof document.body.style.maxHeight === "undefined") {
		  		$(this).find("ul").hide();
		  		$(this).removeAttr("style");
					$(this).removeClass("hovered");
		  	}
		  	else {
					$(this).removeClass("hovered");
		  		$(this).find("ul").hide();
		  	}
		  }
		});
	};
	
	//Opening the menu by clicking the "Catalog" btn
	$.fn.cmCatalogClosedMenu = function(){
		$(this).click(function(){
			$("div#categoriesPopup").slideDown(easeIn);
			$.cmCommonPagesMenu();
		});
		$("div#categoriesPopup").mouseleave(function(){
			$(this).slideUp(easeOut);
		});
	};
	
	//Init - public fn
	$.CategoriesMenuInit = function(){
		var bodyID	=	$("body").attr("id");
		if( bodyID == commonPages){
			$.cmCommonPagesMenu();
		}else{
			$("a#catalogCaller").cmCatalogClosedMenu();
		}
	};
	
})(jQuery);
