

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

	Script		: Page Loader
	Version		: 1.1
	Authors		: Samuel Birch, Markus Timtner
	Desc		: load pages via AJAX with mootools 1.2b2
	Licence		: Open Source MIT Licence

	*additional functionality done by mtness.net*
		on behalf of primel.net
		for http://bc-horvath.de
	*               2008-06-30                  *

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

// create the flash object to be available for the pageloader
var flashObj = null;

var pageLoader = new Class({
	Implements: [Events, Options],
    options: {
		links: 		'.loadMe',
		loadInTo: 	'details',
		loadFrom: 	'details',
		onStart: 	$empty,
		onComplete: $empty
    },	

	initialize: function(options){
			
		this.setOptions(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(reqResponse){
		
		var tempContent = $(this.options.loadInTo).set({ id:'tempDetails' });
		
		var temp = new Element('div').set({ id:'temp', display:'none' }).injectInside(document.body);
		temp.set({html:reqResponse});
		var newEl = $('temp').getElement('#'+this.options.loadFrom);
		newEl.replaces(tempContent);
		newEl.set({ id:this.options.loadInTo });
		temp.destroy();
	},
	
	start: function(el){
		// explicitly wait some time for stupid ie
		(function(){ this.fireEvent('onStart'); }).delay(256);
		
		// here the previously selected element is reset.
		if( $('selected')!= null ) $('selected').set({ id:'' });
		
		this.content = new Request({  
			method: 	'get',  
			url: 		el.href,
            onRequest:	function() {
								//$('content').fade('out');
								$('content').set('tween', {duration: '256'});
								$('content').tween('opacity', 0);
								
								// var stopper init within moosetup.js!
								stopper = 1;
			},
			onComplete: function(result) {
								myresult = result;
								this.complete(myresult);

								// make the portfolio visible 
								if(el.getParent().id == 'Portfolio' ) {
									$('details').setStyle('background', 'none');
									// $('pagebrowser').getElements('div').fade(0.25);
								}

								// explicitly wait some time for stupid ie
								(function(){ 
									//$('content').fade('in'); 
									$('content').set('tween', {duration: '256'});
									$('content').tween('opacity', 1);
								}).delay(512);
			}.bind(this),
			autoCancel: true
		}).get({'rand':$random(0, 4096)});

		// current element should be formatted differently
		el.set({ id:'selected' });
		
	},
	
	complete: function(reqResponse){
		this.setContent(reqResponse);
		this.fireEvent('onComplete');
	}

});

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

window.addEvent('domready', function(){

	new pageLoader();

});

