﻿var MultiplyTest = {};
OrderType={
        Date : 0,
        Finalists : 5,
        MostViewed : 10,
        BestRated : 20,
        Rank : 30,
        UserName : 40,
        Title : 50,
        DisplayDate : 60,
        LastWatchedVideos : 70,
        Search : 80,
        WaitingApproval : 110,
        TopVideos : 130,
        UserVideos : 200
};
PagingType={
        None:0,
        Sliding:10,
        Navigation:20
};
function RateVideo(id,rate){
    JS.Objects.AddRate(VideoRated,null,id,rate);
}
function RefreshRate(video){
    JS.Objects.GetRateInfo(RateIsRefreshed,{obj:video},video.Id);
}
function RateIsRefreshed(obj,ctx){
    alert("RateIsRefreshed is not implemented. Implement it for your page");
}
var incrJob;
function Increment(){
    /*if (incrJob) window.clearTimeout(incrJob);
    if (currentVideo && currentVideo.InterfaceId)
        incrJob=window.setTimeout("JS.Objects.IncrementView(null,null,'" + currentVideo.Id + "',true)",30000);*/
}
function RenderVideo(videoList,index){
    ShowVideo(VideoLists[videoList].collection[index]);
}
function ShowVideo(video){
    alert("ShowVideo is not implemented. Implement it for your page");
}
var CurrentChannel = -1;
var CurrentCategory = -1;
var CurrentPlayList = -1;
var FirstLoadVideo=null;
var initializeCmd="Initialize()";
function SetChannel(oldChannel,cid){
    alert("SetChannel is not implemented. Implement it for your page");
}
function SetCategory(oldCat, cid) {
    alert("SetChannel is not implemented. Implement it for your page");
}
function Initialize() {    

}
onLoadObj.push("PrepareRepeaters()");
function PrepareRepeaters(){
    alert("PrepareRepeaters is not implemented. Implement it for your page");
}


function ExecuteNextSteps(ctx) {
    if (ctx.ToDo) {
        var todo = ctx.ToDo;
        delete ctx.ToDo;
        window.setTimeout(todo, ctx.Time ? ctx.Time : 20);
    }
}
function VideoListReady(objr,ctx){
    var videoList=VideoLists[ctx.id];
    if(videoList && objr.List){
        videoList.Render(objr, ctx.current);
        ExecuteNextSteps(ctx);
    }
}
function VideoListCollectionReady(objr, ctx) {
    var videoList = VideoLists[ctx.id];
    if (objr.List) {
        videoList.SetCollection(objr, ctx.current);
        ExecuteNextSteps(ctx);
    }
}

