//:) Class
    function MDrawCompoList (id, path)
    {
        this.m_elementId = id;
        this.m_pathId = path;
        
        this.m_selectedRow = -1;
        this.m_selectedIdx = -1;
        this.m_selectedItem = null;
        
        this.m_colorSelected;
        this.m_colorChange = 16;
        this.m_colorSafe = new Array ();
        this.m_noOfCols;
        this.m_sumColsWidth = 0;
        this.m_lastVariableIndex;
        this.m_sized = -1;
        this.m_wasPadded = false;
        this.m_cachedWidths = new Array ();
        this.m_cachedHeads = new Array ();
        this.m_colorItem = '';
                
        this.m_sendFn;
        
        this.m_unserialized = false;
        this.m_checking = 'yes';
        this.m_selecting = 'no';
        this.m_lock;
        this.m_hAmount = 1;
        this.m_amount = 1;
        this.m_checkedAll = false;
        this.m_checked = new Array();
        this.m_cells = new Array();    
        this.m_indexers = new Array();
    }
    
//:) Control methods
    MDrawCompoList.prototype.config = function (property, value) {
        switch (property) {
            case 'MDC_STYLE_COLOR_SELECTED':
            	this.m_colorSelected = value;
            	break;
            case 'MDC_STYLE_COLOR_CHANGE':
            	this.m_colorChange = parseInt(value, 10);
            	break;
            case 'MDC_SEND_FUNCTION':
            	this.m_sendFn = value;
            	break;
            case 'MDC_CHECKING':
            	this.m_checking = value;
            	break;
			case 'MDC_SELECTING':
				this.m_selecting = value;
				break;
            case 'MDC_LOCK':
                this.m_lock = value;
                break;
            case 'MDC_HAMOUNT':
                this.m_hAmount = value;
                break;
            case 'MDC_AMOUNT':
                this.m_amount = value;
                break;
            case 'MDC_DIRECTION':
                this.m_direction = value;
                break;
            default:
            	break;
        }
    }
        //{OK}
        MDrawCompoList.prototype._unserialize = function () {
        	if (!this.m_unserialized) {
                var parts = this.m_elementId.split ('_');
        		if (parts[2] && document.all[this.m_pathId + '_cells[' + parts[2] + ']']) {
        			var cells = unescape(document.all[this.m_pathId + '_cells[' + parts[2] + ']'].value);
        			this.m_cells = unserialize(cells);
                    this.m_checkedAll = (!this.m_cells['all'] || isNaN(this.m_cells['all'])) ? false : true;
        		}
                if (!this.m_cells) {
                    this.m_cells = new Array ();
                }
        		if (parts[2] && document.all[this.m_pathId + '_idx[' + parts[2] + ']']) {
        			var indexers = unescape(document.all[this.m_pathId + '_idx[' + parts[2] + ']'].value);
        			this.m_indexers = unserialize(indexers);
        		}
                if (!this.m_indexers) {
                    this.m_indexers = new Array ();
                }
        		this.m_unserialized = true;
        	}
        }
        
        MDrawCompoList.prototype._serialize = function () {
            var parts = this.m_elementId.split ('_');
            if (parts [2] && document.all [this.m_pathId + '_cells[' + parts [2] + ']']) {
//@todo pridat this.m_checkedAll
                this.m_cells ['all'] = this.m_checkedAll;
                var cells = escape (serialize (this.m_cells));
                document.all [this.m_pathId + '_cells[' + parts [2] + ']'].value = cells;
            }            
        }
        
