var NEWVERSION = ".";
var DEBUG = true;
var controllerRegistry = new Array();

jQuery.fn.debug = function(str){if(DEBUG)log(this, str, "debug");}

jQuery.fn.info = function(str){log(this, str, "info");}

jQuery.fn.warn = function(str){log(this, str, "warn");}

jQuery.fn.error = function(str){log(this, str, "error");}

function log(elm, str, level)
{
	if(window.console){
		//eval("console."+level+"('"+str+"')");
		if(level=='info')
			console.info(str)
		else if(level=='warn')
			console.warn(str)
		else if(level=='debug')
			console.debug?console.debug(str):console.info(str)
		else if(level=='error')
			console.error(str)
	}
}

function userErrorAlert(errorMessage, errorTrace)
{
	jAlert('Error '+errorMessage,'Error',null,errorTrace);
}

function fnLoad(id, type, meta, data, url)
{
	var portlet = jQuery("#"+id);
	if(this[type])
	{
		var cntl=null;
		if(!(controllerRegistry[id]))
		{
			cntl = eval("new "+type+"()");
			controllerRegistry[id] = cntl;
		}
		else
		{
			cntl=controllerRegistry[id];
			cntl.resetControllerState();
		}

		if(data && url){
			//for studio data and url will be null
			cntl.url = url;
			cntl.graph=data;
		}
		if(meta.callback){
			cntl.callback = meta.callback;
		}
		cntl.meta=meta
		cntl.id = id;
		cntl.type = type;
		cntl.portlet = portlet;
		currentPortletId = id;
		cntl.init();
		cntl.start();
		this[id] = cntl;

		//for studio scripting api
		this['ScriptableStudio'] = cntl;
	}
	else
		portlet.error("Definition for "+type+" not available");
}

function fnHandleKeys(evt)
{
	var event;
	if(evt)
		event = evt;
	else
		event = window.event

	for(var cnt in controllerRegistry)
		controllerRegistry[cnt].onkeydown(event);
}