$j(document).ready(function(){
	
	collapseAll();
	/**	TODO - Load the correct MenuItem by the cookie url saved
		alert($j.cookie('category_url')); 
	*/
	
	var baseUrl = '/mpginstruments.com/index.php/products/';
	var dir;
	
	/**		Events on Category ITEM		*/
	$j(".LeftMenuPages").hover(function(){
			$j(this).children("a").attr("id", "active_page");
		},function(){	
			$j(this).children("a").attr("id", "");
	});
	
	$j(".LeftMenuItems").hover(function(){
			//$j(this).children("a").attr("id", "active_item");
			$j(this).children("a").addClass("current_item");
		},function(){	
			//$j(this).children("a").attr("id", "");
			$j(this).children("a").removeClass("current_item");
	});
	
	$j(".LeftMenuProducts").click(function(){   				
		$j(this).children("div.LeftMenuPages").each(function(i){   			
   			$j(this).toggle(200);
 		});
		return false;
	}); 	
	
	$j(".LeftMenuProducts").hover(function(){   				
			$j(this).children("a").css("background-color", "#6F6F6F");
		},function(){	
			$j(this).children("a").css("background-color", "#AFAFAF"); 		
	}); 			
	
	
	/** Scelta link CATEGORIA */	
	$j(".LeftMenuPages").click(function(event){   				
		$j(".LeftMenuPages").each(function(){
			$j(this).children("a").attr("id", "");
		});
		$j(this).children("a").attr("id", "active_page");	
		
		var showOrHide = 'show';
		$j(this).children("div.LeftMenuItems:visible").each(function(i){
			showOrHide = 'hide';					
		});
		
		$j(this).children("div.LeftMenuItems").each(function(i){
			eval('$j(this).'+showOrHide+'(200)');			
		});
						
		url = $j(this).children("a").attr("href");
		urlString = url;
		
		/**	Save Current Position */
		/*$j.cookie('category_url', null);
		*/
		$j.cookie('category_url', urlString, { expires: 0, path: '/index.php/products/' }); // set cookie
		/*alert("Saved Category url: "+$j.cookie('category_url'));
		*/
		url = url.split('/');
						
		param1 = url[url.length-2];
		param2 = url[url.length-1];

		/** Load the content ONLY if the url is a new url AND the tree is showing	*/
		/*if($j.cookie('category_url')!=urlString && showOrHide == 'show')
		{
			$j.ajax({
				type: "POST",
				url: baseUrl+param1+'/'+param2,
				dataType: "html",
				beforeSend: function(){
     				/** Handle the beforeSend event */
     			/*	showLoading();
   				},
   				success: function(msg){				
					$j("#catalog_content").empty();
					$j("#catalog_content").append(msg);					
										
					$j("#g_script").empty();
					
					/** Inserito nel default layout */
					/** $j("#g_script").append('<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>');*/
				/*	$j("#g_script").append('<script type="text/javascript">_uacct = "UA-1308755-3";urchinTracker();</script>');
					$j.cookie('category_url', urlString, { expires: 0 }); // set cookie
				}
			})									
    	}
		*/
		event.stopPropagation();
		return false;		
		
	});
	
	
	/** Scelta link PRODOTTO */	
	$j(".LeftMenuItems a").click(function(event){			
		
		$j(".LeftMenuItems").each(function(){
			$j(this).children("a").attr("id", "");
		});
				
		$j(this).attr("id", "active_item");		
		
		url = $j(this).attr("href");
		urlString = url;
		//$j.cookie('product_url', '', { expires: -1 }); // delete cookie
		//$j.cookie('product_url', null);
		$j.cookie('product_url', urlString, { expires: 0, path: '/index.php/products/'}); // set cookie		
		url = url.split('/');
						
		param1 = url[url.length-2];
		param2 = url[url.length-1];
		
		//Load the content ONLY if the url is a new url AND the tree is showing					
		/*
		if($j.cookie('product_url')!=urlString)
		{
			$j.ajax({
				type: "POST",
				url: baseUrl+param1+'/'+param2,
				dataType: "html",
				beforeSend: function(){
     				// Handle the beforeSend event
     				showLoading();
   				},
   				success: function(msg){				
					$j("#catalog_content").empty();
					$j("#g_script").empty();					
					//Inserito nel default layout					
					//$j("#g_script").append('<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>');
					$j("#g_script").append('<script type="text/javascript">_uacct = "UA-1308755-3";urchinTracker();</script>');
				
					
					$j("#catalog_content").append(msg);															
					$j.cookie('product_url', urlString, { expires: 0 }); // set cookie
				}
			})									
    	}
    	*/
    	event.stopPropagation();
		return true;
		//return false;
	});
	

	selectChildren();
});

function showLoading()
{
	$j("#catalog_content").empty();	
	$j("#catalog_content").append('<div id="ajax_load"><img src="/mpginstruments.com/app/webroot/img/icon/wait.gif"> Caricamento in corso...</div>');
}

function selectChildren()
{
	
	url_cat = $j.cookie('category_url');
	
	$j(".LeftMenuProducts").each(function(i){
		finded = false;
		$j(this).children("div.LeftMenuPages").each(function(i){
			
	   		if(url_cat==$j(this).children("a").attr("href"))
			{	
				finded = true;						
			} 		
		});
		if(finded)
		{	
			$j(this).children("div.LeftMenuPages").each(function(i){
				$j(this).show(200);			
			});			
		}	
	});	
	
	
	url_prod = $j.cookie('product_url');	
	
	$j(".LeftMenuPages").each(function(i){
		finded = false;
		
		$j(this).children("div.LeftMenuItems").each(function(i){
	   		
	   		if(url_prod==$j(this).children("a").attr("href"))
			{						
				finded = true;				
				$j(this).children("a").attr("id", "active_item");					
			}
		}); 
		if(finded)
		{	
			$j(this).children("div.LeftMenuItems").each(function(i){
				$j(this).show(200);			
			});			
		}			
	});	
		
}

function collapseAll()
{
	$j(".LeftMenuPages").each(function(){			
			$j(this).children("div.LeftMenuItems").hide();
	});
	$j(".LeftMenuProducts").each(function(){
		$j(this).children("div.LeftMenuPages").each(function(i){
   			$j(this).hide();
 		});	
	});
}

function expandAll()
{
	$j(".LeftMenuPages").each(function(){			
			$j(this).children("div.LeftMenuItems").show();
	});
	$j(".LeftMenuProducts").each(function(){
		$j(this).children("div.LeftMenuPages").each(function(i){
   			$j(this).show();
 		});	
	});
}
