var ID = 0,
	TITLE = 1,
	CONTENTTYPE = 2,
	AUTHOR = 3,
	SIZE = 4,
	SENDER = 5,
	REGISTRATIONDATE = 6,
	DESCRIPTION = 7,
	FILENAME = 8,
	NAME = 9;
var DIRECTION_DOWN = -1, 
	DIRECTION_NONE = 0, 
	DIRECTION_UP = 1;
var DESCRIPTIONS_EXPANDED = false;

/* Grid constructor */
function createGrid(_data, _columns, _name) {
	this.data = _data;
	this.columns = _columns;
	this.name = _name;
	this.direction = new Array(this.columns.length);
	for (var i=0; i<this.columns.length; i++) {
		this.direction[i] = DIRECTION_NONE;
	}
}

/* Sorts the '_grid' on field '_column' (of type string) and draws it inside '_block' */
function sortTable(_block, _grid, _column, _type) {
	if (_grid.direction[_column] < DIRECTION_UP) {
		_grid.direction[_column] = DIRECTION_UP;
	} else {
		_grid.direction[_column] = DIRECTION_DOWN;
	}
	if (_type=="string") {
		sortOnString(_grid.data, _column, _grid.direction[_column]);
	} else if (_type=="int") {
		sortOnInt(_grid.data, _column, _grid.direction[_column]);
	} else if (_type=="date") {
		sortOnDate(_grid.data, _column, _grid.direction[_column]);
	}
	drawTable(_block, _grid, _column);
    
    //descriptions(_grid);
}

/* Sorts the array '_obj' on column 'col' of type string (in a silly, time consuming manner - n^2), with direction of '_sort_type' */
function sortOnString(_obj, _col, _sort_type) {
	if (_sort_type==DIRECTION_UP) {
		for (var i=0; i<_obj.length; i++) {
			for (var k=0; k<_obj.length-1; k++) {
				if (_obj[k][_col].toLowerCase() > _obj[k+1][_col].toLowerCase()) {
					var tempRow = _obj[k+1];
					_obj[k+1] = _obj[k];
					_obj[k] = tempRow;
				}
			}
		}
	} else if (_sort_type==DIRECTION_DOWN) {
		for (var i=0; i<_obj.length; i++) {
			for (var k=0; k<_obj.length-1; k++) {
				if (_obj[k][_col].toLowerCase() < _obj[k+1][_col].toLowerCase()) {
					var tempRow = _obj[k+1];
					_obj[k+1] = _obj[k];
					_obj[k] = tempRow;
				}
			}
		}
	} 
}

/* Sorts the array '_obj' on column 'col' of type string (in a silly, time consuming manner - n^2), with direction of '_sort_type' */
function sortOnString(_obj, _col, _sort_type) {
	if (_sort_type==DIRECTION_UP) {
		for (var i=0; i<_obj.length; i++) {
			for (var k=0; k<_obj.length-1; k++) {
				if (_obj[k][_col].toLowerCase() > _obj[k+1][_col].toLowerCase()) {
					var tempRow = _obj[k+1];
					_obj[k+1] = _obj[k];
					_obj[k] = tempRow;
				}
			}
		}
	} else if (_sort_type==DIRECTION_DOWN) {
		for (var i=0; i<_obj.length; i++) {
			for (var k=0; k<_obj.length-1; k++) {
				if (_obj[k][_col].toLowerCase() < _obj[k+1][_col].toLowerCase()) {
					var tempRow = _obj[k+1];
					_obj[k+1] = _obj[k];
					_obj[k] = tempRow;
				}
			}
		}
	} 
}

/* Sorts the array '_obj' on column 'col' of type int (in a silly, time consuming manner - n^2), with direction of '_sort_type' */
function sortOnInt(_obj, _col, _sort_type) {
	if (_sort_type==DIRECTION_UP) {
		for (var i=0; i<_obj.length; i++) {
			for (var k=0; k<_obj.length-1; k++) {
				if (_obj[k][_col] > _obj[k+1][_col]) {
					var tempRow = _obj[k+1];
					_obj[k+1] = _obj[k];
					_obj[k] = tempRow;
				}
			}
		}
	} else if (_sort_type==DIRECTION_DOWN) {
		for (var i=0; i<_obj.length; i++) {
			for (var k=0; k<_obj.length-1; k++) {
				if (_obj[k][_col] < _obj[k+1][_col]) {
					var tempRow = _obj[k+1];
					_obj[k+1] = _obj[k];
					_obj[k] = tempRow;
				}
			}
		}
	}
}

