
/////////////
// No Label //
//////////////
noLabel = {
	init: function(){
		$(".noLabel").each(function(){
			$(this).addClass("vazio");
			noLabel.buffer[$(this).attr("id")] = $(this).val();
			$(this).unbind('focus',noLabel.foco).unbind('blur',noLabel.desfoco).bind('focus',noLabel.foco).bind('blur',noLabel.desfoco);
		}).parents("form:eq(0)").unbind('submit',noLabel.formulario).bind('submit',noLabel.formulario);
	},
	
	buffer: {},
	
	foco: function(obj){
		obj = typeof($(obj).attr("id")) == "undefined" ? this : obj;
		if($(obj).hasClass("vazio")){
			if($(obj).is("textarea")){
				$(obj).html("");
			}else{
				$(obj).val("");
			}
			$(obj).removeClass("vazio");
		}
	},
	
	desfoco: function(){
		if($.trim($(this).val()) == ""){
			if($(this).is("textarea")){
				$(this).html(noLabel.buffer[$(this).attr("id")]);
			}else{
				$(this).val(noLabel.buffer[$(this).attr("id")]);
			}
			$(this).addClass("vazio");
		}
	},
	
	formulario: function(){
		$(".vazio",this).each(function(){
			noLabel.foco(this);
		});
		return true;
	}
}


////////////
// Select //
////////////
estiloSelect = {
	init: function(){
		var primeiro = 0;
		$("select.select").each(function(){
			var html =  '<div id="' + $(this).attr("id") + '" class="' + $(this).attr("class") + '">';
				html += '	<div class="ativo"></div>';
				html += '	<input id="' + $(this).attr("id") + '_select" name="' + $(this).attr("name") + '" type="hidden" value="" />';
				html += '	<div class="options">';
				html += '	<ul>';
				for(i=0;i<$("option",this).length;i++){
					html += '<li>	<a href="javascript:void(0);" title="' + $("option:eq(" + i + ")",this).text() + '" rel="' + $("option:eq(" + i + ")",this).attr("value")+ '">' + $("option:eq(" + i + ")",this).text() + '</a></li>';
				}
				html += '</ul>';
				html += '</div>';
				html += '</div>';
			
			$("option",this).each(function(i){
				primeiro = this.selected ? i : primeiro;
			});
			
			$(this).replaceWith(html);
			
		});
		//ESCONDE A PRIMEIRA LI QUE REPETIA E EXIBIA O TEXTO CLICADO
		$('.options > ul').each(function(){
			$(this).children('li:first').hide();
		});
		
		$("div.select").css("visibility","visible").each(function(){
			var altura = $("a",this).length -1;
			altura = altura > 5 ? 5 : altura;
			alturaOpt = isNaN($("a:first",this).css("height")) ? 18 : $("a:first",this).css("height");
			altura = alturaOpt * altura
			$(".options",this).css({height:altura + "px", visibility:"visible", display:"none"});
			estiloSelect.change(this,primeiro);
		})
		$("div.select").each(function(){
			$("a", this).each(function(i){
				$(this).unbind().click(function(){
					estiloSelect.change($(this).parents("div.select:eq(0)"),i);
				});
			});
		});
		$("div.select .ativo").click(function(){estiloSelect.abre(this);});
	},
	abre: function(obj){
		$(obj).parent().parent().parent().css("z-index","3");
		$(obj).unbind('click').siblings(".options").slideDown('fast',function(){
			$("html").unbind('click').bind('click',estiloSelect.fecha);
			$("div.select .ativo").unbind('click');
		});
	},
	change: function(obj,option){
		$("a.optionAtivo",obj).removeClass("optionAtivo");
		$("a:eq(" + option + ")",obj).addClass("optionAtivo");
		var texto = $("a:eq(" + option + ")",obj).text();
		var valor = $("a:eq(" + option + ")",obj).attr("rel");
		$(".ativo",obj).html(texto);
		$("input[name!='url']",obj).val(valor);
		estiloSelect.fecha();
	},
	
	fecha: function(){
		$("div.select .options:visible").slideUp('fast',function(){$(this).parent().parent().parent().css("z-index","2")});
		$("html").unbind('click', estiloSelect.fecha);
		$("div.select .ativo").unbind('click').click(function(){estiloSelect.abre(this)});
	}
}


