

/**************************************************************

	Script		: Page Loader
	Version		: 1.1
	Authors		: Samuel Birch
	Desc		: load pages via AJAX.
	Licence		: Open Source MIT Licence
	update		: Add fadeIn fadeOut on container

**************************************************************/

var pageLoader = new Class({
							  
	getOptions: function(){
		return {
			links: '.loadMe',
			loadInTo: 'content',
			loadFrom: 'content',
			_onStart: $empty,
			_onComplete: $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(html){
		var temp = new Element('div').set({
			'id': 'temp',
			'styles': {
				'display': 'none'
			},
			'html': html
		}).inject(document.body);
		
		var newEl = $('temp').getElement('#'+this.options.loadFrom);
		newEl.replaces($(this.options.loadInTo));
		newEl.set('id', this.options.loadInTo);
		newEl.setStyle('opacity', 0);
		temp.destroy();
		
	},
	
	start: function(el){
		this.options._onStart();
		this.contentUrl = el.href;
		var myFx = new Fx.Tween(this.options.loadInTo,{property: 'opacity',duration: 500, transition: Fx.Transitions.Sine.easeInOut});
		myFx.addEvent('complete', function(){
			this.content = new Request.HTML({
							method: 'get',
							onComplete: this.complete.bind(this),
							autoCancel: true
						}).get(el.href)
		}.bind(this));
		myFx.start(1,0)	
	},
	
	complete: function(tree, els, html, js){
		this.setContent(html);
		var myFx = new Fx.Tween(this.options.loadInTo,{property: 'opacity',duration: 500, transition: Fx.Transitions.Sine.easeInOut});
		myFx.start(0,1)	
		this.options._onComplete();
	},
	
	
	fadeIn : function()
	{

	},
	
	fadeInComplete : function(tree, els, html, js)
	{
		alert('Done.'+this.url);
		this.content = new Request.HTML({
							method: 'get',
							onComplete: this.complete.bind(this),
							autoCancel: true
						}).get(this.contentUrl);	
	}

});
pageLoader.implement(new Events);
pageLoader.implement(new Options);


/*************************************************************/

