var path="http://www.confeb.org.br/";
// função para o banner home
//v*ar path="http://localhost/febracorp/febracorp-confeb/";
inx = 0;
function alterna(){
	$('.navegaBanner li a').removeClass('ativo');
	$('.navegaBanner li a:eq('+inx+')').addClass('ativo');
	$('.bannerHome .boxBanner .boxGrande:not(:eq('+inx+'))').hide();
	$('.bannerHome .boxBanner .boxGrande:eq('+inx+')').fadeIn();
	inx = inx == 3 ? 0 : inx += 1;
}
/////////////
// 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;
	}
}

/////////////////////////
// galeria de Imagens //
///////////////////////

$.extend({
	scrollPages: {
		version: 2.5,
		defaults: {
			elementSize: 50,
			elements: 3,
			speed: "slow",
			direction: "up"
		}
	}
});
$.fn.extend({
	scrollPages: function(options){
		options = $.extend({}, $.scrollPages.defaults, options);
		return this.each(function(){
			
			// Definições de variáveis
			var total = $(".scrollMask li",this).length;
			var mask = options.elementSize * options.elements;
			var area = options.elementSize * total;
			var animation = false;
			var obj = this;
			var dimension = options.direction == "left" ? "width" : "height";
			var side = new Array();
			side[0] = options.direction;
			side[1] = options.direction == "up" ? "down" : "right";
			
			$(".scrollButtom",this).css("visibility","visible");
			if(options.elements >= total){
				$(".scrollButtom",this).css("visibility","visible");
				mask = area;
			}else{
				$(".scrollButtom[rel*='"+options.direction+"']",obj).css("visibility","hidden");
			}
			
			// Aplicação de estilos
			if(options.direction == "up"){
				//$(".scrollMask",this).css({"height": mask + "px", overflow: "hidden"}); // M?scara
				$(".scrollMask ul",this).css({"height": area + "px", marginTop: 0}); // UL
				$(".scrollMask li",this).css({"height": options.elementSize + "px", display: "block"}); // LI
			}else{
				//$(".scrollMask",this).css({"width": mask + "px", overflow: "hidden"}); // M?scara
				$(".scrollMask ul",this).css({"width": area + "px", marginTop: 0}); // UL
				$(".scrollMask li",this).css({"width": options.elementSize + "px", display: "block"}); // LI
			}
			if (area <= mask) $(".scrollButtom",obj).css("visibility","hidden");
			//if (area <= mask + 596) $(".buttom2",obj).css("visibility","hidden");

			// Função que execulta a rolagem
			var rolagem = function(direction){

				// Variáveis
				var margem = -parseInt($(".scrollMask ul",obj).css("margin-"+(options.direction == "up" ? "top" : "left")));

				var intervalo = direction == side[1]
					? ( margem + ( mask * 2 ) ) < area
						? mask
						: area - (margem + mask)
					: ( margem - mask ) < 0
						? margem
						: mask
				;
				direction = direction == side[1]
					? -margem - intervalo
					: -margem + intervalo
				;
				
				$(".scrollButtom",obj).css("visibility","visible");
				var rel = -direction == 0 ? side[0] : (-direction + mask) == area ? side[1] : null;
				if(rel != null) $(".scrollButtom[rel*='"+rel+"']",obj).css("visibility","hidden");
				
				
				//Condição especial para galeria com degrade
				//var rel2 = -direction == 0 ? side[0] : (-direction + mask + 596) == area ? side[1] : null;
				//if(rel2 != null) $(".scrollButtom[rel*='"+rel2+"']",obj).css("visibility","hidden");
				
				// Animação
				animation = true;
				if(options.direction == "up"){
					$(".scrollMask ul",obj).animate({marginTop: direction + "px"},options.speed,function(){ animation = false; });
				}else{
					$(".scrollMask ul",obj).animate({marginLeft: direction + "px"},options.speed,function(){ animation = false; });
				}
			}
			
			// Aplicação de eventos
			$(".scrollButtom",this).unbind("click").click(function(){
				if(!animation){
					var direction = $(this).attr("rel");
					rolagem(direction);
				}
			});
		});
	}
});