$(document).ready( function(){
	//noLabel.init();
	
	
	
// FLASH HOME
	$(".banner-home").addFlash({src: "swf/teaser01.swf", width: 990, height: 351, title: "Café Moka"});	
	
	
	
// PRELOADER DE IMAGENS
var img = new Array();
	img[1] = 'images/icones/setinha.png';

	for (var i in img) {
		var loading = new Image;    
		loading.src = img[i];
	}
	estiloSelect.init();

// Validar Form
///////////////////////////

	$('form').submit(function(){
		return validar.form($(this))
	});
	
	
//CAFÉS HOME
var aba = 0
	$('.aba li a').hover(function(){
		var obj = $(this).parents('ul:eq(0)');
		var indice = $('a', obj).index(this);
		aba = setTimeout(function(){
			$('li a', obj).removeClass('ativo');
			$('.combo > .conteudo-abas:not(:eq('+indice+'))').fadeOut('fast');
			$('.combo > .conteudo-abas:eq('+indice+')').fadeIn();
			$(this).addClass('ativo');
		},100);
	},function(){
		$('.aba li a').removeClass('ativo');
		$(this).addClass('ativo');
		clearTimeout(aba);
	});
	
// MENU LATERAL
$('.menu-lateral ul li a .txt2').css('opacity','0');
var menu
	$('.menu-lateral ul li a').hover(function(){
		obj = $(this);
		menu = setTimeout(function(){
			$('.menu-lateral ul li a .txt').css('visibility','hidden');
			$(obj).animate({marginLeft:-60});
			$(obj).children('.txt').css('visibility','visible');
			$(obj).children('.txt').children('span').css('color','#ffff00');
			$(obj).children('.txt2').animate({opacity:1},600);
			
		},100);
	},function(){
		clearTimeout(menu);
		$(obj).animate({marginLeft:0});	
		$(obj).children('.txt').children('span').css('color','#ffffff');
		$(obj).children('.txt2').animate({opacity:0},200);
		$('.menu-lateral ul li a .txt').css('visibility','visible');
	});


	
// BOX HOME
//	$('.home .box').hover(function(){
//		
//		$('.home .box').css({opacity:0.5});
//		$(this).css({opacity:1});
//	},function(){
//		$('.home .box').css({opacity:1});
//	});
	
	/*** :: Link Ativo ::*/
	dmPaginaAtivo = window.location+"";
	dmPaginaAtivo = dmPaginaAtivo.split("/").pop();
	dmPaginaAtivo = dmPaginaAtivo.split("?")[0];
	dmPaginaAtivo = dmPaginaAtivo =="" ? "index.php" : dmPaginaAtivo;
	dmPaginaAtivo = dmPaginaAtivo =="produtos.php" ? "produtos.php?moido" : dmPaginaAtivo;
	if(dmPaginaAtivo != ""){
		$('a[href$="'+dmPaginaAtivo+'"]').addClass('ativo');
		//$('a[href="'+dmPaginaAtivo+'"]').siblings('ul').removeClass('hide');
		//$('a[href="'+dmPaginaAtivo+'"]').parents('ul').removeClass('hide').siblings('a').addClass('ativo');;
	}
		
	$(".blank").live('click',function(){
		window.open($(this).attr("href"));
		return false;
	});
	
	$(".voltar").live('click',function(){
		window.history.go(-1);
		return false;
	});

	
	
// SANFONA DE PRODUTOS
	$(".bl h2").click(function(){
		var relClick = $('a',this).attr('rel');
		if($(this).siblings('.box-prod').is(':visible')){
			$(this).removeClass('ativo');
			$(this).siblings('.box-prod').slideUp("fast");
		}else{
			$(".box-prod:visible").slideUp("fast");
			$(".box-prod:visible").siblings('h2').removeClass("ativo");
			$(this).next().slideDown().parent('.box-prod');
			$(this).addClass('ativo');
		}
		//$('.menu-lateral').each(function(){
//			var relMenu = $('a',this);
//			if(relClick == relMenu){
//				$('a',this).animate({marginLeft:-60});
//			}	
//		})
		
	});
	
	
	$('.produtos .bl h2').each(function(){
		obj = $(this);
		url = window.location+""
		url = url.split("/").pop();
		url = url.split("?")[1];
		rel = $('a',obj).attr('rel');
		if(url == rel){
			$(obj).siblings('.box-prod').removeClass('hide');
			$(obj).addClass('ativo');	
			$('html, body').animate({
				scrollTop: $("a[rel="+rel+"]").offset().top
			},10);
		}
		if(url == 'moido2'){
			$('.produtos .box-prod:lt(2)').removeClass('hide');
			$(obj).addClass('ativo');	
			
		}
		//if(url == rel){
//		{
//			$('.produtos .bl:first h2').siblings('.box-prod').removeClass('hide');	
//			$('.produtos .bl:first h2').addClass('ativo');	
//		}
		
	});
	
	
// SANFONA FAQ
	$(".faq h2").click(function(){
		if($(this).siblings('.faq ul li div').is(':visible')){
			$(this).removeClass('ativo');
			$(this).siblings('.faq ul li div').slideUp("fast");
		}else{
			$(".faq ul li div:visible").slideUp("fast");
			$(".faq ul li div:visible").siblings('h2').removeClass("ativo");
			$(this).siblings('.faq ul li div').slideDown();
			$(this).addClass('ativo');
		}
	});



	/// FLUTUANTE GENERICA 
//	$('.link-flutua').click(function(){
//		var div = $(this).attr('rel');
//		var obj = $('.'+div)
//		$(this).addClass('ativo');
//		$(obj).appendTo('body');
//		bloqueia({bgColor: "#000",opacity:'0.7'},function(){
//			$(obj).appendTo('body').absoluteCenter({animation:false}).absoluteCenter().show();
//		});
//		$('.fechar-flutuante', obj).unbind().click(function(){
//			$('.boxMsg').remove();
//			$(obj).hide();
//			$(obj).children('input').removeClass('form_erro');
//			bloqueia({ speed: "slow", evento: "fim" });
//		});
//	});
	
	
$(".link-flutua").click(function(){
		bloqueia({ speed: "slow", bgcolor: "#000" });
		$(".flutuante").appendTo('body').absoluteCenter({animation:false}).absoluteCenter().show();
	});

	$(".fechar-flutuante").click(function(){
		$(".flutuante").hide();
		bloqueia({ speed: "slow", evento: "fim" });
		$(".ampliada img").attr("src","imagens/blank.gif");
	});	
	
	
	
//////
//  Galeria campanhas
/////////////////////////
	
	$("ul.ampliarFoto a").click(function(){
		var obj = $(".flutuante");
		var texto = $(this).attr('title');
		var description = $(this).children('img').attr('alt');
		if(!$("img", this).hasClass("ativo")){
			$("img.ativo").removeClass("ativo")
			$("img", this).addClass("ativo")
			
			$(".ampliada img", obj).attr("src","imagens/blank.gif");
			
			var src = $(this).attr("rel");
			
			var img = new Image();
			$(".carregando", obj).show();
			
			img.onload = function(){
				//var altura	= img.height;
//				var largura = img.width;
//				if(altura < 420){
//					margem = parseInt((420 - altura) / 2)
//					$(".ampliada img", obj).css("margin",margem+"px auto 0 auto");
//				}else{
//					$(".ampliada img", obj).css("margin","0 auto");
//				}
				$(".carregando", obj).hide();
				$(".ampliada img", obj).attr("src",src).fadeIn();
				$("h2", obj).text(texto);
				$("h3", obj).text(description);
			}
			img.src = src;
		}
	});
							
	$(".mudarFoto").click(function(){
		var obj = $(".flutuante");
		var valor = 0;
		var valor2 = 0
		$("ul.ampliarFoto img").each(function(){
			if($(this).attr("class") == "ativo"){
				valor2 = valor;
			}else{
				valor = valor + 1;
			}
		});
		
		if ($(this).attr("rel") == "right"){
			valor2 = valor2 + 1;
		}else{
			valor2 = valor2 - 1;
		}
		$("ul.ampliarFoto a:eq("+valor2+")").click();
	});	
	
	$('.flutuante').hover(function(){
		$('.mudarFoto').fadeIn();
	},function(){
		$('.mudarFoto').fadeOut();
	})
	
	// FALE CONOSCO
	$('.assunto .options ul li a').live('click',function(){
		var obj = $('.assunto');
		if($('input:hidden',obj).val() == 8){
			$('#form_contato .hide').show();
		}else{
			$('#form_contato .hide').hide();
			$('.boxMsg').remove();
		}
	});
	
	
	

});	
