var bolinhas = 0;

$(document).ready(function(){
	
	$("#aPesq").click(function(){
		$("#formPesq").submit();
	});
	
	$("#bannerMapa").hover(function(){
		$("#bannerFlash").show();
	},function(){});
	

	
	$("#linkNovos").hover(function(){
		$("#submenu_marcas").show();
		$("#linkNovos").css("background-color", "#5E81AA");
	}, function(){});
	
	$(".outbtn").hover(function(){
		if($("#linkNovos").css("display") == "block"){
			$("#submenu_marcas").hide();
			$("#linkNovos").removeAttr("style");
		}
	}, function(){});
	
	$("#menuTopo").hover(function(){}, function(){
		if($("#linkNovos").css("display") == "block"){
			$("#submenu_marcas").hide();
			$("#linkNovos").removeAttr("style");
		}
	});
	
	$("#submenu_marcas").hover(function(){
		$("#submenu_marcas").show();
		$("#linkNovos").css("background-color", "#5E81AA");
	}, function(){
		$("#submenu_marcas").hide();
		$("#linkNovos").removeAttr("style");
	});
	
	totalBolinhas = $(".oreolas").length;
	
	if(totalBolinhas > 0){
		time = setTimeout('changeDestaque(bolinhas, totalBolinhas)', 15000);
	}
	
	$("#goForward").click(function(){
		if(bolinhas + 1 < totalBolinhas){
			value = bolinhas + 1;
			evalTimmerProcess(value);
		}else{
			value = 0;
			evalTimmerProcess(value);
		}
	});
	
	$("#goBackward").click(function(){
		if(bolinhas - 1 > -1){
			value = bolinhas - 1;
			evalTimmerProcess(value);
		}else{
			value = totalBolinhas - 1;
			evalTimmerProcess(value);
		}
	});
	
	$('#baseModelos').cycle({
	    fx:      'scrollUp',
	    next:   '#bt_listagem_modelos',
	    timeout:  0,
	    speed: 2000
	});
	
	$("#btnSearchUsados").click(function(){
		$("#formUsadosSearch").submit();
	});
	
	$("#sites_menu").change(function() {
       		window.location = $(this).val();
    });

	$('.boxStands').jScrollPane({showArrows:true, scrollbarWidth: 22, arrowSize: 22});
  $('.resultBox').jScrollPane({showArrows:true, scrollbarWidth: 22, arrowSize: 22});

	$('#logoNovo').hover(
							function(){
								$('#cetinha').fadeIn();
							},
							function(){
								$('#cetinha').fadeOut();
							}
						);

	$( ".arrasta" ).draggable({ revert: true });
	$( ".placer" ).droppable({
		drop: function( event, ui ) {
		
			$( this ).html($(ui.draggable).children(".thumb").html());
			$("#pesquisa_modelo_proposta").val($(ui.draggable).children(".cid").html());
			setModelo_proposta();
		}
	});
});

function evalTimmerProcess(value){
	script = $("#btn_nav_" + value).attr("href");
	script = script.replace("javascript:", "");
	eval(script);    
	clearTimeout(time);
	time = setTimeout('changeDestaque(bolinhas, totalBolinhas)', 5000);
}

function changeDestaque(bolinhas, totalBolinhas){
	if(bolinhas + 1 < totalBolinhas){
		script = $("#btn_nav_" + (bolinhas + 1)).attr("href");
		script = script.replace("javascript:", "");
		eval(script);
	}else{
		script = $("#btn_nav_0").attr("href");
		script = script.replace("javascript:", "");
		eval(script);
	}
	
	time = setTimeout('changeDestaque(bolinhas, totalBolinhas)', 15000);
	
	return time;
}

function updateMarcas(data)
{
    var marcasArr = data.split(";");
    var marcasSel = document.frmPesquisaHome.marcas;
    marcasSel.options.length = 0;
    marcasSel.options[marcasSel.options.length] = new Option("Marca", -1);
    var modelosSel = document.frmPesquisaHome.modelo;
    modelosSel.options.length = 0;
    modelosSel.options[modelosSel.options.length] = new Option("Modelo", -1);
    var versoesSel = document.frmPesquisaHome.versao;
    versoesSel.options.length = 0;
    versoesSel.options[versoesSel.options.length] = new Option("Versão", -1);
    for(i=0;i<marcasArr.length; i++)
    {
        var marca = marcasArr[i].split("|");
		if(marca[0] != "1231")
			marcasSel.options[marcasSel.options.length] = new Option(marca[1], marca[0]);
    }
	
	$("#marcas").selectbox();
}
function updateModelos(data)
{ 
    var modelosArr = data.split(";");
    var modelosSel = document.frmPesquisaHome.modelo;
    modelosSel.options.length = 0;
    modelosSel.options[modelosSel.options.length] = new Option("Modelo", -1);
    var versoesSel = document.frmPesquisaHome.versao;
    versoesSel.options.length = 0;
    versoesSel.options[versoesSel.options.length] = new Option("Versão", -1);
    for(i=0;i<modelosArr.length; i++)
    {
        var modelo = modelosArr[i].split("|");
        modelosSel.options[modelosSel.options.length] = new Option(modelo[1], modelo[0]);
    }
    
    $("#modelo").selectbox();
}
function updateVersoes(data)
{ 
    var versoesArr = data.split(";");
    var versoesSel = document.frmPesquisaHome.versao;
    versoesSel.options.length = 0;
    versoesSel.options[versoesSel.options.length] = new Option("Versão", -1);
    for(i=0;i<versoesArr.length; i++)
    {
        var versao = versoesArr[i].split("|");
        versoesSel.options[versoesSel.options.length] = new Option(versao[1], versao[0]);
    }
}
function filterByTipo()
{
    var novos = document.frmPesquisaHome.tipoAutomovel[0];
    var usados = document.frmPesquisaHome.tipoAutomovel[1];
    var tipoAuto = 'novos';
    if(!novos.checked){ tipoAuto = 'usados'; }
    
    $.post("/classes/getMarcas.php", { tipo: tipoAuto }, updateMarcas);
}
function filterByMarca(){
    var novos = document.frmPesquisaHome.tipoAutomovel[0];
    var usados = document.frmPesquisaHome.tipoAutomovel[1];
    var tipoAuto = 'novos';
    if(!novos.checked){ tipoAuto = 'usados'; }
    var marcaSel = document.frmPesquisaHome.marcas.value;
    $.post("/classes/getModelos.php", { tipo: tipoAuto, marcaId: marcaSel }, updateModelos);
    
}
function filterByModelo(){
    var novos = document.frmPesquisaHome.tipoAutomovel[0];
    var usados = document.frmPesquisaHome.tipoAutomovel[1];
    var tipoAuto = 'novos';
    if(!novos.checked){ tipoAuto = 'usados'; }
    var modeloSel = document.frmPesquisaHome.modelo.value;    
    $.post("/classes/getVersoes.php", { tipo: tipoAuto, modeloId: modeloSel }, updateVersoes);
}

