// Lewjax xgrid widget stuff
// dependencies: lewjax.xmlwriter.js


function xgrid() {
	this.headings = new Array();
	if (arguments.length==1) {
		for (var i=0; i<arguments[0].length;i++) {
			this.headings[this.headings.length] = new xgridHeading(arguments[0][i][0],arguments[0][i][1],arguments[0][i][2],arguments[0][i][3]);
		}
	}
	this.rows = new Array();
	this.sortIndex = 0;
	this.container = null;
	this.id = null;
	this.lastSort=-1;
}

function xgridHeading(value, ref, type, width, index) {
	this.value = value;
	this.ref = ref;
	this.type = type;
	this.width = width;
	this.index = index;
}

xgrid.prototype.sort = function(i) {
	if (this.lastSort==i) {
		this.rows.reverse();
	}
	else {
		if (this.headings[i].type == 'html') {
			// we can't sort this :s.. can we?
			return true;
		}
		this.lastSort = i;
		if (this.headings[i].type == 'number') {
			this.rows.sort(function(a,b) {
				return a.fields[i].value - b.fields[i].value;
			});
		}
		else {
			// assume string for the mo
			this.rows.sort(function(a,b) {
				if (a.fields[i].value==b.fields[i].value) return 0;
				var test = new Array(a.fields[i].value,b.fields[i].value);
				test.sort();
				if (test[0] == a.fields[i].value) return -1;
				return 1;
			});	
		}
	}
	for (var i=0;i<this.rows.length;i++) {
		this.rows[i].index = i;
	}
	if (this.container!=null && this.id!=null)
		this.shuffleRows((this.rows.length < 100));
}

xgrid.prototype.setContainer = function(c) {
	this.container = c;
}

xgrid.prototype.setHeadings = function() {
	if (arguments.length==1) {
		for (var i=0; i<arguments[0].length;i++) {
			this.headings[this.headings.length] = new xgridHeading(arguments[0][i][0],arguments[0][i][1],arguments[0][i][2],arguments[0][i][3],i);
		}
	}
}

function xgridRow() {
	this.fields = new Array();
	if (arguments.length>=1) {
		for (var i=0;i<arguments[0].length;i++) {
			this.fields[i] = new xgridField(arguments[0][i]);
		}
	}
	this.index = -1;
	this.key='';
	if (arguments.length>=2) {
		this.key = arguments[1];
	}
}

function xgridField() {
	this.value = ((arguments.length == 1)? arguments[0] : '');
}

xgrid.prototype.addRow = function(row) {
	if (row.length != this.headings.length) {
		alert('Xgrid error: Incorrect column count: '+row.length+' - expected '+this.headings.length);
		return false;
	}
	this.rows[this.rows.length] = new xgridRow(row);
	this.rows[this.rows.length-1].index = this.rows.length;
}

xgrid.prototype.addRow = function(row, key) {
	if (row.length != this.headings.length) {
		alert('Xgrid error: Incorrect column count: '+row.length+' - expected '+this.headings.length);
		return false;
	}
	this.rows[this.rows.length] = new xgridRow(row, key);
	this.rows[this.rows.length-1].index = this.rows.length;
}

xgrid.prototype.shuffleRows = function(animate) {
	if (animate) {
		var altered = false;
		for (var i=0;i<this.rows.length;i++) {
			el = document.getElementById(this.rows[i].key);
			pos = parseInt(el.style.top);
			diff = pos - ((17 * (this.rows[i].index +1))-1);
			if (Math.abs(diff) > 300) {
				x = (parseInt(diff/2)==0)? ((diff>0)? 1:-1) : parseInt(diff/2);
				el.style.top = (pos-(x))+'px';
				altered = true;
			}
			else if (Math.abs(diff) > 50) {
				x = (parseInt(diff/3)==0)? ((diff>0)? 1:-1) : parseInt(diff/3);
				el.style.top = (pos-(x))+'px';
				altered = true;
			}
			else if (Math.abs(diff) > 0) {
				x = (parseInt(diff/3)==0)? ((diff>0)? 1:-1) : parseInt(diff/3);
				el.style.top = (pos-(x))+'px';
				altered = true;
			}

		}
		if (altered) {
			setTimeout(associateObjWithTimer(this, 'shuffleRows', true),30);
		}
	}
	else {
		for (var i=0;i<this.rows.length;i++) {
			document.getElementById(this.rows[i].key).style.top=((this.rows[i].index+1) * 16)+'px';
		}
	}
}



