﻿/// <reference path="jquery.js" />

$(document).ready(function () {

    /* =======================
    Transição dos depoimentos
    ======================= */

    $('.slideshow').cycle({
        fx: 'fade',
        speed: 800,
        timeout: 20000
    });

    /* ==================
    Estilo link externo
    ================== */

    window.setTimeout(function() {

        $('#esquerda a[href^="http://"]:not([href*="' + location.hostname + '"]):not(:has(img))').attr('class', 'externo').attr('target', '_blank').attr('title', 'Abre em uma nova janela');

        $('#esquerda a[href*=upload]:not(:has(img))').attr('class', 'site-externo');
        $('#esquerda a[href*=upload]').attr('target', '_blank').attr('title', 'Abre em uma nova janela');

    }, 1000);

    /* ========
    Facebook
    ======== */

    $("#facebook").append('<iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%2FRZT-Comunica%C3%A7%C3%A3o%2F189483154925&amp;width=289&amp;colorscheme=light&amp;show_faces=true&amp;border_color&amp;stream=false&amp;header=true&amp;height=293" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:289px; height:293px;" allowTransparency="true"></iframe>')

    /* ======================
    Validação de formulários
    ====================== */

    $("form").validate({
        onsubmit: false
    });

    /* ==================
    Link externo
    ================== */

    $('a[rel=externo]').attr('target', '_blank');
    $('a[rel=externo]:not(:has(img))').addClass('externo');

    var $titleExterno;

    $('a[rel=externo]').each(
        function () {

            $titleExterno = $(this).attr('title');

            if ($titleExterno == "")
                $(this).attr('title', 'Abre em nova janela');
            else
                $(this).attr('title', $titleExterno + ' - Abre em nova janela');

        }
    );

    /* ==================
    Tamanho da fonte
    ================== */

    $('.tamanho-texto').fontSize({
        alvo: '#esquerda'
    });

    /* ==================
    Twitter
    ================== */

    $(window).load(function () {
        if ($('#twitter').length != 0) {
            $.getScript('http://widgets.twimg.com/j/2/widget.js', function () {
                $.getScript('/lib/javascript/twitter.js', function () {
                    $('#twtr-widget-1').appendTo('#twitter');
                });
            });
        }
    });

    /* ==================
    Na Mídia
    ================== */

    function carregaNaMidia(pagina) {

        $.ajax({
            type: "GET",
            url: "/includes/midia-ajax.aspx",
            /*beforeSend: function () {
            $("#lateral-midia").html("Carregando...");
            },*/
            data: pagina,
            success: function (html) {
                $("#lateral-midia").html(html);
            },
            error: function () {
                alert('Erro! Tente novamente mais tarde.');
            }
        });

    }

    $("#lateral-midia .paginacao-bolinha a").live("click",
        function (e) {
            e.preventDefault();

            var $data = $(this).attr("href").split('?')[1];

            carregaNaMidia($data);
        }
    )

    if ($("#lateral-midia").length != 0) {
        carregaNaMidia("nPag=1");
    }

    /* ==================
    Releases
    ================== */

    function carregaReleases(pagina) {

        $.ajax({
            type: "GET",
            url: "/includes/releases-ajax.aspx",
            /*beforeSend: function () {
            $("#lateral-midia").html("Carregando...");
            },*/
            data: pagina,
            success: function (html) {
                $("#lateral-releases").html(html);
            },
            error: function () {
                alert('Erro! Tente novamente mais tarde.');
            }
        });

    }

    $("#lateral-releases .paginacao-bolinha a").live("click",
        function (e) {
            e.preventDefault();

            var $data = $(this).attr("href").split('?')[1];

            carregaReleases($data);
        }
    )

    if ($("#lateral-releases").length != 0) {
        carregaReleases("nPag=1");
    }

    /* ==================================
    Validação do formulário - Newsletter
    ================================== */

    $("#txtNome").rules("add", {
        required: true,
        messages: {
            required: "Digite o nome!"
        }
    });

    $("#txtEmail").rules("add", {
        required: true,
        email: true,
        remote: {
            url: "/handlers/verifica-dados.ashx",
            type: "post",
            data: {
                usuario: function () {
                    return true;
                }
            }
        },
        messages: {
            required: "Digite o e-mail!",
            email: "Formato de e-mail inválido!",
            remote: "O e-mail digitado já está cadastrado!"
        }
    });

    $('#form-newsletter :text').keydown(function (evt) {
        if (evt.keyCode == 13) {
            evt.preventDefault();
            $('#btn-cadastrar').click();
        }
    });

    $('#btn-cadastrar').click(ValidacaoNewsletter);

    function ValidacaoNewsletter(evt) {

        var $group = $(evt.currentTarget).parents('#form-newsletter');

        var isValid = true;
        var primeiroItem = 0;

        if ($("#txtNome").val() == "Nome") { $("#txtNome").val(""); }
        if ($("#txtEmail").val() == "E-mail") { $("#txtEmail").val(""); }

        $group.find(':input').each(function (i, item) {
            if (!$(item).valid()) {
                if (primeiroItem == 0)
                    $(item).focus();
                primeiroItem = 1;
                isValid = false;
            }
        });

        evt.preventDefault();

        if (isValid) {

            var dataString = "txtNome=" + $("#txtNome").val() + "&txtEmail=" + $("#txtEmail").val();

            $.ajax({
                type: "POST",
                url: "/handlers/newsletter.ashx",
                data: dataString,
                success: function (html) {
                    $("#msg-newsletter").html(html);
                    $("#txtNome").val('Nome');
                    $("#txtEmail").val('E-mail');
                },
                error: function () {
                    alert('Erro! Tente novamente mais tarde.');
                }
            });

        }
    }

});