/* Sorts the array '_obj' on column 'col' of type date(dd/mm/yyyy) (in a silly, time consuming manner - n^2), with direction of '_sort_type' */
function sortOnDate(_obj, _col, _sort_type) {
	if (_sort_type==DIRECTION_UP) {
		for (var i=0; i<_obj.length; i++) {
			for (var k=0; k<_obj.length-1; k++) {
				if (compareDates(_obj[k][_col],_obj[k+1][_col]) > 0) {
					var tempRow = _obj[k+1];
					_obj[k+1] = _obj[k];
					_obj[k] = tempRow;
				}
			}
		}
	} else if (_sort_type==DIRECTION_DOWN) {
		for (var i=0; i<_obj.length; i++) {
			for (var k=0; k<_obj.length-1; k++) {
				if (compareDates(_obj[k][_col],_obj[k+1][_col]) < 0) {
					var tempRow = _obj[k+1];
					_obj[k+1] = _obj[k];
					_obj[k] = tempRow;
				}
			}
		}
	}
}

/* Compares the two given dates with each other */
function compareDates(date1, date2) {
	var dd1, dd2, mm1, mm2, yyyy1, yyyy2, from, to;
	to = date1.indexOf("/");
	dd1 = parseInt(date1.substring(0, to));
	from = to+1;
	to = date1.indexOf("/", from);
	mm1 = parseInt(date1.substring(from, to));
	from = to+1;
	to = date1.indexOf("/", from);
	yyyy1 = parseInt(date1.substring(from));
	to = date2.indexOf("/");
	dd2 = parseInt(date2.substring(0, to));
	from = to+1;
	to = date2.indexOf("/", from);
	mm2 = parseInt(date2.substring(from, to));
	from = to+1;
	to = date2.indexOf("/", from);
	yyyy2 = parseInt(date2.substring(from));
	if (yyyy1>yyyy2) {
		return 1;
	} else if (yyyy1<yyyy2) {
		return -1;
	} else {
		if (mm1>mm2) {
			return 1;
		} else if (mm1<mm2) {
			return -1;
		} else {
			if (dd1>dd2) {
				return 1;
			} else if (dd1<dd2) {
				return -1
			} else {
				return 0;
			}
		}
	}
}

/* Draws the '_grid' inside the block element 'block' (a span or div etc), sorted on '_col_sorted' */
function drawTable(_block, _grid, _col_sorted) {
	var obj = document.getElementById(_block);
	var directionString;
	if (_grid.direction[_col_sorted]==DIRECTION_UP) {
		directionString = "<IMG src='img/up.gif' align='middle'>";
	} else if (_grid.direction[_col_sorted]==DIRECTION_DOWN) {
		directionString = "<IMG src='img/down.gif' align='middle'>";
	} else {
		directionString = "";
	}
	var tempStr = ''+
		'<TABLE width="800" cellspacing="0" cellpadding="0" bgcolor="#ffffff">'+
		  '<TR>'+
		    '<TD height="25" colspan="7"></TD>'+
		  '</TR>'+
		  '<TR>'+
			'<TD width="20">&nbsp;</TD>'+
			"<TD width=\"605\" align=\"left\"><FONT class=\"gridText\"><B><A style='text-decoration: none' href='javascript:sortTable(\""+_block+"\", "+_grid.name+", TITLE, \"string\", "+_grid.direction[TITLE]+")' title=\"Sort table on Title\">Title / (File Name)</A></B>&nbsp;"+(_col_sorted==TITLE?directionString:"")+"</FONT></TD>"+
			"<TD width=\"35\" align=\"right\"><FONT class=\"gridText\"><B><A style='text-decoration: none' href='javascript:sortTable(\""+_block+"\", "+_grid.name+", CONTENTTYPE, \"string\", "+_grid.direction[CONTENTTYPE]+")' title=\"Sort table on Type\">Type</A></B>&nbsp;"+(_col_sorted==CONTENTTYPE?directionString:"")+"</FONT></TD>"+
			"<TD width=\"140\" align=\"center\"><FONT class=\"gridText\"><B><A style='text-decoration: none' href='javascript:sortTable(\""+_block+"\", "+_grid.name+", AUTHOR, \"string\", "+_grid.direction[AUTHOR]+")' title=\"Sort table on Author\">Author</A></B>&nbsp;"+(_col_sorted==AUTHOR?directionString:"")+"</FONT></TD>"+
			<!-- TD for future use-->"<TD width=\"60\" align=\"left\"><FONT class=\"gridText\"><B><A style='text-decoration: none' href='javascript:sortTable(\""+_block+"\", "+_grid.name+", SIZE, \"int\", "+_grid.direction[SIZE]+")' title=\"Sort table on Size\">Size(b)</A></B>&nbsp;"+(_col_sorted==SIZE?directionString:"")+"</FONT></TD>"+	<!--  -->
			"<TD width=\"50\" align=\"center\"><A href=\"javascript:descriptions("+_grid.name+")\" title=\"Expand / Collapse file desceriptions...\"><IMG src=\"img/document_show.png\" border=\"0\"></A></TD>"+
		  '</TR>'+
		  '<TR>'+
		    '<TD height="5" colspan="7"></TD>'+
		  '</TR>'+
		  '<TR>'+
		    '<TD height="3" bgcolor="#7C7974" colspan="7"></TD>'+
		  '</TR>';
	for (var i=0; i<_grid.data.length; i++) {
		tempStr += ''+
		  "<TR onMouseOver=\"this.style.background='#D8DDDE'\" onMouseOut=\"this.style.background=''\">"+
			'<TD width="20" bgcolor="#ffffff"><FONT class="gridText">'+
			  '['+(i+1)+'].&nbsp;&nbsp;</FONT>'+
			'</TD>'+
			'<TD width="605">'+
			  "<A style='text-decoration: none' onMouseOver='createTitle(\""+_grid.data[i][DESCRIPTION]+"<BR>Sender: "+_grid.data[i][NAME]+"\", event.pageX, event.pageY)' onMouseOut='destroyTitle()' href='"+_grid.data[i][FILENAME]+"' >" +
			  '<FONT class="gridText">'+_grid.data[i][TITLE]+'</FONT></A>'+
              '<DIV id="description_'+i+'"></DIV>'+
			'</TD>'+ 
			'<TD width="35" align="center">'+
			  '<FONT class="gridText">'+_grid.data[i][CONTENTTYPE]+'</FONT>'+
			'</TD>'+
			'<TD width="140" align="right">'+
				'<FONT class="gridText">'+_grid.data[i][AUTHOR]+'</FONT>'+
			'</TD>'+
			<!-- TD for future use-->'<TD width="60" align="right">'+'<FONT class="gridText">'+_grid.data[i][SIZE]+'</FONT>'+'</TD>'+<!--  -->
			'<TD width="50" height="30">&nbsp;';


		
		tempStr += ''+
			'</TD>'+
		  '</TR>'+
		  '<TR>'+
		    '<TD height="2" bgcolor="#A9A59F" colspan="7"></TD>'+
		  '</TR>';
	}
	tempStr += "</TABLE>";
	obj.innerHTML = tempStr;

	//descriptions(_grid);
}

