var TableConstants = {
    CLASS_NAME_SELECTED_ROW : "selected",
    CLASS_NAME_MOUSE_OVER   : "mouseOverRow"
};

function Table(tableIdOrElement, highlightOnClick, highlightOnMouseOver){

    if( !tableIdOrElement || tableIdOrElement == 'undefined'){
        alert("Table.js: Parametru tableIdOrElement este null");
        return;
    }

    if( typeof tableIdOrElement == "string"){
        this.oTable = document.getElementById(tableIdOrElement);
    }else{
        //element
        this.oTable = tableIdOrElement;
    }

    if( !this.oTable){
        alert("Table.js: Elementul tabel nu a fost gasit");
        return;
    }

    this.highlightOnClick = highlightOnClick;
    this.highlightOnMouseOver = highlightOnMouseOver;
    this.selectedRow = null;
    this.selectedClassName = TableConstants.CLASS_NAME_SELECTED_ROW;

    this.tbody = this.oTable.getElementsByTagName("tbody")[0];

    if( this.tbody != null){
        var rows = this.tbody.children;
        var self = this;
        for( var i = 0; i < rows.length; i++){
            var row = rows[i];
            if( !(row.nodeName && row.nodeName.toLowerCase() == "tr")) continue;

            if( jQuery(row).hasClass("selected")){
                self.selectedRow = row;
            }

            if( "true" != row.getAttribute("noSelect")){
                if( this.highlightOnClick){

                    if( row.onclick && row.onclick instanceof Function){
                        row.originalOnClick = row.onclick;
                    }

                    row.onclick = function(){
                        self.unselectCurrentRow();
                        self.selectRow( this);

                        if( this.originalOnClick){
                            this.originalOnClick();
                        }
                    }
                }

                if( self.highlightOnMouseOver){
                    row.onmouseover = function(){
                        if( row.className != TableConstants.CLASS_NAME_SELECTED_ROW) {
                            row.className = TableConstants.CLASS_NAME_MOUSE_OVER;
                        }
                    };

                    row.onmouseout = function(){
                        if( row.className != TableConstants.CLASS_NAME_SELECTED_ROW) {
                            row.className =  "";
                        }
                    }
                }
            }
        }
    }
}

Table.prototype = {
    getSelectedRow : function(){
        return this.selectedRow;
    },

    selectRow : function (row){
        this.selectedRow = row;
        Element.addClassName(row, this.selectedClassName);
        //row.className = this.selectedClassName;
    },

    unselectRow : function ( row){
        Element.removeClassName(row, this.selectedClassName);
        //row.className = "";
    },

    unselectCurrentRow : function(){
        var currentRow = this.getSelectedRow();
        if( currentRow != null) this.unselectRow( currentRow);
    },

    getSelectedRowIndex : function(){
        return this.selectedRowIndex;
    },

    setSelectedClassName : function(className){
        this.selectedClassName = className;
    }
};

//domul arata asa: <tr><td><input type="checkbox"></td></tr>
function hightLightRow(oCheck, selectedRowStyle, normalRowStyle){
    var oRow = oCheck.parentNode.parentNode;
    //function hightLightRow(oCheck, id, selectedRowStyle, normalRowStyle){
    //var oRow = document.getElementById(id);
    oRow.className = ( oCheck.checked ? selectedRowStyle : normalRowStyle);
}



(function(){
    if(jQuery){
        jQuery(function() {
            jQuery(".table").each(function (i, val) {
                if(!val.id){
                    new Table(val, true);
                }
            });
        });
    }
})();