
function TableMatrixRenderer(prdId, tblId, data, targetId, paginLineId, paginId, nextId, prevId, sepId) {
    this.rowsPerPage = 50;
    this.curPage = 0;
    this.prdId = prdId;
    this.tblId = tblId;
    this.data = data;
    this.$cnt = $("#" + targetId);
    this.$pageCnt = $("#" + paginLineId);
    this.$pageElm = null;
    this.$nextElm = null;
    this.$prevElm = null;
    this.$sepElm = null;
    this.$inpElm = null;
    this.pageSelEnable = false;
    this.selectedItems = [];
    var _this = this;

    if (paginId != undefined && paginId.length) {
        this.$pageElm = $("#" + paginId);
        this.pageSelEnable = true;
        this.$pageElm.bind("change", function() {
            _this.setRowsPerPage($(this).val());
        });
    }

    if (nextId != undefined && nextId.length) {
        this.$nextElm = $("#" + nextId);
        this.$nextElm.click(function() {
            _this.nextPage();
        });
    }

    if (prevId != undefined && prevId.length) {
        this.$prevElm = $("#" + prevId);
        this.$prevElm.click(function() {
            _this.prevPage();
        });
    }

    if (sepId != undefined && sepId.length) {
        this.$sepElm = $("#" + sepId);
    }
}

TableMatrixRenderer.prototype.render = function() {
    this.showPagination();
    this.$cnt.empty();
    var $tbl = $("<table />");
    var $hdrRow = $("<tr />");
    for (var i=0; i<this.data.totCols; i++) {
        var $td = $("<td />").html(this.data.titles[i]);
       // if (this.data.colType[i] == false)
            $td.addClass("bold");

        $hdrRow.append($td);
    }

    $tbl.append($hdrRow);
    if (this.curPage * this.rowsPerPage > this.data.totRows)
        this.curPage = 0;

    var offStart = this.curPage * this.rowsPerPage;
    var offEnd = offStart + this.rowsPerPage;
    if (offEnd > this.data.totRows)
        offEnd = this.data.totRows;

    //checkRef fixes IE checkbox behaviour - not allowing to check checkbox before it was added to the dom
    var checkRef = [];
    for (var i=offStart, col=0; i<offEnd; i++) {
        var clsName = (col++ % 2) ? "col1" : "col2";
        var $tr = $("<tr />");
        for (var j=0; j<this.data.totCols; j++) {
            var $td = $("<td />").addClass(clsName);
            if (this.data.colType[j] == false) {
                //text
                $td.html(this.data.rows[i][j]);
                  if(j==0)
	            {
	            	   $td.addClass("bold");
	            }
            }
            else if (this.data.rows[i][j].length > 0 && parseFloat(this.data.rows[i][j]) > 0) {
                //checkbox
                var $inp = this.createCheckbox(i, j);
                $td.append($inp);
                if (this.isSelected(i, j) !== false) {
                    checkRef.push($inp);
                }
            }

            $tr.append($td);
        }

        $tbl.append($tr);
    }

    this.$cnt.append($tbl);

    //restore status
    for (var i=0; i<checkRef.length; i++) {
        checkRef[i].attr("checked", true);
    }
}

TableMatrixRenderer.prototype.createCheckbox = function(i, j) {
    var _this = this;
    var $inp = $("<input />").attr("type", "checkbox").bind("change", function() {
        if ($(this).is(":checked")) {
            //add to list
            _this.addToSelection(i, j);
        }
        else {
            //remove from list
            _this.removeFromSelection(i, j);
        }
    });

    //restore state - discared due to ie behaviour
    //var status = (this.isSelected(i, j) === false) ? false : true;
    //$inp.attr("checked", status);

    return $inp;
}

TableMatrixRenderer.prototype.addToSelection = function(i, j) {
    if (this.isSelected(i, j) === false) {
        var selObj = {i : i, j : j};
        this.selectedItems.push(selObj);
    }
}

TableMatrixRenderer.prototype.removeFromSelection = function(i, j) {
    var idx = this.isSelected(i, j);
    if (idx === false)
        return;

    this.selectedItems.splice(idx, 1);
}

TableMatrixRenderer.prototype.loadSelection = function(selection) {
    this.selectedItems = selection;
}

/**
 * note: check agains === false!
 */
TableMatrixRenderer.prototype.isSelected = function(_i, _j) {
    for (var i=0; i<this.selectedItems.length; i++) {
        if (this.selectedItems[i].i == _i && this.selectedItems[i].j == _j)
            return i;
    }

    return false;
}

TableMatrixRenderer.prototype.showPagination = function() {
    var visElm = false;
    var visPrev = false;
    var visNext = false;
    if (this.$prevElm) {
        if (this.curPage > 0) {
            visElm = true;
            visPrev = true;
            this.$prevElm.show();
        }
        else {
            this.$prevElm.hide();
        }
    }

    if (this.$nextElm) {
        if (this.curPage < this.getMaxPages() - 1) {
            visElm = true;
            visNext = true;
            this.$nextElm.show();
        }
        else {
            this.$nextElm.hide();
        }
    }

    if (this.$sepElm) {
        if (visPrev && visNext) {
            this.$sepElm.show();
        }
        else {
            this.$sepElm.hide();
        }
    }

    if (this.$pageCnt) {
        if (this.pageSelEnable || visElm) {
            this.$pageCnt.show();
        }
        else {
            this.$pageCnt.hide();
        }
    }
}

TableMatrixRenderer.prototype.setRowsPerPage = function(tot) {
    var rowsSel = parseInt(tot);
    if (tot <= 0)
        return;

    this.rowsPerPage = tot;
    this.curPage = 0;
    this.render();
}

TableMatrixRenderer.prototype.getMaxPages = function() {
    return Math.ceil(this.data.totRows / this.rowsPerPage);
}

TableMatrixRenderer.prototype.nextPage = function() {
    if (this.curPage >= this.getMaxPages() - 1)
        return;

    this.curPage++;
    this.render();
}

TableMatrixRenderer.prototype.prevPage = function() {
    if (this.curPage == 0)
        return;

    this.curPage--;
    this.render();
}

TableMatrixRenderer.prototype.getEncodedSelection = function() {
    var obj = {prdId : this.prdId, tblId : this.tblId, selection : this.selectedItems};
    return $.base64Encode($.toJSON(obj));
}

TableMatrixRenderer.prototype.hasSelection = function() {
    return (this.selectedItems.length > 0) ? true : false;
}
