var highlitClass;

function highlightRow(n, active){
    var tablecells = n.getElementsByTagName("td");
    for (var b = 0; b < tablecells.length; b++){

	    if (active == 1){
           highlitClass = tablecells[b].className;
           tablecells[b].className = "highlittd";
		} else {
           tablecells[b].className = highlitClass;
		}

    }
}


function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 25);
    }
}

function findElementsByClass(result, n, classNm){
    if ((n.nodeType == 1) && (n.className== classNm))  // Check if n is an Element
        result.push(n);
    var children = n.childNodes;                // Now get all children of n
    for(var i=0; i < children.length; i++) {    // Loop through the children
        findElementsByClass(result,children[i], classNm);      // Recurse on each one
    }
}

function initWindows(){
	var n = document.documentElement;
    var result = new Array();
	findElementsByClass(result, n, 'window');
	for(var i = 0; i < result.length; i++){
		var title = '';
		var color = '';
		var content = '';
        var size ='';
		var attribs = result[i].attributes;
        title =result[i].getAttribute("rel");
        color =result[i].getAttribute("col");
        size =result[i].getAttribute("size");
        if (size == null) size = 'big';

		content = result[i].innerHTML;
		content = buildTable(title, color, content, size);
		result[i].innerHTML=content;
	}
}

function initTables(){
	var n = document.documentElement;
    var result = new Array();
	findElementsByClass(result, n, 'defaulttable');

	for(var i = 0; i < result.length; i++){
        var tablebody = result[i].getElementsByTagName("tbody")[0];
        var tablerows = tablebody.getElementsByTagName("tr");
        var linecount = 0;
        for (var a = 0; a< tablerows.length; a++){
            var tablecells = tablerows[a].getElementsByTagName("td");
            for (var b = 0; b < tablecells.length; b++){
                var classNm = "oddtd";
                if (linecount % 2 == 1) classNm = "eventd";
                tablecells[b].className = classNm;
            }
            linecount ++;
        }
	}
}

function buildTable(title, color, content, size){
    var topurl = "./pix/box_"+color+"_top";
    if (size=='small')
        topurl+="_s";
    topurl += ".png";
	var result ='<div class ="window-'+size+'-root"><div class ="window-'+size+'-top" style="background-image: url('+topurl+')"><h1>'+ title +'</h1></div><div class ="window-'+size+'-middle"><div class ="window-watermark" style="background-image:url(./pix/water_' + color + '.png)"></div><div class ="content">' + content + '</div></div><div class ="window-'+size+'-bottom"></div></div>';

	return result;
}
