function objType (o){
	if (undefined === o) return "undefined";
	if (null === o) return "null";
	return {}.toString.call(o).slice(8, -1).toLowerCase();
}

// End JQuery


//Replace old writeflash function

function writeFlash(a,b,c){
	//this['writeFlash'] = [a,b,c];
}

/*
	Global variable for feeds
*/
var objFeeds = {};
/*
	GLoad plugin function
*/


var GLoad = {
	pluginURL : null,
	LoadFeed : function(f,n,jc,cb){
					$.getJSON(f+jc+'=?',function(j){
						objFeeds[n] = j;
						if(cb){
							cb(j);
						}
					});
	},
	Each : function (a, f) {
			for (var i = 0, l = a.length; i < l; i++){
				f(a[i]);
			}
	},
	Script : function(f, c){
				var e = document.createElement((c) ? 'link': 'script'),
					type = (c) ? 'css': 'javascript',
					url = GLoad.pluginURL + f;
				e.type = 'text/' + type;

				if (c) {
					e.rel = 'stylesheet';
					e.href = url + '/' + f + '.css';
				} else {
					e.id = f + '-js';
					e.src = (f.substr(f.length-3,f.length) =='.js') ? url :url + '/' + f + '.js';
				}
				var head = document.getElementsByTagName("head")[0];
				if (e !== undefined) {
					head.appendChild(e);
					return true;
				}	
	},
	LoadPlugin : function(f, p){
					setTimeout(function() {
						//if the script is in the head : %name%-js
						if (document.getElementById(f + '-js')) {
							//add the feed stuff here	
								this[f](p);
								if(p.feed){
									if(p.feed.length > 1){
										//load all feeds not yet in use 
									} else {
										//only one feed being used.
										//check if a name is set else use the plugin name						
										GLoad.LoadFeed(p.feed[0].url,(p.feed[0].name) ? p.feed[0].name : f,p.feed[0].callback);
									}				
								}
						}
						//else run again
						else GLoad.LoadPlugin(f, p);
					},
					500);
	},
	Load : function(l,t){
				GLoad.pluginURL = (t) ? 'http://icwwwd.cc.ic.ac.uk/imedia/plugins/' : 'http://www.imperial.ac.uk/imedia/plugins/';
				if(objType(l)==='string'){
					GLoad.Script(l);
				} else {
					GLoad.Each(l, function(i) {
						if(GLoad.Script(i.name)) GLoad.LoadPlugin(i.name, i.params);
						if (i.css) GLoad.Script(i.name, true);
						if (i.dependant) GLoad.Script(i.dependant);			
					});
				}	
	}
}


$(function(){	
		if( $('#flashcontainer').length){
			GLoad.Load([
				{
					name	: "mediaplayer",
					dependant:"jScrollPane.js",
					css		:true,
					params	: {}
				}
			]);	
		}
});