////////////////////////
// Select Estilizado //
//////////////////////
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")) ? 25 : $("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();
	estiloSelect.init();
	alterna();
	////////////////////////////
	// Galeria de fotos prettyPhoto
	////////////////////////////
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'facebook',
		autoplay: false,
		show_title: false,
		social_tools: false
	});
	
	$("#noticiaFiltro .options ul li").click(function(){
		$("#formFiltroNoticia").submit();
	});
	
	$("#selTipoEvento1 .options ul li,#selMes1 .options ul li").click(function(){
		$("#form1").submit();
	});
	$("#selTipoEvento2 .options ul li,#selMes2 .options ul li").click(function(){
		$("#form2").submit();
	});
	
	////////////////////////////
	// banner Home Loja
	//////////////////////////
	if ($('.navegaBanner').is('ul')){
		intervalo = setInterval("alterna()", 5000);
	}
	$('.navegaBanner li a').click(function(){
		var obj = $(this).parents('ul:eq(0)');
		inx = $('li a', obj).index(this);
		$('li a', obj).removeClass('ativo');
		$(this).addClass('ativo');
		$('.bannerHome .boxBanner .boxGrande:not(:eq('+inx+'))').hide();
		$('.bannerHome .boxBanner .boxGrande:eq('+inx+')').fadeIn();
	});
	
	$('.bannerHome').hover(function(){
		clearInterval(intervalo);
	}, function(){		
		alterna();
		intervalo = setInterval("alterna()", 5000);
	});

/////////////////////////////
// autoTab
////////////////////////////

	//$('#cep1').autotab({ format: 'number', target: 'cep2' });
	//$('#cep2').autotab({ format: 'number' });

/////////////////////////////
// Validar Form
///////////////////////////

	$('form').submit(function(){
		return validar.form($(this))
	});

////////////////////////
// colorBox
///////////////////////

$(".ampliar").colorbox();

///////////////////////
// Galeria Destaque Home
////////////////////////

	$('.produtoDestaque').scrollPages({elementSize:400, elements:1, direction:'left'});
	
///////////////////////
// Galeria Produtos destaque
////////////////////////

	$('.promocoesHome').scrollPages({elementSize:160, elements:4, direction:'left'});


/////////////////////////////////////	
// Centraliza titulo
/////////////////////////////
	$('.navegaBanner li a .alinhaVertical, .listaClipping .bloco .item .alinhaVertical').each(function(){
		var alturaDiv = $(this).parent().height();
		var alturaTitle = $(this).height();
		
		var margem = alturaDiv / 2 - alturaTitle / 2;
		$(this).css('padding-top',margem+'px');
	})
	
///////////////////////////////////////
// Acerta tamanho menu categorias
//////////////////////////////////////

	var divs= $('.menuCat ul');
	var maior=0;
	for(i=0;i<divs.length;i++){
		if($(divs[i]).height()>maior){
			maior=$(divs[i]).height();	
		}
	}
	
	for(i=0;i<divs.length;i++){
		$(divs[i]).css('height',(maior+15)+'px');
		$('.menuCat .categoria').css('height',(maior+20)+'px');
	}
	
///////////////////////////////////////
// Acerta tamanho Coleções
//////////////////////////////////////

	var divs= $('.colecoes .colecao');
	var maior=0;
	for(i=0;i<divs.length;i++){
		if($(divs[i]).height()>maior){
			maior=$(divs[i]).height();	
		}
	}
	
	for(i=0;i<divs.length;i++){
		$(divs[i]).css('height',maior+'px');
	}

