
$(document).ready(function(){
    //CARROSEL
    jQuery("#mycarousel1").jcarousel({
        scroll: 1,
        /*auto	:	3,*/
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
		
    if($('.fotos').size()){
        $('a[rel*=facebox]').facebox({
            loadingImage : 'app/assets/js/plugins-jquery/src/loading.gif',
            closeImage   : 'app/assets/js/plugins-jquery/src/closelabel.png'
        });
    }
    
    watermark.init();

    /* PLUGIN DE PNG FIX */
    if($.browser.msie && ($.browser.version == "6.0")){
        $(document).pngFix();
    }

    /* SCROLL ANIMATE */
    $('.scrollPage').click(function() {
        var elementClicked = $(this).attr("href");
        var destination = $(elementClicked).offset().top;
        $("html:not(:animated),body:not(:animated)").animate({
            scrollTop: destination-20
        }, 500 );
        return false;
    });
		
    //GALERIA DE VEICULOS
    // Debugger: Efeito hover em div
    $('.hold-largeImg').hover(function(){
        $(this).css('border-color', 'ff9900');
    }, function(){
        $(this).css('border-color', '');
    });
		
    $('#thumb a').each(function(e) {
        $(this).click(function(){
            $('.thumbs img').css('display','block');
            $(this).find('img').css('display','none');
        });
		
    });
		
    // Pega o atributo href do primeiro link e lhe atribui class 'ativo'
    $(".thumbs a:first").addClass("ativo");
		
    var largePath = $(".thumbs a:first").attr('href');
    var largeAlt = $(".thumbs a:first").find('img').attr('alt');
		
    $("#largeImg").attr({
        src: largePath, 
        alt: largeAlt
    });
		
		
    // Leia o de baixo primeiro
    //Quando chegarmos no ultimo link com class ativo adicionamos ao primeiro a class e removemos do ultimo a class		
    $("#largeImg").click(function(){
        if ($(".thumbs a:last-child").is(".ativo")){
            $(".thumbs a:first").addClass("ativo");
            $(".thumbs a:last").removeClass("ativo");
		
            var largePath = $(".ativo").attr('href');
            var largeAlt = $(".ativo").find('img').attr('alt');
		
            $("#largeImg").attr({
                src: largePath, 
                alt: largeAlt
            });
		
		
        //Proxima imagem se houver um class 'ativo', ela sera removida e o proximo a recebera a class
        //armazenamos os atributos da tag com class 'ativo' em uma variavel e a passamos a largeImg
        } else{
            $(".ativo").toggleClass("ativo").next(".thumbs a").addClass("ativo");
		
            var largePath = $(".ativo").attr('href');
            var largeAlt = $(".ativo").find('img').attr('alt');
		
            $("#largeImg").attr({
                src: largePath, 
                alt: largeAlt
            });
        }
		
    });
		
    //Quando o link for clicado inserir class ativo se houver esta class em algum dos elementos irmão remova-os e copie seu valores e passe para large img
    $('.thumbs a').click(function(){
		
        $(this).siblings(".ativo").removeClass("ativo");
        $(this).addClass("ativo");
		
        var largePath = $(this).attr('href');
        var largeAlt = $(this).find('img').attr('alt');
		
        $("#largeImg").attr({
            src: largePath, 
            alt: largeAlt
        });
        return false;
		
    });
		
});

function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.jcarousel-scroll a').bind('change', function() {
        carousel.a.scroll = jQuery.jcarousel.intval(this.a[this.selectedIndex].value);
        return false;
    });

    jQuery('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};


function str_replace(haystack, needle, replacement) {
    var temp = haystack.split(needle);
    return temp.join(replacement);
}

function disabledFormButton(ct) {
    $(ct).attr('disabled','disabled').css('cursor','default').css('filter','alpha(opacity=40)').css('-moz-opacity','0.4').css('opacity','0.4');
}

function enabledFormButton(ct) {
    $(ct).attr('disabled','').css('cursor','pointer').css('filter','alpha(opacity=100)').css('-moz-opacity','1').css('opacity','1');
}

function hideError() {
    $("#retorno_erro").animate({
        left:'+=50',
        height:'toggle'
    },500, function() {
        $("#retorno_erro").html('');
        $("#retorno_erro").css('display','block');
    });
    window.clearTimeout(timeoutID);
}

function topo() {
    window.location = document.URL+'#';
}

function gotoAnchor(value) {
    var elementClicked = '#'+value;
    var destination = $(elementClicked).offset().top;
    $("html:not(:animated),body:not(:animated)").animate({
        scrollTop: destination-20
    }, 500 );
}

var valEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;

function validaContato(){
    msg = "";

    if($("#nome").val() == ""){
        msg += "Nome não preenchido.\n";
    }

    if($("#email").val() == ""){
        msg += "E-mail não preenchido.\n";
    } else if(!valEmail.test($("#email").val())){
        msg += "E-mail inválido.\n";
    }
	
    if($("#fone").val() == ""){
        msg += "Telefone não preenchido.\n";
    }
		
    if($("#mensagem").val() == ""){
        msg += "Sua mensagem não foi preenchida.\n";
    }

    if(msg != ""){
        msg = "Os seguintes campos são obrigatórios:\n\n" + msg;
        alert(msg);
        return false;
    } else {
        return true;
    }	
}

function validaSolicite(){
    msg = "";

    if($("#nomeS").val() == ""){
        msg += "Nome não preenchido.\n";
    }

    if($("#emailS").val() == ""){
        msg += "E-mail não preenchido.\n";
    } else if(!valEmail.test($("#emailS").val())){
        msg += "E-mail inválido.\n";
    }
	
    if($("#foneS").val() == ""){
        msg += "Telefone não preenchido.\n";
    }
		
    if($("#mensagemS").val() == ""){
        msg += "Sua mensagem não foi preenchida.\n";
    }

    if(msg != ""){
        msg = "Os seguintes campos são obrigatórios:\n\n" + msg;
        alert(msg);
        return false;
    } else {
        return true;
    }
}

$(function() {
    $(".pacote_aba_item").click(function() {
        id = $(this).attr("id");
    });
});

function cadastra_news(url){
	var msg = "";

	if($('#nomeNews').val() == "Nome" || $('#nomeNews').val() == ""){
		msg += "Preencha seu nome\n";
	}

	if($('#emailNews').val() == "" || $('#emailNews').val() == "E-mail"){
		msg += "E-mail não preenchido.\n";
	} else if(!valEmail.test($('#emailNews').val())){
			msg += "E-mail inválido.\n\n";
		}

	if(msg != ""){
		alert(msg);
	} else {
			$.ajax({
				type		: 'POST',
				url			: url,
				data		: "nome=" + $('#nomeNews').val() + "&email=" + $('#emailNews').val(),
				success	: function(msg){
					alert('E-mail cadastrado!');
				},
				error: function(msg){
					alert("Erro ao cadastrar, tente novamente mais tarde!");
				}
			});
		}
}