function validaForm()
{
    var frm = document.frmPesquisaHome;
    var novos = document.frmPesquisaHome.tipoAutomovel[0];
    var usados = document.frmPesquisaHome.tipoAutomovel[1];
    var tipoAuto = 'novos';
    if(!novos.checked){ tipoAuto = 'usados'; }
    
    if(frm.marcas.value>0)
    {
        /*if(frm.modelo.value>0)
        {*/
            if(tipoAuto=='novos'){
                //frm.action = '/novos/listanovos.php?marca=' + frm.marcas.value + '&modelo=' + frm.modelo.value + '&versao=' + frm.versao.value + '';
                if(frm.modelo.value>0){
                    frm.action = '/novos/detalhes.php?cat=' + frm.marcas.value + '&id=' + frm.modelo.value + '';
                }
                else
                {
                    frm.action = '/novos/index.php?cat=' + frm.marcas.value + ''; 
                }
                frm.submit();
            }
            else
            {
			comb = frm.pesquisa_combustivel.value;
                if(frm.modelo.value>0){ 
                    frm.action='/usados/listausados.php?pesquisa_usados=1&pesquisa_marca=' + frm.marcas.value + '&pesquisa_modelo=' + frm.modelo.value + '&pesquisa_ano_ini=&pesquisa_ano_fim=&pesquisa_preco_ini=&pesquisa_preco_fim=&pesquisa_concessionario=0&pesquisa_combustivel=' + comb + '&pesquisa_cor=0';
                }
                else
                {
                    frm.action='/usados/listausados.php?pesquisa_usados=1&pesquisa_marca=' + frm.marcas.value + '&pesquisa_modelo=0&pesquisa_ano_ini=&pesquisa_ano_fim=&pesquisa_preco_ini=&pesquisa_preco_fim=&pesquisa_concessionario=0&pesquisa_combustivel=' + comb + '&pesquisa_cor=0';
                }
                frm.submit();
            }
        /*}
        else
        {
            alert('Por favor escolha um modelo');
            frm.modelo.focus();
        }*/
        
    }
    else
    {
        alert('Por favor escolha uma marca');
        frm.marcas.focus();
    }
}

function addToFavorites(empresa) {
    if (document.all) { // Para o IE
    	window.external.AddFavorite('http://www.autosuecoautomoveis.pt', empresa);
    } else {
    	if (window.sidebar) { // Para o Firefox
    		window.sidebar.addPanel(empresa, 'http://www.autosuecoautomoveis.pt', "");
    	} else { // Para outros browsers que não suportem, nem um metódo, nem o outro
    		alert("Para adicionar este site aos seus Favoritos pressione Ctrl+D");
    	}
    }
}

function setDestaque(obj, src, type, link, target, num, imgpath, nolink){
    if(type=='swf'){ 
        so = new SWFObject("/fotos/"+imgpath+"/" + src + "", "mymovie", "698", "319", "8", "#336699");
        so.addParam("wmode", "transparent");
        so.addParam("quality", "high");
        so.write("flashcontent");
    }    
    else
    {
        if(nolink == 1){
          link = "";
          link2 = "";
        }else{
          link = '<a href="' + link + '" ' + target + '>';
          link2 = '</a>';
        }
        var elem = document.getElementById('flashcontent');
        elem.innerHTML = '<div style="display:none" id="fadeMe">' + link + '<img src="/fotos/'+imgpath+'/' + src + '" border="0">' + link2 + '</div>';
        $("#fadeMe").fadeIn("slow");
    }
    
	$("#tituloDestaque").html($("#destaque_titulo_"+num).html());
    $("#textoDestaque").html($("#destaque_texto_"+num).html());
    $("#linkDestaque").attr("href",$("#destaque_link_"+num).html());
    
    $(".oreolas").removeAttr("style");
    $("#" + obj).css("background", "url(/temas/default/imgs/novo/btNavegacao.png) 0 -12px");
    bolinhas = num;
}
