/************************************************************************\
 *
 * CES Vault Wiki
 * Version CES_VERSION
 *
 * CES_VAULT_COPYRIGHT
 *
\************************************************************************/

/**
* Class to handle show/hide of headline sections
*/
function WikiPost_ShowSection()
{
	this.show = null;
	this.hide = null;
	this.closer = [];
}

/**
* Finds all headlines in the post object
*
* @param	int		Unique ID for the containing post
* @param	string	RegEx search string
* @param	string	Tag name for the containing object
*
* @return	object	Object of div objects
*/
WikiPost_ShowSection.prototype.fetch_sections = function(parentobj, indexMatch, indexType)
{
	var postobj = fetch_object(parentobj);
	var divs = fetch_tags(postobj, indexType);

	var sections = [];

	for (var i = 0; i < divs.length; i++)
	{
		if (divs[i].id && divs[i].id.indexOf(indexMatch) != -1)
		{
			sections[sections.length] = divs[i];
		}
	}

	return sections;
};

/**
* Initializes all show/hide links so that they appear
*
* @return	boolean	false
*/
WikiPost_ShowSection.prototype.link_init = function(root)
{
	if (!root)
	{
		root = document;
	}

	var link_spans = YAHOO.util.Dom.getElementsByClassName("top_link", "span", root);
	var ij, j, inner_link, existing, dash_node;
	var imgmatch = new RegExp(/^collapseimg_/);

	for (var i = 0; i < link_spans.length; i++)
	{
		existing = fetch_tags(link_spans[i], "span");

		if (existing.length)
		{
			for (ij = 0; ij < existing.length; ij++)
			{
				link_spans[i].removeChild(existing[ij]);
			}
		}

		for (j = 0; j < link_spans[i].childNodes.length; j++)
		{
			inner_link = link_spans[i].childNodes[j];

			if (inner_link.id && inner_link.id.match(imgmatch))
			{
				dash_node = document.createElement("span");
				dash_node.id = 'colspan' + i + j;
				dash_node.innerHTML = " - ";

				YAHOO.util.Dom.insertAfter(dash_node, inner_link);
				YAHOO.util.Dom.setStyle(inner_link, "display", "inline");
			}
		}
	}

	if (VAULT_40X_COMPAT)
	{
		var collapseEQ = fetch_cookie('vbulletin_collapse');
	}
	else
	{
		var collapseEQ = document.cookie.split("vbulletin_collapse=");
	}

	if ((!collapseEQ || !collapseEQ[1]) && !this.closer.length)
	{
		return false;
	}

	var collapses;

	if (collapseEQ && collapseEQ[1])
	{
		if (VAULT_40X_COMPAT)
		{
			collapses = collapseEQ.split('\n');
		}
		else
		{
			var collapseFind = collapseEQ[1].split(";");
			collapses = collapseFind[0].split("%0A");
		}
	}

	if (this.closer.length)
	{
		var item;

		for (var cl = 0; cl < this.closer.length; cl++)
		{
			item = this.closer[cl][0] + "_" + this.closer[cl][1];

			if (!collapses || collapses.indexOf(item) == "-1")
			{
				this.close(item);
			}
		}
	}

	if (collapses && collapses.length)
	{
		for (var i = 0; i < collapses.length; i++)
		{
			this.close(collapses[i], true);
		}
	}

	return false;
};

/**
* Closes sections on initialization.
*
* @param	array	list of objects to be collapsed
*
* @return	boolean false
*/
WikiPost_ShowSection.prototype.close = function(item, oninit)
{
	var finder;

	if (!(VAULT_40X_COMPAT && oninit) && (finder = YAHOO.util.Dom.get("collapseobj_" + item)))
	{
		YAHOO.util.Dom.setStyle(finder, "display", "none");
	}

	if (finder = YAHOO.util.Dom.get("collapseimg_" + item))
	{
		if (!VAULT_40X_COMPAT && finder.src)
		{
			var findReg = new RegExp("(thead|tcat)\\.gif$");
			finder.src = finder.src.replace(findReg, '$1_collapsed.gif');
		}
		else if (finder.innerHTML == this.hide)
		{
			finder.innerHTML = this.show;
		}
	}

	if (!VAULT_40X_COMPAT && (finder = YAHOO.util.Dom.get("collapsecel_" + item)))
	{
		YAHOO.util.Dom.replaceClass(finder, "thead", "thead_collapsed");
		YAHOO.util.Dom.replaceClass(finder, "tcat", "tcat_collapsed");
	}

	return false;
};

