/************************************************************************\
 *
 * CES Vault Wiki
 * Version 3.0.1
 *
 * Copyright ©2008-2010 Cracked Egg Studios. All Rights Reserved.
 * This file may not be redistributed in whole or significant part.
 * 
 * VAULTWIKI IS NOT FREE SOFTWARE
 * http://www.vaultwiki.org/
 * http://www.vaultwiki.org/pages/License-Agreement
 *
\************************************************************************/

// #############################################################################
// Special_AutoLink
// #############################################################################
function Special_AutoLink()
{
	this.previewVisible = 0;
	this.hoverCurrentId = 0;
	this.hoverTimerId = 0;
	this.hoverAnchorObj = null;
	this.hoverAnchorPos = null;

	this.eventList = {};
	this.previewObj = {};
	this.articleData = {};

	this.init();
};

/**
* Add popup events to compatible links
*/
Special_AutoLink.prototype.init = function()
{
	var autolinks = this.fetch_autolinks();

	for (var i = 0; i < autolinks.length; i++)
	{
		YAHOO.util.Dom.addClass(autolinks[i], "autolink");
		YAHOO.util.Event.on(autolinks[i], "mouseover", this.onmouseover, this, true);
		YAHOO.util.Event.on(autolinks[i], "mouseout", this.onmouseout, this, true);
	}
};

/**
* Locate compatible links
*
* @return	array	list of valid links
*/
Special_AutoLink.prototype.fetch_autolinks = function()
{
	var links = document.getElementsByTagName("a");
	var autolinks = [];

	for (var i = 0; i < links.length; i++)
	{
		if (links[i].id && links[i].id.indexOf("autolink_") != -1)
		{
			links[i].targetid = parseInt(links[i].id.split("_")[1]);

			if (links[i].targetid > 0)
			{
				autolinks[parseInt(autolinks.length)] = links[i];
			}
		}
	}

	return autolinks;
};

/**
* Prevents popup close if we mouseover again
*
* @return	boolean	false
*/
Special_AutoLink.prototype.preview_mouseover = function()
{
	if (!this.hoverTimerId)
	{
		return false;
	}

	clearTimeout(this.hoverTimerId);
	this.hoverTimerId = 0;

	return false;
};

/**
* Hides the open popup
*
* @return	boolean	false
*/
Special_AutoLink.prototype.hide_preview = function()
{
	if (!this.articleData[this.hoverLinkId] || this.articleData[this.hoverLinkId].article_id != this.hoverLinkId)
	{
		this.hoverCurrentId = 0;

		return false;
	}

	if (!VAULT_40X_COMPAT && vBmenu.menus[this.hoverCurrentId])
	{
		vBmenu.menus[this.hoverCurrentId].hide();
	}

	this.hoverCurrentId = 0;

	return false;
};

/**
* Loads the HTML for the popup via AJAX
*
* @return	boolean	false
*/
Special_AutoLink.prototype.show_preview = function()
{
	if (this.articleData[this.hoverLinkId] != undefined)
	{
		this.cached_preview();
		return false;
	}

	YAHOO.util.Dom.setStyle("cursor", "wait");

	YAHOO.util.Connect.asyncRequest("POST", "wiki_ajax.php?do=autolink&t=" + this.hoverLinkId,
	{
		success: this.ajax_complete,
		failure: this.ajax_failure,
		timeout: 15000,
		scope: this
	}, SESSIONURL
		+ 'do=autolink&t=' + this.hoverLinkId
		+ '&securitytoken=' + PHP.urlencode(SECURITYTOKEN));

	return false;
};

/**
* Opens the existing popup div from memory
*
* @return	boolean	false
*/
Special_AutoLink.prototype.cached_preview = function()
{
	if (
		this.articleData[this.hoverLinkId].article_id != this.hoverLinkId ||
		!this.hoverCurrentId ||
		!this.articleData[this.hoverLinkId].content
	)
	{
		return false;
	}

	var previewObj = this.previewObj[this.hoverCurrentId];

	if (!previewObj)
	{
		previewObj = YAHOO.util.Dom.get(this.hoverCurrentId + "_menu");

		if (!previewObj)
		{
			previewObj = document.createElement("div");

			if (!previewObj)
			{
				return false;
			}

			if (VAULT_40X_COMPAT)
			{
				var newObj = document.createElement("span");
				holder = YAHOO.util.Dom.get(this.hoverCurrentId);

				YAHOO.util.Dom.insertAfter(newObj, holder);
				holder.parentNode.removeChild(holder);
				newObj.appendChild(holder);
			}

			previewObj.setAttribute("id", this.hoverCurrentId + "_menu");
			previewObj.innerHTML = this.articleData[this.hoverLinkId].content;

			if (VAULT_40X_COMPAT)
			{
				YAHOO.util.Dom.addClass(holder, "popupctrl");
				YAHOO.util.Dom.addClass(holder, "nogadget");

				YAHOO.util.Dom.addClass(previewObj, "postbit");
				YAHOO.util.Dom.addClass(previewObj, "popupbody");
				newObj.appendChild(previewObj);

				YAHOO.util.Dom.addClass(newObj, "popupmenu");
				YAHOO.util.Dom.addClass(newObj, "hovermenu");
				YAHOO.util.Dom.addClass(newObj, "noclick");
			}
			else
			{
				YAHOO.util.Dom.addClass(previewObj, "autolink_menu");
				document.body.appendChild(previewObj);
			}
		}

		if (!VAULT_40X_COMPAT)
		{
			YAHOO.util.Event.on(previewObj, "mouseover", this.onmouseover_popup, previewObj, true);
			YAHOO.util.Event.on(previewObj, "mouseout", this.onmouseout, this, true);
		}
	}

	if (VAULT_40X_COMPAT)
	{
		if (!YAHOO.vBulletin.vBPopupMenu.popups[this.hoverCurrentId])
		{
			YAHOO.vBulletin.vBPopupMenu.popups[this.hoverCurrentId] = new PopupMenu(
				YAHOO.util.Dom.get(this.hoverCurrentId).parentNode,
				YAHOO.vBulletin.vBPopupMenu
			);

			// vbmenu_register makes onclick return false
			YAHOO.util.Event.on(this.hoverAnchorObj, "click", this.onclick_hover, this, true);
		}
	}
	else
	{
		if (!vBmenu.menus[this.hoverCurrentId])
		{
			vBmenu.register(this.hoverCurrentId, true);

			// vbmenu_register makes onclick return false
			YAHOO.util.Event.on(this.hoverAnchorObj, "click", this.onclick_hover, this, true);
		}

		vBmenu.menus[this.hoverCurrentId].show(YAHOO.util.Dom.get(this.hoverCurrentId));
	}

	this.previewObj[this.hoverCurrentId] = previewObj;

	return false;
};

