var Pagination = Class.create({
 
    initialize : function(container, paginator, service, template, id)
    {
        this.container = $(container);
		this.paginator = $(paginator);
		this.service = service;
		this.templateEven = template.templateEven;
		this.templateCountableEven = template.templateCountableEven;
		this.templateArbitraryEven = template.templateArbitraryEven;
		this.templateOdd = template.templateOdd;
		this.templateCountableOdd = template.templateCountableOdd;
		this.templateArbitraryOdd = template.templateArbitraryOdd;
 		this.id = id;
		
		this.startPage = 1;
		this.itemsPerPage = 10;
		this.maxPages = 7;
		this.prevText = "Prev";
		this.nextText = "Next";
		this.ellipseText = "...";
		
        // load existing contacts
        this.load();
    },
	
	load : function()
    {
		this.currentPage = this.startPage;
		//alert(document.getElementById('bonusid').value);
		var pars = "?cmd=" + this.service;// = '?page=' + this.page;
		if (this.id != undefined)
		{
			//alert(this.bonusid);
			pars += '&id='+this.id;
		}
		
        var options = {
            method    : 'get',
			parameters: pars,
            onSuccess : this._onLoadSuccess.bind(this)
        };
		
        // submit request to retrieve contacts
		//alert(this.service.split("?")[0]);
		new Ajax.Request('dataService.xml', options);//this.service.split("?")[0], options);
    },
 
    _onLoadSuccess : function(transport)
    {
        this.json = transport.responseJSON;
		
		this.updateContainer();
		this.updatePagination(this.currentPage);
    },

	goTo : function(to)
	{
		this.currentPage = parseInt(to);
		this.updateContainer();
		this.updatePagination();
	},
	
	updateContainer : function()
	{
        // clear the contacts container
        this.container.update();
		
		var from = this.currentPage * this.itemsPerPage - this.itemsPerPage;// + 1;
		var to = (this.currentPage * this.itemsPerPage < this.json.length) ? this.currentPage * this.itemsPerPage /*+ 1*/ : this.json.length;
		//alert(this.currentPage+" "+from+" "+to);
		
		if (this.json == 'false')
		{
			this.container.insert('<tr><td colspan="5">Es sind keine Einträge vorhanden!</td></tr>');
		}
		else
		{
			for (i=from; i<to; i++)
			{
				if (i%2 == 0)
				{
					if (new Template('#{prize_arbitrary}').evaluate(this.json[i]) == '1')
						this.container.insert(this.templateArbitraryEven.evaluate(this.json[i]));
					else if (new Template('#{prize_quantity}').evaluate(this.json[i]) >= '1')
						this.container.insert(this.templateCountableEven.evaluate(this.json[i]));
					else
						this.container.insert(this.templateEven.evaluate(this.json[i]));
				}
				else
				{
					if (new Template('#{prize_arbitrary}').evaluate(this.json[i]) == '1')
						this.container.insert(this.templateArbitraryOdd.evaluate(this.json[i]));
					else if (new Template('#{prize_quantity}').evaluate(this.json[i]) >= '1')
						this.container.insert(this.templateCountableOdd.evaluate(this.json[i]));
					else
						this.container.insert(this.templateOdd.evaluate(this.json[i]));
				}
			}
		}
		
	},
	
	updatePagination : function()
	{
		this.paginator.update();
		
		var nop = Math.ceil(this.json.length/this.itemsPerPage);
		var cp = Math.floor(this.maxPages/2);
		
		
		if (this.currentPage > 1)
		{
			var a = new Element('a', { 'class': 'pageinator', href: '#page'+parseInt(this.currentPage-1) }).update(this.prevText);
				//a.observe('click', function(event){this.currentPage = Event.element(event).innerHTML;}).bind(this);
				a.observe('click', (function(event) { event.stop(); this.goTo(this.currentPage-1); }).bind(this))
			this.paginator.insert(a);
			
			if (this.currentPage > cp+1)
			{
				var span = new Element('span', { 'class': 'pageinator' }).update(this.ellipseText);
				this.paginator.insert(span);
			}
		}
		
		var from = (this.currentPage > cp) ? this.currentPage-cp : 1;
		var to = (this.currentPage < nop-cp) ? this.currentPage+cp+1 : nop+1;
		
		for (var i=from; i < to; i++)
		{
			if (i == this.currentPage)
			{
				var span = new Element('span', { 'class': 'pageinator' }).update(i);
				this.paginator.insert(span);
			}
			else
			{
				var a = new Element('a', { 'class': 'pageinator', href: '#page'+parseInt(i) }).update(i);
					//a.observe('click', function(event){this.currentPage = Event.element(event).innerHTML;}).bind(this);
					a.observe('click', (function(event) { event.stop(); this.goTo(Event.element(event).innerHTML); }).bind(this))
				this.paginator.insert(a);
			}
		}
		if (this.currentPage < nop)
		{
			if (this.currentPage < nop-cp)
			{
				var span = new Element('span', { 'class': 'pageinator' }).update(this.ellipseText);
				this.paginator.insert(span);
			}
			
			var a = new Element('a', { 'class': 'pageinator', href: '#page'+parseInt(this.currentPage+1) }).update(this.nextText);
				//a.observe('click', function(event){this.currentPage = Event.element(event).innerHTML;}).bind(this);
				a.observe('click', (function(event) { event.stop(); this.goTo(this.currentPage+1); }).bind(this))
			this.paginator.insert(a);
		}
	
	}
});