var ResultBrowserHelper = function () {};

ResultBrowserHelper.prototype.init = function (resultBrowser) {
    var ce;
    this.current = resultBrowser.getElementsByTagName('strong')[0];
    this.rew = renderer.getElement('.rew', resultBrowser);
    this.ffw = renderer.getElement('.ffw', resultBrowser);
    this.all = renderer.getElements('a', resultBrowser);
    this.pages = parseInt(this.all[this.all.length-1] == this.ffw ? this.all[this.all.length-2].innerHTML : this.all[this.all.length-1].innerHTML);
    this.pages = parseInt(this.current.innerHTML) > this.pages ? parseInt(this.current) : this.pages;
    this.backStep = 1;
    this.ffwStep = 1;
    this.currentPage=parseInt(this.current.innerHTML);
    this.setAvailablePages();
    this.current.style.cursor = 'url(images/icons/wheel.ico), pointer';
    
    if (this.rew) {
        this.rew.tooltip = this.getTooltipBox();
        this.rew.tooltip.backStep = this.rew.tooltip.appendChild(document.createElement('span'));
        this.rew.tooltip.backStep.innerHTML = this.backStep;
        this.rew.tooltip.appendChild(document.createTextNode(' Seite(n) zurück'));
        eventHandler.addEvent(this.rew, 'mouseover', function (e) {
            var o = libScreen.getScrollOffset();
            e = eventHandler.getEvent(e);
            this.rew.tooltip.style.top = e.clientY + o.y + 'px';
            this.rew.tooltip.style.left = e.clientX + o.x + 30 + 'px';
            this.rew.tooltip.style.position = 'absolute';
            this.rew.tooltip.style.zIndex = '10000';
            this.rew.tooltip.renderIn(document.body, 'fade');
        }.bindTo(this));
        eventHandler.addEvent(this.rew, 'mouseout', function () {
            this.rew.tooltip.remove('fade');
        }.bindTo(this));
        
        this.rew.style.cursor = 'url(images/icons/wheel.ico), pointer';
        eventHandler.addEvent(this.rew, "mousewheel", function (e) {
            var x=Math.floor(this.backStep/2),
                y=Math.floor(this.backStep*2);
            if (e > 0) {
                this.backStep = this.currentPage-y > 0 ? y : this.backStep;
            } else if (e < 0) {
                this.backStep = this.currentPage-x < this.currentPage ? x : this.backStep;
            }
            this.rew.href=this.rew.href.replace(/page=[0-9]+/, 'page='+(this.currentPage-this.backStep));
            this.rew.tooltip.backStep.innerHTML = this.backStep;
        }.bindTo(this));
    }
    if (this.ffw) {
        this.ffw.tooltip = this.getTooltipBox();
        this.ffw.tooltip.ffwStep = this.ffw.tooltip.appendChild(document.createElement('span'));
        this.ffw.tooltip.ffwStep.innerHTML = this.ffwStep;
        this.ffw.tooltip.appendChild(document.createTextNode(' Seite(n) vor'));
        eventHandler.addEvent(this.ffw, 'mouseover', function (e) {
            var o = libScreen.getScrollOffset();
            e = eventHandler.getEvent(e);
            this.ffw.tooltip.style.top = e.clientY + o.y + 'px';
            this.ffw.tooltip.style.left = e.clientX + o.x + 30 + 'px';
            this.ffw.tooltip.style.position = 'absolute';
            this.ffw.tooltip.style.zIndex = '10000';
            this.ffw.tooltip.renderIn(document.body, 'fade');
        }.bindTo(this));
        eventHandler.addEvent(this.ffw, 'mouseout', function () {
            this.ffw.tooltip.remove('fade');
        }.bindTo(this));
        
        this.ffw.style.cursor = 'url(images/icons/wheel.ico), pointer';
        eventHandler.addEvent(this.ffw, "mousewheel", function (e) {
            var x=Math.floor(this.ffwStep/2),
                y=Math.floor(this.ffwStep*2);
            if (e > 0) {
                this.ffwStep = this.currentPage+y <= this.pages ? y : this.ffwStep;
            } else if (e < 0) {
                this.ffwStep = this.currentPage+x > this.currentPage ? x : this.ffwStep;
            }
            this.ffw.href=this.ffw.href.replace(/page=[0-9]+/, 'page='+(this.currentPage+this.ffwStep));
            this.ffw.tooltip.ffwStep.innerHTML = this.ffwStep;
        }.bindTo(this));
    }
    
    this.current.tooltip = this.getTooltipBox();
    this.current.tooltip.appendChild(document.createTextNode('Zu Seite '));
    this.current.tooltip.currentPage = this.current.tooltip.appendChild(document.createElement('span'));
    this.current.tooltip.currentPage.innerHTML = this.currentPage;
    this.current.tooltip.appendChild(document.createTextNode(' springen'));
    eventHandler.addEvent(this.current, 'mousewheel', ce = function (e) {
        if (e > 0) {
            this.availablePages.push(this.availablePages.shift());
        } else if (e < 0) {
            this.availablePages.unshift(this.availablePages.pop());
        }
        if (this.availablePages[0] != this.currentPage && !renderer.hasClassName('resultBrowserLink', this.current)) {
            renderer.addClassName('resultBrowserLink', this.current);
        } else if (this.availablePages[0] == this.currentPage && renderer.hasClassName('resultBrowserLink', this.current)) {
            renderer.removeClassName('resultBrowserLink', this.current);
        }
        this.current.innerHTML = this.availablePages[0];
        this.current.tooltip.currentPage.innerHTML = this.availablePages[0];
        if (this.current.tooltip.parentNode == null) {
            var o = libScreen.getScrollOffset();
            this.current.tooltip.style.top = renderer.currentMousePos.y + o.y  + 'px';
            this.current.tooltip.style.left = renderer.currentMousePos.x + o.x + 30 + 'px';
            this.current.tooltip.style.position = 'absolute';
            this.current.tooltip.style.zIndex = '10000';
            this.current.tooltip.renderIn(document.body, 'fade');
        }
    }.bindTo(this));
    eventHandler.addEvent(this.current, 'click', function () {
        if (this.availablePages[0] != this.currentPage) {
            document.location.replace(this.all[0].href.replace(/page=[0-9]+/, 'page='+this.availablePages[0]));
        }
    }.bindTo(this));
    
    eventHandler.addEvent(this.current, 'mouseover', function (e) {
        if (this.availablePages[0] == this.currentPage) return;
        var o = libScreen.getScrollOffset();
        if (typeof this.timeout != 'undefined') {
            window.clearTimeout(this.timeout);
        }
        e = eventHandler.getEvent(e);
        this.current.tooltip.style.top = e.clientY + o.y  + 'px';
        this.current.tooltip.style.left = e.clientX + o.x + 30 + 'px';
        this.current.tooltip.style.position = 'absolute';
        this.current.tooltip.style.zIndex = '10000';
        this.current.tooltip.renderIn(document.body, 'fade');
    }.bindTo(this));
    eventHandler.addEvent(this.current, 'mouseout', function () {
        this.current.tooltip.remove('fade');
        if (this.availablePages[0] == this.currentPage) return;
        this.timeout = window.setTimeout(function () {
            var newElem = document.createTextNode(this.currentPage)
            while(this.current.hasChildNodes()) {
                this.current.removeChild(this.current.firstChild)
            }
            this.current.appendChild(newElem);
            renderer.removeClassName('resultBrowserLink', this.current);
            this.setAvailablePages();
        }.bindTo(this), 2000);
    }.bindTo(this));
};
ResultBrowserHelper.prototype.setAvailablePages = function () {
    var i=this.currentPage;
    this.availablePages = [];
    for (i;i<=this.pages;i++) {
        this.availablePages.push(i);
    }
    for (i=1;i<this.currentPage;i++){
        this.availablePages.push(i);
    }
};
ResultBrowserHelper.prototype.getTooltipBox = function () {
    var tooltipBox = renderer.createElement('div', {
        "className":"resultBrowserTooltip"
    });
    return tooltipBox;
};
