/*
 * @version 0.1.1
 *
 * Just place highlight="highlight" as the attribute of any table and
 * the table's rows will be highlighted while mouse is over. Also click at a row will cause mark/unmark the row.
 * To star the engine execute row_init() function.
 * Additional (optional) table attributes:
 *   highlight_color="<color>"
 *   highlight_marker_color="<color>"
 *   nomarker="true"/"false" do not mark the rows of the table (if true) or do mark (if false)
 * Additional (optional) tr attributes:
 *   nohighlight="nohighlight" do not highlight the row
 *   nomarker="true"/"false" do not mark the row (if true) or do mark (if false)
 *
 * Example:
 *     <body onload="row_init()">
 *     <table ... highlight="highlight" highlight_color="#CCCCCC" nomarker="nomarker">
 *     <tr>
 *         ...
 *     </tr>
 *     <tr nohighlight="nohighlight">
 *       the row that needn't to be highlighted...
 *     </table>
 *     <tr nomarker="false">
 *       the row that need to be marked...
 *     </table>
 *
 *     </body>
 */


var row_defaultHighlightColor = '#DFDFDF';
var row_defaultMarkerColor = '#C2D6E9';
var row_HighlightColor = row_defaultHighlightColor;
var row_MarkerColor = row_defaultMarkerColor;
var row_DoMark = true;

function row_onmouseover(e) {
    if (typeof(e) != 'undefined') event = e;

    row_get_table_settings(event);
    row_highlight(event);
}

function row_onmouseout(e) {
    if (typeof(e) != 'undefined') event = e;

    row_restore(event);
}

function row_onmouseclick(e) {
    if (typeof(e) != 'undefined') event = e;

    row_trigger_marker(event);
}

function row_trigger_marker(event, bgcolor) {
    if (typeof(bgcolor) == 'undefined') bgcolor = row_MarkerColor;

    tr = row_find_tag(event, "TR");

    if (tr == null) return;

    if (!row_do_mark(tr)) return;

    if (!row_is_marked(tr)) {
        row_highlight(event, bgcolor, 'row_previouseBackgroundColorMarker');
        row_mark(tr);
    }
    else {
        row_restore(event, 'row_previouseBackgroundColorMarker');
        row_mark(tr, false);
    }
}

function row_highlight(event, bgcolor, oldColorMemberName, ignoreMarker) {
    if (typeof(oldColorMemberName) == 'undefined') oldColorMemberName = 'row_previouseBackgroundColor';
    if (typeof(ignoreMarker) == 'undefined') ignoreMarker = false;

    if (typeof(bgcolor) == 'undefined') bgcolor = row_HighlightColor;

    p = row_find_tag(event, "TR");

    if (!ignoreMarker && row_is_marked(p)) return;
    if (row_no_highlight(p)) return;

    if (p !== null) {
        tds = p.getElementsByTagName('TD');
        for (i = 0; i < tds.length; i++) {
            tds[i][oldColorMemberName] = tds[i].style.backgroundColor;
            tds[i].style.backgroundColor = bgcolor;
        }
    }
}

function row_restore(event, oldColorMemberName, ignoreMarker) {
    if (typeof(oldColorMemberName) == 'undefined') oldColorMemberName = 'row_previouseBackgroundColor';
    if (typeof(ignoreMarker) == 'undefined') ignoreMarker = false;

    p = row_find_tag(event, "TR");

    if (!ignoreMarker && row_is_marked(p)) return;
    if (row_no_highlight(p)) return;

    if (p !== null) {
        tds = p.getElementsByTagName('TD');
        for (i = 0; i < tds.length; i++) {
            window.status = tds[i][oldColorMemberName];
            tds[i].style.backgroundColor = tds[i][oldColorMemberName];
        }
    }
}

function row_init() {
    tables = document.getElementsByTagName('table');
    for (i = 0; i < tables.length; i++) {
        v = row_get_attribute(tables[i], 'highlight');
        if (v != null) {
            tables[i].onmouseover = row_onmouseover;
            tables[i].onmouseout = row_onmouseout;
            tables[i].onclick = row_onmouseclick;
        }
    }
}

function row_get_table_settings(event) {
    table = row_find_tag(event, 'TABLE');

    v = row_get_attribute(table, 'highlight_color');
    if (v !== null) row_HighlightColor = v;
    else row_HighlightColor = row_defaultHighlightColor;

    v = row_get_attribute(table, 'highlight_marker_color');
    if (v !== null) row_MarkerColor = v;
    else row_MarkerColor= row_defaultMarkerColor;

    v = row_get_attribute(table, 'nomarker');
    row_DoMark = v == null || (v+"").toLowerCase() == 'false';
}

function row_find_tag(event, tagName, fromEvent) {
    if (typeof(fromEvent) == 'undefined') fromEvent = true;

    if (fromEvent) {
        if (typeof(event.target) != 'undefined') event.srcElement = event.target;
        p = event.srcElement;
    }
    else {
        p = event;
    }

    while(p != null && p.tagName != tagName) {
        p = p.parentNode;
    }
    return p;
}

function row_is_marked(tr) {
    return (tr !== null && typeof(tr.row_marked) != 'undefined' && tr.row_marked);
}

function row_get_attribute(tr, attributeName) {
    result = null;

    if (tr !== null && typeof(tr[attributeName]) != 'undefined') {
        result = tr[attributeName];
    }
    else if (tr !== null && typeof(tr.attributes[attributeName]) != 'undefined') {
        result = tr.attributes[attributeName];
    }

    if (result !== null && typeof(result.value) != 'undefined') result = result.value;

    return result;
}

function row_no_highlight(tr) {
    v = row_get_attribute(tr, 'nohighlight');
    return v !== null && (v+"").toLowerCase() !== 'false';
}

function row_do_mark(tr) {
    v = row_get_attribute(tr, 'nomarker');
    if (v === null) return row_DoMark;
    else if ((v+"").toLowerCase() == 'false') return true;
    else return false;
}

function row_mark(tr, toMark) {
    if (typeof(toMark) == 'undefined') toMark = true;
    tr.row_marked = toMark;
}

