var LexiconToolTip = Class.create();

LexiconToolTip.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);
				ToolTips.attach(e, this.getItemTitle(e, itemID), this.getItemContent(e, itemID), LEXICON_WINDOW_WIDTH);
			}
		}
	},

	attach : function(e) {
		this.attachToElement(e);
		this.loadLexiconData();
	},

	detach : function(e) {
		ToolTips.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;

			if (onLoad) {
				onloadEvents.push(function() {
					LexiconToolTips.ajaxRequest.openPost(
							'index.php?page=LexiconIndex' + SID_ARG_2ND, postData,
							function() {
								LexiconToolTips.receiveResponse();
							});
				});
			} else {
				LexiconToolTips.ajaxRequest.openPost(
						'index.php?page=LexiconIndex' + SID_ARG_2ND, postData,
						function() {
							LexiconToolTips.receiveResponse();
						});
			}
		}
	},

  showLoadingInfo : function(element, itemID) {
    LexiconToolTips.current = element;
    LexiconToolTips.requestedItemIDs.push(itemID);
    LexiconToolTips.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
        };
      }
      this.requestedItemIDs.clear();
      
      if(this.current !== false) {
        ToolTips.refresh();
        this.current = false;
      }
    }
	},

	getItem : function(itemID) {
		if (typeof(LexiconToolTips.items[itemID]) != 'undefined')
			return LexiconToolTips.items[itemID];
    return false;
	},

	getItemTitle : function(e, itemID) {
		return function() {
			var item = LexiconToolTips.getItem(itemID);
      
			if (item === false) {
        LexiconToolTips.showLoadingInfo(e, itemID);
        return '';
			}
      else
        return item.title;
		};
	},

	getItemContent : function(e, itemID) {
		return function() {
			var item = LexiconToolTips.getItem(itemID);
			if(item === false) {
				LexiconToolTips.showLoadingInfo(e, itemID);
				return '';
			} else {
				return item.description;
			}
		};
	},

	showError : function(msg) {
		alert('Error@LexiconToolTip: '+msg);
	}
};

var LexiconToolTips = null;

var lexiconWaitFunction = function() {
  if (typeof (ToolTips) == 'undefined' || ToolTips == null) onloadEvents.push(lexiconWaitFunction);
  else LexiconToolTips = new LexiconToolTip();
};

lexiconWaitFunction();
