﻿var timerID = null;
var timerRunning = false;

function showDialogRetorno() {
    $("#divPopUp").show();

    $(".headerPopup a.btClose").unbind("click").bind("click", function (e) {
        e.preventDefault();
        window.location = 'http://www.urbanarts.com.br';
    });
}



function stopclock() {
    if (timerRunning)
        clearTimeout(timerID)

    timerRunning = false;
}

function startclock() {
    stopclock();
    showtime();
    setDate();
}

function carregaMenu() {
    var url = String(window.location);
    var url2 = url.split("/");

    if (url2.length < 3)
        return;
        
    if (url2[3].indexOf(".aspx") == -1) {
        url2 = "box" + url2[3];
        $("#"+url2).show();
    }
}

function showtime() {
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var timeValue = "" + hours;
    timeValue += ((minutes < 10) ? ":0" : ":") + minutes;

    $("#hora").html(timeValue);

    timerID = setTimeout("showtime()", 1000);
    timerRunning = true;
}

function paginar(pg) {
    $('#pg').val(pg);
    document.forms['aspnetForm'].action = document.location;
    document.forms['aspnetForm'].method = 'POST';
    document.forms['aspnetForm'].submit();
}

function setDate() {
    data = new Date();
    dia = data.getDate();
    mes = data.getMonth() + 1;

    if (dia < 10)
        dia = "0" + dia;
    if (mes < 10)
        mes = "0" + mes;

    $("#date").html("São Paulo, " + dia + "/" + mes);
}


function validarLogin() {
    if ($('#ctl00_usuarioLogin').val().trim() == '') {
        Sexy.alert("<h1>Alerta</h1><p>Informe o login.</a></p>");
        $('#ctl00_usuarioLogin').focus();
        return false;
    }

    if ($('#ctl00_senhaLogin').val().trim() == '') {
        Sexy.alert("<h1>Alerta</h1><p>Informe a senha.</a></p>");
        $('#ctl00_senhaLogin').focus();
        return false;
    }

    var serviceArtista = new ServiceArtista();
    var ret = serviceArtista.validar($('#ctl00_usuarioLogin').val(), $('#ctl00_senhaLogin').val(), true);

    if (ret == "") {
        Sexy.alert("<h1>Alerta</h1><p>Login e/ou senha inválidos.</a></p>");
        $('#ctl00_usuarioLogin').focus();
        return false;
    }

    serviceArtista = null;

    document.location = '/Artistas/' + ret;

    return false;
}

function alterarArtista() {
    for (var i = 0; i < $('#ctl00_ContentPlaceHolder1_hiddenTotalIdiomas').val(); i++) {

        var tabId = '#ctl00_ContentPlaceHolder1_TabIdiomas_ctl' + ((i < 10) ? '0' + i : i);
        var ctlId = tabId + '_txtDescricao_' + i;
        var descricao = $(ctlId).val();

        if (descricao == '') {
            Sexy.alert('É necessário que o artista possua descrição em todos os idiomas.');
            $(ctlId).focus();
            return false;
        }
    }
    var artistaNome = $('#ctl00_ContentPlaceHolder1_artistTitle').html();
    document.forms['aspnetForm'].action = "/Artistas/Detail.aspx?i=" + artistaNome.replace(" ", "");
    document.forms['aspnetForm'].method = 'POST';
    document.forms['aspnetForm'].submit();
}


function validarBusca() {
    if ($('#ctl00_txtBusca').val() == '') {
        exibirDialogo("", "Alerta!", "Informe o que deseja buscar."); 
        $('#ctl00_txtBusca').focus();
        return false;
    }

    document.location = '/busca.aspx?q=' + $('#ctl00_txtBusca').val();
    return false;
}


function isArray(obj) {
    //returns true is it is an array
    if (obj.constructor.toString().indexOf("Array") == -1)
        return false;
    else
        return true;
}

