// Ajax keywords

var AjaxKeywords = Class.create();

AjaxKeywords.prototype = {
	initialize: function(container) {
		this.container = $(container);		
		if (this.container) {													
			this.links = this.container.getElementsBySelector('a[rel="tag"]'); // zie microformats.org voor @rel="tag"	
			if (this.links.length > 0) {							
				this.createLemma();													
				this.links.each(this.setupLink.bind(this));
			}																				
		}						
	},	
	createLemma: function() {
		// voeg lemma popup div toe
		this.lemmaContainer = document.createElement('div');
		this.lemmaContainer.setAttribute('id','lemma');
		this.lemmaContainer.style.display = 'none';
		
		var lemmaBody = document.createElement('div');
		lemmaBody.setAttribute('id','lemma-body');
		lemmaBody.className = 'iepngfix';
		this.lemmaContainer.appendChild(lemmaBody);
		
		var lemmaBottom = document.createElement('div'); // ivm IE transparante png fix.
		lemmaBottom.setAttribute('id','lemma-bottom');
		lemmaBottom.className = 'iepngfix';
		lemmaBottom.appendChild(document.createTextNode(' '));	
		this.lemmaContainer.appendChild(lemmaBottom);
		
		this.container.appendChild(this.lemmaContainer);
					
		var closeLemma = document.createElement('a');
		closeLemma.className = 'close';		
		closeLemma.setAttribute('href','#close');
		closeLemma.appendChild(document.createTextNode('close'));			
		this.lemmaContainer.appendChild(closeLemma);
		closeLemma.onclick = this.clickListener.bindAsEventListener(this);
	},
	setupLink: function(link) { //Voeg eventlistener aan keyword links toe		
		Event.observe(link, 'click', this.clickListener.bind(this), false);
	},		
	clickListener: function(event) {
		var link = Event.element(event);	
		Event.stop(event);						
		if (link.hasClassName('close')) {						
			this.hideLemma();
		} else {	
			this.getLemma(link);
			this.updateClassNames(link);
		}
	},	
	getLemma: function(link) {	
		var _this = this;			
		var url =  link.href;						
		new Ajax.Updater('lemma-body', url, { onComplete: function() { _this.updateLinks(); } } );	
	},
	updateLinks: function() {			
		this.lemmaLinks = $$('#lemma-text a', '#parentTerm a', '#childTerms a');		
		this.lemmaLinks.each(this.setupLink.bind(this));
		this.showLemma();
	},
	updateClassNames: function(link) {		
		if (link.up('#lemma-body')) { // link in lemma popup geklikt				
			var url = link.href;							
			this.links.each(function(node) {
				if (node.href == url ) {
					node.addClassName('active');
				} else {
					node.removeClassName('active');
				}
			});			
		} else { // link in  keyword menu				 			
			link.addClassName('active');
			this.links.without(link).each(function(node) {																									
					node.removeClassName('active');															
			});			
		}						
	},
	showLemma: function() {	
		$(this.lemmaContainer).show(); //$() voor IE.
	},
	hideLemma: function(event) {	
		$(this.lemmaContainer).hide(); //$() voor IE.
		this.links.each(function(link) {																									
				link.removeClassName('active');															
		});	
	}
}