$(document).ready(function(){

	/*- CARROSEL -*/		
	if($('#mycarousel').size()){
		jQuery('#mycarousel').jcarousel({scroll: 1});
	}

    /* PLUGIN DE WATERMARK */
    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;
    });
    
});

/* send newsletter */
function sendNews() {
    msg = "";

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

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

    if(msg != ""){
        alert("Os seguintes campos contém erros:\n\n"+msg);
    } else {
        
        $.ajax({
            type: "POST",
            url: base_url+"index.php?on=home&in=saveNews",
            data: "name="+$("#n_name").val()+"&email="+$("#n_email").val(),
            success: function(msg){
                alert(msg);
                $('#n_name').val('Nome');
                $('#n_email').val('E-mail');
            }
        });
    }
}
/* send newsletter */


var timeoutID = '';

function sendQuotation() {
    msg = "";
    
    if($('#formQuotation input[type="button"]').attr('disabled') != 'disabled') {
	
	    if($("#q_name").val() == "" || $("#q_name").val() == "Nome") {
	        msg += "Nome não preenchido. \n";
	    }
	        
	    if($("#q_email").val() == "" || $("#q_email").val() == "Email"){
	        msg += "E-mail não preenchido. \n";
	    } else if(!valEmail.test($("#q_email").val())){
	        msg += "E-mail inválido. \n";
	    } 
	    	
		if($("#q_ddd").val() == "" || $("#q_ddd").val() == "DDD"){
	        msg += "DDD não preenchido. \n";
	    } else if($("#q_ddd").val().length < 2){
	        msg += "DDD inválido. \n";
	    } 
	
	    if($("#q_phone").val() == "" || $("#q_phone").val() == "Telefone"){
	        msg += "Telefone não preenchido. \n";
	    } else if($("#q_phone").val().length < 8){
	        msg += "Telefone inválido. \n";
	    }
		
	    if($("#q_description").val() == "" || $("#q_description").val() == "Mensagem") {
	        msg += "Mensagem não preenchida. \n";
	    }
	
	    disabledFormButton('#formQuotation input[type="button"]');
			
	    if(msg != "") {
	        msg = "Os seguintes campos encontram-se com problemas: \n\n" + msg;
	        alert(msg);
			
	        enabledFormButton('#formQuotation input[type="button"]');
			
	    } else {
	    
	    	var product_id = '';
	    	
	    	if($('#product_id').size()) product_id = $('#product_id').val();
	    
	        $.ajax({
	            type: "POST",
	            url: base_url+"index.php?on=home&in=sendQuote",
	            data: "name="+$("#q_name").val()+"&email="+$("#q_email").val()+"&ddd="+$("#q_ddd").val()+"&phone="+$("#q_phone").val()+"&description="+$("#q_description").val()+"&product_id="+product_id,
	            success: function(msg){
	            	if(msg == 'ok') {
	            		alert('Solicitação enviada com sucesso');
		                $('#q_name').val('Nome');
		                $('#q_email').val('E-mail');
		                $('#q_ddd').val('DDD');
		                $('#q_phone').val('Telefone');
		                $('#q_description').val('Mensagem');
		                
		                watermark.init();
		            } else 
		            	alert('Erro de transmissão tente novamente');
		            	
		           	enabledFormButton('#formQuotation input[type="button"]');
	            }
	        });
	    }
    
    }
	
    return false;
}

function validSearch(form) {
	var msg = '';
	var q = form.find('#q').val();
	
	if(q == '' || q == 'O que deseja buscar') msg = 'Ajuste sua busca';

	if(msg != '') {
		alert(msg);
		return false;
	} else 
		return true;
}

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).removeAttr('disabled');
    $(ct).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}\])$/;



