/*
vuDAT - Michigan State University
written by:  Nathan Lounds
Updated 10/26/2007 to use jQuery
*/
var timer;
var tmp_lesson = "";
var theTabIndex = 0;
function makeLessonHovers(elem) {
	// code in here gets executed once the page loads // begin section
	//console.log("makeLessonHovers("+elem.id+":"+elem.className+")");
	if(elem) {
		$(elem).each(function(i){													
			setItUp(this);
		});
	} else {
		$(".ln a").each(function(i){
			setItUp(this);
		});
		// code in here gets executed once the page loads // end section
	}
}

function setItUp(theLink) {
	var cur_lesson = theLink;
	cur_lesson.onmouseover = function() { doFocus(this); }
	//cur_lesson.onfocus = function() { doFocus(this); }
	cur_lesson.onmouseout = function() { doBlur(); }
	//cur_lesson.onblur = function() { doBlur(); }
	//cur_lesson.onclick = function() {
		/*
		var me = this;
		var the_div = document.getElementById("clickPosText");
		the_div.title = "click to close";
		tmp_lesson = me;
		glossaryTool.getDefinition();
		*/
	//}
	//console.log("setting up");
}

function doFocus(elem) {
	tmp_lesson = elem;
	stopper();
	glossaryTool.getDefinition();
}

function doBlur() {
	var the_div = document.getElementById("clickPosText");
	if(the_div) {
		timer = setTimeout("clickPosTextKill()",800);
	}
}
var glossaryTool = {
	getDefinition : function() {
		//console.log("getDefinition");
		if(tmp_lesson.id) {
			clickPosText(tmp_lesson,"...loading lesson summary...");
			$.ajax({
				type: "GET",
				url: "/lessons/search/lesson_meta.asp",
				data: "lesson="+tmp_lesson.id,
				success: function(str){
					//trace("handleDefinition");
					var the_cont = document.getElementById("clickPosText");
					the_cont.title = "click to close";
					var the_code = str.split("|");
					var resp = "";
					if(the_code[0]=="SUCCESS") {
						resp = the_code[1];
					} else {
						resp = "lesson not found";
					}
					the_cont.innerHTML = resp;
					var my_diff = document.body.clientWidth-document.body.scrollWidth;
					if(my_diff!=0) {
						moveIt(the_cont,my_diff);
					}
				}
			});
		}
	}
}

/* sets the tab index of newly loaded lesson names */
function setTabIndex(id) {
	$("#"+id+" a").each(function(i){
		theTabIndex = theTabIndex+1;
		this.tabIndex = theTabIndex;
	});
	$("#"+id+" a").eq(0).each(function(i){
		this.focus();
	});
}

function clickPosText(obj,txt) {
	//console.log("clickPosText("+obj+","+txt+")");
	clickPosTextKill();
	var container = document.createElement('div');
	var pos = findPos(obj);
	var howfar = pos[0]+(obj.offsetWidth/2);
	if(obj.offsetWidth<400) {
		if($(obj).is("img")!=true) {
			howfar = pos[0]+(obj.offsetWidth/2)+100;
		} else {
			howfar = pos[0]+obj.width;
		}
	}
	container.id = "clickPosText";
	container.style.top = (pos[1]+18) + "px";
	//console.log(obj.offsetWidth + "::" + pos[0]);
	container.style.left = (howfar) + "px";
	container.innerHTML = txt;
	document.body.appendChild(container);
	container.onclick = function() {
		$(this).remove();
	}
	container.onmouseover = function() {
		stopper();
	}
	container.onmouseout = function() {
		timer = setTimeout("clickPosTextKill()",800);
		//trace("timeout to kill (cont)");
	}
}
function clickPosTextKill() {
	//trace("clickPosTextKill()");
	$("#clickPosText").remove();
}
function moveIt(obj,howFar) {
	//trace("moveIt("+obj+","+howFar+")");
	var pos = findPos(obj);
	var the_pos = parseInt(obj.style.left);
	obj.style.left = (the_pos+howFar) + "px";
}
function stopper() {
	//trace("stopper()");
	clearTimeout(timer);
}

/* 
Find Position of object
http://www.quirksmode.org/js/findpos.html
*/
function findPos(obj) {
	//trace("findPos("+obj+")");
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}