/*a html oldalba a </body> elé kell betenni

<div id="DialogBox" style="display: none;">
	<div class="dialogBoxBorder">
		<div id="dialogContent" class="dialogBoxContent"> </div>
	</div>
</div>
<div id="DialogPreLoading" style="display: none;">
	<center>
		<img class="preSpin" alt="loading" src="../img/preSpin.gif"/>
	</center>
</div>
<div id="DialogBackground" style="display: none;"> </div>
*/

/*a html oldalba be kell linkelni a dialogbox.css-t

<link href="path/dialogbox.css" rel="stylesheet" type="text/css" />

*/

/*template fileokat (kiterjesztésük .tmpl) valahavoa felmásolni és az elérését beállítani

TemplateManager.URL = "dialogtemplate/";

*/

/*Példák a hazsnálathoz:

dialogManager.add(new Dialog('Üzenet ami megjelenik!', 'alert'));

dialogManager.add(new Dialog(
				"Biztosan törölni akarod?",
				"confirm",
				[ null, function () { self.removeMessage(messageId, folder, true); }, null ],
				[ "", "Igen", "Nem" ])
				);
*/



/**
 * Converts object's properties to Map, except functions
 * @return Converted object
 * @type Map
 * @author Cser Dániel
 */
Object.prototype.toMap = function() {
	var map = new Map();
	
	for (var key in this) {
		if (typeof(this[key]) != "function") {
			map.put(key, this[key]);
		}
	}
	
	return map;
}

/** 
 * Collection used to store data by key-value pairs.
 * @constructor
 * @author Cser Dániel
 */