//:) Event methods
    //{OK}
    MDrawCompoList.prototype.onScroll = function () {
    	if (document.all[this.m_elementId + '_divHead']) {
    		var divHead = document.all[this.m_elementId + '_divHead'];
    		var divBody = document.all[this.m_elementId + '_divBody'];
    		divHead.scrollLeft = divBody.scrollLeft;
    	}
    }
    
    MDrawCompoList.prototype.onResize = function () {
        var divBody = document.all [this.m_elementId + '_divBody'];
        
        if (this.m_sized != -1 && divBody.offsetWidth != this.m_sized) {
            this._sizeHTML();
            
            if (this.m_wasPadded) {
                divBody.style.height = document.all [this.m_elementId + '_tblBody'].offsetHeight;
    			if (divBody.scrollWidth > divBody.offsetWidth)
    				divBody.style.paddingBottom = '15px';
                else if (divBody.scrollWidth <= divBody.offsetWidth)
                    divBody.style.paddingBottom = '0px';
            }
            
            this.m_sized = divBody.offsetWidth;
        }
    }


	MDrawCompoList.prototype.onLoadText = function (startFlag) {
		var divBody = document.all[this.m_elementId + '_divBody'];
		
		if (startFlag != '' && divBody && divBody.children.length > 0) {
			for (var i = 0; i < divBody.children.length; i++) {
				var child = divBody.children(i);
				if (child && child.tagName) {
					var idx = child.getAttribute('idx');
					if (idx) {
						this.onSelectItem(child);
						break;
					}
				}
			}
		}
	}
  
    
    MDrawCompoList.prototype.onLoad = function (startFlag) {
        var divBody = document.all[this.m_elementId + '_divBody'];
        var bodyCells = document.all[this.m_elementId + '_tblBody'].rows[0].cells;

        this.m_sized = this._sizeHTML();
        
        if (parseInt(divBody.currentStyle.paddingBottom) > 0) {
            this.m_wasPadded = true;                
            divBody.style.height = document.all[this.m_elementId + '_tblBody'].offsetHeight;
			if (divBody.scrollWidth <= divBody.offsetWidth) {
				divBody.style.paddingBottom = '0px';
            }
        }
        
        this._unserialize();
        
        if (startFlag != '') {
        	this.selectRowById(startFlag);
        }
//@deprecated
//        this.setAffectedRows(this.m_cells, true);
/*   
        if (startFlag) {
            var row = 0;
			var bodyCells = document.all[this.m_elementId + '_tblBody'].rows[0].cells;
			
            if (!bodyCells[0].idx) {
                bodyCells = document.all[this.m_elementId + '_tblBody'].rows[1].cells;
                row++;
            }
            this.selectRow(row, bodyCells[0].idx);
        }
*/
    }
    
            MDrawCompoList.prototype._sizeHTML = function () {
                var divBody = document.all[this.m_elementId + '_divBody'];
                var bodyCells = document.all[this.m_elementId + '_tblBody'].rows[0].cells;
                this.m_noOfCols = bodyCells.length;
                this.m_sumColsWidth = 0;
                
                if (document.all [this.m_elementId + '_divHead']) {
                    var divHead = document.all [this.m_elementId + '_divHead'];
                    
                    divHead.style.width = divBody.clientWidth;
                    divHead.style.display = 'inline';
                    
                    var widths = new Array();
                    var headCells = document.all[this.m_elementId + '_tblHead'].rows[0].cells;
                    for (var i = 0; i < this.m_noOfCols; i++) {
                        if (bodyCells[i].style.display != 'none') {
                            this.m_sumColsWidth += bodyCells[i].offsetWidth;
                            widths[i] = this._getWidth(bodyCells[i]);
                        } else {
                            widths[i] = null;
                        }
                    }

                    document.all[this.m_elementId + '_tblHead'].style.width = this.m_sumColsWidth;

                    for (var i = 0; i < this.m_noOfCols; i++) {
                        if (widths[i] != null) {
                            headCells[i].style.width = widths[i];
                        }
                    }
                }
                return divBody.offsetWidth;
            }
            
            MDrawCompoList.prototype._getWidth = function (element) {
                var width = 0;
                if (element.tagName) {
                    var curStyle = element.currentStyle;
                    
                    width += element.offsetWidth;
                    width -= (isNaN (parseInt (curStyle.paddingLeft))) ? 0 : parseInt (curStyle.paddingLeft);
                    width -= (isNaN (parseInt (curStyle.paddingRight))) ? 0 : parseInt (curStyle.paddingRight);
                    width -= (isNaN (parseInt (curStyle.borderLeftWidth))) ? 0 : parseInt (curStyle.borderLeftWidth);
                    width -= (isNaN (parseInt (curStyle.borderRightWidth))) ? 0 : parseInt (curStyle.borderRightWidth);
                }
                return width;
            }


	MDrawCompoList.prototype.onSelectItem = function (element) {
		if (this.m_colorSelected) {
			if (this.m_selectedItem) {
				this.m_selectedItem.className = this.m_colorItem;
			}
			if (element && element.tagName) {
				this.m_selectedItem = element;
				this.m_colorItem = this.m_selectedItem.className;
				element.className = this.m_colorSelected;			 
			}
		}
	}

    //{OK}
    MDrawCompoList.prototype.onSelectRow = function (object) {
        var row = object.rowIndex;
        var element = window.event.srcElement;
        var idx = 0;
        
        if (this.m_lock && this.m_lock != '') {
            eval ('var lock = ' + this.m_lock + ';');
            if (lock) {
                return;
            }
        }
//@todo Poriesit multitexty
        if (element.extra || element.tagName.toLowerCase() == 'img') {
            return;
        } else {
            var tblBodyRows = document.all [this.m_elementId + '_tblBody'].rows;
            while (element.tagName.toLowerCase() != 'td') {
                element = element.parentElement;
            }
            idx = element.getAttribute('idx');
        }
        this.selectRow (row, idx);
    }

