﻿$(function () {

    viewFinalizarCompra();

    var isReply = false;

    if ($("#aspnetForm").attr("action").toString().indexOf("/") > 0) {
        $("#aspnetForm").attr("action", "/" + $("#aspnetForm").attr("action"));
    }

    $(".btReply").click(function (el) {
        el.preventDefault();
        var commentId = $(this).attr('id').replace('rp_', '');
        var replyNome = $(this).attr('userName');
        var replyMail = $(this).attr('userMail');

        if (replyNome == $('#ctl00_ContentPlaceHolder1_hiddenUserName').val()) {

            $('#ctl00_ContentPlaceHolder1_hiddenUserName').val('');
            $('#ctl00_ContentPlaceHolder1_hiddenUserMail').val('');

            $('#formComentario p em').html('');
            isReply = false;

        }
        else {
            $('#ctl00_ContentPlaceHolder1_hiddenUserName').val(replyNome);
            $('#ctl00_ContentPlaceHolder1_hiddenUserMail').val(replyMail);

            $('#formComentario p em').html('Comentário abaixo será em resposta a ' + replyNome);

            isReply = true;
        }

    });

    $(".btDenuncie").click(function () {

        var pId = $(this).attr('id');

        $.post("/Util/Services/denunciarComentario.aspx", { "pId": pId }, function (result) {
            if (result.message.toString() == 'ok') {
                exibirDialogo("ui-custom-bad", "Alerta!", "Comentário denunciado com sucesso.");
            }
            else
                exibirDialogo("ui-custom-bad", "Alerta!", result.message);
        }, "json");
    });

    $(".btEnviarComentario").click(function (e) {
        e.preventDefault();

        var botao = $(this);

        var txtNome = encodeURIComponent($('#ctl00_ContentPlaceHolder1_txtNome').val());
        var txtEmail = $('#ctl00_ContentPlaceHolder1_txtEmail').val();
        var txtSite = $('#ctl00_ContentPlaceHolder1_txtSite').val();
        var txtMensagem = encodeURIComponent($('#ctl00_ContentPlaceHolder1_txtMensagem').val());
        var productId = $('#ctl00_ContentPlaceHolder1_productId').val();
        var linkPagina = window.location.toString();
        var replyNome = $('#ctl00_ContentPlaceHolder1_hiddenUserName').val();
        var replyEmail = $('#ctl00_ContentPlaceHolder1_hiddenUserMail').val();

        var artistaId = $('#ctl00_ContentPlaceHolder1_artistaId').val();

        if (txtNome == '') {
            exibirDialogo("ui-custom-bad", "Alerta!", "Informe o seu nome.");
            $('#ctl00_ContentPlaceHolder1_txtNome').focus();
            return false;
        }

        if (txtEmail == '') {
            exibirDialogo("ui-custom-bad", "Alerta!", "Informe o seu email.");
            $('#ctl00_ContentPlaceHolder1_txtEmail').focus();
            return false;
        }

        if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txtEmail))) {
            exibirDialogo("ui-custom-bad", "Alerta!", "Favor informar um email válido.");
            $('#ctl00_ContentPlaceHolder1_txtEmail').focus();
            return false;
        }

        if (txtMensagem == '') {
            exibirDialogo("ui-custom-bad", "Alerta!", "Informe uma mensagem.");
            $('#ctl00_ContentPlaceHolder1_txtMensagem').focus();
            return false;
        }


        $(botao).hide();

        $.post("/Util/Services/inserirComentario.aspx", { 'txtNome': txtNome, 'txtEmail': txtEmail, 'txtSite': txtSite, 'txtMensagem': txtMensagem, 'productId': productId, 'isReply': isReply, 'linkPagina': linkPagina, 'replyNome': replyNome, 'replyEmail': replyEmail, 'artistaId': artistaId }, function (result) {
            if (result.message.toString() == 'ok') {


                $("#dialogFeedback").html('<p><span class="dialog-error"></span>Comentário adicionado com sucesso!</p>');
                $("#dialogFeedback").dialog({
                    modal: true,
                    resizable: false,
                    dialogClass: typeof (classe) != "undefined" ? classe : "",
                    width: 300,
                    height: "auto",
                    title: "Alerta!",
                    buttons: {
                        'OK': function () {
                            $(this).dialog('close');
                            window.location = window.location;
                        }
                    }
                });
                $(botao).show();

            }
            else {
                exibirDialogo("ui-custom-bad", "Erro!", result.message);
                $(botao).show();
            }

        }, "json");

    });


    $("#sharedButtons a").click(function (e) {
        e.preventDefault();
        var urlArte = window.location;


        switch ($(this).attr("title")) {
            case "Twitter":
                window.open("http://twitter.com/home/?status=Essa arte eu recomendo " + urlArte);
                break;
            case "Delicious":
                window.open("http://delicious.com/save?url=" + urlArte + "&title=..::URBAN - ARTS::..");
                break;
            case "Digg":
                window.open("http://digg.com/submit?phase=2&url=" + urlArte + "&title=..::URBAN - ARTS::..");
                break;
            case "Technorati":
                window.open("http://www.technorati.com/faves?add=" + urlArte);
                break;
            case "Google":
                window.open("http://www.google.com/bookmarks/mark?op=edit&bkmk=" + urlArte + "&title=..::URBAN - ARTS::..");
                break;
            case "Stumbleupon":
                window.open("http://www.stumbleupon.com/submit?url=" + urlArte + "&title=..::URBAN - ARTS::..");
                break;
            case "Facebook":
                window.open("http://www.facebook.com/sharer.php?u=" + urlArte + "&t=..::URBAN - ARTS::..");
                break;
            default:
                break;
        }

        pageTracker._trackEvent('share', $(this).attr("title"));

    });
});




