var CrackerJax = {
	make_tags: function(obj) {
		var item, tag, new_tag, sub_tag, i, n, test, method = obj['method'];
		var len = obj.length;

		for (var n = 0; n < len; n++) {
			// Set variables:
			var method = obj[n]['method'];
			var container = document.getElementById(obj[n]['container']);
			
			// Clear container content if replacing
			if (obj[n]['method'] == 'replace') {
				container.innerHTML = '';
			}

			if (typeof(obj[n]['content']) == 'object') {
				var len2 = obj[n]['content'].length;

				for (var i = 0; i < len2; i++) {
					// Create new tag or append text:
					new_tag = new CrackerJax.sub_tag(obj[n]['content'][i]);

					// Add new tag to the container:
					new CrackerJax.add_tag(method, new_tag, container);
				}
			} else {
				new_tag = new CrackerJax.sub_tag(obj[n]);

				// Add new tag to the container:
				new CrackerJax.add_tag(method, new_tag, container);
			}

		}
	},
	
	add_tag: function(method, tag, container) {
		// Choose a way to add a tag:
		if (method == 'appendBefore') {
			container.insertBefore(tag, container.childNodes[0]);
		} else if (method == 'appendAfter') {
			container.appendChild(tag);
		} else if (method == 'replace') {
			container.appendChild(tag);
		}
	},
	
	sub_tag: function(obj) {
		var new_tag = '';
		new_tag = new CrackerJax.create_tag(obj);

		if (typeof(obj['content']) == 'object') {
			var len = obj['content'].length;

			for(var n=0; n < len; n++) {
				if (obj['content'][n]['tag']) {
					new_tag.appendChild(new CrackerJax.sub_tag(obj['content'][n]));
				} else {
					// No child tag was created, just text. Create a text node containing text and append to last tag:
					if(obj['content'][n]['content']) {
						new_tag.appendChild(document.createTextNode(obj['content'][n]['content']));
					}
				}
			}
		} else {
			// Create a text node containing text and append to last tag:
			if(obj['content']) {
				new_tag.appendChild(document.createTextNode(obj['content']));
			}
		}

		return new_tag;
	},
	
	create_tag: function(obj) {
		var attributes = new Array('className','href','title','alt','id','rel','target','style','onclick','src');
		var new_tag;

		new_tag = document.createElement(obj['tag']);

		var x, attLen = attributes.length;
		for(x = 0; x < attLen; x++) {
			if (obj[attributes[x]]) {
				if (attributes[x] == 'style') {
					
					styles = obj[attributes[x]];
					styles = styles.split(';');
					
					for(var n = 0; n < styles.length; n++) {
						values = styles[n].split(':');
						
						var t = 0;
						for(var i = 0; i < values.length; i++) {
							if (t == 0) {
								stl = values[i];
								t = 1;
							} else {
								val = values[i]
								t = 0;
								
								new_tag.style[stl] = val;
							}
						}
					}
				} else {
					new_tag[attributes[x]] = obj[attributes[x]];
				}
			}
		}
		
		return new_tag;
	}
}
