// JavaScript Document
TableElement.prototype.clickColor = 'yellow';
TableElement.prototype.overColor;
//TableElement.prototype.outColor;
TableElement.prototype.valid = false;
TableElement.prototype.tableElement;
TableElement.prototype.tableRows;

function TableElement(oTableId){
//	this.tableName = oTable;
	var oThis = this;
	if (oTableId != null){
		this.setTable(oTableId);
	}
	if (this.isValid()){
		this.document = this.tableElement.ownerDocument || this.tableElement.document;
	} else {
		this.document = document;
	}

	// samo za IE
	var win = this.document.defaultView || this.document.parentWindow;
	this._onunload = function () {
		oThis.destroy();
	};
	if (win && typeof win.attachEvent != "undefined") {
		win.attachEvent("onunload", this._onunload);
	}
}

TableElement.prototype.setTable = function (oTableId) {
	var t = document.getElementById(oTableId);
	if(t){
		this.tableElement = t;
		this.valid = true;
	}
};

// Provjerava se valjanost skripte
TableElement.prototype.isValid = function () {
	return this.valid;
};

// Postavlja se boja za oznaceni redak
TableElement.prototype.setClickColor = function (oStr) {this.clickColor = oStr;};

// Postavlja se boja za onmouseover retka
TableElement.prototype.setOverColor = function (oStr) {this.overColor = oStr;};

// Postavlja se boja za onmouseout retka
TableElement.prototype.setOutColor = function (oStr) {this.outColor = oStr;};

// Inicijalizacija skripte
TableElement.prototype.init = function () {
	if(!this.isValid())return;
	
	this.tableRows = this.tableElement.rows;
	if(this.tableRows){
		for(var i = 0; i < this.tableRows.length; i++){
			var tmp = this.tableRows[i];
			if(tmp.cells[0].tagName == 'TH'){continue;}
			tmp.onclick = this.clickRow;
			tmp.checked = false;
		//	if(this.clickColor && this.clickColor != ''){tmp.clickColor = this.clickColor;}
			if (tmp.captureEvents){tmp.captureEvents(Event.CLICK)}
			if(this.overColor && this.overColor != ''){
						tmp.onmouseover = function() {this.style.backgroundColor=this.overColor};
						tmp.overColor = this.overColor;
						//tmp.onmouseout = function() {this.style.backgroundColor=(this.checked)?this.clickColor:this.outColor};
						tmp.onmouseout = function() {this.style.backgroundColor=this.outColor};
						tmp.outColor = tmp.style.backgroundColor;
			}
	}}

	// dodajemo obradu eventa onclick na razini tablice
	this.tableElement.onclick = this.clickTable;
	if (tmp.captureEvents){tmp.captureEvents(Event.CLICK)}
	// pretrcimo tablicu za slucaj da je neko polje inicijalno
	this.clickTable();
};

TableElement.prototype.clickRow = function (){
	var inpt = getInpt(this);
	if(!inpt)return;
	inpt.click();
	this.checked = inpt.checked;
	this.style.backgroundColor = (inpt.checked)?this.clickColor:'';

};

// Pravilno setiranje pozadine redaka u tablici
TableElement.prototype.clickTable = function (){
	var t = (this.tableElement)?this.tableElement.rows:this.rows;
	if(t){
		for(var i = 0; i < t.length; i++){
			var tmp = t[i];
			if(tmp.cells[0].tagName == 'TH'){continue;}
			var inp = getInpt(tmp);
			if(!inp)continue;
			if((!inp.checked) && (tmp.style.backgroundColor == tmp.clickColor)){tmp.style.backgroundColor=tmp.outColor;tmp.checked=inp.checked;}
			else if(inp.checked){tmp.style.backgroundColor = tmp.clickColor;tmp.checked=inp.checked;}
	}}
};

// Dohvat input objekta tipa checkbox ili radio u retku
function getInpt(oObj){
	var row = oObj;
	if(!row)return null;
	var t = row;
	while(true){
		if(t.tagName == 'TR')break;
		t = t.parentElement;
	}

	var firstCell = t.cells[0];
	if(firstCell.tagName == 'TH')return null;

	var inpt = firstCell.firstChild;

	while(true){
		if(inpt.tagName == 'INPUT' && (inpt.type == 'checkbox' || inpt.type == 'radio'))break;
		inpt = inpt.nextSibling;
		if(!inpt)return null;
	}
	return inpt;
}

TableElement.prototype.destroy = function () {
	var win = this.document.parentWindow;
	if (win && typeof win.detachEvent != "undefined") {	// only IE needs this
		win.detachEvent("onunload", this._onunload);
	}
	this._onunload = null;
	this.element = null;
	this.document = null;
	this.tableElement = null;
	this.tableRows = null;
};
