/*
 * hwm.js
 * Author: Mordechai Peller
 * e-mail: mep@pellerweb.com
 *
 * This file adds addLoadEvent(), chartBuilder(), addHintEvent(),
 * addClass(), and removeClass() functionality to the
 * HomeworkMessanger system.
 *
 * See bellow for details on functions. 
 */


/*
 * Name: addLoadEvent(func)
 * Parameter    Type    Description
 *    func    Function  Function to be added to window.onload event.
 *
 * Comments:
 * addLoadEvent() should be used instead of adding an onLoad event
 * to the body tag.
 *
 * If window.onload is not set, func will be assigned to it; otherwise,
 * a function will be created and assigned which first invokes the
 * current window.onload event and then func.
 *
 */

function addLoadEvent(func) {
  var oldOnload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldOnload();
      func();
    }
  }
}

/*
 * Name: addClass(ele, className) and removeClass(ele, className)
 * Parameter    Type      Description
 *    ele    DOM element  Element to which className is add or removed
 * className   String     Name of class to be added or removed
 *
 * Comments:
 * If className already exists, addClass() will not add it again.
 * removeClass() will remove all instances of className from class.
 *
 */

function addClass (ele, className) {
	if (ele.className.search("(^|\\s)"+className+"($|\\s)")==-1) ele.className +=((ele.className)?" ":"") + className;
}
function removeClass (ele, className) {
	ele.className = (ele.className.split(new RegExp("(^|\\s)"+className+"(\\s|$)"))).join("");
}
/*
 * Name: chartBuilder(chart,child,baseUri)
 * Parameter     Type     Description
 *  chart    DOM element  Container into which the chart features are built.
 *  child       Object    Object containing arrays, length 4, with:
 *                        behaviourDescription,isRated, and behaviorId
 *  baseUri     String    URI to which the behavior id and value are passed.
 *
 * Comments:
 * It is assumed that an image of the current chart is a child of chart.
 *
 */

function chartBuilder(chart,child,baseUri) {
	var bars,hintLegend;
	var col,div,i,j;
	var num;
	addClass(chart,"hideitems");
	addClass(chart,"chart");
	bars = document.createElement("div");
	bars.className = "bars";
	chart.appendChild(bars);
	
	div = document.createElement("div");
	div.appendChild(document.createTextNode("5 - excelent"));
	div.className ="topLegend";
	chart.appendChild(div);
	
	div = document.createElement("div");
	div.appendChild(document.createTextNode("1 - poor"));
	div.className ="bottomLegend";
	chart.appendChild(div);
	
	hintLegend = document.createElement("div");
	hintLegend.appendChild(document.createElement("span"));
	hintLegend.className="hintLegend hideitems"; 
	hintLegend.firstChild.appendChild(document.createTextNode("Rate a "));
	hintLegend.firstChild.appendChild(document.createElement("strong"));
	hintLegend.firstChild.appendChild(document.createTextNode(" for "));
	for (i=0;i<4;i++) {
		hintLegend.appendChild(document.createElement("span"));
		if (!child.behaviourDescription[i]) {
			child.behaviourDescription[i] = "(description is blank)";
		}
		hintLegend.lastChild.appendChild(document.createTextNode(child.behaviourDescription[i]+"."));
		addClass(hintLegend.lastChild,"hideitems"); 
	}
	chart.appendChild(hintLegend);
	
	chart.onmouseover = function(){removeClass(this,"hideitems");}
	chart.onmouseout = function(){addClass(this,"hideitems");}
	for (var colnum=4, j=3;j>-1;j--) {
		if (child.behaviorId[j]==-1 || child.isRated[j]=="true") continue;
		colnum--;
		col = document.createElement("div");
		for (i=5;i>0;i--) {
			col.onclick = (function(val, behaviorId, behaviourDescription, baseUri) {
				return function(event){
					if (event) {
						event.stopPropagation();
					} else {
						window.event.cancelBubble=true;
					}
					if (confirm('The behavior, "' + behaviourDescription + '," will be rated with '+ val +' point(s).\nContinue?' )) {
						window.location = baseUri + behaviorId + "/rateVal/" + val;
					}
				}
			})(6-i, child.behaviorId[j], child.behaviourDescription[j], baseUri);
			col.onmouseover = function (event) {
				for (var o=this.parentNode;!o.className.length; o = o.parentNode) {
					o.style.backgroundColor = "transparent";
				}
				o.parentNode.parentNode.onmouseover();
				o.onmouseover();
				this.style.backgroundColor = "";
				if(o.hintLegend.firstChild.childNodes[1].firstChild) {
					o.hintLegend.firstChild.childNodes[1].removeChild(o.hintLegend.firstChild.childNodes[1].firstChild);
				}
				o.hintLegend.firstChild.childNodes[1].appendChild(document.createTextNode(this.level));
				if (event) {
					event.stopPropagation();
				} else {
					window.event.cancelBubble=true;
				}
			};
			col.onmouseout= function(){
				if(this.firstChild) this.style.backgroundColor = this.firstChild.style.backgroundColor;
			}
			col.level = 6-i;
			div = document.createElement("div");
			div.appendChild(col);
			col = div;
		}
		col.setAttribute("title","Chart showing ratings of the active behaviors. To rate, slide thermometer and click");
		col.hintLegend = hintLegend;
		col.className ="col"+colnum+" bhv"+j;
		bars.appendChild(col);
		col.onmouseover = (function(hintnum,hintdiv) {
			return function() {
				removeClass(hintdiv,"hideitems");
				removeClass(hintdiv.childNodes[1+hintnum],"hideitems");
			}
		})(j,hintLegend);

		col.onmouseout = (function(hintnum,hintdiv) {
			return function() {
				addClass(hintdiv,"hideitems");
				addClass(hintdiv.childNodes[1+hintnum],"hideitems");
			}
		})(j,hintLegend);
	}
}

/*
 * Name: addHintEvent()
 *
 * Comments:
 * Adds a hint from element's hint attribute to infotext
 * area of current mainTable.
 *
 */

addLoadEvent(addHintEvent);
function addHintEvent() {
	var eles;
	var infotext;
	var allTables = document.getElementsByTagName("table");
	var tables = new Array();
	var infoNum =0;
	for (var i=0; i<allTables.length;i++) {
		if (allTables[i].getAttribute("tID") == "mainTable") {
			infotext = tID(allTables[i],"infotext");
			if (!infotext) continue;
			eles = allTables[i].getElementsByTagName("*");
			for (var j=0; j<eles.length;j++) {
				if (eles[j].getAttribute("hint")) {
					addHint(eles[j],eles[j].getAttribute("hint"),infotext);
				}
			}
		}
	}
}

function addHint(ele,hint,infotext) {
	var hintShow = function (ele) {
		return function() {
			ele.style.display="block";
		}
	};
	var hintHide = function (ele) {
		return function() {
			ele.style.display="none";
		}
	};
	var div = document.createElement("div");
	div.appendChild(document.createTextNode(ele.getAttribute("hint")));
	infotext.appendChild(div);
	ele.onmouseover =hintShow(div);
	ele.onmouseout = hintHide(div);
}