function ampliarConcurso(urls, description, permalink) {
    //var compartilheBts = "<a id=\"\" title=\"Twitter\" target=\"_blank\" href=\"http://twitter.com/home/?status=http://www.urbanarts.com.br/Concurso/" + permalink + "\"><img alt=\"Twitter\" src=\"/Util/Img/twitter.jpg\"/></a>";
    //compartilheBts += "<a id=\"\" title=\"Facebook\" target=\"_blank\" href=\"http://www.facebook.com/share.php?u=http://www.urbanarts.com.br/Concurso/" + permalink + "\"><img alt=\"Facebook\" src=\"/Util/Img/facebook.jpg\"/></a>";

    //Slimbox.open(urls, "Compartilhe a arte \"" + description + "\": " + compartilheBts + "<br /><i> http://www.urbanarts.com.br/Concurso/" + permalink + "</i>", 0);
}

function ampliar(urls) {
    if (urls.constructor.toString().indexOf("Array") == -1) {
        $.slimbox(urls, 0);

    }
    else {
        var images = [];
        for (i = 0; i < urls.length; i++) {
            images.push([urls[i]]);
        }
        $.slimbox(images, 0);
        //Slimbox.open(images, 0);
    }


}

function viewFinalizarCompra() {

    if ($('#finalizarCompra').lenght == 0) return false;

    var total = Carrinho.getTotal();
    if (total <= 0)
        $('#finalizarCompra').hide();
    else
        $('#finalizarCompra').show();
}

function numeros(e) {
    var tecla = (window.event) ? event.keyCode : e.which;

    if ((tecla > 47 && tecla < 58 || tecla > 96 && tecla < 105))
        return true;
    else {
        if (tecla != 8 && tecla != 0)
            return false;
        else
            return true;
    }
}

function setFocusCEP(value) {

    var size = $('#ctl00_ContentPlaceHolder1_txtCEP1').value;

    if (size.length == 5 && value) {
        $('#ctl00_ContentPlaceHolder1_txtCEP2').focus();
        return false;
    }
    else
        return true;

}

function Tab(element, event) {

    var ret = numeros(event);

    if (!ret) return false;

    if (element.value.length == 5) {
        $('#ctl00_ContentPlaceHolder1_txtCEP2').focus();
    }
}


function apreciarProduto(pId) {

    $.post("/Util/Services/apreciarProduto.aspx", { pId: pId }, function (result) {
        
        if (result.message.toString() == 'ok') {
            exibirDialogo("", "Alerta!", "Produto apreciado com sucesso.");
            $("#" + pId).html(parseInt($("#" + pId).html()) + 1);
        }
        else
            exibirDialogo("", "Erro!", result.message);

    },"json");
}

function alternateBanner() {
    var indice = $("#bannerDestaques ul li.current").attr("id");
    var indiceAux = 0;

    if ($("#bannerDestaques ul li").length > indice) {
        indiceAux = parseInt(indice) + 1;
    }
    else {
        indiceAux = 1;
    }

    $("#imgDestaques").attr('src', path + $("#" + indiceAux + " a").attr('id')).unbind("load").load(function() {
        $(this).hide().fadeIn();
        $("#bannerDestaques ul li").removeClass("current");

            $("#" + indiceAux).attr('class', 'current');
            $(this).parent().attr('href', $("#" + indiceAux).attr('url'));
                

    });
}

function exibirDialogo(classe, titulo, texto) {
    $("#dialogFeedback").html('<p><span class="dialog-error"></span>' +  texto + '</p>');    
    $("#dialogFeedback").dialog({
            modal: true,
            resizable: false,
            dialogClass: typeof (classe) != "undefined" ? classe : "",
            width: 300,
            height: "auto",
            title: titulo,
            buttons: {
                'OK': function () {
                    $(this).dialog('close');
                }
            }
        });

}

function exibirDialogoCarrinho(classe, titulo, texto) {
    $("#dialogFeedback").html('<p><span class="dialog-error"></span>' +  texto + '</p>');    
    $("#dialogFeedback").dialog({
        modal: true,
        resizable: false,
        dialogClass: typeof (classe) != "undefined" ? classe : "",
        width: 300,
        height: "auto",
        title: titulo,
        buttons: {
            'OK': function () {
                $(this).dialog('close');
            },
            'CARRINHO': function () {
                window.location = "/carrinho.aspx";
            }
        }
    });
}