/**
* Toggles the display states of options in the title popup object
*
* @param	int		Unique ID for the containing post
*
* @return	boolean	false
*/
WikiPost_ShowSection.prototype.Popup_Init = function(postid, in_article)
{
	var imgprefix = 'collapseimg_';
	var showall = fetch_object(imgprefix + postid + '_show');
	var hideall = fetch_object(imgprefix + postid + '_hide');

	if (!showall && !hideall)
	{
		return false;
	}

	var parentid = this.fetch_id(postid, in_article);
	var tocids = this.fetch_sections(parentid, 'toc_', 'table');
	var toccol = this.fetch_sections(parentid, 'collapseobj_toc_', 'tbody');
	var sectionids = this.fetch_sections(parentid, 'collapseobj_' + postid + '_', 'div');

	if (tocids.length > 0)
	{
		var colsave = '';

		for (var k = 0; k < tocids.length; k++)
		{
			// tricky way to preserve toc width
			colsave = toccol[k].style.display;
			toccol[k].style.display = '';
			tocids[k].style.width = tocids[k].offsetWidth + 'px';
			toccol[k].style.display = colsave;
		}
	}

	if (sectionids.length == 0)
	{
		return false;
	}

	for (var i = 0; i < sectionids.length; i++)
	{
		if (sectionids[i].style.display == 'none')
		{
			var hidden = true;
		}
		else
		{
			var shown = true;
		}

		if (hidden && shown)
		{
			break;
		}	
	}

	if (showall != null && !hidden)
	{
		showall.style.display = 'none';
	}

	if (hideall != null && !shown)
	{
		hideall.style.display = 'none';
	}

	return false;
};

/**
* Gets the ID of the parent object.
*
* @param	integer	Type of parent object
*
* @return	string	Unique HTML ID attribute
*/
WikiPost_ShowSection.prototype.fetch_id = function(postid, in_article)
{
	if (in_article == 2)
	{
		return parentid = 'forum_header';
	}
	else if (in_article == 1)
	{
		return parentid = 'collapseobj_ca-article';
	}
	else
	{
		return parentid = 'post_message_' + postid;
	}
};

/**
* Updates vbulletin_collapse cookie with collapse preferences
*
* @param	string	Collapseable object ID
* @param	boolean	Add a cookie
*/
WikiPost_ShowSection.prototype.save_collapsed = function(targetid, addcollapsed)
{
	if (VAULT_40X_COMPAT)
	{
		var collapsed = fetch_cookie('vbulletin_collapse');
		var tmp = new Array();

		if (collapsed != null)
		{
			collapsed = collapsed.split('\n');

			for (var i in collapsed)
			{
				if (YAHOO.lang.hasOwnProperty(collapsed, i) && collapsed[i] != targetid && collapsed[i] != '')
				{
					tmp[tmp.length] = collapsed[i];
				}
			}
		}

		if (addcollapsed)
		{
			tmp[tmp.length] = targetid;
		}

		expires = new Date();
		expires.setTime(expires.getTime() + (1000 * 86400 * 365));
		set_cookie('vbulletin_collapse', tmp.join('\n'), expires);
	}
	else
	{
		save_collapsed(targetid, addcollapsed);
	}
};
	
/**
* Toggles the collapse state of an object, and saves state to 'vbulletin_collapse' cookie
*
* @param	string	Unique ID for the collapse group
*
* @return	boolean	false
*/
WikiPost_ShowSection.prototype.toggle = function(postid, sectionid)
{
	var imgprefix = 'collapseimg_';
	var sectionids = [];
	var img = null;
	var obj = null;
	var imgid = '';

	if (sectionid == 'showall' || sectionid == 'hideall')
	{
		// this only happens if in_article would be 1
		var parentid = this.fetch_id(postid, 1);

		// get all the objids and put them in the var
		sectionids = this.fetch_sections(parentid, 'collapseobj_' + postid + '_', 'div');
	}
	else
	{
		sectionids[0] = fetch_object('collapseobj_' + postid + '_' + sectionid);
	}

	for (var i = 0; i < sectionids.length; i++)
	{
		obj = sectionids[i];

		if (!obj)
		{
			return false;
		}

		imgid = obj.id.replace('obj', 'img');

		if (sectionid == 'showall' || sectionid == 'hideall')
		{
			img = fetch_object(imgid);
		}
		else
		{
			img = fetch_object(imgprefix + postid + '_' + sectionid);
		}

		if (obj.style.display == 'none' && sectionid != 'hideall')
		{
			obj.style.display = '';

			if (sectionid == 'showall')
			{
				var idstr = obj.id.replace('collapseobj_', '');
			}
			else
			{
				var idstr = postid + '_' + sectionid;
			}

			this.save_collapsed(idstr, false);

			if (img)
			{
				img.innerHTML = this.hide;
			}
		}
		else if (sectionid != 'showall')
		{
			obj.style.display = 'none';

			if (sectionid == 'hideall')
			{
				var idstr = obj.id.replace('collapseobj_', '');
			}
			else
			{
				var idstr = postid + '_' + sectionid;
			}

			this.save_collapsed(idstr, true);

			if (img)
			{
				img.innerHTML = this.show;
			}
		}
	}

	var showall = null;
	var hideall = null;

	if (
		(showall = fetch_object(imgprefix + postid + '_show')) != null &&
		(hideall = fetch_object(imgprefix + postid + '_hide')) != null
	)
	{
		if (sectionid == 'showall')
		{
			showall.style.display = 'none';
		}
		else
		{
			showall.style.display = '';
		}

		if (sectionid == 'hideall')
		{
			hideall.style.display = 'none';
		}
		else
		{
			hideall.style.display = '';
		}
	}

	if (sectionid != 'showall' && sectionid != 'hideall')
	{
		this.Popup_Init(postid);
	}

	return false;
};

