/**
 * @author Peter Fruehwirt
 * @package de.wbb-security.lexicon.parser
 * @copyright www.wbb-security.de 2005-2008
 */
var LexiconIndex = Class.create();

LexiconIndex.prototype = {

	initialize : function() {
		this.items = new Array();
		this.requestedItemIDs = new Array();
		this.ajaxRequest = new AjaxRequest();

		this.window = $$('.lexiconIndexWindow')[0];
		this.current = false;
		
		var elements = $$('.lexiconIndexItem');
		elements.each(this.attachToElement, this);
	},

	attachToElement : function(e) {
		var itemID = this.parseItemID(e);

		if (itemID != 0) {
			if (typeof this.items[itemID] == 'undefined') {
				this.requestedItemIDs.push(itemID);
				LexiconIndexWindow.attach(e, this.getItemTitle(e, itemID), this.getItemContent(e, itemID), LEXICON_WINDOW_WIDTH);
			}
		}
	},

	attach : function(e) {
		this.attachToElement(e);
		this.loadLexiconData();
	},

	detach : function(e) {
		LexiconIndexWindow.detach(e);
	},

	parseItemID : function(e) {

		if (e.className == 'undefined') {
			this.showError("Invalid Element found at function parseItemID.");
		}

		var currentPart = e.className.split('lexiconIndexItem ');
		if (currentPart.length < 2) {
			this.showError("Invalid classNames found at Element " + e
					+ ", e.className: " + e.className);
		}

		currentPart = currentPart[1].split('lexiconItem')
		if (currentPart.length < 2) {
			this.showError("Invalid classNames found at Element " + e
					+ ", e.className: " + e.className);
		}

		return currentPart[1];
	},

	loadLexiconData : function(onLoad) {
		if (typeof onLoad == 'undefined')
			onLoad = false;
		if (this.requestedItemIDs.length != 0) {
			var postData = this.requestedItemIDs.join('&itemID[]=');
			if (postData === '') {
				return;
			}
			postData = 'itemID[]=' + postData;
			
			// TOOD add languages

			// make request
			if (onLoad) {
				onloadEvents.push(function() {
					LexiconIndexManager.ajaxRequest.openPost(
							'index.php?page=LexiconIndex' + SID_ARG_2ND, postData,
							function() {
								LexiconIndexManager.receiveResponse();
							});
				});
			} else {
				LexiconIndexManager.ajaxRequest.openPost(
						'index.php?page=LexiconIndex' + SID_ARG_2ND, postData,
						function() {
							LexiconIndexManager.receiveResponse();
						});
			}
		}
	},
	
	showLoadingInfo : function(e, itemID) {
		if(LexiconIndexManager.window.visible()) {
			LexiconIndexManager.window.hide();
		}
		
		//save current window
		LexiconIndexManager.current = e;
			
		//request id
		LexiconIndexManager.requestedItemIDs.push(itemID);
			
		//show loading window	
		LexiconIndexManager.window.innerHTML = '<div class="lexiconIndexWindowLoading"><img src="'+LEXICON_WINDOW_WORKING+'" alt="" /></div>';
		LexiconIndexManager.window.show();

		
		//load data
		LexiconIndexManager.loadLexiconData();
	},
	
    receiveResponse : function() {
		if (this.ajaxRequest && this.ajaxRequest.xmlHttpRequest.readyState == 4 && this.ajaxRequest.xmlHttpRequest.status == 200 && this.ajaxRequest.xmlHttpRequest.responseXML) {
		    var parserItems = this.ajaxRequest.xmlHttpRequest.responseXML.getElementsByTagName('lexiconItem');

		    for (i = 0; i < parserItems.length; i++) {
				var itemID = parserItems[i].attributes.getNamedItem('id').value;
				this.items[itemID] = {
				    itemID : itemID,
				    title : parserItems[i].childNodes[0].firstChild.nodeValue,
			 	   description: parserItems[i].childNodes[1].firstChild.nodeValue
				};
		    }
		    //remove requested ids 
		    this.requestedItemIDs.clear();
		     
		    if(this.current !== false) {
		    	
		    	var currentItemID = this.parseItemID(this.current);
		    	
			    while(this.window.children.length!=0) {
			    	this.window.removeChild(this.window.firstChild);
			    }

			    LexiconIndexWindow.buildWindow();
		    	LexiconIndexWindow.setWindowContent(this.items[currentItemID].title, this.items[currentItemID].description, LEXICON_WINDOW_WIDTH);
		    	this.window = $$('.lexiconIndexWindow')[0];
		    	this.window.show();
		    }
		}
	},
	
	getItem : function(itemID) {
		if(typeof(LexiconIndexManager.items[itemID]) != 'undefined') {
			return LexiconIndexManager.items[itemID];
		} else {
			return false;
		}
	},
	
	getItemTitle : function(e, itemID) {
		return function() {
			//update window
			//LexiconIndexManager.window = $$('.lexiconIndexWindow')[0];
			
			var item = LexiconIndexManager.getItem(itemID);
			
			//item not found -> not loaded!
			if(item === false) {
			//	if(LexiconIndexManager.window.visible()) {
					LexiconIndexManager.showLoadingInfo(e, itemID);
					return '';
			//	}
			} else {
				return item.title;
			}
		};
	},
	
	getItemContent : function(e, itemID) {
		return function() {
			var item = LexiconIndexManager.getItem(itemID);
			//item not found -> not loaded!
			if(item === false) {
				LexiconIndexManager.showLoadingInfo(e, itemID);
				return '';
			} else {
				LexiconIndexManager.window.show();
				return item.description;
			}
		};
	},
	 
	showError : function(msg) {
		alert('Error@LexiconIndex: ' + msg);
	}
};

var lexiconWaitFunction = function() {
	//if (typeof (LexiconIndexWindow) == 'undefined' || LexiconIndexWindow == null) {
	//	onloadEvents.push(lexiconWaitFunction);
	//} else {
		LexiconIndexManager = new LexiconIndex();
	//}
};

//waiting for data to be loaded
onloadEvents.push(lexiconWaitFunction);

