﻿/// <reference path="jquery-1.4.4.js"/>
/// <reference path="ProxiteamGenericScripts.js"/>

Proxiteam.BbCode = {};

Proxiteam.BbCode.InitialiseSpoilerTags = function (context, show)
{
	/// <summary>Sets up the spoiler tag functionality</summary>
	/// <param name="context" type="jQuery">The element that contains the spoiler tags to set up</param>
	/// <param name="show" type="Boolean">Whether or not to show the contents of the spoiler by default</param>

	var spoilerDivs = $("div.Spoiler", context);

	//Groups the spoiler by the number of spoilers they are nested within
	var tagsByParents = new Array();
	$.each(spoilerDivs, function (i, div)
	{
		var numParents = $(div).parents("div.Spoiler").length;
		if (tagsByParents[numParents] === undefined)
			tagsByParents[numParents] = new Array();
		tagsByParents[numParents].push(div);
	});

	//Iterate through the spoilers in a depth-first fashion (the most nested ones first)
	for (var i = tagsByParents.length - 1; i >= 0; i--)
	{
		$.each(tagsByParents[i], function (i, div)
		{
			var div = $(div);
			var title = $(div).children("div.SpoilerTitle");
			var content = $(div).children("div.SpoilerContent");

			var showHideFunc = function (show, transitionTime)
			{
				if (show)
				{
					title.addClass("SpoilerShown");
					title.removeClass("SpoilerHidden");
					content.show(transitionTime);
					div.data("Spoiler-Hidden", false);
				}
				else
				{
					title.addClass("SpoilerHidden");
					title.removeClass("SpoilerShown");
					content.hide(transitionTime);
					div.data("Spoiler-Hidden", true);
				}
			}

			title.click(function ()
			{
				showHideFunc(div.data("Spoiler-Hidden"), 250);
			});

			showHideFunc(show, 0);
		});
	}
};


Proxiteam.BbCode.InitialiseBbCodes = function (context, preview)
{
	/// <summary>Initialises all the JavaScript powered BbCodes</summary>
	/// <param name="context" type="jQuery">The element that contains the BbCode tags to set up</param>
	/// <param name="preview" type="Boolean">Whether this is initialising a BbCode preview window or not</param>

	Proxiteam.BbCode.InitialiseSpoilerTags(context, preview);
};

$(function () { Proxiteam.BbCode.InitialiseBbCodes($("body"), false) });
