var templateRegistry = new Array();

function getTemplate(id)
{
    if(document.getElementById(id) == null) return null;
	try{
		if(!templateRegistry[id])
			templateRegistry[id] = TrimPath.parseDOMTemplate(id);
		return templateRegistry[id];
	}
	catch(err){
		jAlert('Error in parsing '+ id +' jstl', 'Error', null, err.name+': '+err.message);
	}
}

function getTemplateResource(elementId)
{
	var jstlElement = $("#"+elementId)
	if(jstlElement.length > 0)
		return jstlElement.val()
	return null
}

function userMessage(message,time)
{
	if(!time)
		time=2000;
	$.growlUI('',message,time)
}


jQuery.fn.osload = function()
{
	return this.each
	(
		function()
		{
			if(jQuery.metadata)
			{
				var meta = jQuery.metadata.get(this);
				var type = meta.type;
				var url = meta.url;
				var typeName = meta.typeName;
				var form = meta.form;
				var filter = meta.filter
				var metadataType = meta.metadataType
				if(url)
				{
					var container = $(this)
					var urlList = url.split('?')
					var urlList = urlList[0].split("/")
					var options = {applicationId: urlList[1]}
					if(metadataType)
						options["metadataType"] = metadataType
					options["form"] = form
					if(filter){
						if(arrayContainsKey(filter, "asc")){
							options["asc"] = filter["asc"]
							delete filter["asc"]
						}else if(arrayContainsKey(filter, "desc")){
							options["desc"] = filter["desc"]
							delete filter["desc"]
						}
						options["data"] = filter
					}
					options["success"] =  function(jDataObject, dataObject, json){
												if(typeName == "Report" || typeName == "Worktop")
													dataObject = json
												fnLoad(container.attr('id'), type, meta, dataObject, url);
											}
					options["error"] = errorCallback

					if(typeName == "Editor"){
						options["instanceId"] =  urlList.pop();
						options["modelId"] =  urlList.pop();
						var activityId_report = urlList[4];
						if (activityId_report != "report"){
							//then it is normal editor page
							options["activityId"] = activityId_report
						}
						else{
							//it is report, 6th param contains the reportId
							options["reportId"] = urlList[5];
						}
						container.osInit(container.attr('id'), 'read', options);
					}else if(typeName == "Inbox"){
						options["pageSize"] =  urlList.pop();
						options["pageNumber"] =  urlList.pop().split("p")[1];
						var reportId_activityId =  urlList.pop();
						var modelId_report = urlList.pop();
						if(modelId_report != "report"){
							//then it is inbox
							options["activityId"] = reportId_activityId;
							options["modelId"] =  modelId_report;
						}else{
							//it is report list view
							//modelId_report ,report key word is never used
							options["reportId"] = reportId_activityId;
							options["modelId"] =  urlList.pop();
						}
						container.osInit(container.attr('id'), 'list', options);
					}else if(typeName == "Report" || typeName == "Worktop"){
						options["url"] =  url;
						options["dataType"] =  "json";
						container.osInit(container.attr('id'), 'aggregate', options);
					}else if(typeName == "Search"){
						options["pageSize"] =  urlList.pop();
						options["pageNumber"] =  urlList.pop().split("p")[1];
						//pop search keyword
						urlList.pop();
						options["cellName"] =  urlList.pop()
						options["instanceId"] =  urlList[6];
						options["modelId"] =  urlList[5];
						options["activityId"] = urlList[4];
						if(urlList.length > 7){
							options["referenceInstanceId"] = urlList.pop()
							options["referenceModelId"] = urlList.pop()
						}
						container.osInit(container.attr('id'), 'advancedSearch', options);
					}else{
						jQuery(this).error("unknown type found while loading metadata plugin "+typeName);
					}

				}
				else
					jQuery(this).error("url not specified");
			}
			else
				jQuery(this).error("metadata plugin not loaded");
		}
	);
}