xgrid.prototype.compile = function() {
	var oxd=this;
	var xmlgrid = new xmlWriter('div');
	xmlgrid.writeAttribute('class', 'xgrid');
	xmlgrid.startElement('div');
	xmlgrid.writeAttribute('class', 'xgridrow');
	xmlgrid.writeStyle('top','-1px');
	var total=0;
	for (var i=0;i<this.headings.length;i++) {
		xmlgrid.startElement('div');
		xmlgrid.writeAttribute('class', 'xgridheading');
		xmlgrid.attachEvent('click', 'xgridLaunchColSort', this, i);
		xmlgrid.writeStyle('width',this.headings[i].width+'px');
		total += parseInt(this.headings[i].width);
		xmlgrid.writeText(this.headings[i].value);
		xmlgrid.endElement();
	}
	xmlgrid.writeStyle('width',(total + (this.headings.length * 4))+'px');
	xmlgrid.endElement();
	for (var i=0;i<this.rows.length;i++) {
		xmlgrid.startElement('div');
		xmlgrid.writeStyle('width',(total + (this.headings.length * 4))+'px'); // *4 for 4px of css border/padding
		xmlgrid.writeAttribute('class', 'xgridrow');
		xmlgrid.writeAttribute('id', this.rows[i].key);
		xmlgrid.writeStyle('top',((17 * (i+1))-1)+'px');
		for (var j=0;j<this.rows[i].fields.length;j++) {
			xmlgrid.startElement('div');
//			if (j==0) {
	//			xmlgrid.attachEvent('click', 'charinfo', window, this.rows[i].fields[j].value);
	//		}
			if (this.headings[j].type=='html') {
				xmlgrid.writeAttribute('class', 'xgridfield');
				xmlgrid.writeStyle('width',this.headings[j].width+'px');
				xmlgrid.writeRaw(this.rows[i].fields[j].value);
			}
			else {
				xmlgrid.writeAttribute('class', 'xgridfield'+((this.headings[j].type=='number')? ' xgridnumeric':''));
				xmlgrid.writeStyle('width',this.headings[j].width+'px');
				xmlgrid.writeText(this.rows[i].fields[j].value);
			}
			xmlgrid.endElement();
		}
		xmlgrid.endElement();
	}
	xmlgrid.endElement();
	if (arguments.length==2 && arguments[0] != null) {
		var c = arguments[0];
		while(c.childNodes.length>0) {
			c.removeChild(arguments[0].childNodes[0]);
		}
		this.container = arguments[0];
		this.id = arguments[1];
		var newgrid = xmlgrid.toHTMLNodes();
		newgrid.setAttribute('id', arguments[1]);
		c.style.height = ((17* (this.rows.length+3)))+'px';
		return c.appendChild(newgrid);
	}
	return xmlgrid.toHTMLNodes();
}


xgrid.prototype.loadXML = function (rxml) {
	// expecting rxml to be of the structure <response><griddata><fieldset>...</fieldset><dataset>...</dataset></griddata></response>
	for (var i=0; i<rxml.childNodes[0].childNodes.length; i++) {
		if (rxml.childNodes[0].childNodes[i].childNodes.length == 1)
			this.headings[this.headings.length] = new xgridHeading(rxml.childNodes[0].childNodes[i].childNodes[0].nodeValue, rxml.childNodes[0].childNodes[i].attributes.getNamedItem('src').value, rxml.childNodes[0].childNodes[i].attributes.getNamedItem('type').value, rxml.childNodes[0].childNodes[i].attributes.getNamedItem('width').value);
		else
			this.headings[this.headings.length] = new xgridHeading(' ', rxml.childNodes[0].childNodes[i].attributes.getNamedItem('src').value, rxml.childNodes[0].childNodes[i].attributes.getNamedItem('type').value, rxml.childNodes[0].childNodes[i].attributes.getNamedItem('width').value);
	}
	for (var j=0;j<rxml.childNodes[1].childNodes.length;j++) {
		this.rows[this.rows.length] = new xgridRow();
		this.rows[this.rows.length-1].key = rxml.childNodes[1].childNodes[j].attributes.getNamedItem('key').value
		for (var i=0; i<rxml.childNodes[1].childNodes[j].childNodes.length; i++) {
			if (rxml.childNodes[1].childNodes[j].childNodes[i].childNodes.length == 1) 
				this.rows[this.rows.length-1].fields[i] = new xgridField(rxml.childNodes[1].childNodes[j].childNodes[i].childNodes[0].nodeValue);
			else
				this.rows[this.rows.length-1].fields[i] = new xgridField(' ');
		}
	}
}

xgrid.prototype.xgridLaunchColSort = function(e, srcobj, col){
	this.xgridColSort(col);
}

xgrid.prototype.xgridColSort = function(col) {
	this.sort(col);
}

