/* *****************************************************************************

縞模様を付ける JavaScript クラス

build 08.03.18 - 0.01

***************************************************************************** */

function tablecloth(cname, col1, col2, col3, col4, col5){
    this.tables = new Array();
    this.trs = {};
    this.clickAction = function(obj){};

    // テーブル要素にイベント設定
    this.start = function(){
        var mode  = 0;
        var color = "";
        var tbl = document.getElementsByTagName("table");
        for (var i=0;i<tbl.length;i++){
            if(tbl[i].className && tbl[i].className==cname){ tables.push(tbl[i]); }
        }
        for (var i=0;i<tables.length;i++){
            tables[i].id = "_tablestripe_autoid_" + i;
            trs[tables[i].id] = tables[i].getElementsByTagName("tr");
            for (var j=0;j<trs[tables[i].id].length;j++){
                var tr = trs[tables[i].id][j];
                tr.row = j;
                if(check1(tr,j)){
                    color = (mode) ? col1 : col2;
                    mode = 1 - mode;
                    tr.bgcolor = color;
                    tr.style.backgroundColor = color;
                    tr.onmouseover = function(){ over(this,this.row); }
                    tr.onmouseout  = function(){ out(this,this.row); }
                    tr.onclick = function(){ click(this,this.row); };
                }
            }
        }
    };

    // マウスオーバー処理
    this.over = function(obj,row){
        if(check1(obj,row)){ highlightRow(obj,row); }
    };

    // マウスアウト処理
    this.out = function(obj,row){
        if(check1(obj,row)){ unhighlightRow(obj,row); }
    };

    // マウスクリック処理　非選択→選択／選択→非選択
    this.click = function(obj,row){
        if(check1(obj,row) && obj){
            if(check3(obj)) {
                obj.selected = false;
                obj.style.backgroundColor = col3;
            }
            else{
                obj.selected = true;
                obj.style.backgroundColor = col4;
            }
        }
    };

    // ハイライト設定on
    this.highlightRow = function(obj,row){
        if(obj){
            //obj.className = (check3(obj)) ? "over-selected" : "over";
            obj.style.backgroundColor = (check3(obj)) ? col5 : col3;
        }
    };

    // ハイライト設定off
    this.unhighlightRow = function(obj,row){
        if(obj){
            obj.style.backgroundColor = (check3(obj)) ? col4 : obj.bgcolor;
        }
    };

    // 要素存在チェック
    this.check1 = function(obj,row){
        if(!obj) return false;
        return (!(obj.className.indexOf("empty") != -1));
    }

    // 要素THチェック
    this.check2 = function(obj){
        if(!obj) return false;
        return (!(obj.tagName == "TH"));
    };

    // 要素selectedチェック
    this.check3 = function(obj){
        if(!obj) return false;
        return obj.selected;
    };

    start();
};
