
/**
 * ContentLoad class:
 *	Loads js templates dynamically and makes them available
 *	for other objects to use.
 *
 *	Templates are loaded with synchronous ajax requests and then cached
 *	so they only need to be requested once.
 */

var contentLoad = new content_loader();

function content_loader() {
	
	/**
	 * Loads the required template from remote
	 * page into page header so it is accessable.
	 * @param id	string id of template
	 * @return string template content.
	 */
	this.get_template_by_id = function(id, callback) {
		
		if($('#'+id).html()) {
			return $('#'+id).html();
		}
		
		var template = $.ajax({
			url: "content/templates.php",
			type: "GET",
			data: {t : id},
			async: false}).responseText;
		
		$('head').append("<script type=\"text/html\" id=\""+id+"\">"+template+"</script>");
		
		return template;
		
	};
	
	/**
	 * Moves a template into the html body appended to the end
	 * of the document.
	 * @param id	string id of template.
	 * @return
	 */
	this.move_to_body = function(id) {
		var html = $('#'+id).html();
		$('#'+id).remove();
		$('body').append(html);
	};
	
}
