Emv.Paginator = {};

/**
 * Constructor for the Paginator Slider.
 *
 * @param string elementId
 * @param integer pageCount
 * @param integer pageRange
 * @param integer currentPage
 * @param string basePrefix
 * @param string baseSuffix
 * @param boolean isAjax
 */
Emv.Paginator.Slider = function(elementId, pageCount, pageRange, currentPage, basePrefix, baseSuffix, isAjax) {

	element = document.getElementById(elementId);

	this.baseElement = element;
    this.elementId   = elementId;
    this.pageCount   = pageCount;
    this.currentPage = currentPage;
    this.pageRange   = pageRange;
    this.basePrefix  = basePrefix;
    this.baseSuffix  = baseSuffix;
    this.isAjax      = isAjax;

    this.init();
};

/**
 * re inits the slider with a new element and new currentPage
 * mainly used if you reload content by ajax
 */
Emv.Paginator.Slider.prototype.reinit = function(currentPage) {

	element = document.getElementById(this.elementId);
	this.baseElement = element;
    this.currentPage = currentPage;
    this.init();
};

/**
 * Update the PageCount
 *
 * @param {Object} pageCount
 */
Emv.Paginator.Slider.prototype.setPageCount = function(pageCount) {
    
    this.pageCount = pageCount;
    this.init();
};

/**
 * Update the PageRange
 *
 * @param {Object} pageRange
 */
Emv.Paginator.Slider.prototype.setPageRange = function(pageRange) {
    
    this.pageRange = pageRange;
    this.init();
};

/**
 * Inits the slider
 */
Emv.Paginator.Slider.prototype.init = function() {

    element = this.baseElement;

    //html elements
    this.scrollbar         = YAHOO.util.Dom.getElementsByClassName('scrollbar', 'DIV', element)[0];
    this.sliderBackground  = YAHOO.util.Dom.getElementsByClassName('sliderBackground', 'DIV', element)[0];
    this.positionIndicator = YAHOO.util.Dom.getElementsByClassName('positionIndicator', 'DIV', element)[0];

    //Load other stuff
    this.buildSlider();
    this.drawPaginator();
    this.setupEvents();

    this.scrollToCurrentPage();
    this.setPositionIndicator();
};

/**
 * sets the width of the sliderBackground
 */
Emv.Paginator.Slider.prototype.buildSlider = function() {

	this.sliderBackground.xPos = 0;
    this.sliderBackground.style.display = 'inline';

    if (this.pageRange > this.pageCount) {

        this.sliderBackground.style.width = '100%';
    } else {

        this.sliderBackground.style.width = this.pageRange / this.pageCount * 100 + '%';
    }
};

/**
 * Sets the slider to the correct position on the bar
 */
Emv.Paginator.Slider.prototype.setSlider = function() {

	this.sliderBackground.style.left = this.sliderBackground.xPos + 'px';
};

/**
 * Redraws the Pager and sets the slider to the correct position
 */
Emv.Paginator.Slider.prototype.drawPaginator = function() {

    percentFromLeft = this.sliderBackground.xPos/(this.scrollbar.offsetWidth);

    //happens if pager is on a not visible tab so we init it with 0
    if (isNaN(percentFromLeft)) {

        percentFromLeft = 0;
    }

    baseValue = Math.round(percentFromLeft * this.pageCount);

    if (baseValue < 1) {

        baseValue = 1;
        this.sliderBackground.xPos = 0;
        this.setSlider();

    } else if (baseValue >= (this.pageCount - this.pageRange)) {

            baseValue = this.pageCount - this.pageRange + 1;
            this.sliderBackground.xPos = this.scrollbar.offsetWidth - this.sliderBackground.offsetWidth;
            this.setSlider();
    }

    if (baseValue < 1) {

        baseValue                  = 1;
        this.sliderBackground.xPos = 0;
        this.setSlider();
    }

    subElements = this.baseElement.getElementsByTagName('A');

    for(var i=0; i< subElements.length; i++) {

        curValue           = baseValue + i;
        YAHOO.util.Dom.removeClass(subElements[i], 'active');

        if (curValue == this.currentPage) {

            YAHOO.util.Dom.addClass(subElements[i], 'active');
        } else {

            YAHOO.util.Dom.addClass(subElements[i], 'paginatorLink');
        }

        if (this.isAjax) {

        	subElements[i].href = this.basePrefix + curValue + this.baseSuffix;
        } else {

	        if (curValue > 1 ) {

	            subElements[i].href = this.basePrefix + '/seite-' + curValue + this.baseSuffix;
	        } else {

	            subElements[i].href = this.basePrefix + this.baseSuffix;
	        }
        }

        subElements[i].innerHTML   = curValue;
        subElements[i].style.width = (String(this.pageCount).length * 8.1) + 'px';
    }
};

/**
 * Setups the on Click eg. events to the slider
 */
Emv.Paginator.Slider.prototype.setupEvents = function() {

    var _this = this;

    this.sliderBackground.onmousedown = function(e) {

        if (!e) {
            
            var e = window.event;
        }
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();

        this.dx = YAHOO.util.Event.getXY(e)[0] - this.xPos;
        document.onmousemove = function(e) {

            if (!e) {
                
                var e = window.event;
            }
            _this.sliderBackground.xPos = YAHOO.util.Event.getXY(e)[0] - _this.sliderBackground.dx;
            _this.setSlider();
            _this.drawPaginator();
        };
        document.onmouseup = function() {

            document.onmousemove = null;
            _this.enableSelection();
        };
        _this.disableSelection();
    };

    this.scrollbar.onmousedown = function(e){

        if (!e) {
            
            var e = window.event;
        }

        _this.sliderBackground.xPos = YAHOO.util.Event.getXY(e)[0] - YAHOO.util.Dom.getX(_this.scrollbar) - _this.sliderBackground.offsetWidth/2;
        _this.setSlider();
        _this.drawPaginator();
    };
};

/**
 * Scrolls to current Page
 */
Emv.Paginator.Slider.prototype.scrollToCurrentPage = function() {

    this.sliderBackground.xPos = (this.currentPage - Math.round(this.pageRange/2))/this.pageCount * this.scrollbar.offsetWidth;
    this.setSlider();
    this.drawPaginator();
};

/**
 * Sets the positionIndicator to the correct Position
 */
Emv.Paginator.Slider.prototype.setPositionIndicator = function() {

    if(this.currentPage == 1) {

        this.positionIndicator.style.left = 0;
    } else {

    	this.positionIndicator.style.left = this.currentPage/this.pageCount * this.scrollbar.offsetWidth + 'px';
    }
};

/**
 * Disables that a mouse selection selects anything on the page
 */
Emv.Paginator.Slider.prototype.disableSelection = function() {

    document.onselectstart = function() {

    	return false;
    };
    this.sliderBackground.focus();
};

/**
 * Enables that selecting with the mouse works.
 */
Emv.Paginator.Slider.prototype.enableSelection = function() {

    document.onselectstart = function() {

        return true;
    };
    this.sliderBackground.blur();
};
