﻿function $obj(id) {
    return document.getElementById(id);
}

Array.prototype.contains = function(element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
}

Array.prototype.posicao = function(element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return i;
        }
    }
    return false;
}

var PAGINA = 0;
var TPAGINA = 0;
function pgAnterior() {
    if (PAGINA > 0) {
        PAGINA--;
        PageMethods.pgAnterior(PAGINA, OnSucceeded, OnFailed);
    }
}

function pgProxima() {
    if (PAGINA < TPAGINA - 1) {
        PAGINA++;
        PageMethods.pgProxima(PAGINA, OnSucceeded, OnFailed);
    }
}

function trim(str){ return str.replace(/^\s+|\s+$/g, ""); }

function OnSucceeded(result, userContext, methodName) {
    if (methodName == "pgAnterior" || methodName == "pgProxima") {
        var LI;
        if (result.length > 0) {
            $obj('container_agenda').innerHTML = '';
        }
        for (cc = 0; cc < result.length; cc++) {
            LI = '<div class="agenda' + trim(result[cc].Seleciona) + '">' +
                '<div class="data_agenda' + trim(result[cc].Seleciona) + '">' +
                '<p>' +
                result[cc].Data + '</p>' +
                '<p>' +
                '<span>' + result[cc].Horario + '</span></p>' +
                '</div>' +
                '<div class="info_agenda' + trim(result[cc].Seleciona) + '">' +
                '<h3>' +
                result[cc].Cidade + '-' + result[cc].UF + '</h3>' +
                '<p>' +
                '<span>' + result[cc].Evento + '</span></p>' +
                '<p>' +
                '<span>Local:</span> ' + result[cc].Local + '</p>' +
                '</div>' +
                '<div class="mais_agenda' + trim(result[cc].Seleciona) + '">' +
                '<p>' +
                '<a href="mostrar_show.aspx?id=' + result[cc].ID + '" title="Como Chegar">como chegar</a> <span>+</span></p>' +
                '<p>' +
                '<a href="mostrar_show.aspx?id=' + result[cc].ID + '" title="Detalhes do Evento">detalhes do evento</a> <span>+</span></p>';
            if (result[cc].Link != '') {
                LI += '<p>' +
                '<a href="' + result[cc].Link + '" title="Comprar Ingresso" target="_blank">comprar ingresso</a> <span>+</span></p>';
            }
                if (result[cc].IDGaleria > 0) {
                    LI +='<p class="' + (result[cc].Seleciona > 0) ? '' : 'ocultaItem' + '">' +
                    '<a href="mostrar_fotos.aspx?id=' + result[cc].IDGaleria + '" title="Fotos do Show">fotos do show</a> <span>+</span></p>';
                }
                LI +='</div>' +
                '</div>';
            $obj('container_agenda').innerHTML += LI;
        }
    }
}

function OnFailed(error, userContext, methodName) {
    if (methodName == "pgAnterior" || methodName == "pgProxima") {
        alert(error.get_message());
    }
}

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
    } else if (obj.attachEvent) {
        obj.attachEvent("on" + evType, fn);
    } else {
        window.onload = fn;
    }
}
//addEvent(window, 'load', INI);
//addEvent(window, 'load', CarregaINI);
//window.onload = function() { INI(); CarregaINI(); };

function CarregaINI() {
    PAGINA--;
    pgProxima();
}

function pageLoad() {
    INI();
    CarregaINI();
    try {
        CIDADE();
    } catch (e) { }
}