var controla = 0;
function addForm(a) {
    
    function f() {
        if (controla == 0) {
            controla = 1;
            b.find(".successmessage").hide();
            var f = "";
            if (a == "#form_contato") {
                f = "email.asp?c=1";
            } else if (a == "#form_diretor") {
                f = "email.asp?c=2";
            } else if (a == "#form_trabalhe") {
                f = "email.asp?c=3";
            } else if (a == "#form_info") {
                f = "email.asp?c=4";
            } else if (a == "#form_depoimento") {
                f = "depoimento/enviar.asp";
            } else {
                f = "";
            }
            if (d()) {

                if (!c) {
                    c = true;
                    b.find(".errormessage").hide();
                    b.find(".sendingmessage").show();
                    jQuery.post(f, b.serialize(), function () {
                        if (jQuery("#form_depoimento").length != 0) {
                            b.find("#div_depoimento").hide();
                        }
                        b.find(".sendingmessage").hide();
                        b.find(".successmessage").fadeIn();
                        controla = 0;
                        e();
                    })
                }
            }
            return false
        } else {
            return false
        }
    }
    function e() {
        b.find("input").each(function () {
            jQuery(this).val("")
        });
        b.find("textarea").val(""); 
        c = false
    }

    function d() {
        var a = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        var c = /.{4}/;
        var d = true;
        b.find(".requiredfield").each(function () {
            var b = jQuery(this).attr("name");
            var e = jQuery(this).val();
            if (b == "email") {
                if (!a.test(e)) {
                    jQuery(this).addClass("formerror");
                    d = false
                } else {
                    jQuery(this).removeClass("formerror")
                }
            } else {
                if (!c.test(e)) {
                    jQuery(this).addClass("formerror");
                    d = false
                } else {
                    jQuery(this).removeClass("formerror")
                }
            }
        });
        if (!d) {
            b.find(".errormessage").fadeIn()
        }
        return d
    }
    var b = jQuery(a);
    var c = false; 
    b.find("button[name=send]").click(f)
}

function relative_time(a) {
    var b = a.split(" ");
    a = b[1] + " " + b[2] + ", " + b[5] + " " + b[3];
    var c = Date.parse(a);
    var d = arguments.length > 1 ? arguments[1] : new Date; var e = parseInt((d.getTime() - c) / 1e3);
    e = e + d.getTimezoneOffset() * 60; var f = "";
    if (e < 60) { f = "minuto atrás" } else if (e < 120) { f = "alguns minutos atrás" } else if (e < 45 * 60) { f = parseInt(e / 60).toString() + " minutos atrás" } else if (e < 90 * 60) { f = "hora atrás" } else if (e < 24 * 60 * 60) { f = "" + parseInt(e / 3600).toString() + " horas atrás" } else if (e < 48 * 60 * 60) { f = "1 dia" } else { f = parseInt(e / 86400).toString() + " dias" }
    return f 
}

jQuery(document).ready(function () {
    if (jQuery("#tweets").length != 0) {
        jQuery.ajaxSetup({ cache: true });
        jQuery.getJSON("http://twitter.com/status/user_timeline/cprimeimoveis.json?count=3&callback=?", function (a) {
            jQuery.each(a, function (a, b) {
                jQuery(".tweetlist").append("<li>" + b.text.linkify() + "</li>")
                jQuery(".tweetlist li").attr("target", "_blank");
                jQuery(".tweetlist li a").attr("target", "_blank");
            })
        })
    }

    if (jQuery("#form_contato").length != 0) {
        addForm("#form_contato")
    }
    if (jQuery("#form_diretor").length != 0) {
        addForm("#form_diretor")
    }
    if (jQuery("#form_trabalhe").length != 0) {
        addForm("#form_trabalhe")
    }
    if (jQuery("#form_depoimento").length != 0) {
        addForm("#form_depoimento")
    }
    if (jQuery("#form_info").length != 0) {
        addForm("#form_info")
    }
});


String.prototype.linkify = function () {
    return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function (a) {
        return a.link(a)
    })
}; 
(function (a) {
    a.fn.smoothTabs = function (b) {
        alert('funcao');
        var c=0;var d="smoothTabsLiCurrent";var e="smoothTabsDivHidden";var f="smoothTabsDivVisible";var g=document.location.hash.substr(1,document.location.hash.length);
        if(g&&a("#"+g,this).size()){
            c=a("#"+g,this).index()
        }
        this.each(function(){
            alert('for');
            a("ul li:eq("+c+")",this).addClass(d);
            a(this).children("div").addClass(e);
            a("div:eq("+c+")",this).fadeIn(b).addClass(f).removeClass(e)
            alert(c);
        });
        a("ul li",this).click(function(){
            alert('click');
            var c=a(this).parent();
            var g=a(c).parent();
            a("li",c).removeClass(d);
            a(this).addClass(d);
            var h=a("li",c).index(this);
            var i=a("div",g).get(h);
            if(a(i).attr("class")==f){return false}
            a("."+f,g).fadeOut(b,function(){
                a(i).fadeIn(b).addClass(f).removeClass(e)
            });
            a("."+f,g).removeClass(f).addClass(e)
        })
    }
})(jQuery)


function muda_form(nome) {
    $("#txt_"+nome).hide();
    $("#div_"+nome).fadeIn();
}

function checa(obj, v) {
    if ($(obj).val() == v) {
        $(obj).val('');
    }
}

function checa2(obj, v) {
    if ($(obj).val() == '') {
        $(obj).val(v);
    }
}
