/*
docs 
funky functions .. agg array, ? random entries
*/

var url = null;
//global variable holding the feeds for all loaded plugins
var objFeeds = new Object();
function LoadFeed(feed,name,callback){	
	$.getJSON(feed+callback+'=?',function(json){
		setGlobalFeed(json,name);
	});
}
function setGlobalFeed(f,n){
	objFeeds[n] = f;	
}
function getRandom (from, count, cb){
	var temp = new Array();
	var dummy = new Array();
	for (var t = 0; t < count; t++)
	{
		for (var j = 0; j < from.length-1; j++){
		var i = Math.floor(Math.random()*from.length);
			if(dummy[i] == undefined){						
				temp[t] = from[i];
				dummy[i] = true;
				break;
			}
		}
	}
	if(cb)
		this[cb](temp)
	else 
		return temp;
}
/*function objType(o) {
	if (undefined === o) return "undefined";
	if (null === o) return "null";
	return {}.toString.call(o).slice(8, -1).toLowerCase();
}*/

function Each(array, block) {
    for (var i = 0, l = array.length; i < l; i++)
    block(array[i]);
};
function Plugin(l, test) {
	url = (test) ? 'http://icwwwd.cc.ic.ac.uk/imedia/dynamic_addin/manager/' : 'http://www.imperial.ac.uk/imedia/dynamic_addin/manager/' ;
/*	if(undefined === test){
		url = 'http://www.imperial.ac.uk/imedia/dynamic_addin/manager/';
	} else if(objType(test) == 'boolean' && test == true){
		url = 'http://icwwwd.cc.ic.ac.uk/imedia/dynamic_addin/manager/';
	} else {
		url = test;
	}*/

    Each(l, function(i) {
        script(i.name);
        if (i.css) script(i.name, true);
        LoadPlugin(i.name, i.params);
    });
};
function script(file, css) {
    var elem = document.createElement((css) ? 'link': 'script');
    var type = (css) ? 'css': 'javascript';
    elem.type = 'text/' + type;

    if (css) {
        elem.rel = 'stylesheet';
        elem.href = url + file + '/' + file + '.css';
    } else {
        elem.id = file + '-js';
        elem.src = url + file + '/' + file + '.js';
    }
    if (elem != undefined) document.getElementsByTagName("head")[0].appendChild(elem);
}

function LoadPlugin(f, p) {
    setTimeout(function() {
		//if the script is in the head : %name%-js
        if (document.getElementById(f + '-js')) {
            //add the feed stuff here
			if(p.feed){
				if(p.feed.length > 1){
					//load all feeds not yet in use 
				} else {
					//only one feed being used.					
					LoadFeed(p.feed[0].url,(p.feed[0].name) ? p.feed[0].name : f,p.feed[0].callback);
				}				
			}
			//call the function / plugin constructor			
			this[f](p);
			//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}
									}]; 
                   Plugin(ie6fix);
                }
            }
        }
		//else run again
        else LoadPlugin(f, p)
    },
    500);
}
