$(document).ready(function(){
    // click actions on albums
    $('.JS_PA_LC_NAME').click(albumClick);
    
    selectInitial();
});

function selectInitial()
{
    // see what first album is, and fetch items for it.
    lastAlbumId =     $('.JS_ALBUM_SELECTOR:first').attr('rel');
    
    albumName    =    $('.JS_ALBUM_SELECTOR:first span').html();
    
    $('.JS_PA_NAME').html(albumName);
    $('#JS_FRM_ALBUMID').val(lastAlbumId);
    
    getAlbumItems();
}

function albumClick()
{
    var albumName = $(this).html();
    var albumId      = $(this).parent("td").attr('rel');
    
    // remove selected class from other and apply to selected
    $('.album td').removeClass('sel');
    $(this).parent("td").addClass('sel');
    
    // set name and id-thingies
    $('.JS_PA_NAME').html(albumName);
    $('#JS_FRM_ALBUMID').val(albumId);
    
    lastAlbumId    =    $(this).parent("td").attr("rel");

    // load items in this album
    getAlbumItems();
}

function getAlbumItems()
{
    date = new Date();
    timestamp = date.getTime();
    
    if (lastAlbumId != null) {
        $.ajax({
          url: "/photos/ajax-get-pics/"+lastAlbumId+"/?ts="+timestamp,
          success: function(answer)
          {
            // clear the thumb area and add the newly fetched items
            $('#thumb_area_lst').html('');
            
            // walk the items and add to the thumb area
            var thumbs = answer.items;
            
            if (thumbs != undefined) {
                for (var i = 0; i < thumbs.length; i++) {
                    
                    photoPath = userFolder + "photos/" + thumbs[i].name_on_disk + "_t." + thumbs[i].extension;
                    photoPathLrg = userFolder + "photos/" + thumbs[i].name_on_disk + "." + thumbs[i].extension;
                    
                    thumbitem = "<li><a class='fancybox' rel='gallery-" + lastAlbumId + "' title='" + thumbs[i].title + "' href='" + photoPathLrg + "'><img src='" + photoPath + "' /></a>";
                    $('#thumb_area_lst').append(thumbitem);
                }
            }
            $("a[class=fancybox]").fancybox({
                'transitionIn'      : 'elastic',
                'transitionOut'     : 'elastic',
                'titlePosition'     : 'over',
                'overlayOpacity'    : '0.8',
                'overlayColor'      : '#000000',
                'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
                    return '<span id="fancybox-title-over">' + TXT_IMAGE + ' ' +  (currentIndex + 1) + ' ' +TXT_OF+ ' ' + currentArray.length + ': ' + title + '</span>';
                }
            });
          },
        error: function() { 
            $('#thumb_area').html(txtFetchingItemsFailed).css('color', 'red');
        },
        dataType: "json" });
    } else {
        $('#thumb_area').html('<img src="/images/default/jibr/icon_site_info.png" style="width: 20px; height: 20px; margin-right: 10px;" />' + txtNoAlbums).css({'background-color': 'white', 'padding':'10px'})
    }
    
    return false;
}