// #############################################################################
// other frequently used functions

/**
* Fix for anchor URLs
*
* @param	string	name
*
* @return	boolean false
*/
Toc_Jump = function(ancname)
{
	if (ancname.indexOf('footnote_') != -1 || ancname.indexOf('ref') != -1)
	{
		var anchor = document.getElementById(ancname);
		anchor.setAttribute("footnote", 1);
	}
	else
	{
		var anchor = document.getElementsByName(ancname)[0];
	}

	if (anchor)
	{
		if (anchor.getAttribute("footnote") == 1)
		{
			var select_anc = YAHOO.util.Dom.getElementsByClassName("ref_select", "a");
			if (select_anc && select_anc.length)
			{
				for (var q = 0; q < select_anc.length; q++)
				{
					if (YAHOO.util.Dom.hasClass(select_anc[q], "ref_select"))
					{
						YAHOO.util.Dom.removeClass(select_anc[q], "ref_select");
					}
				}
			}

			YAHOO.util.Dom.addClass(anchor, "ref_select");
		}

		if (anchor.scrollIntoView)
		{
			anchor.scrollIntoView(true);
		}
	}

	return false;
};

/**
* Fix for anchor URLs on edit preview
*
* @param	string	name
*
* @return	boolean false
*/
Toc_Create_URLs = function(parentid)
{
	var toc = YAHOO.util.Dom.get(parentid);
	var links = YAHOO.util.Dom.getElementsByClassName("toc_gen_url", "span", toc);
	var anchor = null;

	if (!links.length)
	{
		return false;
	}

	for (i = 0; i < links.length; i++)
	{
		anchor[i] = document.createElement("a");
		anchor[i].href = links[i].name;
		anchor[i].innerHTML = links[i].innerHTML;

		links[i].parentNode.appendChild(anchor[i]);
		links[i].parentNode.removeChild(links[i]);
	}
};

/**
* Rewrite of PostBit_Init
*
* @param	object	postbit
* @param	integer	post id
*/
PostBit_Init = function(obj, postid)
{
	console.log("PostBit Init: %d", postid);

	if (typeof vBmenu != 'undefined')
	{
		// init profile menu(s)
		var divs = fetch_tags(obj, 'div');
		for (var i = 0; i < divs.length; i++)
		{
			if (divs[i].id && divs[i].id.substr(0, 9) == 'postmenu_')
			{
				vBmenu.register(divs[i].id, true);
			}
		}
	}

	if (typeof SpecialAutoLink != 'undefined')
	{
		SpecialAutoLink = new Special_AutoLink();
	}

	if (typeof vB_Section != 'undefined')
	{
		// reset show/hide links
		vB_Section.link_init();
	}

	if (typeof vB_QuickEditor != 'undefined')
	{
		// init quick edit controls
		vB_AJAX_QuickEdit_Init(obj);
	}

	if (typeof vB_QuickReply != 'undefined')
	{
		// init quick reply button
		qr_init_buttons(obj);
	}

	if (typeof mq_init != 'undefined')
	{
		// init quick reply button
		mq_init(obj);
	}

	if (typeof vBrep != 'undefined')
	{
		if (typeof postid != 'undefined' && typeof postid != 'null')
		{
			vbrep_register(postid);
		}
	}

	if (typeof inlineMod != 'undefined')
	{
		im_init(obj);
	}

	if (typeof vB_Lightbox != 'undefined')
	{
		init_postbit_lightbox(obj);
	}

	child_img_alt_2_title(obj);
};

// #############################################################################
// initialize the section class

var vB_Section = new WikiPost_ShowSection();
var ajax_load_url = [];

/* License Box */