// JavaScript Document
if($){
	$.fn.outerHTML=function(){		
		return $("<div></div>").append(this).html();
	}
	//SimpleTable - builds a simple table (w/options)
	function SimpleTable(cols, rows) {
		this.width = "100%";
		this.padding = 0;
		this.spacing = 0;
		this.border = 0;
		this.className;
		this.id;
		
		var thisRef = this;
		var cCount = parseInt(cols);
		var rCount = parseInt(rows);
			
		//Cell :)
		var _Cell = function() {
			this.value = "";
					
			this.attributes = new Array();
			var attr = function(n, v) {this.name = n;this.value = v;}
			//Finds attribute
			this.indexOf = function(n) {
				var attLen = this.attributes.length - 1;
				var idx = 0;
				var cAtt;
				if(attLen >= 0) {
					do {
						cAtt = this.attributes[attLen];
						if(cAtt.name == n) return attLen;
					} while(attLen--);
				}
				return -1;
			}
			
			//Sets Attribute (overwrites existing)
			this.setAttribute = function(n, v) {
				var idxOf = this.indexOf(n);
				if(idxOf != -1) {
					this.attributes[idxOf].value = v;
				} else {
					this.attributes.push(new attr(n,v));
				}
			}
		}
		//Get Cell :)
		this.Cell = function(c, r) {
			if((c >= 0 && c <= cCount) && (r >= 0 && r <= rCount)) {
				return thisRef.Rows[r][c];
			}
			return null;
		}	
			
		//set all attributes for a column
		function buildCell(col, cell) {
			if(col.attributes.length > 0) {
				var aLen = col.attributes.length - 1;
				var cAtt;
				do {
					cAtt = col.attributes[aLen];
					cell.attr(cAtt.name, cAtt.value);
				} while(aLen--);
			}
		}
		
		//Create Grid
		if(cCount > 0 && rCount > 0) {
			this.Rows = new Array(rCount);
			var tRow, tCols, tRows;
			tRows = rCount-1;
			do {
				tCols = cCount-1;
				tRow = new Array();
				thisRef.Rows[tRows] = tRow;
				do {	
					tRow[tCols] = new _Cell();					
				} while(tCols-- != 0);		
			}while(tRows-- != 0);
		}
		//Output a table
		this.toHTML = function() {
			var nTable = $("<table></table>");
			if(thisRef.width != null) { nTable.attr("width", thisRef.width); }
			if(thisRef.padding != null) { nTable.attr("cellpadding", thisRef.padding); }
			if(thisRef.spacing != null) { nTable.attr("cellspacing", thisRef.spacing); }
			if(thisRef.border != null) { nTable.attr("border", thisRef.border); }
			if(thisRef.className != null) { nTable.attr("class", thisRef.className); }
			if(thisRef.id != null) { nTable.attr("id", thisRef.id); }
			
			var tr = $("<tr></tr>");
			var td = $("<td></td>");
			
			var cc, rc, cTr, cTd, cl;
			var r = 0;
			var c = 0;
			rc = thisRef.Rows.length - 1;
			do {
				c = 0;
				cc = thisRef.Rows[r].length - 1;			
				cTr = tr.clone();
				do {
					cTd = td.clone();
					cl = thisRef.Rows[r][c];				
					cTd.html((cl.value != null)?cl.value:"&nbsp;");
					buildCell(cl, cTd);
					cTr.append(cTd);
				} while(c++ < cc);
				nTable.append(cTr);
			} while(r++ < rc);
			return nTable.outerHTML();				
		}
	}
	
	//SimpleImage
	function SimpleImage(src) {
		this.link;
		this.width;
		this.height;
		this.src = src;
		this.alt;
		this.className;
		
		var thisRef = this;
		
		this.toHTML = function() {
			var iObj = $("<img></img>");
			
			if(thisRef.src != null) { iObj.attr("src", thisRef.src); }
			if(thisRef.className != null) { iObj.attr("class", thisRef.className); }
			if(thisRef.width != null) { iObj.attr("width", thisRef.width); }
			if(thisRef.height != null) { iObj.attr("height", thisRef.height); }
			if(thisRef.alt != null) { iObj.attr("alt", thisRef.alt); }
			if(thisRef.link != null) {
				iObj.attr("border", 0);
				var aObj = $("<a></a>");
					aObj.attr("href", thisRef.link);
					aObj.append(iObj);				
					return aObj.outerHTML();
			} else {
				return aObj.outerHTML();
			}				
		}
	}
	
	// Simple Button
	function SimpleButton(text) {
		this.id;
		this.action;
		this.className;
		this.width;
		this.height;
		this.value = text;
		this.isDisabled = false;
		
		var thisRef = this;
		
		this.toHTML = function() {
			var btn = $("<input></input>");
				btn.attr("type", "button");
				btn.attr("value", thisRef.value);
				
				if(thisRef.id != null) { btn.attr("id", thisRef.id); }
				if(thisRef.action != null) { btn.attr("onClick", thisRef.action); }
				if(thisRef.className != null) { btn.attr("class", thisRef.className); }
				if(thisRef.width != null) { btn.attr("width", thisRef.width); }
				if(thisRef.height != null) { btn.attr("height", thisRef.height); }
				if(thisRef.isDisabled) { btn.attr("disabled", "true"); }
			return btn.outerHTML(btn);
		}
	}
	
	//Simple Select
	function SimpleSelect() {
		this.id;
		this.action;
		this.className;
		this.width;
		this.isDisabled = false;
		this.multiple = false;
		this.size;
		
		var thisRef = this;
		var Items = new Array();
		
		this.AddItem = function(name, value, isDef) {
			Items.push([name, value, (isDef != null && isDef == true)?true:false]);		
		}
		
		this.SelectItem = function(idx) {
			if(idx > 0 && idx < Items.length) {
				Items[idx][2] = true;
			}
		}
		
		this.toHTML = function() {
			var sel = $("<select></select>");
			
			if(thisRef.id != null) { sel.attr("id", thisRef.id); }
			if(thisRef.action != null) { sel.attr("onChange", thisRef.action); }
			if(thisRef.className != null) { sel.attr("class", thisRef.className); }
			if(thisRef.width != null) { sel.attr("width", thisRef.width); }
			if(thisRef.size != null) { sel.attr("size", thisRef.size); }
			if(thisRef.isDisabled) { sel.attr("disabled", "true"); }
			if(thisRef.multiple) { sel.attr("multiple", "true"); }
			
			var opt = $("<option></option>");
			if(Items.length > 0) {
				var oLen = 	Items.length-1;	
				var o = 0;
				var thisOpt;
				var thisItem;
				do {
					thisItem = Items[o];
					if(thisItem[2]) { thisOpt =  $("<option selected=\"selected\"></option>");} else {
						thisOpt = opt.clone();
					}
					thisOpt.html(thisItem[0]);
					thisOpt.attr("value", thisItem[1]);				
					sel.append(thisOpt);
				} while(o++ < oLen);
			}
			return sel.outerHTML(sel);
		}
	}
	
	//Simple Link
	function SimpleLink(url, title) {
		this.id;
		this.href = url;
		this.className;
		this.target;
		this.title = title;
		
		var thisRef = this;
		
		this.toHTML = function() {
			var lnk = $("<a></a>");
			if(thisRef.id != null) { lnk.attr("id", thisRef.id); }
			if(thisRef.href != null) { lnk.attr("href", thisRef.href); }
			if(thisRef.className != null) { lnk.attr("class", thisRef.className); }
			if(thisRef.target) { lnk.attr("target", thisRef.target); }
			lnk.html(thisRef.title);
			
			return lnk.outerHTML(lnk);
		}	
	}
} else {alert("jQuery library is not present");}