var VideoLists={};
function VideoList(template, container, id, orderType, top, paging, totalRecsInPage, navigationTemplate, maxNavItems, takeCount, channel, localChannel, category, localCategory, nocache){
     this.id = id;
     VideoLists[id]=this;
     this.currentRow=1;
     this.template = template;
     this.container = container;
     this.orderType = orderType;
     this.top = top;
     if(!totalRecsInPage || totalRecsInPage<0)
        this.paging=PagingType.None;
     else this.paging = paging?paging:PagingType.None;
     if(this.paging!=PagingType.None){
        this.totalRecsInPage = totalRecsInPage;
        if(paging==PagingType.Navigation)
            this.maxNavItems = maxNavItems; 
     }
     this.takeCount = !!takeCount && paging==PagingType.None;
     this.navigationTemplate = navigationTemplate;
     this.totalRecs = 0;
     this.search=null;
     this.channel = channel ? channel : -1;
     this.category = category ? category : -1;
     this.localChannel = !!localChannel;
     this.localCategory = !!localCategory;
     this.collection=[];
     this.nocache = !!nocache;
}
VideoList.prototype = {
    Search: function(searchStr, callback) {
        this.search = searchStr;
        if ($get(this.id + "_searchStr")) $get(this.id + "_searchStr").innerHTML = searchStr.indexOf("TAG::") == 0 ? searchStr.substr(5) : searchStr;
        this.GetList(callback);
    },
    SetCollection: function(collection, currentRow) {
        if (collection) {
            this.collection = collection.List;
            this.totalRecs = collection.Total;
            this.currentRow = currentRow;
        }
    },
    Render: function(collection, currentRow) {
        this.SetCollection(collection, currentRow);
        if (!this.container || !this.template) return;
        if (!$get(this.container)) alert(this.container);
        $get(this.container).innerHTML = RenderRepeater(this.template, this.collection, this.id);
        this.prepareNavigation();
    },
    Fetch: function(page, callback, type) {
        var start = this.paging != PagingType.None ? page : -1;
        var end = this.paging != PagingType.None ? page + this.totalRecsInPage - 1 : this.top;
        if (this.orderType == OrderType.Search) {
            if(!this.nocache)
                JS.Objects.GetVideoCollection(type == 1 ? VideoListReady : VideoListCollectionReady, { id: this.id, current: page, ToDo: callback }, start, end, this.search, this.localChannel ? this.channel : CurrentChannel, this.localCategory ? this.category : CurrentCategory, this.takeCount);
            else
                JS.Objects.GetVideoCollection__NoCache(type == 1 ? VideoListReady : VideoListCollectionReady, { id: this.id, current: page, ToDo: callback }, start, end, this.search, this.localChannel ? this.channel : CurrentChannel, this.localCategory ? this.category : CurrentCategory, this.takeCount);
        }
        else{
            if(!this.nocache)
                JS.Objects.GetVideoCollection(type == 1 ? VideoListReady : VideoListCollectionReady, { id: this.id, current: page, ToDo: callback }, start, end, this.orderType, this.localChannel ? this.channel : CurrentChannel, this.localCategory ? this.category : CurrentCategory, this.takeCount);
            else
                JS.Objects.GetVideoCollection__NoCache(type == 1 ? VideoListReady : VideoListCollectionReady, { id: this.id, current: page, ToDo: callback }, start, end, this.orderType, this.localChannel ? this.channel : CurrentChannel, this.localCategory ? this.category : CurrentCategory, this.takeCount);
        }
    },
    GetList: function(callback) {
        this.currentRow = 1;
        this.ChangePage(1, callback);
    },
    ChangePage: function(page, callback) {
        this.Fetch(page, callback, 1);
    },
    prepareNavigation: function() {
        switch (this.paging) {
            case PagingType.Sliding:
                this.prepareSlidingNavigation();
                break;
            case PagingType.Navigation:
                this.preparePageNavigation();
                break;
        }
    },
    showNavDiv: function(id, visible, html) {
        var navDiv = $get(this.id + "_navDiv" + id);
        if (navDiv) {
            navDiv.style.display = visible ? "block" : "none";
            if (visible) navDiv.innerHTML = html;
        }
    },
    prepareSlidingNavigation: function() {
        if (this.totalRecs <= this.totalRecsInPage) {
            this.showNavDiv(1, false);
            this.showNavDiv(2, false);
            return;
        }


        var NextItem = this.navigationTemplate.NextItem;
        var PreviousItem = this.navigationTemplate.PreviousItem;

        var currentPage = Math.ceil(this.currentRow / this.totalRecsInPage);

        if (this.currentRow == 1) {
            //Find last page
            var lastPage = Math.floor((this.totalRecs - 1) / this.totalRecsInPage);
            this.showNavDiv(1, true, String.Format(PreviousItem, lastPage, lastPage * this.totalRecsInPage + 1, this.id));
        }
        else
            this.showNavDiv(1, true, String.Format(PreviousItem, currentPage - 1, this.currentRow - this.totalRecsInPage, this.id));

        if ((this.currentRow + this.totalRecsInPage) <= this.totalRecs)
            this.showNavDiv(2, true, String.Format(NextItem, currentPage + 1, this.currentRow + this.totalRecsInPage, this.id));
        else
            this.showNavDiv(2, true, String.Format(NextItem, 1, 1, this.id));
    },
    preparePageNavigation: function() {
        if (this.totalRecs <= this.totalRecsInPage) {
            this.showNavDiv(1, false);
            this.showNavDiv(2, false);
            return;
        }

        var SelectedItem = this.navigationTemplate.SelectedItem;
        var NormalItem = this.navigationTemplate.NormalItem;
        var NextItem = this.navigationTemplate.NextItem;
        var PreviousItem = this.navigationTemplate.PreviousItem;
        var ItemSeperator = this.navigationTemplate.Seperator ? this.navigationTemplate.Seperator : "";
        var maxNavItems = this.maxNavItems;
        var currentPage = Math.ceil(this.currentRow / this.totalRecsInPage);
        var lastPage = Math.ceil(this.totalRecs / this.totalRecsInPage);
        var leftCount = Math.floor(this.maxNavItems / 2);
        var rightCount = this.maxNavItems - leftCount - 1;
        var current = this.currentRow;
        var navPage = 1;
        var page = 1;
        var startRec = 1;


        var innerHTML = "";
        if (current != 1)
            innerHTML += String.Format(PreviousItem, currentPage - 1, current - this.totalRecsInPage, this.id);
        var showAll = (this.maxNavItems + 2) * this.totalRecsInPage >= this.totalRecs;
        var showFullLeft = showAll || currentPage < leftCount + 3;
        var showFullRight = showAll || (currentPage + rightCount + 1) * this.totalRecsInPage >= this.totalRecs;

        var startSeperator = false;
        if (!showFullLeft && this.maxNavItems > 0 && (this.maxNavItems * this.totalRecsInPage <= this.totalRecs)) {
            innerHTML += String.Format(NormalItem, 1, 1, this.id);
            innerHTML += currentPage >= leftCount + 3 ? "..." : ItemSeperator;
        }
        if (!showFullLeft && currentPage > leftCount) {
            page = currentPage - leftCount;
            startRec = (page - 1) * this.totalRecsInPage + 1;
        }
        if (showFullLeft)
            maxNavItems++;
        if (showFullRight) {
            maxNavItems++;
            if (startRec != 1) {
                startRec = (lastPage - maxNavItems) * this.totalRecsInPage + 1;
                page = lastPage - maxNavItems + 1;
            }

        }

        for (var i = startRec; i <= this.totalRecs; i += this.totalRecsInPage) {
            if (startSeperator)
                innerHTML += ItemSeperator;
            else
                startSeperator = true;

            if (i == current) {
                innerHTML += String.Format(SelectedItem, page, i, this.id);
            }
            else
                innerHTML += String.Format(NormalItem, page, i, this.id);
            navPage++;
            if (!showAll && navPage > maxNavItems) break;
            page++;
        }

        if (!showFullRight && this.maxNavItems > 0 && page * this.totalRecsInPage < this.totalRecs) {
            innerHTML += (page + 1) < lastPage ? "..." : ItemSeperator;
            innerHTML += String.Format(NormalItem, lastPage, (lastPage - 1) * this.totalRecsInPage + 1, this.id);
        }
        if ((current + this.totalRecsInPage) <= this.totalRecs)
            innerHTML += String.Format(NextItem, 0, current + this.totalRecsInPage, this.id);
        this.showNavDiv(1, true, innerHTML);
        this.showNavDiv(2, true, innerHTML);
    }
}
function GetURL(video,postfix) {
    var url = video.PublishingStatus == 20 || !self.IsMainServer ? (self.AjaxRepositoryBaseUrl || AJAX_WebBaseURL) + "videos/" : "http://" + self.sysBaseServer + "/videos/" + (self.compId || self.AJAX_BaseURL.substring(self.AJAX_BaseURL.lastIndexOf("/")+1)) + "/";
    return url + video.Id + postfix;
}
function PlayListVideos(template, container, id, plId,uselocal) {
    this.id = id;
    VideoLists[id] = this;
    this.currentRow = 1;
    this.template = template;
    this.container = container;
    this.paging = PagingType.None;
    this.totalRecs = 0;
    this.collection = [];
    this.plId = plId;
    this.useLocal = !!uselocal;
    this.nocache = true;
}
PlayListVideos.prototype = new VideoList;
PlayListVideos.prototype.GetVideos = function(plId, callback) {
    if(plId)
        this.plId = plId;
    this.GetList(callback);
}
PlayListVideos.prototype.Fetch = function(page, callback, type) {
    JS.Objects.GetVideoCollection__NoCache(type == 1 ? VideoListReady : VideoListCollectionReady, { id: this.id, current: page, ToDo: callback }, this.useLocal ? this.plId : CurrentPlayList);
}