//:( Selecting methods
    //{OK}
    MDrawCompoList.prototype.selectRowById = function (idx) {
        var tblBodyRows = document.all [this.m_elementId + '_tblBody'].rows;
        var iterateBy = Math.floor(this.m_noOfCols / this.m_hAmount);
        
        var col = 0;
        while (col < this.m_noOfCols) {
            var row = 0;
            while (tblBodyRows[row] && tblBodyRows[row].cells[col]) {
                if (tblBodyRows[row].cells[col].idx == idx) {
                    this.selectRow(row, idx);
                    return;
                }
                var rowspan = parseInt(tblBodyRows[row].cells[col].rowSpan);
                row += (rowspan == 0) ? 1 : rowspan;
            }
            col += iterateBy;
        }
    }

    //{OK}
    MDrawCompoList.prototype.selectRow = function (row, idx) {
        var tblBodyRows = document.all [this.m_elementId + '_tblBody'].rows;
        
        while (tblBodyRows[row] && tblBodyRows[row].cells.length != this.m_noOfCols) {
        	row--;
        }
        if (this.m_colorSelected) {
            if (this.m_selectedRow != -1) {
                this._deselectRows(this.m_selectedRow, this.m_selectedIdx);
            }
            this._selectRows(row, idx);
        }
        this.m_selectedRow = row;
        this.m_selectedIdx = idx;

        if (this.m_checking == 'yes') {
        	var selecting = this.m_selecting;
			this.m_selecting = 'no';
            this.affectRow (idx, -1);
			this.m_selecting = selecting; 
        }
        if (this.m_sendFn) {
            var elementId = this.m_elementId.split ('_');
            var parts = this.m_sendFn.split ('#');
            if (parts.length > 1) {
	            eval (parts [0] + elementId [1] + parts [1] + idx + parts [2] + ';');
	        } else {
	        	eval (this.m_sendFn + ';');
	        }
        }
    }
        //{OK}
        MDrawCompoList.prototype._selectRows = function (row, idx) {
            var tblBodyRows = document.all[this.m_elementId + '_tblBody'].rows;
            
            do {
                this.m_colorSafe[row] = new Array();
                for (var c = 0; c < this.m_noOfCols; c++) {
                    if (tblBodyRows[row].cells[c]
                        && tblBodyRows[row].cells[c].idx == idx
                        && !tblBodyRows[row].cells[c].extra
                        )
                        {
                        this.m_colorSafe[row][c] = new Array();
                        this.m_colorSafe[row][c]['class'] = tblBodyRows[row].cells[c].className;
                        this.m_colorSafe[row][c]['bgcolor'] = tblBodyRows[row].cells[c].currentStyle.backgroundColor;
                        tblBodyRows[row].cells[c].style.backgroundColor = '';
                        tblBodyRows[row].cells[c].className = this.m_colorSelected;
                    }
                }
        		row++;
        	} while (row < tblBodyRows.length && tblBodyRows[row].cells.length != this.m_noOfCols)
        }
        //{OK}
        MDrawCompoList.prototype._deselectRows = function (row, idx) {
            var tblBodyRows = document.all [this.m_elementId + '_tblBody'].rows;
            
            do {
                for (var c = 0; c < this.m_noOfCols; c++) {
                    if (tblBodyRows[row].cells[c]
                        && tblBodyRows[row].cells[c].idx == idx
                        && this.m_colorSafe[row]
                        && this.m_colorSafe[row][c])
                        {
                        tblBodyRows [row].cells[c].className = this.m_colorSafe[row][c]['class'];
                        tblBodyRows [row].cells[c].style.backgroundColor = this.m_colorSafe[row][c]['bgcolor'];
                    }
                }
        		row++;
        	} while (row < tblBodyRows.length && tblBodyRows [row].cells.length != this.m_noOfCols)
        }
    
