/*
	# Copyright 2009, Hoop Associates Ltd
	# Hoop Associates   www.thisishoop.com   mail@hoopassociates.co.uk
*/
function Loader(){
	this.options = {};
	this.callback = (function(){});
	this.scripts = [];
	this.loadedScripts = [];
	this.what = '';
	this.progressbar = {};
}

Loader.prototype.load = function(what, options, callback) {
	var self = this;
	this.what = what || this.what;
	this.options = options || this.options;
	this.callback = callback || this.callback;
	$("head script, head link").each(function(){
		self.loadedScripts.push(this.nodeName.toLowerCase() == 'link' ? this.href : this.src);
	});
	this.progressbar = new Progressbar("progressbar");
	(typeof this[what] == 'function') && (this[what]());
};

Loader.prototype.get = function(scripts, finishedSingleCallback, finishedAllCallback, position) {
	(position == undefined) && (position = 0);
	var self = this; 
	this.scripts = scripts;
	// don't load this script if it has already been loaded
	if (new RegExp("/.*"+scripts[position]+".*/", "i").test(this.loadedScripts.join(" ")) || new RegExp("/.*"+(scripts[position].toString().replace(/^\/sledge/, ""))+".*/", "i").test(this.loadedScripts.join(" "))) {
		if (position+1 == scripts.length) {
			setTimeout(finishedAllCallback, 1);
			return;
		}
		position++;
		(typeof finishedSingleCallback == "function") && (finishedSingleCallback(position));
		this.get(scripts, finishedSingleCallback, finishedAllCallback, position);
	} else {
		var xhr = 
		$.get(scripts[position], function(data){
			self.loadedScripts.push(scripts[position]);
			if (/js$/.test(scripts[position])) {
				$("head").append('<script type="text/javascript">'+data+'</script>');	
			} else if (/css$/.test(scripts[position])) {
				$("head").append('<style type="text/css">'+data+'</style>');	
			}
			if (position+1 == scripts.length) {
				setTimeout(finishedAllCallback, 1);
				return;
			}
			position++;
			(typeof finishedSingleCallback == "function") && (finishedSingleCallback(position));
			self.get(scripts, finishedSingleCallback, finishedAllCallback, position);
		});
	}
};

Loader.prototype.TagManager = function(){
	var
	 self = this,
	scripts = [
		["/sledge/css/"+this.options.edition+"_tree.css"],
		["/sledge/css/"+this.options.edition+"_tagmanager.css"],
		["/sledge/js/ui/ui_core.js"],
		["/sledge/js/ui/ui.mouse.js"],
		["/sledge/js/ui/"+this.options.edition+"_ui_resizable.js"],
		["/sledge/js/jquery_history.js"],
		["/sledge/js/tree.js"]
	];
	if (this.options.edition == "site") {
		scripts.push(["/sledge/css/"+this.options.edition+"_tagmanager_thickbox.css"]);
		scripts.push(["/sledge/js/"+this.options.edition+"_tagmanager_thickbox.js"]);
	}
	for (var type in this.options.itemtypes) {
		if (!/ALL/i.test(this.options.itemtypes[type])) {
			if (this.options.itemtypes[type] == 'person') {
				scripts.push("/sledge/js/"+this.options.edition+"_tagmanager_permissions.js");
			}
			scripts.push("/sledge/js/"+this.options.edition+"_tagmanager_" + this.options.itemtypes[type] + ".js");
			scripts.push("/sledge/css/"+this.options.edition+"_"+this.options.itemtypes[type]+"manager.css");
		}
	}
	scripts.push("/sledge/js/" +this.options.edition+ "_tagmanager.js");
	this.get(scripts, 
	function(position){
		self.progressbar.inc(Math.round((position/self.scripts.length)*100));
	},
	function(){
		self.progressbar.inc(100);
		setTimeout(function(){
			$("#components").remove();
			$("#hiddenpanes").fadeIn(function(){
				for (var i in self.options) {
					window[self.what][i] = self.options[i];
				}
				(typeof window[self.what].init == "function") && (window[self.what].init());
			});
		}, 200);
	});
}

var Loader = new Loader();

function Progressbar(id) {
	this.id = id || '';
	this.value = 0;
	this.barlayer = $();
	this.valuelayer = $();
	(id) && (this.init(id));
}
Progressbar.prototype.init = function(id){
	this.id = id || '';
	this.barlayer = $("#"+this.id).attr("class", "loader-progressbar loader-corner-all");
	if (!this.barlayer.length) {
		return;
	}
	this.valuelayer = $('<div></div>').attr("class", "loader-progressbar-value loader-corner-all").css({width:this.value+"%"});
	this.barlayer.append(this.valuelayer);
}
Progressbar.prototype.inc = function(width){
	this.valuelayer.css({width:width+"%"});
}
