// JavaScript Document
var activePage="-";
var activeSubpage="-";
(function($) 
{
    $(function()
	{
		try
		{
			if(jQuery.browser["msie"]!=true)
			{
				Cufon.replace('a', {forceHitArea: true,hover: true}); // Works without a selector engine
				Cufon.replace('p'); // Requires a selector engine for IE 6-7, see above
			}
		}catch (e) {}

		//swfaddress
		//$.address.state("/");
		$('#menu li a, .navigation').address();

		$.address.change(function(event) 
		{  
			
			
			var content;
			if (event.value=="" || event.value=="/" )
				content="/homepage";
			else 
				content=event.value;
			
			temp=content.split("\/");
			
			
			if (activePage!=temp[1])//menus principais
			{
				activePage="-";
				if (temp.length>2)//submenus
				{
					activeSubpage=temp[2];
				}else activeSubpage="-";
				
				
				$("#menu li a.active").removeClass("active");
				$("#menu li a[href='\/"+temp[1]+"\/']").addClass("active");
				$("#menu li a[href='http\:\/\/www.delicat.com.pt\/"+temp[1]+"\/']").addClass("active");
			
				loadContent("/datasource/"+temp[1] + '.inc.php');
				activePage=temp[1];
				
				setTimeout(
				function()
				{
					try{
						if(jQuery.browser["msie"]!=true)
						{
							Cufon.replace('#menu a');
						}
					}catch(e){}
				}, 500);
				
				 
			}else if (temp.length>2)//submenus
			{
				activeSubpage=temp[2];
				activateSubMenu();
				
			}
			
			
			
			
		});  

	});
})(jQuery);

function preloader(showPreloader)
{
	
	$("#preloader").css( "display","block");
	if (showPreloader)
		$("#preloader").css( "visibility","visible");
		
	$("#preloader").animate(
		{
			height: showPreloader?250:0
		}, 
		200, 
		function() 
		{
			$("#preloader").css( "visibility",showPreloader?"visible":"hidden");
		}
	);

}
var loader;
function loadContent(url)
{
	preloader(true);
	  
	$('#contentData').slideUp(400,
		function()
			{
				if (loader)
					loader.abort();//kill last request
			
				loader = $.ajax({
					type: "GET",
					url: url,
					success: function(_html)
					{
						$('#contentData').html(_html);
						$('#contentData .submenu a').address( function() {  
											//alert($(this).attr('href'));
											//return "LLL";
											 return $(this).attr('href').replace("http\:\/\/www.delicat.com.pt", '');  
										 });
						
						$("#prev").click(function()
										   {
												prev();
												return false;
										   });
						$("#next").click(function()
										   {
												next();
												return false;
										   });
						try
						{
							if(jQuery.browser["msie"]!=true)
							{
								Cufon.replace('#contentData p');
								Cufon.replace('.producao');
								Cufon.replace('#contentData a', {forceHitArea: true,hover: true});
							}
						}catch (e) {}
						preloader(false);
						$('#contentData').slideDown(400).show();
						
						//if (activeSubpage!="-")
							activateSubMenu();
			
					},
					error:function(msg)
					{
					 $('#contentData').html(msg);
					  preloader(false);
					}
				});
				
			}
		)
}

function activateSubMenu()
{
	processSubmenu();
	
	
	
	setTimeout(
	function()
	{
		try{
			if(jQuery.browser["msie"]!=true)
			{
				Cufon.refresh();//replace('#contentData .submenu a');
			}
		}catch(e){}
	}, 500);
	//alert(activePage+"\/"+activeSubpage);
}

//contents 
function next()
{
	nextImg=$('#i'+activeSubpage+" img:visible:first").fadeOut(500).next();
	if (nextImg.length > 0)
		nextImg.fadeIn(500);
	else 
	{
		$('#i'+activeSubpage+" img:first").fadeIn(500);
	}
}
function prev()
{
	
	prevImg=$('#i'+activeSubpage+" img:visible:first").fadeOut(500).prev();
	if (prevImg.length > 0)
		prevImg.fadeIn(500);
	else
	{
		$('#i'+activeSubpage+" img:last").fadeIn(500);
	}
}
function processSubmenu()
{
	//se a página nao for do tipo galeria de imagens não processa esta função
	if (activePage!="cozinhas" && activePage!="roupeiros" && activePage!="casas-de-banho")
		return;
		
	//caso não seja especificado 
	if (activeSubpage=="-")
	{
		if (activePage=="cozinhas")
			activeSubpage="star";
		else if (activePage=="roupeiros")
			activeSubpage="asia";
		else if (activePage=="casas-de-banho")
			activeSubpage="abacate";
	}
	
	$('#i'+activeSubpage+' a').each(function(index) {
		$(this).replaceWith("<img src='"+$(this).attr("href")+"'  width='850' height='420'  />");
	  });
	
	$(".submenu a.active").removeClass("active");
	$(".submenu a[href='\/"+activePage+"\/"+activeSubpage+"\/']").addClass("active");
	
	if($('#imagens img:visible').length > 1)
		$('#imagens img:visible').fadeOut(0);
		
	$('#imagens li:not(#i'+activeSubpage+')').fadeOut(500);
	$('#acessorios li:not(#a'+activeSubpage+')').fadeOut(500);
	
	$('#i'+activeSubpage).fadeIn(200); 
	$('#a'+activeSubpage).fadeIn(500);  

	//se tiver mais de uma imagem mostra o controlador
	if($('#i'+activeSubpage+" img").length > 1)
	{
		$('#controller').fadeIn(500);
	}else $('#controller').fadeOut(500);
	
	
	//show first image
	$('#imagens img:not(#i'+activeSubpage+' img:first)').fadeOut(500);
	$('#i'+activeSubpage+" img:first").fadeIn(500);
	
	try
	{
		posY=$('.submenu a.active').position().top-5;
		$('#controller').animate({top: posY<0?0:posY}, 300);
	}catch(e){}
	

}

