
$(window).ready(function () {

    // related
    getPhotoList('album',0);
    getPhotoList('user',0);

    if (window.webdephoto && window.webdephoto.origin) {
        // search results pager - previous
        if (window.webdephoto.offset > 0) {
            var prevPhoto = '&offset='+(window.webdephoto.offset-1);
            if (window.webdephoto.origin === 'search') {
                prevPhoto = prevPhoto+'&search='+window.webdephoto.search;
            }

            $.ajax({url: 'api/feed.php',
                data: 'limit=1'+prevPhoto,
                success: function(d, s) {
                    var prev = $(d).find('photo').attr('id');
                    $('#searchLinkPrev').attr('href', 'photo.php?id=' + prev + prevPhoto +
                        '&origin='+window.webdephoto.origin).css({'display': 'inline'});
                    $('#searchLinkPrev > img').attr('alt',$(d).find('name').text());
                }
            });
        }

        // search results pager - next
        var nextPhoto = '&offset='+(window.webdephoto.offset+1);
        if (window.webdephoto.origin === 'search') {
            nextPhoto = nextPhoto+'&search='+window.webdephoto.search;
        }

        $.ajax({url: 'api/feed.php',
            data: 'limit=1'+nextPhoto,
            success: function(d, s) {
                var next = $(d).find('photo').attr('id');
                if (next !== undefined) {
                    $('#searchLinkNext').attr('href','photo.php?id=' + next + nextPhoto + 
                        '&origin='+window.webdephoto.origin).css({'display': 'inline'});
                    $('#searchLinkNext > img').attr('alt',$(d).find('name').text());
                }
            }
        });
    }
});

var lim = 2;

getPhotoList = function(list, off) {
    var searchTerm;
    if (list === 'user') {
        searchTerm = '&user='+window.webdephoto.user+'&sort=albums';
    } else if (list === 'album') {
        searchTerm = '&user='+window.webdephoto.user+'&album='+window.webdephoto.album;
    } else {
        return;
    }
    var height = 100;
    var width = 100;
    var bgcolor = '000000';
    $.ajax({
        url:  'api/feed.php',
        data: 'limit='+lim+'&offset='+off+'&height='+height+'&width='+width+'&background='+bgcolor+searchTerm,
        beforeSend: function () {
            $('#status').html('<img src="images/loadingAnimation.gif" />');
        },
        success: function(data, reqStatus) {
            $('#status').html('&nbsp;');
            showListPhotos(list, data);
            updateListLinks(list, off, $(data).find('photos').attr('results'));
        }
    });
};


showListPhotos = function (list, data) {
    var items = [];
    $(data).find('photo').each(function(idx, elm) {
        var e = $(elm);
        var p = $(document.createElement("div"));
        p.attr('class', 'photo_list_item'+(idx+1));
        var a = $(document.createElement('a'));
        a.attr('href', 'photo.php?id='+e.attr('id'));
        p.append(a);
        var i = $(document.createElement('img'));
        i.attr('src',$(e.find('img')).attr('src'));
        i.attr('height',$(e.find('img')).attr('height'));
        i.attr('width',$(e.find('img')).attr('width'));
        i.attr('alt',$(e.find('img')).attr('alt'));
        a.append(i);

        items.push(p);
    });
    while (items.length < 2) {
        items.push($(document.createElement('div')).attr('class',
        'photo_list_item'+(items.length+1)).append($(document.createElement('img')).attr({
            'src': 'images/img_empty.png',
            'alt': 'empty'})));
    }
    
    l = (list === 'user' ? 'userAlbums' : 'albumPhotos');
    $('#'+l).html('');
    $(items).each(function(i,e) {
        $('#'+l).append(e);
    });
};


updateListLinks = function (list, off, tot) {
    $('#'+list+'LinkNext').unbind('click');
    $('#'+list+'LinkPrev').unbind('click');
    
    if (off > 0) {
        $('#'+list+'LinkPrev').replaceWith('<a href="#" id="' + list +
                'LinkPrev"><img src="images/list_prev.png" alt="previous" /></a>');
        $('#'+list+'LinkPrev').click(function() {
            getPhotoList(list, off-1);
            return false;
        });
    } else {
        $('#'+list+'LinkPrev').replaceWith('<img id="' + list +
                'LinkPrev" src="images/list_prev_neg.png" alt="disabled" />');
    }

    if (off + lim > tot) {
        $('#'+list+'LinkNext').replaceWith('<img id="' + list +
                'LinkNext" src="images/list_next_neg.png" alt="disabled" />');
    } else {
        $('#'+list+'LinkNext').replaceWith('<a href="#" id="' + list +
                'LinkNext"><img src="images/list_next.png" alt="next" /></a>');
        $('#'+list+'LinkNext').click(function() {
            getPhotoList(list, off+1);
            return false;
        });
    }
};

