﻿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++) {
            var localizacao = result[cc].Cidade + '-' + result[cc].UF;
            var agenda_class = '';
            var has_local = true;
            var como_chegar = detalhes = 'mostrar_show.aspx?id=' + result[cc].ID;
            
            if  (result[cc].Data == '06/12') {
                result[cc].Data = '06 a 10/12';
                localizacao = 'Ensaio Gravação do DVD';
                result[cc].Evento = 'Ensaio Fechado';
                result[cc].Horario = '';
                como_chegar = detalhes = has_local = false;
            }
            else if (result[cc].Data == '14/12') {
                result[cc].Data = '14 a 17/12';
                localizacao = 'Cruzeiro Luan Santana';
                result[cc].Evento = 'Embarque/Desembarque em Santos';
                result[cc].Horario = '';
                como_chegar = has_local = false;
                detalhes = 'http://www.olympiacruzeiros.com.br/luansantana/';
            }
            /*else if (result[cc].Data == '18/12'){
                localizacao = 'Show Corporativo';
                result[cc].Evento = 'Evento Corporativo Fechado';
                result[cc].Horario = '';
                result[cc].Local = 'Rio de Janeiro/RJ';
                detalhes = como_chegar = false;
            }*/
            else if (result[cc].Data == '20/12') {
                result[cc].Data = '20/12/10 a 13/01/11';
                localizacao = 'Férias';
                result[cc].Horario = '';
                agenda_class = 'duplo';
                como_chegar = detalhes = has_local = false;
            }
            
            LI = '<div class="agenda' + trim(result[cc].Seleciona) + '">' +
                '<div class="data_agenda ' + trim(result[cc].Seleciona) + agenda_class + '">' +
                '<p>' +
                result[cc].Data + '</p>' +
                '<p>' +
                '<span>' + result[cc].Horario + '</span></p>' +
                '</div>' +
                '<div class="info_agenda' + trim(result[cc].Seleciona) + '">' +
                '<h3>' + localizacao + '</h3>' +
                '<p>' +
                '<span>' + result[cc].Evento + '</span></p>' +
                '<p>';
                if (has_local)
                {
                    LI += '<span>Local:</span> ' + result[cc].Local + '</p>';
                }
                 LI += '</div>' +
                '<div class="mais_agenda ' + trim(result[cc].Seleciona) + '">';
                if (como_chegar != false) {
                    LI += '<p><a href="'+ como_chegar + '" title="Como Chegar">como chegar</a> <span>+</span></p>';
                }
                if (detalhes != false) {
                    LI += '<p><a href="' + detalhes + '" 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) { }
}
