if (!SAPO.Widget || typeof(SAPO.Widget) == 'undefined') {
    SAPO.namespace('Widget');
}
SAPO.Widget.NavVideos2 = function(element, dataObject, options) {
    this.init(element, dataObject, options);
};
SAPO.Widget.NavVideos2.count = 0;
SAPO.Widget.NavVideos2.oldLocation = document.location.hash;
SAPO.Widget.NavVideos2.elements = [];
SAPO.Widget.NavVideos2.refresh = function() {
    if (SAPO.Widget.NavVideos2.oldLocation != document.location.hash) {
        SAPO.Widget.NavVideos2.oldLocation = document.location.hash;
        var called = false;
        for (var i in SAPO.Widget.NavVideos2.elements) {
            if (typeof(SAPO.Widget.NavVideos2.elements[i]) == 'function') {
                continue;
            }
            SAPO.Widget.NavVideos2.elements[i].initialized = false;
            SAPO.Widget.NavVideos2.elements[i].initLoad();
            if (!called && SAPO.Widget.NavVideos2.elements[i].options.callback) {
                eval(SAPO.Widget.NavVideos2.elements[i].options.callback + '()');
                called = true;
            }
        }
    }
}
SAPO.Widget.NavVideos2.prototype = {
    init: function(element, dataObject) {
        SAPO.Widget.NavVideos2.count++;
        if (SAPO.Widget.NavVideos2.count == 1) {
            window.setInterval('SAPO.Widget.NavVideos2.refresh()', 600);
        }
        var options = Object.extend({
            lines: 2,
            columns: 3,
            jump: false,
            navLeft: false,
            navRight: false,
            template: false,
            callback: false,
            pagesDiv: false,
            pagesDesc: false,
            ajaxData: false,
            countTotal: false,
            initOnDemand: false,
            bullet: false
        }, arguments[2] || {});
        try {
            this.idName = 'nav' + SAPO.Widget.NavVideos2.count;
            this.options = options;
            this.countTotalVerified = false;
            this.ajaxCache = [];
            this.element = document.getElementById(element);
            if (this.options.navLeft) {
                this.elmLeft = document.getElementById(this.options.navLeft);
            }
            if (this.options.navRight) {
                this.elmRight = document.getElementById(this.options.navRight);
            }
            this.dataObject = dataObject;
            if (this.options.template) {
                this.templating = new SAPO.Utility.Templating();
            }
            this.currentPage = 0;
            this.totalPerPage = Number(this.options.lines) * Number(this.options.columns);
            if (!this.options.jump) {
                this.options.jump = Number(this.totalPerPage);
            }
            if (typeof(this.options.pagesDiv) == 'string') {
                this.options.pagesDiv = new Array(this.options.pagesDiv);
            }
            if (typeof(this.options.pagesDesc) == 'string') {
                this.options.pagesDesc = new Array(this.options.pagesDesc);
            }
            if (this.options.pagesDiv) {
                this.pagesDiv = new Array(this.options.pagesDiv.length);
                for (var i = 0; i < this.options.pagesDiv.length; i++) {
                    this.pagesDiv[i] = document.getElementById(this.options.pagesDiv[i]);
                }
            } else {
                this.pagesDiv = false;
            }
            if (this.options.pagesDesc) {
                this.pagesDesc = new Array(this.options.pagesDesc.length);
                for (var i = 0; i < this.options.pagesDesc.length; i++) {
                    this.pagesDesc[i] = document.getElementById(this.options.pagesDesc[i]);
                }
            } else {
                this.pagesDesc = false;
            }
            if (this.options.bullet) {
                this.bullet = document.getElementById(this.options.bullet);
            }
            this.setButtonEvents();
            this.initialized = false;
            if (!this.options.initOnDemand) {
                this.initLoad();
            }
            SAPO.Widget.NavVideos2.elements[this.idName] = this;
        } catch (e) {}
    },
    initLoad: function() {
        if (this.initialized) return;
        this.initialized = true;
        this.currentPage = 0;
        var dados = document.location.hash.substring(1);
        if (dados) {
            dados = this.parse_str(dados);
            for (var i in dados) {
                if (i == this.idName) {
                    this.currentPage = (parseInt(dados[i]) - 1) * this.totalPerPage;
                    if (this.currentPage < 0) {
                        this.currentPage = 0;
                    }
                }
            }
        }
        this.verifyAjax(this.currentPage / this.totalPerPage);
        this.writeTemplate(this.currentPage);
    },
    parse_str: function(str, array) {
        var glue1 = '=';
        var glue2 = '&';
        var array2 = str.split(glue2);
        var array3 = [];
        for (var x = 0; x < array2.length; x++) {
            var tmp = array2[x].split(glue1);
            array3[unescape(tmp[0])] = unescape(tmp[1]).replace(/[+]/g, ' ');
        }
        if (array) {
            array = array3;
        } else {
            return array3;
        }
    },
    setButtonEvents: function() {
        if (this.elmLeft) {
            Event.observe(this.elmLeft, 'click', this.goLeft.bindAsEventListener(this));
        }
        if (this.elmRight) {
            Event.observe(this.elmRight, 'click', this.goRight.bindAsEventListener(this));
        }
    },
    writeTemplate: function(curPage) {
        this.clearElement();
        this.currentPage = curPage;
        var start = curPage;
        var end = curPage + this.totalPerPage;
        if (this.options.ajaxData) {
            end = this.dataObject.length;
        }
        if (end > this.dataObject.length) {
            end = this.dataObject.length;
        }
        if (curPage == 0 && this.elmLeft) {
            this.elmLeft.style.visibility = 'hidden';
        } else if (this.elmLeft) {
            this.elmLeft.style.visibility = 'visible';
        }
        if (this.options.ajaxData) {
            if ((Math.ceil(curPage / this.totalPerPage) == Math.ceil(this.options.countTotal / this.totalPerPage) - 1 || this.options.countTotal == 0) && this.elmRight) {
                this.elmRight.style.visibility = 'hidden';
            } else if (this.elmRight) {
                this.elmRight.style.visibility = 'visible';
            }
        } else {
            if (end == this.dataObject.length && this.elmRight) {
                this.elmRight.style.visibility = 'hidden';
            } else if (this.elmRight) {
                this.elmRight.style.visibility = 'visible';
            }
        }
        var next = curPage;
        if (this.options.ajaxData) {
            next = 0;
        }
        var source = false;
        do {
            var source = this.templating.gen(this.options.template, this.dataObject[next]);
            if (source) {
                this.element.innerHTML += source;
            }
            next++;
        } while (next < end);
        this.writePagination();
        this.writeBullets();
    },
    goLeft: function(e) {
        this.currentPage -= this.options.jump;
        if (this.currentPage >= 0) {
            this.verifyAjax(this.currentPage / this.totalPerPage);
            this.writeTemplate(this.currentPage);
            if (this.options.callback) {
                eval(this.options.callback + '()');
            }
        } else {
            this.currentPage += this.options.jump;
        }
        this.modifyHash();
    },
    goRight: function(e) {
        var numPages = 0;
        if (this.options.ajaxData) {
            numPages = Math.ceil(this.options.countTotal / this.totalPerPage);
        } else {
            numPages = Math.ceil(this.dataObject.length / this.totalPerPage);
        }
        this.currentPage += this.options.jump;
        if (this.currentPage < (this.totalPerPage * numPages)) {
            this.verifyAjax(this.currentPage / this.totalPerPage);
            this.writeTemplate(this.currentPage);
            if (this.options.callback) {
                eval(this.options.callback + '()');
            }
        } else {
            this.currentPage -= this.options.jump;
        }
        this.modifyHash();
    },
    goPage: function(e) {
        var link = Event.findElement(e, 'a');
        var page = parseInt(link.href.substr(link.href.lastIndexOf('#') + 1));
        this.currentPage = (page - 1) * this.totalPerPage;
        this.verifyAjax(page - 1);
        this.writeTemplate(this.currentPage);
        if (this.options.callback) {
            eval(this.options.callback + '()');
        }
        this.modifyHash();
    },
    showPage: function(page) {
        this.currentPage = (page - 1) * this.totalPerPage;
        this.verifyAjax(page - 1);
        this.writeTemplate(this.currentPage);
        this.modifyHash();
    },
    modifyHash: function() {
        var page = (this.currentPage / this.totalPerPage) + 1;
        var dados = document.location.hash.substring(1);
        if (dados) {
            dados = this.parse_str(dados);
        } else {
            dados = [];
        }
        dados[this.idName] = page;
        var ret = '';
        for (var i in dados) {
            if (typeof(dados[i]) == 'function' || dados[i] == 'undefined') {
                continue;
            }
            if (ret == '') {
                ret += '#';
            } else {
                ret += '&';
            }
            ret += i + '=' + dados[i];
        }
        SAPO.Widget.NavVideos2.oldLocation = document.location.hash = ret;
    },
    verifyAjax: function(page) {
        if (this.options.ajaxData) {
            if (this.ajaxCache[page]) {
                eval(this.ajaxCache[page]);
                this.dataObject = dados_videos;
                if (this.countTotalVerified) {
                    this.options.countTotal = this.countTotalVerified;
                }
            } else {
                new Ajax.Request(this.options.ajaxData, {
                    parameters: 'rd=' + Math.random() + '&page=' + page,
                    method: 'get',
                    asynchronous: false,
                    onSuccess: this.verifyAjaxSuccess.bind(this)
                });
            }
        }
    },
    verifyAjaxSuccess: function(req) {
        eval(req.responseText);
        this.ajaxCache[this.currentPage / this.totalPerPage] = req.responseText;
        this.dataObject = dados_videos;
        if (this.dataObject.length == this.options.countTotal) {
            this.options.ajaxData = false;
        }
        if (this.countTotalVerified) {
            if (this.countTotalVerified < this.currentPage + dados_videos.length) {
                this.countTotalVerified = this.currentPage + dados_videos.length;
            }
            this.options.countTotal = this.countTotalVerified;
        }
        if (this.currentPage > this.options.countTotal) {
            this.currentPage = Math.floor(this.options.countTotal / this.totalPerPage) * this.totalPerPage;
            this.verifyAjax(this.currentPage / this.totalPerPage);
            return;
        }
        if (dados_videos.length == 0 && this.currentPage > 0) {
            this.currentPage = this.currentPage - this.totalPerPage;
            this.countTotalVerified = this.currentPage;
            this.verifyAjax(Math.floor(this.currentPage / this.totalPerPage));
            return;
        }
    },
    writeBullets: function(curPage) {
        if (this.bullet) {
            this.bullet.innerHTML = '';
            var curPage = Math.floor(this.currentPage / this.totalPerPage);
            if (this.options.ajaxData) {
                countElements = this.options.countTotal;
                numPages = Math.ceil(this.options.countTotal / this.totalPerPage);
            } else {
                countElements = this.dataObject.length;
                numPages = Math.ceil(this.dataObject.length / this.totalPerPage);
            }
            for (var my_curr = 0; my_curr < numPages; my_curr++) {
                if (my_curr == curPage) {
                    var li = document.createElement('li');
                    li.className = "active";
                    this.bullet.appendChild(li);
                } else {
                    var li = document.createElement('li');
                    var a = document.createElement('a');
                    a.href = '#' + (my_curr + 1);
                    a.onclick = function() {
                        return false;
                    };
                    Event.observe(a, 'click', this.goPage.bindAsEventListener(this));
                    li.appendChild(a);
                    this.bullet.appendChild(li);
                }
            }
        }
    },
    writePagination: function(curPage) {
        var numPages = 0;
        var countElements = 0;
        if (this.options.ajaxData) {
            countElements = this.options.countTotal;
            numPages = Math.ceil(this.options.countTotal / this.totalPerPage);
        } else {
            countElements = this.dataObject.length;
            numPages = Math.ceil(this.dataObject.length / this.totalPerPage);
        }
        var curPage = Math.floor(this.currentPage / this.totalPerPage);
        if (this.pagesDesc) {
            for (var my_curr = 0; my_curr < this.pagesDesc.length; my_curr++) {
                if (!this.pagesDesc[my_curr]) {
                    continue;
                }
                this.pagesDesc[my_curr].innerHTML = '';
                if (numPages > 1) {
                    var max = ((curPage + 1) * this.totalPerPage);
                    if (countElements < max) {
                        max = countElements;
                    }
                    this.pagesDesc[my_curr].innerHTML = ((curPage * this.totalPerPage) + 1) + '-' + max + ' de ' + countElements;
                }
            }
        }
        if (this.pagesDiv) {
            for (var my_curr = 0; my_curr < this.pagesDiv.length; my_curr++) {
                if (!this.pagesDiv[my_curr]) {
                    continue;
                }
                var aCurPages = this.pagesDiv[my_curr].getElementsByTagName('a');
                for (var i = (aCurPages.length - 1); i >= 0; i--) {
                    aCurPages[0].parentNode.removeChild(aCurPages[i]);
                }
                this.pagesDiv[my_curr].innerHTML = '';
                if (curPage > 4) {
                    var link = document.createElement('A');
                    link.href = '#1';
                    link.innerHTML = '<strong>|<</strong>';
                    link.onclick = function() {
                        return false;
                    };
                    link.title = 'Ir para a primeira';
                    Event.observe(link, 'click', this.goPage.bindAsEventListener(this));
                    this.pagesDiv[my_curr].appendChild(link);
                    this.pagesDiv[my_curr].appendChild(document.createTextNode(' '));
                    var link = document.createElement('A');
                    link.href = '#' + (curPage - 4);
                    link.innerHTML = '<strong><<</strong>';
                    link.onclick = function() {
                        return false;
                    };
                    link.title = 'Recuar 5 p\341ginas';
                    Event.observe(link, 'click', this.goPage.bindAsEventListener(this));
                    this.pagesDiv[my_curr].appendChild(link);
                    this.pagesDiv[my_curr].appendChild(document.createTextNode(' '));
                }
                if (numPages > 5 && curPage > 4) {
                    var link = document.createElement('A');
                    link.href = '#' + (curPage);
                    link.innerHTML = '<strong><</strong>';
                    link.onclick = function() {
                        return false;
                    };
                    link.title = 'Recuar 1 p\341gina';
                    Event.observe(link, 'click', this.goPage.bindAsEventListener(this));
                    this.pagesDiv[my_curr].appendChild(link);
                    this.pagesDiv[my_curr].appendChild(document.createTextNode(' '));
                }
                for (var j = Math.floor(curPage / 5) * 5 + 1; j <= numPages && j <= Math.floor(curPage / 5) * 5 + 5 && numPages > 1; j++) {
                    if (j != (curPage + 1)) {
                        var link = document.createElement('A');
                        link.href = '#' + j;
                        link.innerHTML = '<strong>' + j + '</strong>';
                        link.onclick = function() {
                            return false;
                        };
                        Event.observe(link, 'click', this.goPage.bindAsEventListener(this));
                        this.pagesDiv[my_curr].appendChild(link);
                    } else {
                        var u = document.createElement('u');
                        var strong = document.createElement('strong');
                        u.appendChild(strong);
                        strong.appendChild(document.createTextNode(j));
                        this.pagesDiv[my_curr].appendChild(u);
                    }
                    if (j != numPages && j != Math.floor(curPage / 5) * 5 + 5) {
                        var span = document.createElement('span');
                        span.setAttribute('class', 'ftgreen');
                        span.appendChild(document.createTextNode(' | '));
                        this.pagesDiv[my_curr].appendChild(span);
                    }
                }
                if (numPages > 5 && Math.floor(curPage / 5) * 5 < numPages - 5) {
                    var link = document.createElement('A');
                    link.href = '#' + (curPage + 2);
                    link.innerHTML = '<strong>></strong>';
                    link.onclick = function() {
                        return false;
                    };
                    link.title = 'Avan\347ar 1 p\341gina';
                    Event.observe(link, 'click', this.goPage.bindAsEventListener(this));
                    this.pagesDiv[my_curr].appendChild(document.createTextNode(' '));
                    this.pagesDiv[my_curr].appendChild(link);
                }
                if (curPage < numPages - 5) {
                    var link = document.createElement('A');
                    link.href = '#' + (curPage + 6);
                    link.innerHTML = '<strong>>></strong>';
                    link.onclick = function() {
                        return false;
                    };
                    link.title = 'Avan\347ar 5 p\341ginas';
                    Event.observe(link, 'click', this.goPage.bindAsEventListener(this));
                    this.pagesDiv[my_curr].appendChild(document.createTextNode(' '));
                    this.pagesDiv[my_curr].appendChild(link);
                }
                if (Math.floor(curPage / 5) * 5 < numPages - 5) {
                    var link = document.createElement('A');
                    link.href = '#' + numPages;
                    link.innerHTML = '<strong>>|</strong>';
                    link.onclick = function() {
                        return false;
                    };
                    link.title = 'Ir para a \372ltima';
                    Event.observe(link, 'click', this.goPage.bindAsEventListener(this));
                    this.pagesDiv[my_curr].appendChild(document.createTextNode(' '));
                    this.pagesDiv[my_curr].appendChild(link);
                }
            }
        }
    },
    appendElement: function(source) {
        this.element.innerHTML += source;
    },
    clearElement: function() {
        var elms = this.element.getElementsByTagName('*');
        for (var i = (elms.length - 1); i >= 0; i--) {
            elms[i].parentNode.removeChild(elms[i]);
        }
    },
    debug: function(txt) {}
};
