function JTemplate()
{
	this.text_area_temp_id = null;
	this.templateFile = null;
	this.dataFile = null;
	this.resultElementId = null;
	this.postParams = null;
	this.callbackFunction = null;
	this.myspace = false;

	this.generate = function(templateFile, dataFile, resultElementId, paramz)
	{
		var user_language = "";

		try
		{
			var prefs = new gadgets.Prefs();

			if(prefs != null && prefs.getLang() != null)
			{
				// only translated templates and supported languages needs to be
				// included, everything else is english and defauuuulttt ;p
				if(templateFile == "movies_default_page_mostpopularamongfriends_jstl_template.html" ||
					templateFile == "movies_default_page_myspacepopular_jstl_template.html" ||
					templateFile == "movies_get_all_quizzes_template.html" ||
					templateFile == "movies_leaderboard_friends_jstl_template.php" ||
					templateFile == "movies_leaderboard_network_jstl_template.php" ||
					templateFile == "movies_homepage_template.html" ||
					templateFile == "movie_quiz_jstl_template_1.html")
				{
					if(prefs.getLang() == "ru")
					{
						user_language = ".ru";
					}
				}
			}
		}
		catch(e) { }

		var template_file_path = "http://apps.rockyou.com/images/opensocial/movie_trivia/templates/" + templateFile;
		var data_file_path = "google_apps/movies/server/" + dataFile;
		var dot_index = templateFile.indexOf(".");

		this.text_area_temp_id = "template_" + templateFile.substring(0, dot_index);
        this.templateFile = template_file_path + user_language;
		this.dataFile = data_file_path;
		this.resultElementId = resultElementId;

		this.postParams = paramz;

        var ajax_params = {};

        try
        {
	        ajax_params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.HTML;
	        ajax_params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
			// ajax_params[gadgets.io.RequestParameters.POST_DATA] = this.getPostParameterString();
			gadgets.io.makeRequest(this.templateFile, this.template_file_path_response.bind(this), ajax_params);
		}
		catch(e)
		{
			// we're on myspace, so use diff names
			this.myspace = true;
			ajax_params = {};
            ajax_params[opensocial.ContentRequestParameters.CONTENT_TYPE] = opensocial.ContentRequestParameters.ContentType.HTML;
	        ajax_params[opensocial.ContentRequestParameters.METHOD] = opensocial.ContentRequestParameters.MethodType.POST;
			ajax_params[opensocial.ContentRequestParameters.POST_DATA] = this.getPostParameterString();
			opensocial.makeRequest(this.templateFile, this.template_file_path_response.bind(this), ajax_params);
		}
	}

	this.template_file_path_response = function(response)
	{
		var template_data = response.data;

		if(this.myspace || (template_data == undefined))
		{
			template_data = response;
		}

		var text_area     = document.createElement("textarea");
		text_area.setAttribute("style", "display:none;visibility:hidden;height:0px;width:0px;");

		try
		{
			if(text_area.style)
			{
				text_area.style.display = "none";
				text_area.style.visiblity = "hidden";
				text_area.style.height = "0px";
				text_area.style.width = "0px";
			}
		}
		catch(e)
		{
			/* ignore */
		}

		text_area.id = this.text_area_temp_id;

		try
		{
			text_area.innerHTML = template_data;
		}
		catch(e)
		{
			// IE fix....
			text_area.value = template_data;
		}

		var resultElem = document.getElementById(this.resultElementId);
		resultElem.appendChild(text_area);

		genericJSONPost(this.dataFile,
			this.data_file_path_response.bind(this), this.getPostParameterString());
	}

	this.data_file_path_response = function(response)
	{
		var data = response.return_data;
		var parsedJSON = data.evalJSON();
		var quiz_templateResult = document.getElementById(this.resultElementId);

		var result = TrimPath.processDOMTemplate(this.text_area_temp_id, parsedJSON);
		quiz_templateResult.innerHTML = result;

		if(this.callbackFunction != null)
		{
			this.callbackFunction();
		}
	}

	this.getPostParameterString = function()
	{
		var parameter_string = "";

		if(this.postParams != undefined && this.postParams != null)
		{
			parameter_string = this.postParams.toQueryString();
		}

		return parameter_string;
	}

	this.registerAfterGenerateCallback = function(fun)
	{
		this.callbackFunction = fun;
	}
}
