jQuery(document).ready(function() {
	jQuery("#toBottom").hide();	
	jQuery("#toTop").hide();	
	jQuery("#toTopM").hide();		
	jQuery("#open").hide();
	jQuery("#menuTop").hide();		
	my_variable = {};
	my_variable.numberOfLi = 0;
	

	jQuery("#menuTopM").mouseout( function() {
		if(document.getElementById("menuNotification").value != "false"){
			jQuery(this).hover(function() {  
			}, function(){  
			jQuery("#notificationFront").fadeIn(250);
			});}
	}); 
	
	
	jQuery("#menuTopM li").mouseover( function() {
			jQuery("#notificationFront").stop(true, true).fadeOut(250)
			jQuery(this).children("ul").stop(true, true).fadeIn(250);
			jQuery(this).hover(function() {  
			}, function(){  
            jQuery(this).find(".sub-menu").stop(true, true).hide();

        });  
	}); 	

	
	function refreshMenu(){
	
		jQuery("#menuTopM").mouseout( function() {
			if(document.getElementById("menuNotification").value != "false"){
				jQuery(this).hover(function() {  
				}, function(){  
				jQuery("#notificationFront").fadeIn(250);
				});}
		}); 
		
		jQuery("#menuTopM li").mouseover( function() {
				jQuery("#notificationFront").stop(true, true).fadeOut(250);
				jQuery(this).children("ul").stop(true, true).fadeIn(250);
				jQuery(this).hover(function() {  
				}, function(){  
				jQuery(this).find(".sub-menu").stop(true, true).hide();

			});  
		}); 

	
		jQuery("#menuTopM ul li").not("ul li ul li").mouseover( function() {
			my_variable.numberOfLi  = 0;

			jQuery(this).find("ul li").not("ul li ul li ul li").each(function(index) {
				my_variable.numberOfLi++;
			});	
			
		}); 
	
		jQuery("#menuTopM  .menu").mouseover( function() {
			var widthOfSubMeni = jQuery(this).children("ul").width();
			jQuery(this).children("ul").each(function(index) {
				if(jQuery(this).width() > widthOfSubMeni)
					widthOfSubMeni = jQuery(this).width();
			});
			
			jQuery("#menuTopM .sub-menu").css( "width",widthOfSubMeni +"px" );
		}); 
	
		
	jQuery("#menuTopM .sub-menu li").has('ul').mouseover( function() {

		var widthOfSubSubMeni = jQuery(this).find('ul li a').html().length;
		var listItem = jQuery(this).index();
		border = document.getElementById("border").value;
		listItemSum = my_variable.numberOfLi  - listItem -1;

		listItem = listItemSum *29.33-border;
		
		jQuery(this).children("ul").children("li").each(function(index) {
			if(jQuery(this).find('a').html().length > widthOfSubSubMeni)
				widthOfSubSubMeni = jQuery(this).find('a').html().length;
		});		
		if(widthOfSubSubMeni <= 24){
			widthOfSubSubMeniSum  = widthOfSubSubMeni * 8 + 20;
			}
		if(widthOfSubSubMeni > 24){
			widthOfSubSubMeniSum  = widthOfSubSubMeni * 8 + 20;	
			}
		if(widthOfSubSubMeni > 50){
			widthOfSubSubMeniSum  = widthOfSubSubMeni * 7 + 40;	
			}
		var marginLeft = jQuery(this).width();
		jQuery(this).find('ul').css( "margin-left",+marginLeft+"px" );
		jQuery(this).find('ul').css( "width",widthOfSubSubMeniSum+"px" );
		if(document.getElementById("menuPosition").value == "pmc_menuBottom"){
			jQuery(this).find('ul').css( "bottom",+listItem+"px" );
			jQuery(this).find('ul').find('li').last().css( "height",30+listItemSum+"px !important" );
		}
		else {
			jQuery(this).find('ul').css( "bottom","" );
			jQuery(this).find('ul').find('li').last().css( "height","" );		
		
		}
	
	});		
		

	}
		
	
	jQuery(".pmc_menuBottom li").mouseover( function() {
			jQuery("#notificationFront").fadeOut(250);
	}); 		
	

	
	jQuery("#menuTopM ul li").not("ul li ul li").mouseover( function() {
		my_variable.numberOfLi  = 0;

		jQuery(this).find("ul li").not("ul li ul li ul li").each(function(index) {
			my_variable.numberOfLi++;
		});	
		
	}); 
	
	jQuery("#menuTopM  .menu").mouseover( function() {
		var widthOfSubMeni = jQuery(this).children("ul").width();
		jQuery(this).children("ul").each(function(index) {
			if(jQuery(this).width() > widthOfSubMeni)
				widthOfSubMeni = jQuery(this).width();
		});
		
		jQuery("#menuTopM .sub-menu").css( "width",widthOfSubMeni +"px" );
	}); 
	
		
	jQuery("#menuTopM .sub-menu li").has('ul').mouseover( function() {

		var widthOfSubSubMeni = jQuery(this).find('ul li a').html().length;
		var listItem = jQuery(this).index();
		border = document.getElementById("border").value;
		listItemSum = my_variable.numberOfLi  - listItem -1;

		listItem = listItemSum *29.33-border;
		
		jQuery(this).children("ul").children("li").each(function(index) {
			if(jQuery(this).find('a').html().length > widthOfSubSubMeni)
				widthOfSubSubMeni = jQuery(this).find('a').html().length;
		});		
		if(widthOfSubSubMeni <= 24){
			widthOfSubSubMeniSum  = widthOfSubSubMeni * 8 + 20;
			}
		if(widthOfSubSubMeni > 24){
			widthOfSubSubMeniSum  = widthOfSubSubMeni * 8 + 20;	
			}
		if(widthOfSubSubMeni > 50){
			widthOfSubSubMeniSum  = widthOfSubSubMeni * 7 + 40;	
			}
		var marginLeft = jQuery(this).width();
		jQuery(this).find('ul').css( "margin-left",+marginLeft+"px" );
		jQuery(this).find('ul').css( "width",widthOfSubSubMeniSum+"px" );
		if(document.getElementById("menuPosition").value == "pmc_menuBottom"){
			jQuery(this).find('ul').css( "bottom",+listItem+"px" );
			jQuery(this).find('ul').find('li').last().css( "height",30+listItemSum+"px !important" );
		}
	
	}); 		
	
	jQuery(".pmc_menuBottom li").mouseout( function() {
		if(document.getElementById("menuNotification").value != "false"){
			jQuery(this).hover(function() {  
			}, function(){  
			jQuery("#notificationFront").fadeIn(250);
			});}
	});

	jQuery("#close").click( function() {
		jQuery("."+document.getElementById("menuPosition").value).fadeOut(250);
		jQuery("#open").fadeIn(250);
		document.cookie = "menupcCokieClose=false;path=/";
		if(document.getElementById("menuPosition").value == "pmc_menuBottom"){
			document.getElementById("menuShowPosition").value = "bottom"
			document.cookie = "menupcShowCokie=top;path=/";
		}
		document.getElementById("menuShow").value = "false";
		
	}); 	
	
	jQuery("#open").click( function() {
		jQuery("#open").fadeOut(250);			
		document.cookie = "menupcCokieClose=true;path=/";
		document.cookie = "menupcCokie=pmc_menuTop;path=/";
		
		jQuery(".pmc_menuBottom").attr('class','pmc_menuTop');
		jQuery(".pmc_menuBottomM").attr('class','pmc_menuTopM');		
		document.getElementById("menuShow").value = "true";
		jQuery(".pmc_menuTop").fadeIn(250);
		jQuery("#toBottom").fadeIn(250);	
		jQuery(".pmc_notificationBottom").attr('class','pmc_notification');	
		jQuery("#pmc_notificationFront").fadeIn(250);
		if(document.getElementById("menuShowPosition").value == "bottom" && document.getElementById("menuPosition").value == "pmc_menuBottom"){
			var order = [2,1];
			var container = jQuery(".pmc_menuContainer");
			var children = container.children();
			container.empty();
			for (var i = 0; i < order.length; i++){
				container.append(children[order[i]-1])
			}	
			jQuery(".pmc_closeBottom").attr("class","pmc_close");	
			jQuery(".pmc_socialFrontBottom").attr("class","pmc_socialFront");
			jQuery("#toTop").fadeOut(250);	
			document.getElementById("menuShowPosition").value = "top";
			document.cookie = "menupcShowCokie=top";
			
		}
		var container = jQuery(".pmc_menuContainer");
		var children = container.children();
		container.empty();		
		container.append(children)
		document.getElementById("menuPosition").value = "pmc_menuTop";
		refreshMenu()
	});	
	
	jQuery("#toBottom").click( function() {
		jQuery(".pmc_menuTop").attr('class','pmc_menuBottom');
		jQuery(".pmc_menuTopM").attr('class','pmc_menuBottomM');	
		jQuery(".pmc_close").attr("class","pmc_closeBottom");	
		jQuery(".pmc_notification").attr('class','pmc_notificationBottom');	
		jQuery(".pmc_socialFront").attr("class","pmc_socialFrontBottom");
		jQuery("#toBottom").hide();	
		document.cookie = "menupcCokie=pmc_menuBottom;path=/";
		document.cookie = "menupcShowCokie=bottom;path=/";		
		document.getElementById("menuPosition").value = "pmc_menuBottom";
		jQuery("#toTop").show();	
		var order = [2,1];
		var container = jQuery(".pmc_menuContainer");
		var children = container.children();
		container.empty();
		for (var i = 0; i < order.length; i++){
			container.append(children[order[i]-1])
		}
		document.getElementById("menuShowPosition").value = "bottom";
		refreshMenu()
	}); 
	
	jQuery("#toTop").click( function() {
		jQuery(".pmc_menuBottom").attr('class','pmc_menuTop');
		jQuery(".pmc_menuBottomM").attr('class','pmc_menuTopM');	
		jQuery(".pmc_closeBottom").attr("class","pmc_close");		
		jQuery(".pmc_notificationBottom").attr('class','pmc_notification');	
		jQuery(".pmc_socialFrontBottom").attr("class","pmc_socialFront");	
		jQuery("#toTop").hide();	
		document.cookie = "menupcCokie=pmc_menuTop;path=/";	
		document.cookie = "menupcShowCokie=top;path=/";	
		document.getElementById("menuPosition").value = "pmc_menuTop";
		jQuery("#toBottom").show();	
		var order = [2,1];
		var container = jQuery(".pmc_menuContainer");
		var children = container.children();
		container.empty();
		for (var i = 0; i < order.length; i++){
			container.append(children[order[i]-1])
		}	
		document.getElementById("menuShowPosition").value = "top";
		refreshMenu()
	}); 		


					
					
});



