Dvintage.Pager = Class.create({
    initialize: function(){
		
		this.NUMBER_OF_SHOWN_LINKS = 8;
        this.pager = new Element('ul').addClassName('pageblock');
		
		var pagingwrapper = new Element('div', {id:'paging'});
		$('sorting').insert({before:pagingwrapper})
        pagingwrapper.insert(this.pager);
		var clearer = new Element('div', {id:'clearhits'});
		$('hits').insert({bottom:clearer});
		if (Dvintage.channel.isKids() || Dvintage.channel.isFashion()) {
			this.ad_loader = new Element('img', {src:'images/main/ad_loader_kids.gif'});			
		} else {
			this.ad_loader = new Element('img', {src:'images/main/ad_loader.gif'});
		}

    },
	
	_fireRequest: function(pageindex){
		if (!Dvintage.advertisements) {
			return;
		}
		this.pager.setStyle({opacity:.5}).removeClassName('readyForInput');

		$('maincanvas').setStyle({overflow:'hidden',height:'2000px'});
		var direction = (pageindex < this.pageIndex)? 764:-764;

		$('maincanvas').insert(this.ad_loader);
		this.ad_loader.setStyle({zIndex:-1,position:'absolute',left:'347px',top:180 - $('facetdrawer').getHeight() + 'px'})
	
        this.effect = new Effect.Move($('hits'), {
			x: direction, 
			y: 0, 
			mode: 'absolute', 
			duration:.5
			});

		Dvintage.advertisements.setPageIndex(pageindex);
        Dvintage.advertisements.doRequest();
		
	},
	
    update: function(pageIndex, pageSize, totalHitCount, pageHitCount) {

		if (this.effect) {
			this.effect.cancel();
		}

		// check if non_ajax: do not clear.
		if ($('no_ajax_content_loaded')) {
			$('no_ajax_content_loaded').remove();
		} else {
			$('hits').update().setStyle({left:0});			
		}

/*
        if (this.ad_loader.up()) {
            $('hits').update().setStyle({left:0});
        }
*/
        this.pager.setStyle({opacity:1}).addClassName('readyForInput');
		$('maincanvas').setStyle({height:'auto',overflow:'visible'});

		if (this.ad_loader.up()) 
			this.ad_loader.remove();
		this.pageIndex = pageIndex;
		this.pageSize= pageSize;
		this.pager.update();
        var numOfPages = Math.ceil(totalHitCount / pageSize);
        if(numOfPages > 1) {
            var prev = new Element('li');
            prev.update('&lt;&lt; previous');
            if(this.pageIndex > 0) {
                prev.observe('click', function() {
					this._fireRequest(this.pageIndex - 1);
                }.bind(this));
            } else {
                prev.addClassName('disabled');
            }
            this.pager.appendChild(prev);
        }

        var min = (this.pageIndex < Math.floor(this.NUMBER_OF_SHOWN_LINKS / 2))? 0 : this.pageIndex - Math.floor(this.NUMBER_OF_SHOWN_LINKS / 2);
        var max = ((this.pageIndex + Math.floor(this.NUMBER_OF_SHOWN_LINKS / 2)) >= numOfPages)? numOfPages : this.pageIndex + Math.floor(this.NUMBER_OF_SHOWN_LINKS / 2);
		
		if (this.pageIndex - Math.floor(this.NUMBER_OF_SHOWN_LINKS / 2) <= 0) {
			max = (this.NUMBER_OF_SHOWN_LINKS < numOfPages)? this.NUMBER_OF_SHOWN_LINKS : numOfPages;
		} else if (this.pageIndex + Math.floor(this.NUMBER_OF_SHOWN_LINKS / 2) >= numOfPages) {
			min = (numOfPages - this.NUMBER_OF_SHOWN_LINKS < 0)? 0:numOfPages - this.NUMBER_OF_SHOWN_LINKS;
		}
		
		var lowerdots = (min > 2)? true:false;
        var upperdots = (max < (numOfPages-2))? true:false;

        //correct loop values
        min = (lowerdots)?min:0;
        max = (upperdots)?max:numOfPages;

        if (lowerdots) {
            //add start page and dots
            var start = new Element('li');
            start.update(1);
            this.pager.appendChild(start);
            start.observe('click', function() {
				this._fireRequest(0);
            }.bind(this));
            var page = new Element('li').addClassName('dots');
            page.update('...');
            this.pager.appendChild(page);
        }


		$R(min, max-1).each(function(i){

            var page = new Element('li');
            page.update(i+1);

            this.pager.appendChild(page);

			page.observe('click', function() {
				this._fireRequest(i);
            }.bind(this));

            if(i == this.pageIndex) {
                page.addClassName('active');
            }
			
		}.bind(this))
		
		if (upperdots) {
            if(numOfPages - max > 1) {
                var page = new Element('li').addClassName('dots');
                page.update('...');
                this.pager.appendChild(page);
            }
            /* var end = new Element('li');
            end.update('end');
            this.pager.appendChild(end);
            end.observe('click', function() {
				this._fireRequest(numOfPages - 1);
            }.bind(this)); */

        }


        if(numOfPages > 1) {
            var next = new Element('li');
            next.update('next &gt;&gt;');
            if(this.pageIndex < numOfPages-1) {
                next.observe('click', function() {
					this._fireRequest(this.pageIndex + 1);
                }.bind(this));
            } else {
                next.addClassName('disabled');
            }
            this.pager.appendChild(next);

        }
		
//		c(this.pager.getOffsetParent().getWidth())
//		this.pager.hide();
//		c(this.pager.getWidth())

		// this.setCentered();
//		this.pager.show();
    },
	
	setCentered: function(){
		var length = this.pager.select('li').length;
		var margin = (length * 25) + 100;
		this.pager.setStyle({marginLeft:Math.round((this.pager.getOffsetParent().getWidth() - margin) / 2) + 'px'});
	}

});