/*
	Helper functions
*/
function in_array( needle, haystack)
{ 
	return ( haystack.indexOf( needle ) < 0 ) ? false : true;
}
function objType (o){
	if (undefined === o) return "undefined";
	if (null === o) return "null";
	return {}.toString.call(o).slice(8, -1).toLowerCase();
}
function randomArray (from, count){ 
    var temp = new Array();
    for (var t = 0; t < count; t++) 
    { 
        for (var j in from){ 
        	var i = Math.floor(Math.random()*from.length); 
			if(!in_array(from[i], temp)){
				temp.push(from[i]);
				break;
			}
        } 
    }
    return temp; 
}
// 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);
					
					if ($.browser.msie && $.browser.version <= 7) //add extra css for IE 6 +7
					{
						var IEcss = document.createElement('link');
						IEcss.type = 'text/css';
						IEcss.rel = 'stylesheet';
						
						var IEfile = ($.browser.msie && $.browser.version < 7) ? '_ie6.css' : '_ie7.css';
						IEcss.href = url + '/' + f + IEfile;
						head.appendChild(IEcss);							
					}
					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);
									}				
								}				
								//call the function / plugin constructor										
								//if browser is less than 7
								if ($.browser.msie && $.browser.version < 7) {
									//if there is png's that need fixing & it hasnt already been done :)
									if (typeof p.ie6 != 'undefined' && !document.getElementById('ie6-js')) {
										//fix all images with .png
										var ie6fix = [{
														name	: "ie6",
														params	: {ie6 : p.ie6}
														}]; 
									   GLoad.Load(ie6fix);
									}
								}
						}
						//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 for old Plugin calls
*/
function Plugin(l, t) {
	GLoad.Load(l, t);
}

$(function(){
/* 	if((window.location.hostname == 'www1.imperial.ac.uk' || window.location.hostname.indexOf('fomdev') != -1) && $('#flashcontainer').length){
		GLoad.Load([
			{
				name	: "mediaplayer",
				dependant:"jScrollPane.js",
				css		:true,
				params	: {}
			}
		]);
	}	 */
});