/**
* Puts the HTML into the popup when AJAX is complete
*
* @param	object	YUI AJAX
*
* @return	boolean	false
*/
Special_AutoLink.prototype.ajax_complete = function(ajax)
{
	if (ajax.responseXML)
	{
		YAHOO.util.Dom.setStyle(document.body, "cursor", "auto");

		if (fetch_tag_count(ajax.responseXML, "error"))
		{
			// do nothing
		}
		else
		{
			var articleResponse = fetch_tags(ajax.responseXML, 'article')[0];

			if (!articleResponse || !articleResponse.firstChild)
			{
				return false;
			}

			var articleId = articleResponse.getAttribute("threadid1");
			var redirectId = articleResponse.getAttribute("threadid2");

			if (!articleId || !redirectId)
			{
				return false;
			}

			this.articleData[articleId] = {
				'article_id' : articleId,
				'title' : articleResponse.getAttribute("title"),
				'content' :	articleResponse.firstChild.nodeValue
			};

			if (articleId == this.hoverLinkId)
			{
				this.cached_preview();
			}
		}
	}

	return false;
};

/**
* Cleanup on popup failure
*/
Special_AutoLink.prototype.ajax_failure = function(ajax)
{
	vBulletin_AJAX_Error_Handler(ajax);

	YAHOO.util.Dom.setStyle("cursor", "auto");
};

// =============================================================================
// Special_AutoLink Event Handlers

/**
* Create or load popup when hovering over a link
*/
Special_AutoLink.prototype.onmouseover = function(e)
{
	YAHOO.util.Event.stopEvent(e);

	var autolink = YAHOO.util.Event.getTarget(e);
	var targetid = autolink.targetid;

	if (!targetid || targetid == this.hoverCurrentId)
	{
		return false;
	}

	this.previewVisible = 1;
	this.hoverCurrentId = autolink.getAttribute("id");

	if (this.hoverTimerId)
	{
		clearTimeout(this.hoverTimerId);
	}

	this.hoverLinkId = targetid;
	this.hoverAnchorObj = autolink;

	this.show_preview();

	return false;
};

/**
* Set timer to close popup if user stops hovering
*/
Special_AutoLink.prototype.onmouseout = function(e)
{
	YAHOO.util.Event.stopEvent(e);

	this.preview_mouseover();
	this.previewVisible = 0;

	if (!this.hoverCurrentId || !this.articleData[this.hoverCurrentId])
	{
		this.hide_preview();
	}
	else if (!this.previewVisible && this.hoverCurrentId)
	{
		this.hoverTimerId = setTimeout("SpecialAutoLink.hide_preview()", 500);
	}
};

/**
* Follow the link even if popup is open
*/
Special_AutoLink.prototype.onclick_hover = function(e)
{
	YAHOO.util.Event.stopEvent(e);

	var autolink = YAHOO.util.Event.getTarget(e);
	window.location = autolink.href;
};

/**
* Prevent timer from closing popup when moving from link to popup
*/
Special_AutoLink.prototype.onmouseover_popup = function(e)
{
	YAHOO.util.Event.stopEvent(e);

	var splitId = this.getAttribute("id").split("_");

	SpecialAutoLink.hoverCurrentId = this.getAttribute("id").split("_menu")[0];

	if (SpecialAutoLink.hoverTimerId)
	{
		clearTimeout(SpecialAutoLink.hoverTimerId);
	}

	SpecialAutoLink.hoverLinkId = splitId[1];
	SpecialAutoLink.previewVisible = 1;
};

/**
* Change popups or menus during open state
*/
Special_AutoLink.prototype.vbmenu_hover = function(obj)
{
	if (vBmenu.activemenu != null && !SpecialAutoLink.hoverCurrentId)
	{
		if (vBmenu.menus[vBmenu.activemenu].controlkey != this.id)
		{
			this.show(obj, true);
		}
	}
};

if (!VAULT_40X_COMPAT)
{
	vB_Popup_Menu.prototype.hover = Special_AutoLink.prototype.vbmenu_hover;
}

/* IE 6 & 7 Fixes */
if (!Array.indexOf)
{
	Array.prototype.indexOf = function (obj, start)
	{
		for (var i = (start || 0); i < this.length; i++)
		{
			if (this[i] == obj)
			{
				return i;
			}
		}

		return -1;
	}
}

// #############################################################################
// initialize the editor class

var SpecialAutoLink = new Special_AutoLink();

/*======================================================================*\
 * Downloaded: 03:44, Thu Jul 1st 2010
 * Licensed to: http://GBLCG.com
\*======================================================================*/