var ajaxSuccess = false;
var gallery = [];
$.ajax({
	url: "fotos-"+galleryType+".xml",
	dataType: "xml",
    success: function(xml){
        var result = $(galleryType,xml).text().split(/[\r\n]/i);
        for (i in result) {
            if (result[i] != "") {
                gallery.push(result[i]); 
            }
        }
        gallery.push(null);
        ajaxSuccess = true;
    }
});

var animeSteps = 0;

var showNav = function() {
    if(animeSteps == $('#thumbNav ul').length) {
        $('#thumbNext').show();
        $('#thumbPrev').hide();
    }
    else if(animeSteps > 1 && animeSteps < $('#thumbNav ul').length) {
        $('#thumbNext').show();
        $('#thumbPrev').show();
    }
    else if(animeSteps == 1) {
        $('#thumbNext').hide();
        $('#thumbPrev').show();
    }
}

loadedImages = "";
$(document).ready(function() {
    
    if (!ajaxSuccess) {
       if ($.browser.opera) {
           setTimeout(function(){
               window.location.reload();
           }, 1500);
       }
       else {
           window.location.reload();
       }
    }
    
    $('#imgPreLoad').html('<img src="i/' + galleryType + '/foto_' + 1 + '.jpg" class="photo"/>');
    
    gallery.reverse();
    gallerySize = Math.floor(gallery.length / 2);
    
    var thumbsHTML = "<ul>";
    for (i = 1; i <= gallerySize; i++) {
        var liClass = (i % 4 == 0 && i != 0) ? ' class="break"' : '';
        thumbsHTML += '<li' + liClass + '><img src="i/' + galleryType + '/foto_' + i + '_t.jpg" photoID="' + i + '"/></li>';
        if (i % 12 == 0 && i < gallerySize) {
            thumbsHTML += '</ul><ul>';
        }
        if (i == gallerySize) {
            thumbsHTML += '</ul>';
        }
    }
    $('#thumbNav').html(thumbsHTML);
    
    
    animeSteps = $('#thumbNav ul').length;
    
    $('#thumbNext').click(function(){
        $('#thumbNav').animate({
            marginLeft: "-=320px"
        }, 1500);
        animeSteps--;
        showNav();
    });
    
    $('#thumbPrev').click(function(){
        $('#thumbNav').animate({
            marginLeft: "+=320px"
        }, 1500);
        animeSteps++;
        showNav();
    });
    
    showPhoto = function(id){
        id = id * 1;
        var info = id * 2;
        
        if (id > 1 && id < gallerySize) {
            $('#imgPreLoad').html('<img src="i/' + galleryType + '/foto_' + (id+1) + '.jpg"/><img src="i/' + galleryType + '/foto_' + (id-1) + '.jpg"/>');
           loadedImages +='#'+(id-1)+'#'+(id+1);

        }
        else if(id == 1) {
            $('#imgPreLoad').html('<img src="i/' + galleryType + '/foto_' + (id+1) + '.jpg"/>');
           loadedImages +='#'+(id+1);
        }
        else if(id == gallerySize) {
            $('#imgPreLoad').html('<img src="i/' + galleryType + '/foto_' + (id-1) + '.jpg"/>');
           loadedImages +='#'+(id-1);
        }

        
        var photoHTML = '<img src="i/' + galleryType + '/foto_' + id + '.jpg" class="photo"/>' +
                        '<p class="legend">' + gallery[info] + '</p>' +
                        '<h5>' + gallery[info - 1] + '</h5>';
        
        photoPrevHTML = (id > 1) ? '<div class="prev"><img alt="Anterior" src="i/prev-button.gif" id="photoPrev" onclick="showPhoto(' + (id - 1) + ')"/></div>' : '';
        photoNextHTML = (id < gallerySize) ? '<div class="next"><img alt="Proxima" src="i/next-button.gif" id="photoNext" onclick="showPhoto(' + (id + 1) + ')"/></div>' : '';

        if($.browser.mozilla || loadedImages.indexOf('#'+id)!=-1) {
            $('#photoPrint').html(photoHTML+photoPrevHTML+photoNextHTML);
        }
        else {
            $('#photoPrint').html(photoHTML);
            setTimeout(function() {
                $('#photoPrint').append(photoPrevHTML+photoNextHTML);
            },1000);
        }
    }
    
    $('#thumbNav img').click(function(){
        showPhoto($(this).attr("photoID"));
    });
    
    setTimeout('showPhoto(1)', 1000);
});

