﻿var pageSize = 5;

$(function() {
    $("#feedreader a.close").click(function() {
        $('#feedreader').hide();
    });
});

function previous(slug, pageIndex, containerid, itemCount) {
    var container = $(containerid);
    var nextAnchor = container.find('.next');
    var previousAnchor = container.find('.prev');

    if (pageIndex >= 0) {

        displayWaiting(1, container);

        // hide reader
        $('#feedreader').hide();

        // replace feed
        replaceFeed(containerid, slug, pageIndex, itemCount);

        if (pageIndex == 0) {
            previousAnchor.unbind();
            previousAnchor.attr('disabled', '-1');
            previousAnchor.removeAttr('href');
        } else {
            // register events
            previousAnchor.unbind();
            previousAnchor.click(function() {
                previous(slug, --pageIndex, containerid, itemCount);
                return false;
            });
        }
        // register events
        nextAnchor.unbind();
        nextAnchor.removeAttr('disabled');
        previousAnchor.attr('href', '#');
        nextAnchor.click(function() {
            next(slug, ++pageIndex, containerid, itemCount);
            return false;
        });
        displayWaiting(0, container);
    }    
}

function next(slug, pageIndex, containerid, itemCount) {
    var container = $(containerid);
    var nextAnchor = container.find('.next');
    var previousAnchor = container.find('.prev');

    if (((pageIndex) * pageSize) < itemCount) {

        // hide reader
        $('#feedreader').hide();

        displayWaiting(1, container);

        // replace feed
        replaceFeed(containerid, slug, pageIndex, itemCount);

        // register events
        nextAnchor.unbind();

        if (itemCount > pageSize * pageIndex) {
            nextAnchor.click(function() {
                next(slug, ++pageIndex, containerid, itemCount);
                return false;
            });
        } else {
            nextAnchor.attr('disabled', '-1');
            nextAnchor.removeAttr('href');
        }

        // enable anchor
        previousAnchor.removeAttr('disabled');
        previousAnchor.attr('href', '#');

        previousAnchor.unbind();
        previousAnchor.click(function() {
            previous(slug, --pageIndex, containerid, itemCount);
            return false;
        });
        displayWaiting(0, container);
    } else {
        nextAnchor.unbind();
        nextAnchor.attr('disabled', '-1');
        nextAnchor.removeAttr('href');
    }
}

function displayWaiting(visibiliy, container) {

    if (visibiliy == 1) {
        container.find('.fploader').css('display', 'block');
        container.find('.pager').css('display', 'none');
        container.find('.pages').css('display', 'none');
    } else {
        container.find('.fploader').css('display', 'none');
        container.find('.pager').css('display', 'block');
        container.find('.pages').css('display', 'block');
    }
}

function loadFeeds(slug, containerid) {
    var container = $(containerid);
    var itemCount;
    container.find('.fploader').css('display', 'block');
    container.find('.pager').css('display', 'none');

    $.ajax({ type: 'POST',
        url: $('#hdnRootUrl').attr('value') + 'services/feedservice.asmx/GetFeedItems',
        dataType: 'xml',
        data: 'Slug=' + slug + "&PageSize=" + pageSize,
        processData: false,
        error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxError(XMLHttpRequest, textStatus, errorThrown); },
        success: function(xml) {
            container.find('.fploader').css('display', 'none');
            container.find('.pager').css('display', 'block');
            container.find('.pages').html($("string", xml).eq(2).text());

            itemCount = $("string", xml).eq(3).text();

            if (itemCount > pageSize) {
                container.find('.next').attr('href', '#');
                container.find('.next').click(function() {
                    next(slug, 1, containerid, $("string", xml).eq(3).text());
                    return false;
                });
            } else {
                container.find('.next').removeAttr('href');
                container.find('.next').attr('disabled', '-1');
            }
            container.find('.prev').removeAttr('href');
            container.find('.prev').attr('disabled', '-1');
        }
    });
}

function replaceFeed(containerid, slug, pageIndex, itemCount) {
    var container = $(containerid);

    $.ajax({ type: 'POST',
        url: $('#hdnRootUrl').attr('value') + 'services/feedservice.asmx/AppendFeedItems',
        dataType: 'xml',
        data: 'Slug=' + slug + '&PageIndex=' + pageIndex + '&PageSize=' + pageSize + '&ItemCount=' + itemCount,
        processData: false,
        error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxError(XMLHttpRequest, textStatus, errorThrown); },
        success: function(xml) {
            container.find('.pages').html($("string", xml).eq(2).text());
        }
    });
}

function loadFeed(id, sender) {

    var offset = $(sender).offset();
    
    $.ajax({ type: 'POST',
        url: $('#hdnRootUrl').attr('value') + 'services/feedservice.asmx/GetFeedItem',
        dataType: 'xml',
        data: 'ItemID=' + id,
        processData: true,
        error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxError(XMLHttpRequest, textStatus, errorThrown); },
        success: function(xml) {
            var container = $('#feedreader');
            var contents = $('#right').children();
            contents.eq(0).html($("string", xml).eq(0).text());
            contents.eq(1).html($("string", xml).eq(1).text());
            contents.eq(2).text($("string", xml).eq(2).text());
            container.css('top', offset.top - 40);
            container.css('left', offset.left + 100);
            container.css('display', 'block');
            container.css('position', 'absolute');
        }
    });
}

function ajaxError(xmlObj, textStatus, errorThrown) {
}