﻿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 OnSucceeded(result, userContext, methodName) {
    if (methodName == "pgAnterior" || methodName == "pgProxima") {
        var LI;
        if (result.length > 0) {
            $obj('fotos').innerHTML = '';
        }
        for (cc = 0; cc < result.length; cc++) {
            LI = '<div class="foto">' + 
                '<a href="mostrar_fotos.aspx?id=' + result[cc].ID + '" title="' + result[cc].Cidade + '">' + 
                '<img src="images/galerias/' + result[cc].ID + '/1.jpg?w=219&amp;h=156&amp;c=2" alt="'+ result[cc].Titulo + '" />' + 
                '</a>' + 
                '<div class="info_show_index">' + 
                '<p>' + 
                '<span>' + 
                result[cc].Data + '</span></p>' + 
                '<p>' + 
                '<a href="mostrar_fotos.aspx?id=' + result[cc].ID + '" title="' + result[cc].Cidade + '"><span>' + 
                result[cc].Cidade + ' - ' + result[cc].UF +  '</span></a></p>' + 
                '<p class="legenda_foto_index">' + 
                result[cc].Titulo + '</p>' + 
                '</div>' + 
                '</div>';
            $obj('fotos').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();
}

//window.onload = function() { CarregaContatos(); };