

/**************************************************************

	Script		: Page Loader
	Version		: 1.0
	Authors		: Samuel Birch
	Desc		: load pages via AJAX.
	Licence		: Open Source MIT Licence

**************************************************************/

var pageLoader = new Class({
							  
	getOptions: function(){
		return {
			links: '.loadMe',
			loadInTo: 'center',
			loadFrom: 'center',
			onStart: Class.empty,
			onComplete: Class.empty
			
		};
	},

	initialize: function(options){
	
		this.setOptions(this.getOptions(), options);
		
		this.links = $$(this.options.links);
		this.links.each(function(el,i){
			el.addEvent('click',function(e){
				if(e != undefined){
					new Event(e).stop();
				}
				this.start(el);
			}.bind(this));
		}.bind(this));
	},
	
	setContent: function(){
		var temp = new Element('div').setProperties({id:'temp'}).setStyles({display:'none'}).injectInside(document.body);
		temp.setHTML(this.content.response.text);
		var newEl = $('temp').getElement('#'+this.options.loadFrom);
		$(this.options.loadInTo).replaceWith(newEl);
		newEl.setProperties({id:this.options.loadInTo});
		temp.remove();
	},
	
	start: function(el){
		this.options.onStart();
		this.content = new Ajax(el.href, {
								method: 'get',
								onComplete: this.complete.bind(this),
								autoCancel: true
							}).request();
	},
	
	complete: function(){
		this.setContent();
		this.options.onComplete();
		Lightbox.init({descriptions: '.lightboxDesc', showControls: true});
	}

});
pageLoader.implement(new Events);
pageLoader.implement(new Options);


/*************************************************************/
