﻿/* 

    EmTableObj From ------------
    
    Tablecloth 
    written by Alen Grakalic, provided by Css Globe (cssglobe.com)
    please visit http://cssglobe.com/lab/tablecloth/
	
*/


function EmTableObj(tableid, highlightCols, highlightRows, selectable) {
    // CONFIG 

    // this function is called when 
    // add your own code if you want to add action 
    // function receives object that has been clicked
    function clickAction(obj) {
        //alert(obj.innerHTML);

    };

    var tableobj = document.getElementById(tableid);

    // END CONFIG



    var tableover = false;
    this.start = function() {
        tableobj.onmouseover = function() { tableover = true };
        tableobj.onmouseout = function() { tableover = false };
        this.rows(tableobj);
    };

    this.rows = function(table) {
        var css = "";
        var tr = table.getElementsByTagName("tr");
        for (var i = 0; i < tr.length; i++) {
            css = (css == "odd") ? "even" : "odd";
            tr[i].className = css;
            var arr = new Array();
            for (var j = 0; j < tr[i].childNodes.length; j++) {
                if (tr[i].childNodes[j].nodeType == 1) arr.push(tr[i].childNodes[j]);
            };
            for (var j = 0; j < arr.length; j++) {
                arr[j].row = i;
                arr[j].col = j;
                //if (arr[j].innerHTML == "&nbsp;" || arr[j].innerHTML == "") arr[j].className += " empty";
                arr[j].css = arr[j].className;
                arr[j].onmouseover = function() {
                    mouseover(table, this, this.row, this.col);
                };
                arr[j].onmouseout = function() {
                    mouseout(table, this, this.row, this.col);
                };
                arr[j].onmousedown = function() {
                    mousekeydown(table, this, this.row, this.col);
                };
                arr[j].onmouseup = function() {
                    mousekeyup(table, this, this.row, this.col);
                };
                arr[j].onclick = function() {
                    mouseclick(table, this, this.row, this.col);
                };
            };
        };
    };

    // appyling mouseover state for objects (th or td)
    function mouseover(table, obj, row, col) {
        if (!highlightCols && !highlightRows) obj.className = obj.css + " over";
        if (check1(obj, col)) {
            if (highlightCols) highlightCol(table, obj, col);
            if (highlightRows) highlightRow(table, obj, row);
        };
    };
    // appyling mouseout state for objects (th or td)	
    function mouseout(table, obj, row, col) {
        if (!highlightCols && !highlightRows) obj.className = obj.css;
        unhighlightCol(table, col);
        unhighlightRow(table, row);
    };
    // appyling mousedown state for objects (th or td)
    function mousekeydown(table, obj, row, col) {
        obj.className = obj.css + " down";
    };
    // appyling mouseup state for objects (th or td)
    function mousekeyup(table, obj, row, col) {
        obj.className = obj.css + " over";
    };
    // onclick event for objects (th or td)
    function mouseclick(table, obj, row, col) {
        if (check1) {
            if (selectable) {
                unselect(table);
                if (highlightCols) highlightCol(table, obj, col, true);
                if (highlightRows) highlightRow(table, obj, row, true);
                document.onclick = unselectAll;
            }
        };
        clickAction(obj);
    };

    function highlightCol(table, active, col, sel) {
        var css = (typeof (sel) != "undefined") ? "selected" : "over";
        var tr = table.getElementsByTagName("tr");
        for (var i = 0; i < tr.length; i++) {
            var arr = new Array();
            for (j = 0; j < tr[i].childNodes.length; j++) {
                if (tr[i].childNodes[j].nodeType == 1) arr.push(tr[i].childNodes[j]);
            };
            var obj = arr[col];
            if (check2(active, obj) && check3(obj)) obj.className = obj.css + " " + css;
        };
    };
    function unhighlightCol(table, col) {
        var tr = table.getElementsByTagName("tr");
        for (var i = 0; i < tr.length; i++) {
            var arr = new Array();
            for (j = 0; j < tr[i].childNodes.length; j++) {
                if (tr[i].childNodes[j].nodeType == 1) arr.push(tr[i].childNodes[j])
            };
            var obj = arr[col];
            if (check3(obj)) obj.className = obj.css;
        };
    };
    function highlightRow(table, active, row, sel) {
        var css = (typeof (sel) != "undefined") ? "selected" : "over";
        var tr = table.getElementsByTagName("tr")[row];
        for (var i = 0; i < tr.childNodes.length; i++) {
            var obj = tr.childNodes[i];
            if (check2(active, obj) && check3(obj)) obj.className = obj.css + " " + css;
        };
    };
    function unhighlightRow(table, row) {
        var tr = table.getElementsByTagName("tr")[row];
        for (var i = 0; i < tr.childNodes.length; i++) {
            var obj = tr.childNodes[i];
            if (check3(obj)) obj.className = obj.css;
        };
    };
    function unselect(table) {
        tr = table.getElementsByTagName("tr")
        for (var i = 0; i < tr.length; i++) {
            for (var j = 0; j < tr[i].childNodes.length; j++) {
                var obj = tr[i].childNodes[j];
                if (obj.className) obj.className = obj.className.replace("selected", "");
            };
        };
    };
    function unselectAll() {
        if (!tableover) {
            tables = document.getElementsByTagName("table");
            for (var i = 0; i < tables.length; i++) {
                unselect(tables[i])
            };
        };
    };
    function check1(obj, col) {
        return (!(col == 0 && obj.className.indexOf("empty") != -1));
    }
    function check2(active, obj) {
        return (!(active.tagName == "TH" && obj.tagName == "TH"));
    };
    function check3(obj) {
        return (obj.className) ? (obj.className.indexOf("selected") == -1) : true;
    };

    this.start();
}
