var texthighlight = new Object();
texthighlight.tooltip = new Object();

texthighlight.tooltip.win = false;
texthighlight.tooltip.content = new Object();
texthighlight.tooltip.content.curword = 0;

texthighlight.tooltip.create = function(){
	this.win = document.createElement("div");
	this.win.className = "tooltip";
	this.pics = gui.createElementTo("div",this.win)
	this.pics.className = "pics";
	this.pic = gui.createElementTo("img",this.pics);
	this.loadingpic = gui.createElementTo("img",this.pics);
	
	this.headline = gui.createElementTo("h1",this.win);
	this.headlineText = gui.createTextNodeTo("",this.headline);
	this.paragraph = gui.createElementTo("p",this.win);
	this.text = gui.createTextNodeTo("",this.paragraph);
	
	var preLoadedImg = new Image();
	preLoadedImg.src = homePath+"/layout/templates/cms/wait.gif";
}

texthighlight.tooltip.show = function(e,parent,word){
	if(!this.win)
		this.create();
		
	document.body.appendChild(this.win);
	
	if(texthighlight.tooltip.content.curword != word)
		texthighlight.tooltip.getData(word);
}

texthighlight.tooltip.loading_status = -1;
texthighlight.tooltip.loading = function(status){
	switch(status){
		case 1:
			if((texthighlight.tooltip.loading_status&&2) == 2)
				this.loadingpic.style.display = "none";
			else
				this.loadingpic.style.display = "block";
			
			this.win.style.backgroundImage = "none";
			texthighlight.tooltip.loading_status += 1;
			this.win.style.height = "auto";
		break;
		case 2:
			
			if((texthighlight.tooltip.loading_status&&1) == 1)
				this.win.style.backgroundImage = "none";
			this.loadingpic.style.display = "none";
			texthighlight.tooltip.loading_status += 2;
		break;
		default:
			/*this.loadingpic.style.margin = "25px auto";
			this.loadingpic.style.display = "block";*/
			//this.loadingpic.src = homePath+"/layout/templates/cms/wait.gif";
			this.loadingpic.src = homePath+"/layout/templates/cms/wait.gif";
			this.loadingpic.style.display = "none";
			this.win.style.backgroundImage = "url('"+homePath+"/layout/templates/cms/wait.gif');";
			texthighlight.tooltip.loading_status = 0;
			this.win.style.height = "100px";
		break;
	}
	
}

texthighlight.tooltip.getData = function(word){
	texthighlight.tooltip.loading();
	this.paragraph.style.display = "none";
	this.headline.style.display = "none";
	url = homePath+"/module/texthighlight/texthighlight.ajax.php";
	onload = texthighlight.tooltip.progressData;
	

	post = "&word="+word;
		
	var ongather = null;
	var onerror = function(){
		debug("error fetching data! Status:"+this.ajax.status);
	};
	
	req = new net.postRequest(url,onload,post,ongather,onerror);	
	this.pic.src = homePath+"/module/texthighlight/texthighlight.pic.php?word="+word;
	this.pic.onload = texthighlight.tooltip.onloadImage;
	this.pic.onerror = texthighlight.tooltip.onerrorImage;
	this.pic.style.display = "none";
}

texthighlight.tooltip.progressData = function(word){
	var xml = this.ajax.responseXML;
	if(!xml){
		return alert(this.ajax.responseText);
	}
	var word = new Object();
	easyXml.read.Object(xml.documentElement.firstChild,word);
	texthighlight.tooltip.content.text = word.description;
	texthighlight.tooltip.content.headline = word.word;
	texthighlight.tooltip.loading(1);
	texthighlight.tooltip.updateData();
	texthighlight.tooltip.content.curword = word.id;
}

texthighlight.tooltip.updateData = function(){
	this.paragraph.style.display = "block";
	this.headline.style.display = "block";
	this.text.nodeValue = this.content.text;
	this.headlineText.nodeValue = this.content.headline;
	
	//this.text.nodeValue = this.content.text+" - - - "+this.pic.src;
}

texthighlight.tooltip.onloadImage = function(){
	texthighlight.tooltip.loading(2);
	texthighlight.tooltip.pic.style.display = "block";
}

texthighlight.tooltip.onerrorImage = function(){
	texthighlight.tooltip.loading(2);
	texthighlight.tooltip.pic.style.display = "none";
}

texthighlight.tooltip.mouseout = function (e){
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	//if (tg.nodeName != 'DIV') return;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	if(!reltg)
		return;
	while (reltg != tg && reltg.nodeName != 'BODY'){
		if(reltg == document)
			break;
		reltg= reltg.parentNode;
	}
	if (reltg== tg) return;
	
	texthighlight.tooltip.hide(e);
}

texthighlight.tooltip.hide = function (e){
	if(this.win)
		if(this.win.parentNode)
			this.win.parentNode.removeChild(this.win);
}

texthighlight.mousemove = function(event){
	if(window.event)
			event = window.event;
	if(event.clientX){
		xPos = event.clientX;
		yPos = event.clientY;

	}
	
	if(event.pageX){
		yPos = event.pageY;
		xPos = event.pageX;
	}
	if(texthighlight.tooltip.win){
		texthighlight.tooltip.win.style.top = yPos+20+"px";
		texthighlight.tooltip.win.style.left = xPos+10+"px";
	}
	
	if(texthighlight.mousemoveOld)
		texthighlight.mousemoveOld(event)
}

texthighlight.mousemoveOld = document.onmousemove;
document.onmousemove = texthighlight.mousemove;
