// JavaScript Document
var caminho_http = 'http://servidor/aluminarte/[01]%20setembro%202011/site/';
//var caminho_http = 'http://www.dataprisma.com.br/projetos/aluminarte/site/';
//var caminho_http = 'http://aluminarte.iphotel.info/';
var caminho_http = 'http://www.aluminarte.com.br/';
$(document).ready(function(){
	//seleciona o menu ativo
	$("#"+page).addClass("selected");
	$("#"+page).find('a').css("color","#FFFFFF"); 
	$("#"+page).css("color","#FFFFFF"); 
	$("#"+page).css("background","#000000"); 
	$("#"+page).css("padding-bottom","7px"); 
	$("#"+page).next().css("padding-bottom","7px"); 
	//inicializa o shadowbox
	Shadowbox.init({
		language: 'en',
		handleOversize: "drag",
		players:  ['img', 'html', 'iframe', 'swf']
		//troubleElements: new Array("embed", "canvas")
	});
	//inicializa o cufon
	Cufon.replace('.tituloEntrada', { hover: true} );
	Cufon.replace('.itemMenu', { hover: true } );
	Cufon.replace('.titPag', { hover: true } );
	Cufon.replace('.titProdEnt', { hover: true } );
	
	//efeito hover do menu principal
	$(".itemMenu").hover(
		function() {					
			$(this).next().css("padding-bottom","7px");
			$(this).css("padding-bottom","7px");
			$(this).find('a').css("color","#FFFFFF");	
			Cufon.refresh();
		}, 
		function() {			
			$(this).next().css("padding-bottom","0"); 
			$(this).css("padding-bottom","0");
			$(this).find('a').css("color","#000000");	
			$("#"+page).css("padding-bottom","7px"); 
			$("#"+page).next().css("padding-bottom","7px"); 
			$("#"+page).find('a').css("color","#FFFFFF");			
			Cufon.refresh();
		}
	);
	//categoria lateral ativa
	$(".itemSelecionado").find('a').css("color","#FFFFFF");	
	$(".itemSelecionado").css("color","#FFFFFF");
	$(".itemSelecionado").css("padding","10px 5px");
	if ($(".itemSelecionado1").length > 0)
		$(".itemSelecionado").css("margin-bottom","2px");
	$(".itemSelecionado").parent().css("padding","0px");
	setTimeout(abreMenu,2000);
	function abreMenu() {
		$(".itemSelecionado").parent().find(".subCat").slideDown("slow");
	}
	//submenu lateral ativo
	$(".itemSelecionado1").find('a').css("color","#FFFFFF");	
	//menu lateral
	/*$(".itemCat").click(function(event) {
		if ($(this).parent().find(".subCat").length > 0) {
			$(".subCat").slideUp("slow");
			//desativa			
			$(".itemCat").each(function(){				
				if ($(this).hasClass("itemSelecionado")){					
				} else {
					$(this).css("color","#000000");
					$(this).css("background","none");					
					$(this).parent().css("padding","10px 5px");
					$(this).css("padding","0");					
					
				}
			});							
			//abre o menu
			if ($(this).parent().find(".subCat").is(":hidden")){
				$(this).parent().find(".subCat").slideDown("slow");				
				$(this).parent().find(".subCat").css("zoom",'1');
				if ($(this).hasClass("itemSelecionado")){					
				} else {
					//ativa				
					$(this).css("color","#FFFFFF");
					$(this).css("background","#444243");					
					$(this).css("padding","10px 5px");
					$(this).parent().css("padding","0");
				}
			} else {
				if ($(this).parent().find(".subCat").length > 0) {			
					$(this).parent().find(".subCat").slideUp("slow");
					if ($(this).hasClass("itemSelecionado")){
					} else {
						//desativa				
						$(this).css("color","#000000");
						$(this).css("background","none");					
						$(this).parent().css("padding","10px 5px");
						$(this).css("padding","0");
					}
				}
								
			}			
		} else {
			var ir = $(this).attr('link');
			document.location= ir;
		}
		
	});	
	*/
	$("a[rel='_blank']").attr("target","_blank").removeAttr("rel");
	$("input[type=hidden]").empty();
	
	if(!$.browser.msie){		
		$(".listagem ul li").live("mouseover",function(){ $(this).css("background-color","#ededed;"); }).live("mouseout",function(){ $(this).css("background-color",""); });
		$(".conteudoCarrinho").corner("5px");
	}
	
	//$("img[alt]").each(function(){ $(this).attr("title",$(this).attr("alt")); });
	
	$(".barraMenu div[id]").click(function(){	  
		if ($(this).attr("id") == "logout"){			
	
			var vazio = $(".conteudoCarrinho").html();
			vazio = vazio.indexOf("0 Itens");
			
			if (vazio == -1){
				if (confirm('Ao sair, seu carrinho não será salvo, deseja continuar?')){
					location.href = caminho_http + $(this).attr("id")+".php";
				}
			} else {
				location.href = caminho_http + $(this).attr("id")+".php";
			}
		}
	});	
	
	$(".listagem li").click(function(){
									 
		var hidden = $(this).parent().parent().prev("input");
		var id = getId($(this));
		updateHidden(id,hidden);
		
		$(this).toggleClass("selected");
		
		filtrarProdutos();
	});
	$(".limparSelecao").click(function(){
		$(this).parent().next("input[type=hidden]").val('');
		var prox = $(this).parent().next().next().children();
		
		if (prox.is("ul")){
			prox.children().each(function(){
				if ($(this).hasClass("selected")){
					$(this).removeClass("selected");
				}
			});
		} else {
			prox.each(function(){
				if ($(this).attr("selected") == "selected"){
					$(this).removeAttr("selected");
				}
			});
		}
		filtrarProdutos();
	});
	
	$(".conteudoCarrinho").next().andSelf().click(function(){
		document.location = caminho_http + 'carrinho/';
	});
	$(".btFinalizar").click(function(){
		
		var carrinho = $(".conteudoCarrinho").html();
		var total = $("#valorTotal").html();
		total = parseInt(total.replace("R$ ",""));
		
		if (carrinho.indexOf("0 Itens") === 0){
			alert("O carrinho está vazio!");
			return false;
		} else if (total < 800) {
			if (confirm("Pedido minimo R$ 800,00 Finalizar mesmo assim?")) {
				document.location = caminho_http +  'finalizar.php';
			} else {
				return false;
			}
		} else {
			
		}
	});
	
	$(".itemCarrinho img.remover").bind("click",function(){
		var id = $(this).parent().attr("id");
		id = id.replace(/[^0-9]*/,"");
		
		if (confirm("Deseja realmente remover este produto do carrinho?")){
			$.ajax({ url: caminho_http + "ajax.php?acao=removerItem&item="+id, success: function(response){
				if (response == "ok"){
					window.location.reload();
				} else {
					alert(response);
				}
			}});
		}
	});	
	
});
function filtrarProdutos(){
	var url = caminho_http + "ajax.php?busca=filtro";
	var categorias = $("#listaCategorias").val();	
	var data = "";
	
	if (categorias != ""){
		data += "categorias="+categorias;
	}	
	if (data.indexOf("&") === 0){
		data.substring(1);
	}
	
	if (data == ""){
		window.location.reload();
	} else {
		$.ajax({ url: url, data:data, type:'POST', success: function(response){ 
			var div = $("div.slider");
			
			if ($(".conteudo").length > 0) {
				$(".conteudo").remove();
			}
			if ($(".conteudo2").length > 0) {
				$(".conteudo2").remove();
			}			
			if ($(".msgBusca").length > 0) {
				$(".msgBusca").remove();
			}
			if ($("div.slider").length > 0){
			 	$("div.slider").remove();
				$("div.conteudoHome").remove();
				$("div.conteudoHome2").remove();
			} else if ($(".amostraProduto").length > 0) {
				$(".amostraProduto").remove();
				$(".amostraProduto2").remove();
			} 
			
			$(".center").append(response);
			
			var rows = Math.ceil($(".amostraProduto").length/3);
			
			if (rows > 2){
				var margin = 290 * (rows - 2);
				$(".rodapeMenuLateral").css("margin-bottom",margin+"px");
			} else {
				$(".rodapeMenuLateral").css("margin-bottom","0px");
			}
			
			
		}});
	}
}
/*
function verificaBusca(){
	var conteudo = remover_espacos($("#busca").val());
	
	if (conteudo.length == 0 || $("#busca").val() == "Informe sua busca"){
		alert("Informe um termo para a busca!");
		return false;
	}
	return true;
}
*/
