//balloon
function Balloon(){
	this.x = 0;
	this.y = 0;
	this.balloonOpen = false;
	this.tempX = 0;
	this.tempY = 0;
	this.countdown_balloon = 3;
	this.getMouseXY = function(mousePos){
		this.x = mousePos.x;
		this.y = mousePos.y;
		var ob = this.getPureElement('balloon');
		if(this.balloonOpen){
			ob.top = this.y+10;
			ob.left = this.x+10;
		}
		return true;
	}
	this.resetCountdown = function(){this.countdown_balloon = 3;};
	this.getPureElement = function(obj){
		if(document.getElementById)
			ro = document.getElementById(obj).style;       
		else
			ro = eval("document." + obj);     
	   return ro;
	}
	this.showBalloon = function(titel, content){
		this.countdown_balloon = 5;
		$("balloonTitel").innerHTML= titel;
		$("balloonContent").innerHTML= content.replace("images/","/kennissysteem/images/");
		var ob = this.getPureElement('balloon');
		ob.top = this.y+10;
		ob.left = this.x+10;		
		ob.visibility = 'visible'; 
		this.balloonOpen = true; //een beetje verkeerd woord
		var t = this;
		$('balloon').onmousemove = function(){t.resetCountdown();};
		$('balloon').onmouseout = function(){t.countdown_balloon=0;};
	};
	this.startHide = function(){
		this.resetCountdown();
		this.checkCountdown();
		this.balloonOpen = false;
	};
	this.hideBalloon = function(){
		var ob = this.getPureElement('balloon');
		ob.visibility = 'hidden'; 		
	}
	this.checkCountdown = function(){
		this.countdown_balloon--;
		if (this.countdown_balloon < 1) this.hideBalloon();
		else setTimeout("balloon.checkCountdown()",2000);
	}
	this.checkCountdown();
}
function initEncyclopedie(){
	var encyclopedieArticles = new Array();
	function balloonIt(article_id,on){ return function() { getBalloon(article_id,on);};}
	function getBalloon(article_id,on){
		if (on) {
			article = getEncyclopedieArticle(article_id);
			if (article == null){
				article = new Article();
				article.load(article_id,"balloon.showBalloon(article.getTitel(),article.getTekst());");
				var _encyclopedieArticle = new Array();
				_encyclopedieArticle.push(article_id);
				_encyclopedieArticle.push(article);
				encyclopedieArticles.push(_encyclopedieArticle);
			} else {
				balloon.showBalloon(article.getTitel(),article.getTekst());
			}
		} else {
			//balloon.hideBalloon();
			balloon.startHide();
		}
	}
	function getEncyclopedieArticle(article_id){
		_article = null;
		for (i=0;i<encyclopedieArticles.length;i++){
			if (encyclopedieArticles[i][0] == article_id) {
				_article = encyclopedieArticles[i][1];
				break;
			}
		}
		return _article;
	}
	var elements=document.getElementsByTagName('a');
	//verander alle <a ... rel="encyclopedie:article_id"></a> naar <a ... onmouseover="showBalloon(article_id)" onMouseOut="hideBalloon();" class="encyclopedie"></a>
	for(var i=0;i<elements.length;i++){
		if (elements[i].getAttribute("rel")!=null){
			var rel = elements[i].getAttribute("rel");
			if (rel.indexOf('encyclopedie')!=-1){
				var relArray = rel.split(':');
				var article_id = relArray[1];
				elements[i].setAttribute('class','encyclopedie');
				elements[i].setAttribute('className','encyclopedie');
				elements[i].onmouseover = balloonIt(article_id,true);
				elements[i].onmouseout = balloonIt(article_id,false);
				elements[i].setAttribute('href','encyclopedie.jsp?id='+article_id);
			}
		}
	}
}
