﻿(function() {

    DS.getPublishedPhotos = function(config, callback) {
        DS.doAjaxPost("Data.asmx/GetPagedLatestPublishedPhotos", {
            startIndex: config.page,
            pageSize: config.pageSize
        }, callback);
    };


    var JWSGallery = function(element, config) {
        element = $(element);
        var obj = this;

        var autoScrollDirection = 1;
        var autoScrollTimer = null;


        this.config = $.extend({
            page: 0,
            pageSize: 4,
            autoScroll: true,
            autoScrollDelay: 5000,
            speed: 1000,
            listing: element.find(".jws-photos"),
            view: element.find(".view-all")
        }, config);
        var co = obj.config;

        this.init = function() {
            co.view.find("#next-button").click(function(ev) {
                $(obj).trigger("next");
                ev.preventDefault();
            });
            co.view.find("#previous-button").click(function(ev) {
                $(obj).trigger("previous");
                ev.preventDefault();
            });

            if (co.autoScroll) {
                obj.startAutoScroll();
            }
        };
        this.scroll = function() {
            var multiplier = -1;
            var itemWidth = co.listing.find("ul li:eq(0)").outerWidth();
            var scrollLength = (co.page) * itemWidth * multiplier;
            co.listing.find("ul").animate({ left: scrollLength }, { duration: co.speed });
        };



        this.goToNextPage = function(direction) {

            var co = obj.config;
            // go to previous page
            if (direction < 0) {
                if (co.page > 0) {
                    co.page--;
                    obj.scroll();

                }
            } else {
                co.photoId = null;
                var onLastPage = co.listing.find("ul li").length == Math.abs(co.page) + 1;
                if (co.listing.find("ul li.last-item").length === 0 && onLastPage) {
                    co.page++;
                    var li = $(document.createElement("li"));
                    var itemWidth = co.listing.find("ul li:eq(0)").outerWidth();
                    co.listing.find("ul").append(li).css("width", co.listing.find("ul li").length * itemWidth);
                    obj.scroll();

                    setTimeout(function() {
                    co.listing.addClass("loading");
                    
                    DS.getPublishedPhotos(co, function(data) {
                        for (var i = 0; i < data.length; i++) {
                            if (data[i].isLastItem) { li.addClass("last-item"); }
                            if (i == 0)
                                li.append("<a href=\"photo-gallery.aspx?view=" + data[i].id + "\"><img src=\"GetImage.aspx?file=" + data[i].imageUrl + "&width=87&height=63&crop=true\" style=\"padding:0px !important\"/></a>");
                            else
                                li.append("<a href=\"photo-gallery.aspx?view=" + data[i].id + "\"><img src=\"GetImage.aspx?file=" + data[i].imageUrl + "&width=87&height=63&crop=true\" /></a>");

                        }
                        co.listing.removeClass("loading");
                    });
                }, co.speed);

                
            } else {
                if (!onLastPage) {
                    co.page++;
                    obj.scroll();
                }
            }
        }
    };

    this.autoScroll = function() {
        var co = obj.config;
        var currentPage = co.page;
        obj.goToNextPage(autoScrollDirection);
        if (co.page == currentPage) {
            autoScrollDirection = -autoScrollDirection;
            obj.goToNextPage(autoScrollDirection);
        }
        obj.startAutoScroll();
    };

    this.startAutoScroll = function() {
        if (autoScrollTimer != null) {
            window.clearTimeout(autoScrollTimer);
        }
        autoScrollTimer = window.setTimeout(function() { obj.autoScroll(); }, co.autoScrollDelay);
    };

    this.stopAutoScroll = function() {
        if (autoScrollTimer != null) {
            window.clearTimeout(autoScrollTimer);
        }
        autoScrollTimer = null;
    };

    $(this).bind("next", function() {

        obj.goToNextPage(1);
        return false;
    });

    $(this).bind("previous", function() {
        obj.goToNextPage(-1);
        return false;
    });

    $(element).find('#previous-button, #next-button').hover(
            function() { obj.stopAutoScroll(); },
            function() { obj.startAutoScroll(); });

    $(element).find('li').live('mouseover', function() {
        obj.stopAutoScroll();
    });

    $(element).find('li').live('mouseout', function() {
        obj.startAutoScroll();
    });

};
$.fn.photoGallery = function(config) {
    return this.each(function() {
        var photoGallery = new JWSGallery(this, config);
        photoGallery.init();
    });
};
})(jQuery);
