/**
 * AJAX Pager
 *
 * PARAMETERS
 * container   (String):     id of slides container
 * maxPages    (String|int): number of available slides (e.g.4 => page 0..3)
 * urlPattern  (String):     AJAX url with a placeholder for the page number
 * initialLoad (Boolean):    Optional parameter: Loads the first page on initialization (default = true)
 * 
 * EXAMPLE:
 * new AjaxPager("image_container", 4, "nextSlide_[page].html");
 *
 * AUTHOR:
 * Hendrike Heydenreich
 * 02.08.2011
 */
AjaxPager = Class.create({
	
	patterns: {
		PAGE_IN_URL: "[page]",
		TRIGGER: "data-pagination-trigger",
		INDICATOR: "img.indicator"
	},
	
	initialize: function(container, maxPages, urlPattern, initialLoad) {
		this.container = $(container);
		this.maxPages = parseInt(maxPages, 10);
		this.urlPattern = urlPattern;
		this.page = 0;
		this.indicator = $$(this.patterns.INDICATOR).first() || null;
		
		if(!(initialLoad === false)) {
			this.initialLoad();
		}
		
		this._observe();
	},
	
	_observe: function() {
		$$("a[" + this.patterns.TRIGGER + "]").each(function(trigger) {
			var direction = trigger.readAttribute(this.patterns.TRIGGER);
			if(direction == "next") {
				trigger.observe("click", this.next.bind(this));	
			} else {
				trigger.observe("click", this.prev.bind(this));
			}
		}.bind(this));
	},
	
	initialLoad: function() {
		this.paginate();
	},
	
	next: function(event) {
		this.page++;
		if(this.page >= this.maxPages) {
			this.page = 0;
		}
		this.paginate();
		event.preventDefault();
	},
	
	prev: function(event) {
		this.page--;
		if(this.page < 0) {
			this.page = (this.maxPages - 1);
		}
		this.paginate();
		event.preventDefault();
	},
	
	paginate: function() {
		var url = this.urlPattern.replace(this.patterns.PAGE_IN_URL, this.page);
		this.showIndicator();
		new Ajax.Updater(this.container, url, {
			onComplete: this.hideIndicator.bind(this),
			encoding: 'iso-8859-1'
		});
	},
	
	showIndicator: function() {
		if(this.indicator) {
			this.indicator.show();
		}
	},
	
	hideIndicator: function() {
		if(this.indicator) {
			this.indicator.hide();
		}
	}
});


/**
 * AjaxEffectsPager
 * extends AjaxPager with some loading effects
 *
 * PARAMETERS
 * container   (String):     id of slides container
 * maxPages    (String|int): number of available slides (e.g.4 => page 0..3)
 * urlPattern  (String):     AJAX url with a placeholder for the page number
 * initialLoad (Boolean):    Optional parameter: Loads the first page on initialization (default = true)
 * 
 * EXAMPLE:
 * new AjaxPager("image_container", 4, "nextSlide_[page].html");
 *
 * AUTHOR:
 * Hendrike Heydenreich
 * 02.08.2011
 */
AjaxEffectsPager = Class.create(AjaxPager, {

	initialLoad: function() {
		this.faded = true;
		this.loadNextPage();
	},
	
	paginate: function() {
	  this.container.fade({
	    duration: 0.5,
	  	afterFinish: function() {
	  		this.faded = true;
	  	}.bind(this)
	  });
	  
		this.loadNextPage();
	},
	
	loadNextPage: function() {
		var url = this.urlPattern.replace(this.patterns.PAGE_IN_URL, this.page);
		new Ajax.Request(url, {
			onComplete: this.onNextPageLoaded.bind(this),
			encoding: 'iso-8859-1'
		});
	},
	
	onNextPageLoaded: function(response) {
		this.interval = window.setInterval(this.checkLoaded.bind(this, response), 100);
	},
	
	checkLoaded: function(response) {
		if(this.faded) {
			this.faded = false;
			window.clearInterval(this.interval);
			this.showNewPage(response.responseText);
		}
	},
	
	showNewPage: function(content) {
		this.container.update(content);
		this.container.appear();
	}
});
