/**
 * A3non Sandbox
 * 
 * @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.0 BETA4
 */
var Sandbox = new Class({
	Implements: [Options, Events],
	
	options: {
		page404: '',
		languages: {
			'Deutsch': 'de',
			'English': 'en'
		},
		openContentLinksInExternalWindow: true,
		debugMessages:true,
		container: {},
		updateURI: true
	},
	
	version: '1.0',
	baseuri: '',
	container: null,
	loader: null,
	loadedResources: {
		js: [],
		css: []
	},

	initialize: function(options){
		this.setOptions(options);
		
		// setup container
		this.container = new Hash(this.options.container);
		
		// set base uri
		this.baseuri = new URI(document.URL);
		
		// initialize loader
		this.loader = new Sandbox.Net.Request.Loader(this.container, this.baseuri);
		
		// add loader events
		this.loader.addEvent('failure', function(code, msg){
			this.fireEvent('error', [code, msg]);	
		}.bind(this));

		this.loader.addEvent('loaded', function(jsondata){
			this.fireEvent('loaded');
		}.bind(this));		

		// open links in external window ?
		this.addEvent('loaded', function(){
			this.addLinkEvent(this.container);
		}.bind(this));
		
	},
		
	startup: function(){
		this.fireEvent('loaded');
			
		// check if initial anchor is set
		if (this.baseuri.get('fragment') != ''){
			this.load(this.baseuri.get('scheme') + '://' + this.baseuri.get('host') + this.baseuri.get('fragment'));
		}
	},
	
	load: function(url){
		var uri = new URI(url);
				
		// check for http(s) links			
		if (uri.get('scheme') == 'http' || uri.get('scheme') == 'https') {
			if (uri.get('host') && uri.get('host') != this.baseuri.get('host')) {
				if (this.options.openContentLinksInExternalWindow) {
					window.open(url);
					return false;
				}
			}else{
				this.loader.load(url);
				return false;
			}
		}

	},
	
	addLinkEvent: function(cnt){
		cnt.each(function(el, index){
			el.getElements('a').each(function(obj){
				obj.removeEvents('click');
				obj.addEvent('click', function(){
					return this.load(obj.get('href'));
				}.bind(this));
			}.bind(this));
		}.bind(this));
	}
	
});

/**
 * NAMESPACES
 */
Sandbox.UI = {};
Sandbox.Net = {};
Sandbox.Net.Request = {};
Sandbox.Util = {};
