﻿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);
    //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 FirstLoadVideo=null;
var initializeCmd="Initialize()";
function SetChannel(oldChannel,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");
}
if(document.location.search && (document.location.search.toLowerCase().indexOf("channelid=")!=-1 || document.location.search.toLowerCase().indexOf("videoid=")!=-1)){
   var qss=document.location.search.substr(1).split("&");
   for(var i=0;i<qss.length;i++)
        if(qss[i].toLowerCase().indexOf("channelid=")==0){
                CurrentChannel=eval(qss[i].substr("channelid=".length));
        }else if(qss[i].toLowerCase().indexOf("videoid=")==0){
                initializeCmd="JS.Objects.GetVideo(function(ret){if(ret){FirstLoadVideo=ret;CurrentChannel=ret.ChannelId;}Initialize();},null,\'" + qss[i].substr("videoid=".length) + "\');";
        }
}

onLoadObj.push(initializeCmd);

function VideoListReady(objr,ctx){
    videoList=VideoLists[ctx.id];
    if(videoList && objr.List){
        videoList.Render(objr.List,objr.Total,ctx.current);
        if(ctx.ToDo)window.setTimeout(ctx.ToDo,ctx.Time?ctx.Time:20);
    }
}
var VideoLists={};
function VideoList(template, container, id, orderType, top, paging, totalRecsInPage, navigationTemplate, maxNavItems, takeCount, channel, localChannel){
     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.localChannel = !!localChannel;
     this.collection=[];
}
VideoList.prototype = {
    Search:function(searchStr,callback){
        this.search=searchStr;
        $get(this.id + "_searchStr").innerHTML=searchStr.indexOf("TAG::")==0?searchStr.substr(5):searchStr;
        this.GetList(callback);
    },
    Render:function(list,totalRecs,currentRow){
        this.collection = list;
        this.totalRecs = totalRecs;
        this.currentRow = currentRow;
	if(!this.container || !this.template) return;
        $get(this.container).innerHTML=RenderRepeater(this.template, this.collection);
        this.prepareNavigation();
    },
    GetList:function(callback){
       this.currentRow=1;
       this.ChangePage(1,callback);
    },
    ChangePage:function(page,callback){
        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)
           JS.Objects.GetVideoCollection(VideoListReady,{id:this.id, current:page, ToDo:callback},start,end,this.search,this.localChannel?this.channel:CurrentChannel,this.takeCount);
        else
           JS.Objects.GetVideoCollection(VideoListReady,{id:this.id, current:page, ToDo:callback},start,end,this.orderType,this.localChannel?this.channel:CurrentChannel,this.takeCount);
    },
    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));
            }
            else
                this.showNavDiv(1,true,String.Format(PreviousItem, currentPage - 1, this.currentRow - this.totalRecsInPage));

            if ((this.currentRow + this.totalRecsInPage) <= this.totalRecs)
                this.showNavDiv(2,true,String.Format(NextItem, currentPage + 1, this.currentRow + this.totalRecsInPage));
            else
                this.showNavDiv(2,true,String.Format(NextItem, 1, 1));    
    },
    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);
            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);
                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);
                }
                else
                    innerHTML += String.Format(NormalItem, page, i);
                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);
            }
            if ((current + this.totalRecsInPage) <= this.totalRecs)
                innerHTML += String.Format(NextItem, 0, current + this.totalRecsInPage);
            this.showNavDiv(1,true,innerHTML);
            this.showNavDiv(2,true,innerHTML);
    }
}

