﻿var currentIndex = 0;
var packageSlug;
var pageIndex = 1;
var pageSize = 10;
var itemCount = 0;

$(document).ready(function() {

    $("#feedreader a.close").click(function() {
        $('#feedreader').hide();
    });

    $("#feedpreview a.close").click(function() {
        $('#feedpreview').hide();
        $('#feedreader').hide();
        $('#addfeed').hide();
    });

    $('#scrollup').click(function() {
        if (currentIndex < 0 && itemCount != -1) {
            currentIndex += 1;
            $('#feedpreview').find('ol').animate({ top: currentIndex * 195 }, 2000, function() {
                $(this).css('top', currentIndex * 195 + 'px');
            });
        }
    }, function() {
    });

    $('#scrolldown').click(function() {
        if (itemCount != -1 && (((currentIndex + 1) * pageSize) < itemCount)) {
            currentIndex -= 1;
            $('#feedpreview').find('ol').animate({ top: currentIndex * 195 }, 2000, function() {
                $(this).css('top', currentIndex * 195 + 'px');
            });
            appendFeed();
        }
    }, function() {
    });

});

    function loadFeeds(slug, sender) {
        $('#addfeed').hide();
        $('#feedreader').hide();
        var container = $('#feedpreview');
        container.css('top', findPos(sender)[1] - 30);
        container.css('left', 100);
        container.find('div > h3').text("");
        container.find('.byline').html("");
        container.find('.pages').html("");
        container.find('.backgrounddimmer').css('display', 'block');
        // reset scroll
        $('#feedpreview').find('ol').css('top', '0px');
        currentIndex = 0;
        pageIndex = 1;
        itemCount = 0;
        container.css('display', 'block');

        $.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('.backgrounddimmer').css('display', 'none');
                container.find('div > h3').text($("string", xml).eq(0).text());
                container.find('.byline').html($("string", xml).eq(1).text());
                container.find('.pages').html($("string", xml).eq(2).text());
                itemCount = $("string", xml).eq(3).text();
                packageSlug = slug;
            }
        });
    }

    function appendFeed() {
        $('#feedreader').hide();
        var container = $('#feedpreview');
        $.ajax({ type: 'POST',
            url: $('#hdnRootUrl').attr('value') + 'services/feedservice.asmx/AppendFeedItems',
            dataType: 'xml',
            data: 'Slug=' + packageSlug + '&PageIndex=' + pageIndex + '&PageSize=' + pageSize + '&ItemCount=' + itemCount,
            processData: false,
            error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxError(XMLHttpRequest, textStatus, errorThrown); },
            success: function(xml) {
                container.find('.pages').append($("string", xml).eq(2).text());
                pageIndex++;
            }
        });        
    }

    function loadFeed(id, sender) {
        $.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', findPos(sender)[1] - 45);
                container.css('left', findPos(sender)[0] + 90);
                container.css('display', 'block');
            }
        });
    }

    function addFeed(slug, sender) {
        $('#addfeed').css('top', findPos(sender)[1]).css('left', findPos(sender)[0] - 50).css('display', 'block');
        $('#hdnFeedPackageID').attr('value', slug);
    }    

    function ajaxError(xmlObj, textStatus, errorThrown) {
        alert(errorThrown);
    }

    function findPos(obj) {
        var curleft = curtop = 0;

        if (obj.offsetParent) {
            do {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;

            } while (obj = obj.offsetParent);
        }
        return [curleft, curtop];
    }