/* Expands or collapses file descriptions inside the grid */ 
function descriptions (_grid) {
	var obj = null;
	if (DESCRIPTIONS_EXPANDED) {
		for (var i=0; i<_grid.data.length; i++) {
			obj = document.getElementById("description_"+i);
			obj.innerHTML = "";
		}
		DESCRIPTIONS_EXPANDED = false;
	} else {
		for (var i=0; i<_grid.data.length; i++) {
			obj = document.getElementById("description_"+i);
			obj.innerHTML = ''+
				'<TABLE width="100%" cellspacing="0" cellpadding="0">'+
				  '<TR>'+
				    '<TD>'+
					  '<FONT class="smalltext">'+_grid.data[i][DESCRIPTION]+', Sender: '+_grid.data[i][NAME]+'</FONT>'+'</DIV>'+
					'</TD>'+
				  '</TR>'+
				'</TABLE>';
		}
		DESCRIPTIONS_EXPANDED = true;
	}
}

/* Follows: functions for description pop-up layers */
var agt = navigator.userAgent.toLowerCase();
var originalFirstChild;

function createTitle(string, x, y) {
	// record the original first child (protection when deleting)
	if (typeof(originalFirstChild) == 'undefined') {
		originalFirstChild = document.body.firstChild;
	}

	x = document.all ? (event.clientX + document.body.scrollLeft) : x;
	y = document.all ? (event.clientY + document.body.scrollTop) : y;
	element = document.createElement('div');
	element.style.position = 'absolute';
	element.style.zIndex = 1000;
	element.style.visibility = 'hidden';
	excessWidth = 0;
	if (document.all) {
		excessWidth = 50;
	}
	excessHeight = 20;
	element.innerHTML = '<div class="bodyline"><table width="400" cellspacing="0" cellpadding="0" border="0"><tr><td><table width="100%"><tr><td><span class="bodylineText">' + string + '</span></td></tr></table></td></tr></table></div>';
	renderedElement = document.body.insertBefore(element, document.body.firstChild);
	renderedWidth = renderedElement.offsetWidth;
	renderedHeight = renderedElement.offsetHeight;

	// fix overflowing off the right side of the screen
	overFlowX = x + renderedWidth + excessWidth - document.body.offsetWidth;
	x = overFlowX > 0 ? x - overFlowX : x;

	// fix overflowing off the bottom of the screen
	overFlowY = y + renderedHeight + excessHeight - window.innerHeight - window.pageYOffset;
	y = overFlowY > 0 ? y - overFlowY : y;

	renderedElement.style.top = (y + 15) + 'px';
	renderedElement.style.left = (x + 15) + 'px';

	// windows versions of mozilla are like too fast here...we have to slow it down
	if (agt.indexOf('gecko') != -1 && agt.indexOf('win') != -1) {
		setTimeout("renderedElement.style.visibility = 'visible'", 1);
	} else {
		renderedElement.style.visibility = 'visible';
	}
}

function destroyTitle() {
	// make sure we don't delete the actual page contents (javascript can get out of alignment)
	if (document.body.firstChild != originalFirstChild) {
		document.body.removeChild(document.body.firstChild);
	}
}
