﻿(function() {
    var RakVideo = function(element, config) {
        element = $(element);
        var obj = this;
        this.config = $.extend({
            page: 0,
            pageSize: 6,
            video: element.find(".video .inner"),
            listing: element.find(".listing"),
            loadingBottom: element.find(".listing .loading"),
            ul: element.find(".listing .inner .w1 ul"),
            itemWidth: 90,
            itemMargin: 10,
            category: 2,
            type: "V"
        }, config);

        this.init = function() {
            var co = obj.config;
            co.listing.find(".inner .next-button").click(function(ev) {
                $(obj).trigger("next");
                ev.preventDefault();
            });
            co.listing.find(".inner .previous-button").click(function(ev) {
                $(obj).trigger("previous");
                ev.preventDefault();
            });
            co.ul.css({ width: ((co.page + 2) * co.pageSize) * (co.itemWidth + co.itemMargin) });
            //            obj.getDataSource(function(data) {
            //                var slug;
            //                var location = window.location.href.indexOf("#");
            //                if(location !== -1) { slug = window.location.href.substring(location); }
            //                if(slug) {
            //                    co.listing.find(".inner .w1 ul li a[href$=" + slug + "]").trigger("click");
            //                } else { obj.showVideo(data); }
            //            });
        };

        //        this.getDataSource = function(callback) {
        //            var co = obj.config;
        //            co.width = 82;
        //            co.height = 60;
        //            DS.getVideosByTypeAndCategory(co, function(data) {
        //                if(data.length > 0)
        //                {
        //                    obj.renderList(data);
        //                    if($.isFunction(callback)) { callback(data[0]); }
        //                }
        //            });
        //      };

        /*this.renderList = function(data) {
        var co = obj.config;
        var con = co.listing.find(".inner .w1");
        var ul = con.find("ul");
        var data = data;
             
        for(var i=0; i<data.length; i++) {
        var li = $("<li></li>");
        if(data[i].isLastItem) { li.addClass("last-item"); }
        li.append($("<a href=\"" + co.currentUrl + "#" + data[i].slug + "\"></a>").attr("title", data[i].title).prepend("<img src=\"" + data[i].thumbnail + "\" />").append("<span class=\"play\"></span>"));
        ul.append(li);
        co.itemWidth = li.outerWidth();
        }
        ul.find("li:first").addClass("first-item");
        ul.css({width: ((co.page + 1) * co.pageSize) * (co.itemWidth + co.itemMargin) });
             
        var remainder = data.length % co.pageSize;
        if(remainder !== 0) {
        for(var i=0; i<co.pageSize-remainder; i++) {
        var li = $("<li></li>");
        li.append("<span class=\"empty\"></span>");
        ul.append(li);
        }
        }
            
        ul.find("a").each(function(index) {
        $(this).click(function(ev) {
        var ind = ((index + 1) % 6) - 1;
        if(ind < 0) { ind = 5; }
        obj.showVideo(data[ind]);
        });
        });
        };*/

        this.scroll = function() {
            var co = obj.config;
            var scrollLength = (co.page) * ((co.pageSize * (co.itemWidth + co.itemMargin)) - 10);
            co.listing.find(".inner .w1").animate({ scrollLeft: scrollLength + (co.page * 10) }, 1000);
        };

        $(this).bind("next", function() {
            var co = obj.config;
            
            var onLastPage = (co.page * co.pageSize) + 6 == co.listing.find(".inner ul li").length;
            if (!onLastPage && co.listing.find(".inner ul li").length  > 6 ) {
                //alert(co.listing.find(".inner ul li").length);
                //alert((co.page * co.pageSize) + 7);
                obj.config.page++;
                obj.scroll();
            }
            return false;
        });

        $(this).bind("previous", function() {
            var co = obj.config;
            //if (!co.listing.find(".inner ul li:eq(" + (co.page * co.pageSize) + ")").hasClass("first-item")) {
            //   alert(obj.config.page);
            if (obj.config.page != 0) {
                obj.config.page--;
                obj.scroll();
            }
            //}
            return false;
        });

        this.showVideo = function(data) {
            var co = obj.config;
            co.video.empty();
            co.video.append(data.embedCode);
            var details = $("<div></div>").css({ "text-align": "left", "padding": "10px" });
            details.append("<h4 style=\"font-style: normal;\">" + data.title + "</h4>");
            details.append("<p>" + data.description + "</p>");
            co.video.append(details);
            DS.incrementVideoHits(data.id);
        };
    };
    $.fn.rakVideo = function(config) {
        return this.each(function() {
            var rakVideo = new RakVideo(this, config);
            rakVideo.init();
        });
    };
})(jQuery);