//menu_set.js

/*
This function is called on $(document).ready Event
The function sets the menu items widths accordint to their text, in onlu two possible widths
Seperation between IE and other browsers (since the diference in defintion for width - 100% / 'auto').
*/


$(document).ready(function(){

//height of the div container of the menu - for IE - checks if height changes-means the li is not wide enough
var menu_height = document.getElementById('menu_container').offsetHeight;
//width of regular menu items
//var item_width=95;

//if changeing the width, change also the ul width in nanochip/modules/mod_superfishmenu/tmpl/css/superfish.css
var item_width=111;

//width of wide menu items - in case the first wide can't contain the item text
var wide_item_width=123;
//checks if it's wide li - for increasing the wide of all its sub menus
var increase_width=false;


//vars for shadows to menu's first level item
//the margin between the menu items element of the first level
var items_margin=4;
//the div where need to put shaddow for the first level of the menu
var shadow_el=document.getElementById('shadow_container');
//path to shadow image
var shadow_img="assets/menu/shadow.png";


//initialize margin counter for shadow image
var margin_left=0;


//for IE Browsers
var version=navigator.appVersion;
var pos=version.indexOf("MSIE");

if (pos!=-1){
	
	$("#menu_container").find("li").each(function(i) {
	
		if ($(this).css("width")=='auto'){
			increase_width=false;
			
			$(this).css("width",item_width+"px");
			
			if (menu_height!==document.getElementById('menu_container').offsetHeight){
				
				$(this).css("width",wide_item_width+"px");
				$(this).find("ul").css("width",wide_item_width+"px");
				
				increase_width=true;
				
				//add shaddow to menu first level items
				shadow_el.innerHTML += "<img src='"+shadow_img+"' alt='' style=\"position:absolute;margin-top:-1px;left:"+(margin_left+3)+"px;width:"+(wide_item_width+3)+"px;height:29px;\" />";
				margin_left += wide_item_width+items_margin;				

			}else{

				//add shaddow to menu first level items
				shadow_el.innerHTML += "<img src='"+shadow_img+"' alt='' style=\"position:absolute;margin-top:-1px;left:"+(margin_left+3)+"px;width:"+(item_width+3)+"px;height:29px;\" />";
				margin_left += item_width+items_margin;	
			}			
		
		}else{
			if (increase_width){
				$(this).css("width",wide_item_width+"px");
			}
				
		}
		
		
	});
	
}else{
	//all other browsers
	$("#menu_container").find("li").each(function(i) {

		if (parseInt($(this).css("width"))!=item_width){

			//first level menu items
			increase_width=false;
			var w=$(this).css("width");
			var el=$(this);
			//alert(w);
			w=parseInt(w);
			if (w<item_width){
				$(this).css("width",item_width+"px");
				
				//add shaddow to menu first level items
				shadow_el.innerHTML += "<img src='"+shadow_img+"' alt='' style=\"position:absolute;top:0px;left:"+(margin_left+3)+"px;width:"+(item_width+3)+"px;height:29px;\" />";
				margin_left += item_width+items_margin;	
				
				
			}else{
				$(this).css("width",wide_item_width+"px");
				increase_width=true;
				
				//change the ul padding - in order to make the shaddow wider - as wide as the wide_item_width
				$(this).find("ul").css("padding","0 "+(8+wide_item_width-item_width)+"px 9px 0");	
	
				
				//add shaddow to menu first level items
				shadow_el.innerHTML += "<img src='"+shadow_img+"' alt='' style=\"position:absolute;top:0px;left:"+(margin_left+3)+"px;width:"+(wide_item_width+3)+"px;height:29px;\" />";
				margin_left += wide_item_width+items_margin;	
				
	
			}
			
		}else{
				//if the menu item had been set to wider then increase the width of all its sub items
			if (increase_width){
				$(this).css("width",wide_item_width+"px");
			}
		}

	});

}


//menu's first level items - shadow fix, chrome doesn't need
if (!detect_chrome()){
	$('#shadow_container').css("top","2px");
}

//after rendering the changes in the menu show it
document.getElementById('menu_container').style.visibility = "visible";

});

/*
//shadow tests:
//$(this).css("background","url('assets/menu/shadow2.png') no-repeat bottom right");
//$(this).css("background-position","-5px 0px");
//$(this).css("padding","0 8px 9px 0px");
//$(this).css("padding","0 6px 7px 0px");
//$(this).css("background-color","#7b939f");
*/
