/************************************************************************\
	KEV SCRIPT SHOW HIDE 2008
\************************************************************************/

//extra functions for spoilers etc

//modify postbit function (might cause bugs)
if(typeof PosBit_Init == 'function')
{
	var PostBit_Init_Old = PostBit_Init;
	PostBit_Init = function(obj, postid)
	{
		PostBit_Init_Old(obj, postid);
		if(typeof initShowHide == 'function')
			initShowHide();
	}
}

/**
 * Function to see if a particular class is in a classname string, when multiple class names have been given to an element
 */
function classTest(needle, haystack)
{
	var haystackArray = haystack.split(" ");
	for(var i = 0; i < haystackArray.length; i++)
	{
		if(needle == haystackArray[i])
			return true;
	}
	return false;
}
/**
 * Function to initialise hidden content bbcodes, special case for nsfw
 */
function initShowHide()
{
	if(!document.getElementsByTagName)return;
	var allDivs = document.getElementsByTagName('div');
	for(var i = 0; i < allDivs.length; i++)
	{
		if(allDivs[i].className == "noscript")
			allDivs[i].className = "hidden";

		if(!classTest("showhide", allDivs[i].className))
			continue;

		var showHideAs = allDivs[i].getElementsByTagName('a');

		for(var j = 0; j < showHideAs.length; j++)
		{
			if(typeof showHideAs[j].onclick != "function")
			{
				if(classTest("nsfw", allDivs[i].className))
					showHideAs[j].onclick = setNsfw;
				else
					showHideAs[j].onclick = swapShowHide;
			}
		}
	}	

}

/**
 * Function to initialise a nsfw tag to display the image when clicked.
 * different to other show hides because the element isn't created til pressed
 */
function setNsfw()
{
	if(!document.createElement) return true;

	var href = this.getAttribute('href');

	if(href == null || href == '') return true;

	var el = this;
	while(el = el.parentNode)
	{
		if(classTest("showhide", el.className))
			break;
	}

	var newImg = document.createElement('img');
	newImg.setAttribute('src', href);
	newImg.setAttribute('alt', '');
	var newDiv = document.createElement('div');
	newDiv.className = 'alt2';
	newDiv.appendChild(newImg);
	el.appendChild(newDiv);

	var showHideAs = el.getElementsByTagName('a');

	for(var i = 0; i < showHideAs.length; i++)
	{
		if(showHideAs[i].className == "link")
		{
			var linkText = showHideAs[i].firstChild.nodeValue;
			if(linkText.substring(0,4) == "Show")
			{
				var linkText = "Hide" + linkText.substring(4, linkText.length);
				showHideAs[i].firstChild.nodeValue = linkText;
			}
		}

		if(showHideAs[i].className == "collapsed")
		{
			showHideAs[i].className = "collapse";
			showHideAs[i].getElementsByTagName('span')[0].firstChild.nodeValue = "Hide";
		}


		showHideAs[i].onclick = swapShowHide;

	}

	return false;
	
}
/**
 * Function to swap around showing and hiding things
 */
function swapShowHide()
{
	//need to get show hide div
	var el = this;
	while(el = el.parentNode)
	{
		if(classTest("showhide", el.className))
			break;
	}
	
	var divs = el.getElementsByTagName('div');
	//create as variable
	var as;
	for(var i = 0; i < divs.length; i++)
	{
		if(divs[i].className == "hidden")
		{
			divs[i].className = "alt2";
			as = el.getElementsByTagName('a');
			for(var j = 0; j < as.length; j++)
			{
				if(as[j].className == "link")
				{
					var linkText = as[j].firstChild.nodeValue;
					if(linkText.substring(0,4) == "Show")
					{
						var linkText = "Hide" + linkText.substring(4, linkText.length);
						as[j].firstChild.nodeValue = linkText;
					}
				}

				if(as[j].className == "collapsed")
				{
					as[j].className = "collapse";
					as[j].getElementsByTagName('span')[0].firstChild.nodeValue = "Hide";
				}

			}

		} 
		else if(divs[i].className == "alt2")
		{
			divs[i].className = "hidden";
			as = el.getElementsByTagName('a');
			for(var j = 0; j < as.length; j++)
			{
				if(as[j].className == "link")
				{
					var linkText = as[j].firstChild.nodeValue;
					if(linkText.substring(0,4) == "Hide")
					{
						var linkText = "Show" + linkText.substring(4, linkText.length);
						as[j].firstChild.nodeValue = linkText;
					}
				}

				if(as[j].className == "collapse")
				{
					as[j].className = "collapsed";
					as[j].getElementsByTagName('span')[0].firstChild.nodeValue = "Show";
				}
			}

		}

	}
	return false;
}
