RollingArchives = Class.create();

RollingArchives.prototype = {
	initialize: function(targetitem, url, query, pagecount) {
		var rolling = this;

		this.targetitem = targetitem;
		this.url = url;
		this.query = query;
		this.pagecount = pagecount;
		this.pagenumber = 1;

		this.rollnext = $('rollnext');
		this.rollprev = $('rollprevious');

		var sliderValues = new Array(this.pagecount);
		for (var i = 0; i < this.pagecount; i++) {
			sliderValues[i] = i + 1;
		}
		this.PageSlider = new Control.Slider('pagehandle','pagetrack', {
			range: $R(rolling.pagecount, 1),
			values: sliderValues,
			sliderValue: 1,
			onSlide: function(v) { $('rollpages').innerHTML = 'Page '+v+' of '+rolling.pagecount; },
			onChange: function(v) { rolling.gotoPage(v); },
			handleImage: 'pagehandle'
		});

		Event.observe('rollprevious', 'click', function(){ rolling.rollPrevPage(); });
		Event.observe('rollnext', 'click', function(){ rolling.rollNextPage(); });

		this.checkRollingElements();
		this.rollRemoveLoad();

		this.initialized = true;
	},

	rollNextPage: function() {
		this.PageSlider.setValueBy(-1);
	},

	rollPrevPage: function() {
		this.PageSlider.setValueBy(1);
	},

	checkRollingElements: function() {
		if (this.pagenumber == 1) {
			$('rollprevious').className = null;
			$('rollnext').className = 'inactive';
			$('rollhome').className = 'inactive';
		} else if (this.pagenumber > 1) {
			$('rollnext').className = null;
			$('rollhome').className = null;
		}

		if (this.pagenumber >= this.pagecount) {
			$('rollprevious').className = 'inactive';
		} else {
			$('rollprevious').className = null;
		}

		$('rollpages').innerHTML = 'Page '+this.pagenumber+' of '+this.pagecount;
	},
	
	gotoPage: function(newpage) {
		var direction = 0;

		if (newpage != this.pagenumber) {
			if (newpage > this.pagecount) {
				direction = 'end';
			} else if (newpage < 1) {
				direction = 'home';
			} else {
				direction = 1;
			}
			this.pagenumber = (newpage - 1);
			this.rollArchive(direction);
		}
	},

	rollRemoveLoad: function() {
		new Effect.Fade('rollload', {duration: .1});
	},

	rollSuccess: function() {
		this.rollRemoveLoad();
	},

	rollError: function() {
		$('rollnotices').innerHTML = 'Error!';
	},

	processQuery: function() {
		if (this.query.indexOf('&paged=') != -1) {
			this.query = this.query.replace(/&paged=\d+/,'&paged='+this.pagenumber);
		} else {
			this.query += "&paged=" + this.pagenumber;
		}

		if (this.query.indexOf('&rolling=') == -1) {
			this.query += '&rolling=1';
		}
	},

	rollArchive: function(direction) {
		if (direction == 1 || direction == -1) {
			this.pagenumber += direction;
			new Effect.Appear('rollload', {duration: .1});
		} else if (direction == 'home') {
			this.pagenumber = 1;
		}

		this.checkRollingElements();
		this.processQuery();

		new Ajax.Updater(
			this.targetitem,
			this.url,
			{
				method: 'get',
				parameters: this.query,
				onSuccess: this.rollSuccess.bind(this),
				onFailure: this.rollError.bind(this)
			}
		);
	}
};