// JavaScript Document
function initMenu() 
{
	
	var type = window.location.hash;
	var url = window.location.pathname;
	
	
	
	$('#LeftMenu ul').hide();
	/**someone has select the menu**/
	if (type!=null && type!="" && type!="#")
	{
		$("[href="+url+type+"]").parent().parent().show();
	    $("[href="+url+type+"]").css
		({
				backgroundImage:"url(assets/images/sidemenu_selected_subrow.png)",
				width:"255px",
				height:"38px",	
				position:"relative"
		});
		
		ie6hack_leftmenu_returnNegMargin();
		//$("[href="+type+"]").parent().parent().css({backgroundImage:"url(assets/images/sidemenu_row_selected.png)"});
		
	}else if (type =="")
	{
		SearchForOpenTab(url);
		//$('#LeftMenu ul:first').show();
		//$('#LeftMenu li:first > a').css({backgroundImage:"url(assets/images/sidemenu_row_selected.png)"});
		
	}
	
  
	/**once the catogery is clicked**/
   $('#LeftMenu > li > a').click(
		function() 
		{
			/**reset the menu**/
			//$('#LeftMenu li ul li a').css
			//({
			//		backgroundImage:"url(assets/images/sidemenu_subrow.png)",
			//		width:"249px",
			//		height:"30px"
			//});
			
			
			/**reset the menu**/
			$('#LeftMenu > li > a').css({backgroundImage:"url(assets/images/sidemenu_row.png)"});
			$(this).css({backgroundImage:"url(assets/images/sidemenu_row_selected.png)"});
			
		   var checkElement = $(this).next();
		   //alert($(this).next().html());
		   //ie6hack_leftmenu_returnZeroMargin();
		   /**orginial item is clicked**/
		   if((checkElement.is('ul')) && (checkElement.is(':visible'))) 
		   {
			   	
			   //ie6hack_leftmenu_returnZeroMargin();
			   $(this).parent('ul:visible').slideUp('normal');
			   //$(this).css({backgroundImage:"url(assets/images/sidemenu_row.png)"});
			   
				return false;
		   }
		   /**new item is clicked**/
		  if((checkElement.is('ul')) && (!checkElement.is(':visible'))) 
		  {
			ie6hack_leftmenu_returnZeroMargin();
			/**other rather than that one slideup**/
			$('#LeftMenu ul:visible').slideUp('normal');
			
			checkElement.slideDown('normal');
			return false;
		  }
		  
		  
		}
	);
	/**once the sub item is clicked**/
	$('#LeftMenu > li > ul > li > a').click
	(
		function()
		{
			
			/**reset menu**/
			$('#LeftMenu > li > ul > li > a').css({
					backgroundImage:"url(assets/images/sidemenu_subrow.png)",
					width:"249px",
					height:"26px"
			});
			/**change the selected subrow**/
			$(this).css
			({
					backgroundImage:"url(assets/images/sidemenu_selected_subrow.png)",
					width:"255px",
					height:"34px",	
					position:"relative",
					color:"#7f889e",
					fontWeight:"bold"
			}); 
			
		   var checkElement = $(this).next();
		   //alert($(this).next().html());
		   //ie6hack_leftmenu_returnZeroMargin();
		   /**orginial item is clicked**/
		   if((checkElement.is('ul')) && (checkElement.is(':visible') )) 
		   {
			   //ie6hack_leftmenu_returnZeroMargin();
			   $(this).parent('ul:visible').slideUp('normal');
			   //$(this).css({backgroundImage:"url(assets/images/sidemenu_row.png)"});
				return false;
		   }
		   /**new item is clicked**/
		  if((checkElement.is('ul')) && (!checkElement.is(':visible'))) 
		  {
			ie6hack_leftmenu_returnNegMargin();
			$('#LeftMenu > li > ul > li > ul :visible').slideUp('normal');
			$('#LeftMenu > li > ul > li > ul > li > a').show();
			$('#LeftMenu > li > ul > li > ul > li ').show();
			checkElement.slideDown('normal');
			return false;
		  }
		  
		 	$(this).css
			({
					backgroundImage:"url(assets/images/sidemenu_selected_subrow.png)",
					width:"255px",
					height:"34px",	
					position:"relative"
			}); 
			
		}
								  
	);		
	/**once the sub sub item is clicked**/
	$('#LeftMenu > li > ul >  li > ul > li a').click
	(
		function()
		{
			/**reset the menu**/
			$('#LeftMenu li ul li ul li a').css
			({
					//backgroundImage:"url(assets/images/sidemenu_subrow.png)",
					width:"234px",
					height:"26px"
			});
			
			//$(this).css
			//({
					//backgroundImage:"url(assets/images/sidemenu_selected_subrow.png)",
					//width:"255px",
					//height:"38px",	
					//position:"relative"
			//});
			
			
		}
								  
	);	
	
}

function SearchForOpenTab(url)
{
	var totalitem = 0
	
	//for each big category
	$('#LeftMenu > li > a').each(
		function()
		{
			//take  the big category link
			var cat = $(this).attr("href");
			//increase one by the category
			totalitem+=1;
			//if the current link is equal to the big category but didn't select the item, expand the menu
			if (url == cat)
			{
				$(this).next().show();
				$(this).css({backgroundImage:"url(assets/images/sidemenu_row_selected.png)"});
			}//if the current link is equal to the big category and hv selected the item, expand the menu and select the sub cat
			else if (url.match(cat))
			{
				$(this).next().show();
				$(this).css({backgroundImage:"url(assets/images/sidemenu_row_selected.png)"});
				$(this).next().find('li > a').each(
					function()
					{
						//taking href from the sub grene
						var subcat = $(this).attr("href");
						//var regex = new RegExp("((?:\\/[\\w\\.\\-]+)+)","");
						//todo : better regex
						var regex = /#\d*/gi ;
						
						subcat = subcat.replace(regex,"");
						//compare the url with the sub grene , if match , then change the css to selected
						if (url.match(subcat))
						{
							$(this).css
							({
									backgroundImage:"url(assets/images/sidemenu_selected_subrow.png)",
									width:"255px",
									height:"34px",	
									position:"relative",
									color:"#7f889e",
									fontWeight:"bold"
							}); ;
							ie6hack_leftmenu_returnNegMargin();
						}
				});
			}
		}
	);
	
	//if there is only one item, expand the menu
	if (totalitem ==1)
	{
		$('#LeftMenu ul:first').show();
		$('#LeftMenu li:first > a').css({backgroundImage:"url(assets/images/sidemenu_row_selected.png)"});
	}
}


/**ie6 hack**/
function ie6hack_leftmenu_returnZeroMargin()
{
	if (jQuery.browser.msie)
	{
	  if(parseInt(jQuery.browser.version) == 6) {
		  $("#RightContainer").css({ "margin-left":"0px"  });

	  }
	} 	
}

function ie6hack_leftmenu_returnNegMargin()
{
	if (jQuery.browser.msie)
	{
	  if(parseInt(jQuery.browser.version) == 6) {
		  $("#RightContainer").css({ "margin-left":"-7px"  });

	  }
	} 	
}

function ie6hack_leftmenu_returndoubleNegMargin()
{
	if (jQuery.browser.msie)
	{
	  if(parseInt(jQuery.browser.version) == 6) {
		  $("#RightContainer").css({ "margin-left":"-15px"  });

	  }
	} 	
}
