﻿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 TFotos() {
    PageMethods.TFotos(OnSucceeded, OnFailed);
}

function enviaRecado() {
    if ($obj('txtRecado').value != '') {
        PageMethods.enviaRecado($obj('txtRecado').value, OnSucceeded, OnFailed);
    } else {
        alert('Você não escreveu nenhum recado');
    }
}

function OnSucceeded(result, userContext, methodName) {
    if (methodName == "pgAnterior" || methodName == "pgProxima") {
        var item;
        if (result.length > 0) {
            $obj('fotos_internauta').innerHTML = '';
        }
        for (cc = 0; cc < result.length; cc++) {
            item = '<div class="foto_internauta">' +
                '<a rel="lightbox-foto" onclick="$(this).lightbox({start:true,events:false}); return false;" href="images/fotosfa/' + result[cc].ID + '.jpg?w=640&h=480&c=1" title="' + result[cc].Cidade + '">' +
                '<img src="images/fotosfa/' + result[cc].ID + '.jpg?w=219&h=156&c=2" alt="' + result[cc].Evento + '" /></a>' +
                '<div class="info_foto_internauta">' +
                '<p>' +
                '<span>' + result[cc].Data + ' - ' + result[cc].Cidade + '-' + result[cc].UF + '</span></p>' +
                '<p>' +
                result[cc].Descricao +
                '</p>' +
                '</div>' +
                '</div>';
            $obj('fotos_internauta').innerHTML += item;
        }
    }
    if (methodName == "enviaRecado") {
        $obj('txtRecado').value = '';
        alert('Recado enviado com sucesso. Antes de ser liberado será analisado por nossa equipe.');
    }
}

function OnFailed(error, userContext, methodName) {
    if (methodName == "pgAnterior" || methodName == "pgProxima" ||
        methodName == "enviaRecado") {
        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(); MSG(); };

function CarregaINI() {
    PAGINA--;
    pgProxima();
}

function pageLoad() {
    INI();
    CarregaINI();
    MSG();
    try {
        CIDADE();
    } catch (e) { }
}

//window.onload = function() { CarregaContatos(); };