
var boxShown = null;

function showbox(boxid)
{
	if (!boxid) return;
	if (boxShown == boxid) {
		hidebox(boxShown);
		return false;
	}
	else {
		hidebox(boxShown);
	}
	
	boxShown = boxid;
	var box = document.getElementById(boxid);
	box.style.display = 'block';
	return false;
}

function hidebox(boxid)
{
	if (!boxid) return;
	var box = document.getElementById(boxid);
	box.style.display = 'none';
	boxShown = null;
	return false;
}

function toggleExpand(lnk, boxid, small)
{
	if (!boxid) return;
	var box = document.getElementById(boxid);
	box.style.height = '';
	lnk.style.display = 'none';
	return false;
}

// If full, go to sized, otherwise go to full
var swapInProgress = false;   // Global var to prevent concurrant execution
function swapImageSize(el)
{
	if (swapInProgress) return;  // Do nothing
	// Try block ensures we do not lock out this function forever.
	
	try
	{
		swapInProgress = true;
		var notice = document.getElementById('sizenotice');   // <-- poor form
		
		if (mci.fullIsCurrent)
		{
			mci.fullIsCurrent = false;
			el.src = mci.smallUrl;
			if (notice) {
				notice.style.visibility = 'visible';
				notice.innerHTML = "Loading...";
			}
			
		}
		else
		{
			mci.fullIsCurrent = true;
			el.src = mci.fullUrl;
			if (notice) {
				notice.style.visibility = 'visible';
				notice.innerHTML = "Loading...";
			}
		}
	}
	finally
	{
		swapInProgress = false;	
	}
}

function imageLoadComplete(el)
{
	var notice = document.getElementById('sizenotice');   // <-- poor form
	if (mci.fullIsCurrent) notice.style.visibility = 'hidden';
	else notice.innerHTML = "Image resized. Click image for full size."
}

//
// Picture Selection Class
//

function MultiChoiceImage(fUrl, fx, fy, sUrl)
{
	this.fullUrl = fUrl;
	this.fullX = fx;
	this.fullY = fy;
	this.smallUrl = (sUrl) ? sUrl : fUrl;
	
	this.fullIsCurrent = null;
	
	var cachedBestUrl = null;
	
	this.getBestFitUrl = function()
	{
		if (cachedBestUrl == null)
		{
			var w = this.getWindowWidth();
			var usableWidth = w - 75;   // This is an adjustable constant.
			
			if (this.fullX > usableWidth) {
				cachedBestUrl = this.smallUrl;
				this.fullIsCurrent = false;
			}
			else {
				cachedBestUrl = this.fullUrl;
				this.fullIsCurrent = true;
			}
		}		
		return cachedBestUrl;
	}
	
	this.getWindowWidth = function()
	{
		return filterResults (
			window.innerWidth ? window.innerWidth : 0,
			document.documentElement ? document.documentElement.clientWidth : 0,
			document.body ? document.body.clientWidth : 0);
	}
	
	this.getWindowHeight = function()
	{
		return filterResults (
			window.innerHeight ? window.innerHeight : 0,
			document.documentElement ? document.documentElement.clientHeight : 0,
			document.body ? document.body.clientHeight : 0);
	}

	function filterResults(n_win, n_docel, n_body)
	{
		var n_result = n_win ? n_win : 0;
		if (n_docel && (!n_result || (n_result > n_docel)))
			n_result = n_docel;
		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	}
}

//
// Generic magic box class
//

function ReplyBlock(ele)
{
	// State, true if open.
	this.reply_open = false;
	
	// Store the element itself
	this.blockElement = ele;
	
	// Pointer to the element containing our block. (starts as none)
	this.container = ele.parentNode;	
	
	// Accessors
	this.getBlock = function()
	{
		return this.blockElement;
	}
	
	// Attach to a new container.  This also requires detaching from the old one.
	this.attachTo = function(c)
	{
		var oldCont = this.container;
		if (oldCont) {
			oldCont.removeChild(this.blockElement);
		}
		c.appendChild(this.blockElement);
		this.container = c;
	}
	
	this.foldout = function()
	{
		this.blockElement.style.visibility = 'hidden';
		this.blockElement.style.height = '10px';
		this.show();  // Ensure it will be visible while folding out.
		
		var closureThis = this;  /* What is closure? */
		this.animating = 1;
		this.timer = setInterval(function() {closureThis.animateOut() }, 2);
	
		
		//setTimeout(this.foldout( (timeleft - interval), distance, interval), 100);
	}
	
	this.endFoldout = function()
	{
		clearInterval (this.timer);
		this.animating = 0;
		this.blockElement.style.visibility = 'visible';
	}
	
	this.animateOut = function()
	{
		if (this.blockElement.offsetHeight >= 130) {
			return this.endFoldout();
		}
		this.blockElement.style.height = (this.blockElement.offsetHeight + 40) + 'px';		
	}
	
	
	// Animation properties
	this.timer = null;
	this.animating = 0;
	this.targetSize = 0;
		
	this.show = function()
	{
		this.blockElement.style.display = 'block';
	}
	
	this.hide = function()
	{
		this.blockElement.style.display = 'none';
	}
	
	// Specialized params
	
	this.setFormParam = function(paramName, newValue)
	{
		var inputs = this.blockElement.getElementsByTagName('input');
		
		for (i = 0; i < inputs.length; i++)
		{
			if (inputs[i].getAttribute('name') == paramName)
			{
				inputs[i].setAttribute('value', newValue);
				return;
			}
		}
		// Didn't find it, raise error
		alert('Oops, couldnt find ' + paramName);
	}
}


//
// Stuff specific to comment replies
//

// Global variable to contain the Reply Block instance
var replyToBlock;

// Sets it up
function initDynamicReply(blockId)
{
	var rBlock = document.getElementById(blockId);
	replyToBlock = new ReplyBlock(rBlock);	
	replyToBlock.getBlock().style.clear = 'both';
}

// Performs simple work on the object.
function setReplyTo(cid, iid)
{
	var comment = document.getElementById('c' + cid);
	
	if (replyToBlock.container == comment) return;
	
	replyToBlock.getBlock().style.visibility = 'hidden';
	replyToBlock.attachTo(comment);
	replyToBlock.setFormParam('itemid', iid);
	replyToBlock.setFormParam('parentid', cid);	
	replyToBlock.foldout();
	
	// Keep outside of object for now, set textarea focus.
	// Disabled because it breaks stuff in firefox (IE untested)
	//replyToBlock.getBlock().getElementsByTagName('textarea').item(0).focus();
}

//
// Check if a string is likely to be a well-formed tag, or user misunderstood.
//

function checkTagString(str)
{
	var wellFormed = true;
	var msg = '';

	var lines = new Array();
	lines = str.split('\n');
	if (lines.length < 2 && str.indexOf(' ') != -1) {
		wellFormed = false;
		msg += 'You have only one tag "' + str + '" which has a space in it.  You probably meant for each word here to be its own tag.\n';
	}

	if (str.indexOf(',') != -1) {
		wellFormed = false;
		msg += 'Your tags have a comma in them. You probably were trying to separate your tags with commas (badger,plants,vore).\n';
	}

	msg += 'Each tag must go on its own line: \nbadger\nsoft vore\nplants\nLike so.  Please click OK to go back and fix your tags, or CANCEL if you are sure you entered the tags correctly.';

	if (!wellFormed)
	{
		var res = confirm(msg);
		if (res)
			return false;
		else
			return true;
	}
	else {
		return true;
	}
}

