$(
	function()
	{
		/*
		 * Placing the html function in a closure so
		 * that it cannot be accessed outside this
		 * source file.
		 */

		var html = function(tag, attributes, contents)
		{
			var result = '<' + tag;

			/*
			 * attributes is map
			 * contents is array, function or string
			 */

			if (contents == undefined)
			{
				if (attributes)
				{
					if (typeof attributes == 'object')
					{
						if (attributes.length)
						{
							contents = attributes;
							attributes = undefined;
						}
					}
					else
					{
						contents = attributes;
						attributes = undefined;
					}
				}
			}

			if (attributes)
			{
				for (var name in attributes)
				{
					/*
					 * IE does not allow class as a map name
					 * except in quotes, so let us allow clazz
					 * to be used instead.
					 */

					if (name == 'clazz')
					{
						result += ' class="' + attributes[name] + '"';
					}
					else
					{
						result += ' ' + name + '="' + attributes[name] + '"';
					}
				}
			}

			var join = function(contents)
			{
				if (contents)
				{
					var type = typeof contents;

					if (type == 'string')
					{
						return contents;
					}
					else if (type == 'function')
					{
						return join(contents());
					}
					else
					{
						var result = '';

						for (var index in contents)
						{
							result += join(contents[index]);
						}

						return result;
					}
				}
				else
				{
					return '';
				}
			}

			result += '>' + join(contents) + '</' + tag + '>';
			return result;
		};

		return function()
		{
			$h1 = function(attributes, contents) {return html('h1', attributes, contents);};
			$h2 = function(attributes, contents) {return html('h2', attributes, contents);};
			$h3 = function(attributes, contents) {return html('h3', attributes, contents);};

			$table = function(attributes, contents) {return html('table', attributes, contents);};
			$caption = function(attributes, contents) {return html('caption', attributes, contents);};
			$tr = function(attributes, contents) {return html('tr', attributes, contents);};
			$th = function(attributes, contents) {return html('th', attributes, contents);};
			$td = function(attributes, contents) {return html('td', attributes, contents);};

			$a = function(attributes, contents) {return html('a', attributes, contents);};

			$form = function(attributes, contents) {return html('form', attributes, contents);};
			$input = function(attributes, contents) {return html('input', attributes, contents);};
			$textarea = function(attributes, contents) {return html('textarea', attributes, contents);};
			$select = function(attributes, contents) {return html('select', attributes, contents);};
			$option = function(attributes, contents) {return html('option', attributes, contents);};

			$p = function(attributes, contents) {return html('p', attributes, contents);};
			$br = function(attributes, contents) {return html('br', attributes, contents);};

			$ul = function(attributes, contents) {return html('ul', attributes, contents);};
			$li = function(attributes, contents) {return html('li', attributes, contents);};

			$img = function(attributes, contents) {return html('img', attributes, contents);};

			$var = function(attributes, contents) {return html('var', attributes, contents);};

			$div = function(attributes, contents) {return html('div', attributes, contents);};
			$span = function(attributes, contents) {return html('span', attributes, contents);};

			$em = function(attributes, contents) {return html('em', attributes, contents);};
		}();
	}
);
