WikipediaLinkModule = new Object();
WikipediaLinkModule._xmlHttp = null;
WikipediaLinkModule._searchText = null;
WikipediaLinkModule._element = null;
WikipediaLinkModule._popupPanel = null;
WikipediaLinkModule.WikipediaSubdomain = null;
WikipediaLinkModule.ReadArticleText = null;
WikipediaLinkModule.NoInformationText = null;
WikipediaLinkModule.ErrorText = null;
WikipediaLinkModule.Configure = function(wikipediaSubdomain, readArticleText, noInformationText, errorText)
{
	this.WikipediaSubdomain = wikipediaSubdomain;
	this.ReadArticleText = readArticleText;
	this.NoInformationText = noInformationText;
	this.ErrorText = errorText;
}
WikipediaLinkModule.Close = function()
{
	if (this._popupPanel && this._popupPanel.IsShown())
		this._popupPanel.Hide();
}
WikipediaLinkModule.Closed = function()
{
	if (this._element)
		this._element.className = 'wiki_link';
}
WikipediaLinkModule.Lookup = function (searchText, element, event)
{
	if (!wiki2html)
	{
		alert(this.ErrorText + '\n\nThe Wikipedia interpreter is no longer available.  Wikipedia links will not work.');
		return;
	}
	
	try
	{
		if (!this._popupPanel)
			this._popupPanel = new Telligent_PopupPanel('WikipediaLinkModule._popupPanel', 'wiki_popuppanel', 'updown', 1000, null, new Function('WikipediaLinkModule.Closed();'), true, '');

		this.Close();
		
		this._searchText = searchText;
		this._element = element;
		this._element.className = 'wiki_link_open';
		
		var header = document.createElement('div');
		var body = document.createElement('div');
			
		header.className = 'wiki_header';
		body.className = 'wiki_body';
			
		header.innerHTML = 'Wikipedia: ' + this._searchText.replace(/[<>]/g, '');
			
		this._popupPanel.ClearPanelContent();
		this._popupPanel.AddNodeToPanel(header);
		body.innerHTML = '...';
		this._popupPanel.AddNodeToPanel(body);
				
		this._popupPanel.ShowAtElement(element);
			
		var url = window.location.href;
		if (url.indexOf('?') > -1)
			url += "&WikipediaQuery=";
		else
			url += "?WikipediaQuery=";

		if (window.XMLHttpRequest)
		{
			this._xmlHttp = new XMLHttpRequest();
			this._xmlHttp.onreadystatechange=new Function('WikipediaLinkModule.LookupCallback();');
			this._xmlHttp.open('GET', url + encodeURIComponent(this._searchText) + '&subdomain=' + this.WikipediaSubdomain, true);
			this._xmlHttp.send(null);
		}
		else if (window.ActiveXObject)
		{
			this._xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
			if (this._xmlHttp)
			{
				this._xmlHttp.onreadystatechange=new Function('WikipediaLinkModule.LookupCallback();');
				this._xmlHttp.open('GET', url + encodeURIComponent(this._searchText) + '&subdomain=' + this.WikipediaSubdomain, true);
				this._xmlHttp.send(null);
			}
		}
	}
	catch (err)
	{
		alert(this.ErrorText + '\n\n' + err.message);
		if (this._popupPanel)
			this._popupPanel.Hide();
	}
	
	return false;
}
WikipediaLinkModule.LookupCallback = function()
{
	if (this._xmlHttp.readyState == 4)
	{
		if (this._popupPanel)
		{
			var header = document.createElement('div');
			var body = document.createElement('div');
			var footer = document.createElement('div');
			
			header.className = 'wiki_header';
			body.className = 'wiki_body';
			footer.className = 'wiki_footer';
			
			header.innerHTML = 'Wikipedia: ' + this._searchText.replace(/[<>]/g, '');
			
			this._popupPanel.ClearPanelContent();
			this._popupPanel.AddNodeToPanel(header);
		
			try
			{
				var text = this._xmlHttp.responseText;
			}
			catch (err)
			{
				body.innerHTML = this.NoInformationText;
				this._popupPanel.AddNodeToPanel(body);
				return;
			}
			
			if (!text)
			{
				body.innerHTML = this.NoInformationText;
				this._popupPanel.AddNodeToPanel(body);
				return;
			}

			var textLength = -1;
			while (textLength != text.length)
			{
				textLength = text.length;
				text = text.replace(/\{\{[^\}\{]*?\}\}/g, '');
			}
			
			text = text.replace(/\:\'\'[^\n]*?\n/g, '\n');
			
			textLength = -1
			while (textLength != text.length)
			{
				textLength = text.length;
				text = text.replace(/\[\[Image:[^\n]*?\n/ig, '\n');
			}
			
			text = wiki2html(text);
			text = text.replace(/\<img[^\>]*?\>/gi, '');
			
			re = /href=(\"|\')[^\'\"\>]*?\/wiki\//g;
			while ((arr = re.exec(text)) != null)
			{
				text = text.replace(arr[0], 'href=' + arr[1] + 'http://' + this.WikipediaSubdomain + '.wikipedia.org/wiki/');
			}
			
			var inTag = false;
			var newText = '';
			var done = false;
			var i = 0, newLength = 0;
			var c;
			while (!done && i < text.length)
			{
				c = text.substr(i, 1);
				if (c == '<')
					inTag = true;

				if (!inTag)
					newLength++;
					
				if (c == '>')
					inTag = false;
				
				newText += c;
					
				if (newLength > 250 && !inTag && c.match(/[^a-zA-Z0-9]/))
					done = true;
					
				i++;
			}		
						
			if (newText.length < text.length)	
			{
				newText += "...";
				footer.innerHTML = '<a href="http://' + this.WikipediaSubdomain + '.wikipedia.org/wiki/' + encodeURIComponent(this._searchText) + '">' + this.ReadArticleText + '</a>';
			}
			else
				footer.innerHTML = '<a href="http://' + this.WikipediaSubdomain + '.wikipedia.org/wiki/' + encodeURIComponent(this._searchText) + '">' + this.ReadArticleText + '</a>';
				
			body.innerHTML = newText + "<p />";
				
			this._popupPanel.AddNodeToPanel(body);				
			this._popupPanel.AddNodeToPanel(footer);
		}
		else
		{
			body.innerHTML = this.ErrorText;
			this._popupPanel.AddNodeToPanel(body);
		}
	}
}