/////////////////////////////////////	
// Centraliza Paginação
/////////////////////////////
	$('.paginacao ul').each(function(){
		var larguraDiv = $(this).parent().width();
		var larguraUl = $(this).width();
		
		var margem = larguraDiv / 2 - larguraUl / 2;
		$(this).css('margin-left',margem+'px');
	})

///////////////////////////////////
// DropDown Topo
/////////////////////////////////
	var tempo 
	esconde = function(){
		//$('.menuPrincipal li .subMenu').children().hide();
		$('.menuPrincipal li .subMenu').slideUp('fast');
	}
	$('.menuPrincipal li').hover(function(){
		//$(this).children('.subMenu').children().show();
		$(this).children('.subMenu').slideDown('fast');
		clearTimeout(tempo);
	},function(){
		tempo = setTimeout(esconde,500);
	});
	
/////////////////////////////
// Abas Informações
/////////////////////////////
	
	$('.informacoes .abas li a').click(function(){
		var ul = $(this).parents('ul:eq(0)');
		var indice = $('a', ul).index(this);
		$('.informacoes .abas li a.ativo').removeClass('ativo');
		$(this).addClass('ativo');
		$('.informacoes .conteudoAbas > div:not(:eq('+indice+'))').hide();
		$('.informacoes .conteudoAbas > div:eq('+indice+')').show();
		
		$('.informacoes .conteudoAbas > div:not(:eq('+indice+'))').hide();
		$('.informacoes .conteudoAbas > div:eq('+indice+')').show();
	});
	
/////////////////////////////
// Abas Veja Também
/////////////////////////////
	
	$('.vejaTambem .abas li a').click(function(){
		var ul = $(this).parents('ul:eq(0)');
		var indice = $('a', ul).index(this);
		$('.vejaTambem .abas li a.ativo').removeClass('ativo');
		$(this).addClass('ativo');
		$('.vejaTambem .conteudoAbas > div:not(:eq('+indice+'))').hide();
		$('.vejaTambem .conteudoAbas > div:eq('+indice+')').show();
		
		$('.vejaTambem .conteudoAbas > div:not(:eq('+indice+'))').hide();
		$('.vejaTambem .conteudoAbas > div:eq('+indice+')').show();
	});
	
////////////////////////////////////////
// abre e fecha detalhes do pedido
/////////////////////////////////////////

	$('.tdMaster a.abreDetalhes').click(function(){
		$(this).hide();
		$(this).siblings('a.fechaDetalhes').show();
		$(this).parents('tr:first').siblings('tr').show();
	});
	
	$('.tdMaster a.fechaDetalhes').click(function(){
		$(this).hide();
		$(this).siblings('a.abreDetalhes').show();
		$(this).parents('tr:first').siblings('tr').hide();
	});
	
	/** Copiar essas funções 
	 * 
	 */
	
	$(".cmbtpu .options ul li").click(function(){
		var tipo = $("#selU_select").val();
		$(".cmbtpb div.ativo").text($(this).children('a').text())
		$(this).children().text()
		$.ajax({
			type:"POST",
			url:path+"/eventos_e_cursos/filtra",
			data:"tipo="+tipo,
			success:function(msg){
				$(".destaquesLista").html(msg);
			}
		});

	});
	$(".cmbtpb .options ul li").click(function(){
		var tipo = $("#selB_select").val();
		$(".cmbtpu div.ativo").text($(this).children('a').text())
		$.ajax({
			type:"POST",
			url:path+"/eventos_e_cursos/filtra",
			data:"tipo="+tipo,
			success:function(msg){
				$(".destaquesLista").html(msg);
			}
		});
	});
	
	$(".galeriaFotosNews .galeriaImg li img").load(function(){
		tamanhoMaior = 0;
		$(this).each(function(){		
			if($(this).height() > tamanhoMaior) {
				tamanhoMaior = $(this).height();
			}
		});
		$(".galeriaFotosNews .galeriaImg li:not(:last-child)").height(tamanhoMaior);
	});
	
	$(".navegaBanner li:last-child").find("a").css("height","65px");
});
