﻿var PagerClient = new Class({

    divPager: null,
    numberPagesForShow: null,
    numberOfPages: null,
    currentPageNumber: null,
    numInPage: null,
    totalCount: null,
    ajaxType: null,
    loadingPane: null,
    loadingPaneID: null,
    pagerID: null,

    divPrev: null,
    aPrev: null,
    Prev: null,
    PrevA: null,

    divNext: null,
    aNext: null,
    Next: null,
    NextA: null,
    sortType: null,
    categoryID: null,

    initialize: function(divPagerID, numberPagesForShow, numberOfPages, pageNumber, numInPage, totalCount, ajaxType,
                         divPrev, aPrev, Prev, PrevA, divNext, aNext, Next, NextA, sortType, categoryID, loadingPaneID, pagerID) {
        this.pagerID = pagerID;
        this.divPager = $(divPagerID);
        this.numberPagesForShow = parseInt(numberPagesForShow);
        this.numberOfPages = parseInt(numberOfPages, 10);
        this.currentPageNumber = parseInt(pageNumber, 10);
        this.numInPage = numInPage;
        this.totalCount = parseInt(totalCount, 10);
        this.ajaxType = ajaxType;
        this.loadingPaneID = loadingPaneID;
        if (loadingPaneID != null && loadingPaneID != "")
            this.loadingPane = $(this.loadingPaneID);

        this.divPrev = $(divPrev);
        this.aPrev = $(aPrev);
        this.Prev = $(Prev);
        this.PrevA = $(PrevA);

        this.divNext = $(divNext);
        this.aNext = $(aNext);
        this.Next = $(Next);
        this.NextA = $(NextA);
        this.sortType = sortType;
        this.categoryID = categoryID;
    },

    loadPage: function(pageNumber, sortType, categoryID) {
        var pageData, retrieveTotalCount = false;
        if (sortType != this.sortType || categoryID != this.categoryID) {
            retrieveTotalCount = true;
        }
        this.sortType = sortType;
        this.categoryID = categoryID;
        this.currentPageNumber = parseInt(pageNumber, 10);

        var passedSortType = this.sortType;
        var temp = parseInt(this.categoryID, 10);
        if (!isNaN(temp))
            passedSortType += "_" + temp;

        if (this.ajaxType == '')
            pageData = 'method=' + page.myVideos.method + '&studioOwnerID=' + page.studioOwnerID + '&retrieveTotalCount=' + retrieveTotalCount;
        else if (this.ajaxType == "watch.comments")
            pageData = 'method=' + page.Comments.method + '&currentVideoID=' + page.currentVideoID + '&viewingUserID=' + page.viewingUserID + '&commentsThresold=' + page.commentsThresold;
        else if (this.ajaxType == "manage.thumbnails")
            page.fillThumbnails(pageNumber, this.numInPage, this.totalCount);

        if (!this.loadingPane && this.loadingPaneID != null && this.loadingPaneID != "")
            this.loadingPane = $(this.loadingPaneID);
        if (this.loadingPane)
            this.loadingPane.setStyle("display", "block");

        this.http = new Request({
            method: 'post',
            url: 'Handlers/PagingHandler.aspx',
            data: pageData + '&pageNumber=' + pageNumber + '&numInPage=' + this.numInPage + '&totalCount=' + this.totalCount + '&sortType=' + passedSortType,
            onSuccess: this.loadPaginatedObjectsSucceeded.bind(this),
            onFailure: this.loadPaginatedObjectsFailed.bind(this)
        }).send();
    },

    loadPaginatedObjectsSucceeded: function() {
        if (this.ajaxType == '') {
            page.myVideos.divPaginatedObjetsContainer.innerHTML = "";
            if (this.http.response != null && this.http.response.text != null)
                page.myVideos.divPaginatedObjetsContainer.innerHTML = this.http.response.text.stripScripts(true);
        } else if (this.ajaxType == "watch.comments") {
            var divReplyContainer = $('divReplyContainer');
            var divReply = $('divReply');

            //Empty Comment and Captcha fields
            addReply.txtText.value = "";
            addReply.txtCaptcha.value = "";
            addReply.msg.className = "text_3 text_blue";
            addReply.msg.innerHTML = "";

            divReply.injectInside(divReplyContainer);

            divReply.style.display = 'none';

            page.Comments.divPaginatedObjetsContainer.innerHTML = "";
            if (this.http.response != null && this.http.response.text != null)
                page.Comments.divPaginatedObjetsContainer.innerHTML = this.http.response.text;
        }
        
        // UGLY CODE
        
        if (!window.PAGER_CLIENT_NO_REDRAW) this.reDraw();

        if (this.loadingPane)
            this.loadingPane.setStyle("display", "none");
    },

    loadPaginatedObjectsFailed: function() {
        if (this.ajaxType == '')
            page.myVideos.divPaginatedObjetsContainer.innerHTML = "";
        else if (this.ajaxType == "watch.comments")
            page.Comments.divPaginatedObjetsContainer.innerHTML = "";

        if (this.loadingPane)
            this.loadingPane.setStyle("display", "none");
    },

    reDraw: function() {
        var startPage, showPoints = true, spanLastPage = null;
        startPage = parseInt(1, 10);
        this.divPager.innerHTML = '';
        this.divPager.style.paddingTop = "7px";
        //last pages
        if ((this.numberOfPages - this.currentPageNumber) < this.numberPagesForShow) {
            showPoints = false;

            if ((this.numberOfPages - this.numberPagesForShow + 1) > 0)
                startPage = this.numberOfPages - this.numberPagesForShow + 1;

            for (var i = startPage; i <= this.numberOfPages; i++) {
                if (i != this.currentPageNumber)
                    this.createLinkToPage(i + '', i + '').injectInside(this.divPager);
                else {
                    var spanCurrentPage;
                    spanCurrentPage = this.createSpan(i + '', null, 'text_1 text_black');
                    spanCurrentPage.injectInside(this.divPager);
                }

                if (i != this.numberOfPages) {
                    var seperatoprSpan = this.createSeperator();
                    seperatoprSpan.injectInside(this.divPager);
                }
            }
        } else {
            if ((this.currentPageNumber - parseInt(this.numberPagesForShow / 2)) > 1)
                startPage = this.currentPageNumber - parseInt(this.numberPagesForShow / 2);

            for (var i = startPage; (i <= this.numberOfPages) && (i <= (this.numberPagesForShow + startPage - 1)); i++) {
                if (i == this.numberOfPages)
                    showPoints = false;

                if (i != this.currentPageNumber)
                    this.createLinkToPage(i + '', i + '').injectInside(this.divPager);
                else {
                    var spanCurrentPage;
                    spanCurrentPage = this.createSpan(i + '', null, 'text_1 text_black');
                    spanCurrentPage.injectInside(this.divPager);
                }
            }

            spanLastPage = this.createLinkToPage(this.numberOfPages + '', this.numberOfPages + '');
        }

        if (showPoints)
            this.createSpan('...', null, 'text_1 text_link_pager').injectInside(this.divPager);

        if (spanLastPage != null)
            spanLastPage.injectInside(this.divPager);

        if (this.currentPageNumber > 1) {
            var prevPage = this.currentPageNumber - 1;
            this.divPrev.style.display = '';
            this.aPrev.setProperty('href', 'javascript:page.pagerClient_' + this.pagerID + '.loadPage(' + '\'' + prevPage + '\'' + ', ' + '\'' + this.sortType + '\'' + ');');
            this.Prev.style.display = '';
            this.PrevA.setProperty('href', 'javascript:page.pagerClient_' + this.pagerID + '.loadPage(' + '\'' + prevPage + '\'' + ', ' + '\'' + this.sortType + '\'' + ');');
        } else {
            this.divPrev.style.display = 'none';
            this.Prev.style.display = 'none';
        }


        if (this.currentPageNumber < this.numberOfPages) {
            var nextPage = this.currentPageNumber + 1;
            this.divNext.style.display = '';
            this.aNext.setProperty('href', 'javascript:page.pagerClient_' + this.pagerID + '.loadPage(' + '\'' + nextPage + '\'' + ', ' + '\'' + this.sortType + '\'' + ');');
            this.Next.style.display = '';
            this.NextA.setProperty('href', 'javascript:page.pagerClient_' + this.pagerID + '.loadPage(' + '\'' + nextPage + '\'' + ', ' + '\'' + this.sortType + '\'' + ');');
        } else {
            this.divNext.style.display = 'none';
            this.Next.style.display = 'none';
        }
    },

    createLinkToPage: function(pageNumber, innerHTML, padding) {
        var span, anchor, link;
        anchor = this.createAnchorToPage(pageNumber, innerHTML);
        span = this.createSpan('', padding, 'text_1 text_black');
        anchor.injectInside(span);
        return span;
    },

    createSpan: function(innerHTML, padding, className) {
        var span;

        if (padding == null || padding == 'undefined')
            padding = '5px';
        span = new Element('span',
                        {
                            'class': className,
                            'styles': { 'padding-left': padding }
                        });
        if (innerHTML != '')
            span.innerHTML = innerHTML;
        return span;
    },

    createSeperator: function() {
        var span;
        span = new Element('span',
                            {
                                'class': 'seperatorNumber'
                            });
        span.innerHTML = '|';

        return span;
    },

    createAnchorToPage: function(pageNumber, innerHTML) {
        var anchor = new Element('a',
                        {
                            'class': 'text_1 text_link_pager lnkOuter',
                            'styles': {},
                            'href': 'javascript:page.pagerClient_' + this.pagerID + '.loadPage(' + '\'' + pageNumber + '\'' + ', ' + '\'' + this.sortType + '\'' + ');'
                        });

        anchor.innerHTML = innerHTML;
        return anchor;
    }
});
