// JavaScript Document

$("document").ready(function() {
									  
	// Menu Dropdown
	$("#menu li").hover(
		function() { $(this).addClass("over"); }, 
		function() { $(this).removeClass("over"); }
	);

	// Search
	$('#search-input').focus(function() {
		if($(this).val() == "Pesquise no site..."){
			$(this).val("");
		}
	});
	$('#search-input').blur(function() {
		if($(this).val() == ""){
			$(this).val("Pesquise no site...");
		}
	});

	// Tabs
	$('.ui-tabs-nav a').click(function(){
		aba = $(this).attr('href');
		$('#tabs li').removeClass('active');
		$(this).parent().addClass('active');
		$('#tabs > div').hide();
		$('#tabs').find(aba).show();
		return false;
	});


	/* DESTAQUES [inicio] */
	if ($('#destaques').length){
		var proximo = 2;
		var totalDestaques = 3;
		var tempoDestaque = 4000; /* Intervalo entre um destaque e outro */

		$('#destaques div:first, #destaques li:first').addClass('selecionado');

		function exibirDestaque(num) {
			$('#destaques div.anterior').removeClass('anterior');

			var anterior = $('#destaques div.selecionado');
			var selecionado = $('#destaques #destaque' + num);

			anterior.removeClass('selecionado').addClass('anterior');
			selecionado.addClass('selecionado');

			$('#destaques li.selecionado').removeClass('selecionado');
			$('#destaques li:nth-child(' + num + ')').addClass('selecionado');

			/* Exibição da imagem */
			$(selecionado).find('img').fadeOut(0);
			$(selecionado).find('img').fadeIn(400);
			
			proximo = num+1
			if (proximo > totalDestaques){
				proximo = 1;
			}

		}

		/* Mouseover */
		$('#destaques li').mouseenter(function(){
			clearInterval(iniciarSlideshow); /* Parar slideshow */

			num = $(this).attr('value');
			if($(this).hasClass('selecionado')) { return; }
			exibirDestaque(num);
		});

		/* Slideshow */
		function slideShow() { exibirDestaque(proximo); }

		iniciarSlideshow = window.setInterval(slideShow, tempoDestaque);
	}
	/* DESTAQUES [fim] */



	/* FILTRO FORNECEDORES [inicio] */
	if($('#fornecedores-filtro').length){
		$.get('/fornecedores-xml/', function(data){
			$fornecedores = $(data);
			$cidades = $fornecedores.find('cidade');
			$filtroCidade = $('#filtro-cidade');
			$filtroCategoria = $('#filtro-categoria');
	
			// config cidades
			for(i=0; i < $cidades.length; i++){
				label = $cidades.eq(i).attr('title')
				valor = $cidades.eq(i).attr('slug')
				$filtroCidade[0].options[i+1] = new Option(label, valor);
			}
			
			$filtroCidade.change(function(){
				selected = this.selectedIndex;
				$categorias = $cidades.eq(selected-1).find('categoria');
				$filtroCategoria.html('');
				$filtroCategoria[0].options[0] = new Option('-selecione-', 0);
				$filtroCategoria[0].selectedIndex = 0;
				for(i=0; i < $categorias.length; i++){
					label = $categorias.eq(i).text()
					valor = $categorias.eq(i).attr('slug')
					$filtroCategoria[0].options[i+1] = new Option(label, valor);
				}
			});
			
			$('#form-fornecedores').submit(function(){
				cidadeSelecionada = $filtroCidade.val();
				categoriaSelecionada = $filtroCategoria.val();
				if (cidadeSelecionada != 0 && categoriaSelecionada != 0) {
					linkFornecedor = '/fornecedores/' + cidadeSelecionada + '/' + categoriaSelecionada + '/';
					//alert(linkFornecedor);
					window.location = linkFornecedor;
				} else {
					alert('Selecione uma cidade e uma categoria');
				}
				return false;
			});
		
		});
	}
	/* FILTRO FORNECEDORES [fim] */



	/* GALERIA */
	$("#listaBase").change(function () {
		$("#listaSecundaria_a").html('<option value="">- Escolha uma op&ccedil;&atilde;o</option>');
		$("#listaSecundaria_b").html('<option value="">- Escolha uma op&ccedil;&atilde;o</option>');
		var value_a = "";
		var value_b = "";
		$("#listaBase option:selected").each(function () {
			value_a = eval($(this).val() + "_a");
			value_b = eval($(this).val() + "_b");
		});
		if (value_a != null || value_b != null)
		{
			$("#listaSecundaria_a").html(value_a);
			$("#listaSecundaria_b").html(value_b);
		}
	});
	
	var thumbId = 0;
	var thumbHref = new Array();
	var thumbTitle = new Array();
	
	$("#albumThumbs a").each(function () {
		thumbHref[thumbId] = $(this).attr("href");
		thumbTitle[thumbId] = $(this).attr("title");
		$(this).attr("rel", thumbId);
		thumbId++;
	});
	if ($("#next_photo").size() > 0)
	{
		$("#next_photo").addClass('photo_nav').attr("href", thumbHref[1]).attr("data-legenda", thumbTitle[1]).attr("rel", 1);
	}
	
	
	var maxThumb = thumbHref.length - 1;
	
	$("#albumThumbs a,#prev_photo,#next_photo").click(function () {
		var imagem = $(this).attr("href");
		var legenda = $(this).attr("data-legenda");
		var actualImage = new Number($(this).attr("rel"));
		var nextImage = actualImage + 1;
		var prevImage = actualImage - 1;
		
		$("#prev_photo").removeClass('photo_nav').attr("href", '').attr("data-legenda", '');
		$("#next_photo").removeClass('photo_nav').attr("href", '').attr("data-legenda", '');
		

		if (actualImage == 0)
		{
			$("#next_photo").addClass('photo_nav').attr("href", thumbHref[nextImage]).attr("data-legenda", thumbTitle[nextImage]).attr("rel", nextImage);
		}
		else if (actualImage == maxThumb)
		{
			$("#prev_photo").addClass('photo_nav').attr("href", thumbHref[prevImage]).attr("data-legenda", thumbTitle[prevImage]).attr("rel", prevImage);
		}
		else
		{
			$("#next_photo").addClass('photo_nav').attr("href", thumbHref[nextImage]).attr("data-legenda", thumbTitle[nextImage]).attr("rel", nextImage);
			$("#prev_photo").addClass('photo_nav').attr("href", thumbHref[prevImage]).attr("data-legenda", thumbTitle[prevImage]).attr("rel", prevImage);
		}
		
		$("#albumImage img").attr("src", imagem);
		$("#albumImage div").html(legenda);
		return false;
	});
	
	$("#listaBase option:selected").each(function () {
		value_a = eval($(this).val() + "_a");
		value_b = eval($(this).val() + "_b");
		$("#listaSecundaria_a").html(value_a);
		$("#listaSecundaria_b").html(value_b);
	});

	
	/*$("#albumImage").hover(
		function () {
			$("#prev_photo").fadeIn('fast');
			$("#next_photo").fadeIn('fast');
		},
		function () {
			$("#prev_photo").fadeOut('fast');
			$("#next_photo").fadeOut('fast');
		}
	);*/
	
	/* GALERIA [fim] */

});
