/**
 * A3non Sandbox - Content Loader
 * 
 * @author Andi Dittrich <andi.dittrich@a3non.org>
 * @url http://www.a3non.org
 * @license Creative Commons BY-NC, Creative Commons Namensnennung-Keine kommerzielle Nutzung 3.0 Deutschland Lizenz, 
 * @license http://creativecommons.org/licenses/by-nc/3.0/de/
 * @version 1.1
 */
Sandbox.Net.Request.Loader = new Class({
	Implements: Events,

	destinations: null,
	baseuri: null,
	
	initialize: function(destinations, baseuri){
		this.destinations = destinations;
		this.baseuri = baseuri;
	},
	
	load: function(url){
		var THIS = this;
		var requestComplete = false;
		var uri = new URI(url);
		
		// get json url
		var json_url = url.replace(/.html/, '.json');
		
		// show loading overlayer
		var overlayer = new Sandbox.UI.Overlay.Loading({
			opacity: 0.9,
			duration: 500,
			url: url,
			image: 'http://gridnode0.a3non.org/WhiteSandbox/images/ajax-loader.gif',
		});
		overlayer.show();

		// scroll top
		var myFx = new Fx.Scroll($(document.body), {
			onComplete: function(){
				if (requestComplete){
					overlayer.hide();
				}else{
					requestComplete = true;
				}
		    }

		}).toTop();
		
		// change href
		location.href = this.baseuri.get('scheme') + '://' + this.baseuri.get('host') + this.baseuri.get('directory') + this.baseuri.get('file') + '#' + uri.get('directory') + uri.get('file');
					
		// change content
		var myHTMLRequest = new Sandbox.Net.Request.SandboxContent(this.destinations, {
			onSuccess: function(jsondata){
				// well some event stuff..
				if (requestComplete) {
					overlayer.hide();
				}else{
					requestComplete = true;
				}

				// fire event
				this.fireEvent('loaded', [jsondata]);

			}.bind(this),
			onFailure: function(xhr){
				// well some event stuff..
				if (requestComplete) {
					overlayer.hide();
				}else{
					requestComplete = true;
				}			

				this.fireEvent('failure', [xhr.status, xhr.statusText]);	
				
			}.bind(this)

		}).get(json_url);
	}
});