//:) Affecting methods
    //{OK}
    MDrawCompoList.prototype.affectRow = function (idx, value) {
        if (!this.m_checkedAll) {
            if (document.all [this.m_elementId + '_radRow']) {
            	var object = event.srcElement;
                var radios = document.all [this.m_elementId + '_radRow'];
				if (radios.length) {
	                for (var r = 0; r < radios.length; r++) {
	                    var radio = document.all [this.m_elementId + '_radRow'][r];
	                    if (radio.parentElement.idx && !radio.disabled && radio.parentElement.idx == idx) {
	                    	if (object && object.tagName.toLowerCase() != 'input') {
								radio.checked = (radio.checked) ? false : true;
							} else {
		                    	radio.checked = (!this.m_cells[idx]) ? true : false;
	                    	}  
							this.m_cells = new Array ();
							this.m_cells [idx] = radio.checked;
	                        break;
	                    }
	                }
	            } else if (radios) {
            		if (radios.parentElement.idx && !radios.disabled && radios.parentElement.idx == idx) {
                    	if (object && object.tagName.toLowerCase() != 'input') {
							radios.checked = (radios.checked) ? false : true;
						} else {
	                    	radios.checked = (!this.m_cells[idx]) ? true : false;
                    	}
						this.m_cells = new Array ();
						this.m_cells [idx] = radios.checked;
                    }
	            }
            } else if (document.all [this.m_elementId + '_chckRow[' + idx + ']']) {
                var checkbox = document.all [this.m_elementId + '_chckRow[' + idx + ']'];
                if (!checkbox.disabled) {
	                if (value == -1) {
	                    checkbox.checked = (checkbox.checked) ? false : true;
	                } else if (typeof (value) != 'undefined') {
	                	checkbox.checked = value;
	                }
	                if (!checkbox.checked) {
	                    var iterateBy = Math.floor(this.m_noOfCols / this.m_hAmount);
	                    var scope = Math.floor(checkbox.parentElement.cellIndex / iterateBy)
	                    this.m_checked[scope] = false;
	                }
	                this.m_cells [idx] = checkbox.checked;
                }
            }
//@todo serializacia
            this._serialize();
            
			if (this.m_selecting == 'yes') {
				var checking = this.m_checking;
				this.m_checking = 'no';
				this.selectRowById(idx);
				this.m_checking = checking;
			}
        }
        window.event.cancelBubble = true;
    }
    
    MDrawCompoList.prototype.affectAllRows = function () {
        this.m_checkedAll = (this.m_checkedAll) ? false : true;
        var value = this.m_checkedAll;
        for (var i = 0; i < this.m_indexers.length; i++) {
            var idx = this.m_indexers[i];
            if (document.all [this.m_elementId + '_chckRow[' + idx + ']']) {
                var checkbox = document.all [this.m_elementId + '_chckRow[' + idx + ']'];
                checkbox.disabled = value;
//@deprecated                
//                checkbox.checked = value;
//                this.m_cells[idx] = value;
            }
        }
//@todo serializacia
        this._serialize();
    }
    
    MDrawCompoList.prototype.affectRows = function (object) {
        if (!this.m_checkedAll) {
            var column = object.parentElement.cellIndex;
            var iterateBy = Math.floor(this.m_noOfCols / this.m_hAmount);
            var scope = Math.floor(column / iterateBy);
            
            if (this.m_direction == 'horizontal') {
                var step = this.m_hAmount;
                var start = scope;
                var to = this.m_indexers.length;
            } else {
                var start = scope * this.m_amount;
                var to = start + this.m_amount;
                var step = 1;
            }
    
            this.m_checked[scope] = (this.m_checked[scope]) ? false : true;
            var value = this.m_checked[scope];
            for (var i = start; i < to; i += step) {
                var idx = this.m_indexers[i];
                if (document.all [this.m_elementId + '_chckRow[' + idx + ']']) {
                    var checkbox = document.all [this.m_elementId + '_chckRow[' + idx + ']'];
                    checkbox.checked = value;
                    this.m_cells[idx] = value;
                }
            }
//@todo serializacia
            this._serialize();
        }
    }