function Map() {
	
	/**
	 * Data holding maps elements
	 * @type Array
	 */
	var data;
	
	/**
	 * Pointer to this
	 * @type Map
	 */
	var self = this;
	
	/**
	 * Put a value by a given key. If the key already exists, it will be overridden.
	 * @param String Key of data
	 * @param mixed Value of data
	 */
	this.put = function(elementKey, elementValue) {
		for (var i = 0; i < data.length; i++) {
			if (data[i].getKey() == elementKey) {
				data[i].setValue(elementValue);
				return;
			}
		}
		
		data.push(new MapElement(elementKey, elementValue));
	}
	
	/**
	 * Returns value by a given key.
	 * @param String Key of value
	 * @return Found value
	 * @type mixed
	 */
	this.get = function(elementKey) {
		for (var i = 0; i < data.length; i++) {
			if (data[i].getKey() == elementKey) {
				return data[i].getValue();
			}
		}
		
		return null;
	}
	
	/**
	 * Returns true when key is exists in the map
	 * @param String Key of value
	 * @return True when key exists
	 * @type boolean
	 */
	this.containsKey = function(elementKey) {
		for (var i = 0; i < data.length; i++) {
			if (data[i].getKey() == elementKey) {
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * Returns true when value is exists in the map
	 * @param mixed Value
	 * @return True when value exists
	 * @type boolean
	 */
	this.containsValue = function(elementValue) {
		for (var i = 0; i < data.length; i++) {
			if (data[i].getValue() == elementValue) {
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * Returns the keyset of thew map
	 * @return Keyset of the map
	 * @type Set
	 */
	this.keys = function() {
		var set = new Set();
		
		for (var i = 0; i < data.length; i++) {
			set.add(data[i].getKey());
		}
		
		return set;
	}
	
	/**
	 * Returns the keyset of thew map
	 * @return Keyset of the map
	 * @type Array
	 */
	this.keysArray = function() {
		var array = new Array();
		
		for (var i = 0; i < data.length; i++) {
			array.push(data[i].getKey());
		}
		
		return array;
	}
	
	/**
	 * Returns the valueset of the map
	 * @return Valueset of the map
	 * @type Set
	 */
	this.values = function() {
		var set = new Set();
		
		for (var i = 0; i < data.length; i++) {
			set.add(data[i].getValue());
		}
		
		return set;
	}
	
	/**
	 * Returns the valueset of the map
	 * @return Valueset of the map
	 * @type Array
	 */
	this.valuesArray = function() {
		var array = new Array();
		
		for (var i = 0; i < data.length; i++) {
			array.push(data[i].getValue());
		}
		
		return array;
	}
	
	/**
	 * Removes an element by the given key
	 * @param String key
	 */
	this.remove = function(elementKey) {
		var newData = new Array();
		
		for (var i = 0; i < data.length; i++) {
			if (data[i].getKey() != elementKey) {
				newData.push(data[i]);
			}
		}
		
		data = newData;
	}
	
	/**
	 * Empties the map
	 */
	this.clear = function() {
		data = new Array();
	}
	
	/**
	 * Returns true when map is empty
	 * @return True when map is empty
	 * @type boolean
	 */
	this.isEmpty = function() {
		return data.length == 0;
	}
	
	/**
	 * Returns the size of the map
	 * @return Size of the map
	 * @type Number
	 */
	this.size = function() {
		return data.length;
	}
	
	/**
	 * Converts map to object
	 * @return Converted map
	 * @type Object
	 */
	this.toObject = function() {
		var obj = new Object();
		
		for (var i = 0; i < data.length; i++) {
			obj[data[i].getKey()] = data[i].getValue();
		}
		
		return obj;
	}
	
	/**
	 * Sorts contained elements by the given function
	 * @param Function Function used to sort
	 */
	this.sort = function(sortBy) {
		data = data.sort(sortBy);
	}
	
	/**
	 * Calls callback on every element of the map
	 * @param Function Callback function
	 */
	this.map = function(callback) {
		for (var i = 0; i < data.length; i++) {
			callback(data[i].getValue());
		}
	}
	
	/**
	 * Returns true if keys and values of maps are equal
	 * @return True if keys and values of maps are equal
	 * @type Boolean
	 */
	this.equalsTo = function(otherMap) {
		if (! checkType(otherMap, Map)) {
			return false;
		}
		
		if (! self.keys().equalsTo(otherMap.keys()) || ! self.values().equalsTo(otherMap.values())) {
			return false;
		}
		
		return true;
	}
	
	/**
	 * Returns the next element relative to the given
	 * @param mixed Key of element
	 * @return The next element relative to the given
	 * @type mixed
	 */
	this.nextElement = function(elementKey) {
		for (var i = 0; i < data.length; i++) {
			if (data[i].getKey() == elementKey) {
				return (data[i + 1] != undefined ? data[i + 1].getValue() : null);
			}
		}
		
		return null;
	}
	
	/**
	 * Returns the previous element relative to the given
	 * @param mixed Key of element
	 * @return The previous element relative to the given
	 * @type mixed
	 */
	this.prevElement = function(elementKey) {
		for (var i = 0; i < data.length; i++) {
			if (data[i].getKey() == elementKey) {
				return (data[i - 1] != undefined ? data[i - 1].getValue() : null);
			}
		}
		
		return null;
	}
	
	//Initialize
	data = new Array();
	
	/**
	 * Internal class for holding a key-value pair
	 * @param String Key of element
	 * @param mixed Value of element
	 */
	function MapElement(elementKey, elementValue) {
		
		/**
		 * Key of element
		 * @type String
		 */
		var key = elementKey;
		
		/**
		 * Value of element
		 * @type mixed
		 */
		var value = elementValue;
		
		/**
		 * Return the key of element
		 * @return Key of element
		 * @type String
		 */
		this.getKey = function() {
			return key;
		}
		
		/**
		 * Returns the value of element
		 * @return Value of element
		 * @type mixed
		 */
		this.getValue = function() {
			return value;
		}
		
		/**
		 * Set the new value of element
		 * @param mixed New value
		 */
		this.setValue = function(elementValue) {
			value = elementValue;
		}
		
	}
	
}

/**
 * Prototype.js style getElementById
 * @param String Id of DOM object
 * @return Found DOM object
 * @type Object
 */
function $(id) {
	return document.getElementById(id);
}

/**
 * Check if the variable is not null and is defined
 * @param mixed Variable to check
 * @return True if it's OK
 * @type Boolean
 */
function check(variable) {
	return (variable != null && variable != undefined);
}

/**
 * Check if the variable is not null and is defined and is instanceof type
 * @param mixed Variable to check
 * @return True if it's OK
 * @type Boolean
 */
function checkType(variable, type) {
	return (variable != null && variable != undefined && variable instanceof type);
}


/**
 * Returns a file content by a given URL
 * @param String URL of file
 * @throws When URL is null
 * @throws When XMLHttpRequest object cannot be created.
 * @return File content
 * @type String
 */
function getStringByUrl(url){
	if (! check(url) || url.length == 0)
		alert("getStringByUrl(): Invalid parameters.");
		
	var randomSeed = Math.floor(Math.random() * (10001));
	
	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
	self.xmlHttpReq.open('GET', url + "?random=" + randomSeed, false);
	self.xmlHttpReq.send(null);
	
	if (self.xmlHttpReq.status != 200) {
		//TODO: Log error
		return;
	}

	return self.xmlHttpReq.responseText;
}


/**
 * Represents an element in the dialog queue
 * @param String Message
 * @param String Type
 * @param Array Callbacks
 * @param Array Captions
 * @author Cser Dániel
 */
function Dialog(dialogMessage, dialogType, dialogCallbacks, dialogCaptions) {
	
	/**
	 * Message
	 * @type String
	 */
	var message;
	
	/**
	 * Type
	 * @type Number
	 */
	var type;
	
	/**
	 * Callbacks
	 * @type Array
	 */
	var callbacks;
	
	/**
	 * Captions
	 * @type Array
	 */
	var captions;
	
	/**
	 * OK constant for captions and callbacks
	 * @type Number
	 */
	Dialog.OK = 0;
	
	/**
	 * YES constant for captions and callbacks
	 * @type Number
	 */
	Dialog.YES = 1;
	
	/**
	 * NO constant for captions and callbacks
	 * @type Number
	 */
	Dialog.NO = 2;
	
	/**
	 * Returns message
	 * @return Message
	 * @type String
	 */
	this.getMessage = function() {
		return message;
	}
	
	/**
	 * Sets message
	 * @param String Message
	 */
	this.setMessage = function(dialogMessage) {
		if (! dialogMessage) {
			//TODO: Log error
			return;
		}
		
		message = String(dialogMessage);
	}
	
	/**
	 * Returns type
	 * @return Type
	 * @type Number
	 */
	this.getType = function() {
		return type;
	}
	
	/**
	 * Sets type
	 * @param Number Type
	 */
	this.setType = function(dialogType) {
		if (! dialogType) {
			//TODO: Log error
			return;
		}
		
		type = Number(dialogType);
	}
	
	/**
	 * Returns the requested callback
	 * @param Number Which callback
	 * @return Requested callback
	 * @type String
	 */
	this.getCallback = function(callbackType) {
		//az összes callback állítása esetén ez nem tömb, ezért ezzel csak az x karaktert adja vissza,
		//mivel String tömbként kezeli, így ezt vizsgálni és kezelni kell
		//return callbacks[Number(callbackType)];
		if( callbacks instanceof Array )
			return callbacks[Number(callbackType)];	
		else
			return callbacks;
	}
	
	/**
	 * Sets the given callback
	 * @param Number Which callback
	 * @param Function Callback function
	 */
	this.setCallback = function(callbackType, dialogCallback) {
		if (! callbackType || ! dialogCallback) {
			//TODO: Log error
			return;
		}
		
		callbacks[Number(callbackType)] = dialogCallback;
	}
	
	/**
	 * Sets all callbacks
	 * @param Array Callback array
	 */
	this.setCallbacks = function(dialogCallbacks) {
		if (! dialogCallbacks || ! dialogCallbacks instanceof Array) {
			//TODO: Log error
			return;
		}
		
		callbacks = dialogCallbacks;
	}
	
	/**
	 * Returns the requested caption
	 * @param Number Which caption
	 * @return Requested caption
	 * @type String
	 */
	this.getCaption = function(captionType) {
		return captions[Number(captionType)];
	}
	
	/**
	 * Sets the given caption
	 * @param Number Which caption
	 * @param String Caption
	 */
	this.setCaption = function(captionType, dialogCaption) {
		if (! captionType || ! dialogCaption) {
			//TODO: Log error
			return;
		}
		
		captions[Number(captionType)] = String(dialogCaption);
	}
	
	/**
	 * Sets all captions
	 * @param Array Captions array
	 */
	this.setCaptions = function(dialogCaptions) {
		if (! dialogCaptions || ! dialogCaptions instanceof Array) {
			//TODO: Log error
			return;
		}
		
		captions = dialogCaptions;
	}
	
	//Initialize
	message = dialogMessage || "";
	type = dialogType || "";
	callbacks = dialogCallbacks || [null, null, null];
	captions = dialogCaptions || ["Ok", "Igen", "Nem"];
	
}

/**
 * Default parser used to parse template
 * @constructor
 * @author Cser Dániel
 */
var DefaultParser = {
	
	/**
	 * Object containing statements
	 * @type Object
	 */
	statements: {
		"if":      { delta: 1,  prefix: "if (", suffix: ") {\n", minParams: 1 },
		
		"elseif":  { delta: 0,  prefix: "} else if (", suffix: ") {\n" },
		
		"else":    { delta: 0,  prefix: "} else {\n" },
		
		"/if":     { delta: -1, prefix: "}\n" },
		
		"for":     {
			delta: 1,
			
			prefix: function(parameters) {
				if (parameters[1] != "in") {
					throw new TemplateParseError("Error parsing template " + name + ", loop with bad parameters: " + parameters.join(" ") + ".");
				}
				
				return [
					"if (typeof(_FORS) == \"undefined\" || ! _FORS.length) {\n  var _FORS = [];\n}\n",
					"_FORS.push(0);\n",
					"if (typeof(" + parameters[2] + ") != \"undefined\" && " + parameters[2] + " instanceof Array) {\n",
					"  var " + parameters[0] + "Array = " + parameters[2] + ";\n",
					"  for (var " + parameters[0] + "Iterator = 0; " + parameters[0] + "Iterator < " + parameters[0] + "Array.length; " + parameters[0] + "Iterator++) {\n",
					"    var " + parameters[0] + " = " + parameters[0] + "Array[" + parameters[0] + "Iterator];\n",
					"    _FORS[_FORS.length - 1]++;\n"].join("");
			},
			
			minParams: 3
		},
		
		"forelse": { delta: 0,  prefix: "  }\n}\n if (_FORS[_FORS.length - 1] == 0) {\n  if (true) {\n" },
		
		"/for":    { delta: -1, prefix: "  }\n}\n" },
		
		"eat":     { delta: 1, prefix: "/*\n" },
		
		"/eat":    { delta: -1, prefix: "*/\n" },
		
		"include": {
			delta: 0,
			
			prefix: function(parameters) {
				var templateName = parameters.shift();
				var templateWith = parameters.shift();
				
				if (templateWith != "with") {
					throw new TemplateParseError("Error parsing template " + name + ", include with bad parameters: " + parameters.join(" ") + ".");
				}
				
				return "_OUT.push(templateManager.getTemplate(" + templateName + ").process({ " + parameters.join(" ") + " }));\n";
			},
			
			minParams: 3 }
	},
	
	/**
	 * Object containing modifiers
	 * @type Object
	 */
	modifiers: {
		//Eats the whole string
		"eat":        function(s) { return ""; },
		
		//Upper-cases string
		"upperCase":  function(s) { return String(s).toUpperCase(); },
		
		//Lower-cases string
		"lowerCase":  function(s) { return String(s).toLowerCase(); },
		
		//Return first param if that is not a nullstring, second param else
		"default":    function(s1, s2) { return String(s1).length > 0 ? s1 : s2; },
		
		//Upper-cases first characters of every word
		"capitalize": function(s) {
			var words = String(s).split(" ");
			var capitalized = [];
			
			for (var i = 0; i < words.length; i++) {
				capitalized.push(this["ucFirst"](words[i]));
			}
			
			return capitalized.join(" ");
		},
		
		//Upper-cases first character
		"ucFirst":    function(s) {
			var oldStr = String(s);
			var newStr = String(oldStr[0]).toUpperCase();
			
			for (var i = 1; i < oldStr.length; i++) {
				newStr += oldStr[i];
			}
			
			return newStr;
		},
		
		//Lower-cases first character
		"lcFirst":    function(s) {
			var oldStr = String(s);
			var newStr = String(oldStr[0]).toLowerCase();
			
			for (var i = 0; i < oldStr.length; i++) {
				newStr += oldStr[i];
			}
			
			return newStr;
		},
		
		//Trims whitespaces form the beginning and end of the string
		"trim":       function(s) { return String(s).replace(/^\s+|\s+$/, ''); }
	}
	
}



/**
 * Represents a parsed template
 * @constructor
 * @author Cser Dániel
 */
function Template(templateName, tmpl, templateParser) {
	
	/**
	 * Template source
	 * @type String
	 */
	var source;
	
	/**
	 * Parsed string
	 * @type String
	 */
	var template;
	
	/**
	 * Name of template
	 * @type String
	 */
	var name;
	
	/**
	 * Template parser object
	 * @type Object
	 */
	var parser;
	
	/**
	 * Temp variable to check the number of start/end tags
	 * @type Number
	 */
	var delta;
	
	/**
	 * Parses the whole template and return the parsed and eval'd code
	 * @param String Template to parse
	 * @throws TemplateParseError on template parsing errors
	 * @return Parsed template
	 * @type String
	 */
	function parse(tmpl) {
		tmpl = new String(tmpl);
		
		tmpl = tmpl.replace(/\t/g, "    "); //convert \t to four spaces
		tmpl = tmpl.replace(/\r\n/g, "\n"); //convert windows line delimiters to unix style
		tmpl = tmpl.replace(/\r/g, "\n"); //convert macosx line delimiters to unix style
		
		var actualPos = -1;
		var parsed = ["function evalTmpl(_OUT, _CONTEXT, _MODIFIERS) { with (_CONTEXT) {\n"];
		
		while (actualPos + 1 < tmpl.length) {
			var statementStart = tmpl.indexOf("{", actualPos);
			
			if (statementStart < 0) {
				break;
			}
			
			var statementEnd = tmpl.indexOf("}", statementStart + 1);
			
			if (statementEnd < 0) {
				break;
			}
			
			if (tmpl.charAt(statementStart - 1) == "$") { //expression which will be shown
				parseText(parsed, tmpl.substring(actualPos, statementStart - 1));
				parseExpression(parsed, tmpl.substring(statementStart + 1, statementEnd));
			} else { //statement
				parseText(parsed, tmpl.substring(actualPos, statementStart));
				parseStatement(parsed, tmpl.substring(statementStart + 1, statementEnd));
			}
			
			actualPos = statementEnd + 1;
		}
		
		if (delta != 0) {
			throw new TemplateParseError("Error parsing template " + name + ", tag start/end tags number are not the same.");
		}
		
		//last piece of text
		parseText(parsed, tmpl.substring(actualPos, tmpl.length));
		
		parsed.push("} }");
		
		eval(parsed.join(""));
		
		return evalTmpl;
	}
	
	/**
	 * Parses the text
	 * @param Array Array to push results
	 * @param String Text to parse
	 */
	function parseText(out, text) {
		if (! text && text.length == 0) {
			return "";
		}
		
		var nlPrefix = 0; //index to first non-newline in prefix.
	    var nlSuffix = text.length - 1; //index to first non-space/tab in suffix.
	    
	    while (nlPrefix < text.length && text.charAt(nlPrefix) == "\n") {
	    	nlPrefix++;
	    }
	    
	    while (nlSuffix >= 0 && (text.charAt(nlSuffix) == " " || text.charAt(nlSuffix) == "\t")) {
	    	nlSuffix--;
	    }
	    
	    if (nlSuffix < nlPrefix) {
	    	nlSuffix = nlPrefix;
	    }
	    
	    var lines = text.substring(nlPrefix, nlSuffix + 1).split("\n");
	    
	    for (var i = 0; i < lines.length; i++) {
			out.push("_OUT.push(\"" + lines[i].replace(/"/g, "\\\"") + "\");\n");
			
			if (i < lines.length - 1) {
	            out.push('_OUT.push("\\n");\n');
	        }
		}
	}
	
	/**
	 * Parses the expressions
	 * @param Array Array to push results
	 * @param String Expression to parse
	 * @throws TemplateParseError on template parsing errors
	 */
	function parseExpression(out, expression) {
		if (! expression && expression.length == 0) {
			return "";
		}
		
		var modifiers = expression.split("|");
		expression = modifiers.shift();
		
		var expr = expression;
		var parts;
		var modifier;
		
		for (var i = 0; i < modifiers.length; i++) {
			parts = modifiers[i].split(":");
			modifier = parts.shift();
			
			if (parser.modifiers[modifier] == null) {
				throw new TemplateParseError("Error parsing template " + name + ", no such modifier: " + modifier + ".");
			}
			
			expr = "_MODIFIERS[\"" + modifier + "\"](" + expr + (parts.length > 0 ? ", " + parts[0].replace(/"/g, "\"") : "") + ")";
		}
		
		out.push("_OUT.push(" + expr + ");\n");
	}
	
	/**
	 * Parses the statements
	 * @param Array Array to push results
	 * @param String Statements to parse
	 * @throws TemplateParseError on template parsing errors
	 */
	function parseStatement(out, stmt) {
		if (! stmt && stmt.length == 0) {
			return;
		}
		
		parameters = stmt.split(" ");
		statement = parameters.shift();
		
		statement = parser.statements[statement];
		
		if (statement == null) {
			parseText(out, stmt);
		}
		
		delta += statement.delta;
		
		if (delta < 0) {
			throw new TemplateParseError("Error parsing template " + name + ", fewer start tags then end tags.");
		}
		
		if (statement.minParams != null && statement.minParams > parameters.length) {
			throw new TemplateParseError("Error parsing template " + name + ", too few parameters.");
		}
		
		if (typeof(statement.prefix) == "function") {
			out.push(statement.prefix(parameters));
		} else {
			out.push(statement.prefix);
		}
		
		if (statement.suffix != null) {
			out.push(parameters.join(" "));
			out.push(statement.suffix);
		}
	}
	
	/**
	 * Processes the template with the given context
	 * @param Object Context of template
	 * @throws TemplateProcessError template on processing errors
	 * @return Ready-to-use template
	 * @type String
	 */
	this.process = function(context) {
		if (! context || ! template) {
			return "";
		}
		
		var processed = [];
		
		try {
			template(processed, context, parser.modifiers);
		} catch(e) {
			throw new TemplateProcessError(e.message, e.fileName, e.lineNumber, e.stack);
		}
		
		return processed.join("");
	}
	
	/**
	 * Returns HTML source
	 * @return HTML source
	 * @type String
	 */
	this.getSource = function() {
		return source;
	}
	
	//Initialize
	if (! tmpl || ! templateName || tmpl.length == 0 || templateName.length == 0) {
		return null;
	}
	
	if (templateParser != null) {
		parser = templateParser;
	} else {
		parser = DefaultParser;
	}
	
	name = templateName;
	delta = 0;
	source = new String(tmpl);
	template = parse(tmpl);
	
}
/**
 * Loads and caches templates.
 * @constructor
 * @author Cser Dániel
 */
function TemplateManager() {
	
	/**
	 * Base dir of templates.
	 * Set in config.js
	 * @type String
	 */
	TemplateManager.URL = "dialogtemplate/";
	
	/**
	 * Map to store templates
	 * @type Map
	 */
	var templates;
	
	/**
	 * Corrects the name of template
	 */
	function correctName(templateName) {
		return String(templateName).toLowerCase();
	}
	
	/**
	 * Returns the template and loads it when necessary
	 * @param String Name of template
	 * @throws On parsing errors
	 * @return Template
	 * @type Template
	 */
	function get(templateName) {
		templateName = correctName(templateName);
		if (! templates.containsKey(templateName)) {
			if (! check(TemplateManager.URL)) {
				//TODO: Log error
				return null;
			}
			
			var templateUrl = TemplateManager.URL + templateName.replace(/_/g, "/") + ".tmpl";
			
			try {
				var templateData = getStringByUrl(templateUrl);
			} catch (e) {
				alert("TemplateHandler.get(): Couldn't read file " + templateUrl + ".");
				//throw new TemplateParseError("TemplateHandler.get(): Couldn't read file " + templateUrl + ".");
			}
			templates.put(templateName, new Template(templateName, templateData));
		}
		return templates.get(templateName);
	}
	
	/**
	 * Returns the given template
	 * @param String Name of template
	 * @throws On parsing errors
	 * @return Template
	 * @type Template
	 */
	this.getTemplate = function(templateName) {
		return get(templateName);
	}
	
	/**
	 * Processes the given template with the given context, then puts it into the given DOM object
	 * @param String Name of template
	 * @param Object Template context
	 * @param String Id of DOM object to put the template
	 * @throws On parsing errors
	 */
	this.processTemplate = function(templateName, templateData, domId) {
		$(domId).innerHTML = get(templateName).process(templateData);
	}
	
	//Initialize
	templates = new Map();
	
}
/**
 * Represents a template parsing error.
 * @constructor
 * @author Cser Dániel
 */
function TemplateParseError(message, fileName, lineNumber, stack) {
	
	/**
	 * Message of error
	 * @type String
	 */
	this.message = message;
	
	/**
	 * Name of the file where the error occured
	 * @type String
	 */
	this.fileName = fileName;
	
	/**
	 * Number of line where the error occured
	 * @type String
	 */
	this.lineNumber = lineNumber;
	
	/**
	 * Stact trace of error
	 * @type Object
	 */
	this.stack = stack;
	
	/**
	 * Name of error
	 * @type String
	 */
	this.name = "TemplateParseError";
	
}
/**
 * Represents a template processing error.
 * @constructor
 * @author Cser Dániel
 */
function TemplateProcessError(message, fileName, lineNumber, stack) {
	
	/**
	 * Message of error
	 * @type String
	 */
	this.message = message;
	
	/**
	 * Name of the file where the error occured
	 * @type String
	 */
	this.fileName = fileName;
	
	/**
	 * Number of line where the error occured
	 * @type String
	 */
	this.lineNumber = lineNumber;
	
	/**
	 * Stact trace of error
	 * @type Object
	 */
	this.stack = stack;
	
	/**
	 * Name of error
	 * @type String
	 */
	this.name = "TemplateProcessError";
	
}




/**
 * Dialog window manager class.
 * @constructor
 * @author Cser Dániel
 */
function DialogManager() {
	
	/**
	 * DialogBox width size
	 * @type integer
	 */
	var width = 310;
	
	/**
	 * DialogBox height size
	 * @type integer
	 */
	var height = 180;

	/**
	 * Array of disabled selects
	 * @type Array
	 */
	var disabledSelects;
	
	/**
	 * Queue array
	 * @type Array
	 */
	var queue;
	
	/**
	 * Running state
	 * @type Boolean
	 */
	var running;
	
	/**
	 * Adds a new task
	 * @param Dialog Dialog to add
	 */
	this.add = function(dialog) {
		//when this is a progress or there's no progress in the queue
		queue.push(dialog);
		//return when already displaying or there's nothing to display
		if (running) {
			return;
		}
		
		//start showing
		this.running = true;
	    
	    //disable active selects
		var allSelects = document.getElementsByTagName("select");
		for (i = 0; i < allSelects.length; i++) {
    		if (allSelects[i].disabled == false) {
    			disabledSelects[i] = allSelects[i];
    			disabledSelects[i].disabled = true;
    		}
			allSelects[i].style.visibility = 'hidden';
    	}
	    
	    //hider div
	    var pageSize = getPageSize();
	    setOpacity( $("DialogBackground"), 0.9 );
	    $("DialogBackground").style.height = pageSize.pageHeight+'px';
	    $("DialogBackground").style.display = "block";
	    
	    //dialog box
	    getWindow( height, width );
	    
	    //start showing
	    show();
	}
	
	/**
	 * Shows a task
	 */
	function show() {
		try {
			window.scrollBy(0, 0);
		    templateManager.processTemplate(queue[0].getType(), { dialog: queue[0] }, "dialogContent");
		} catch(e) {
			//TODO: Log error
		}
	}
	
	/**
	 * One task ended
	 * @param Number Result code
	 */
	this.done = function(result) {		
		
		if (queue.length == 0 || result == null || result == undefined) {
			//TODO: Log error
			return;
		}
		
		var fn = queue[0].getCallback(result);
		if (typeof(fn) == "function") {
			try {
				fn();
			} catch(e) {
				//TODO: Log error
			}
		}
	    //remove first element
	    queue.shift();
	    //when this was the last one in queue
	    if (queue.length == 0) {
	    	running = false;
	    	
			//hide dialog and bakground
			$("DialogBackground").style.display = "none";
			$("DialogBox").style.display = "none";
			$("dialogContent").innerHTML = "";
			//enable all disabled selects and clear the array holding them
	    	for (var i = 0; i < disabledSelects.length; i++) {
		    	disabledSelects[i].disabled = false;
				disabledSelects[i].style.visibility = 'visible';
	    	}
	    	
	    	disabledSelects = new Array();
			
		    return;
	    }
	    show();
	}
	
	/**
	 *
	 */
	this.showBackground = function() {
		var pageSize = getPageSize();
	    setOpacity( $("DialogBackground"), 0.3 );
	    $("DialogBackground").style.height = pageSize.pageHeight + 'px';
	    $("DialogBackground").style.display = "block";
		return;
	}
	
	/**
	 *
	 */
	this.hideBackground = function() {
		$("DialogBackground").style.display = "none";
		return;
	}
	
	/**
	* set DialogBackground style
	* @param htmlObject 
	* @param opacity value
	*/
	function setOpacity(element, value) {
	    if (typeof element == 'string')
		element= $(element);
	    if (value == 1) {
		element.style.opacity = (/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ? 0.999999 : 1.0 ;
		if(/MSIE/.test(navigator.userAgent) && !window.opera)
		    element.style.filter = element.style.filter.replace(/alpha\([^\)]*\)/gi,'');
	    } else {
		if(value < 0.00001) value = 0;
		    element.style.opacity = value;
		if(/MSIE/.test(navigator.userAgent) && !window.opera)
		    element.style.filter = element.style.filter.replace(/alpha\([^\)]*\)/gi,'') + 'alpha(opacity='+value*100+')';
	    }
	    return element;
	}
	
	
	/**
	* set DialogBox style and position
	* @param box height
	* @param box width
	*/
	function getWindow(height, width) {
	    var DialogBox = $("DialogBox");
	    var pageSize = getPageSize();
	    var pos = realOffset(document.body);
	    
	    DialogBox.style.top = (pageSize.windowHeight/2 - height/2 + pos[1])+'px';
	    DialogBox.style.left = (pageSize.windowWidth/2 - width/2 + pos[0])+'px';
	    
	    DialogBox.style.display = "block";
	}
	
	/**
	* set DialogBox real position
	* @return top, left size
	* @type Array
	*/
	function realOffset(element) {
	    var valueT = 0, valueL = 0;
	    do {
		valueT += element.scrollTop  || 0;
		valueL += element.scrollLeft || 0;
		element = element.parentNode;
	    } while (element);
	    return [valueL, valueT];
	}
	
	
	/**
	* get page, widow, scroll height and width size 
	* @return size data
	* @type object
	*/
	function getPageSize() {
	    var xScroll, yScroll;
            if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	    } else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	    } else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	    }
	    
	    var windowWidth, windowHeight;
	    if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	    } else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	    } else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	    }
	    
	    if(yScroll < windowHeight) {
		pageHeight = windowHeight;
	    } else {
		pageHeight = yScroll;
	    }
            if(xScroll < windowWidth) {
	        pageWidth = windowWidth;
	    } else {
		pageWidth = xScroll;
	    }
	    return {
		'pageWidth':pageWidth,
		'pageHeight':pageHeight,
		'windowWidth':windowWidth,
		'windowHeight':windowHeight,
		'yScroll':yScroll,
		'xScroll':xScroll
	    }
	}
	
	//Initialize
	disabledSelects = new Array();
	queue = new Array();
	running = false;
	
}

templateManager = new TemplateManager();
dialogManager = new DialogManager();
