/**
 * general.js - Contains generally used JavaScripts
 * @author Marius Pedersen Taule <marius@vikingboard.com>
 * @author Various others, specified in source
 * @package Vikingboard
 * @version 0.2.0
 *
 * $Id: general.js 1664 2007-08-31 20:43:18Z paitrakt $
 */


/**
 * Dollar function, aka GetElementById
 *
 * Short for getElementById, pluss some more
 * @author Prototype JavaScript Framwork,  Sam Stephenson <http://www.prototypejs.org/>
 * @param string, object
 * @return array elements
 */
function $()
{
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++)
	{
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
Object.prototype.$	=	$;

/**
 * Double dollar function, aka objTableGetElementsByClassname
 *
 * @author Jonathan Snook <http://www.snook.ca/jonathan>
 * @author Robert Nyman <http://www.robertnyman.com>
 * @param string strTagName
 * @param string strClassName
 * @return array
 */
function $$(strTagName, strClassName)
{
	strClassName	=	strClassName.replace(/\-/g, "\\-");

	var arrElements			=	(strTagName == "*" && this.all) ? this.all : this.getElementsByTagName(strTagName);
	var arrReturnElements	=	new Array();
	var oRegExp				=	new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;

	for(var i = 0, l = arrElements.length; i < l; i++)
	{
		oElement	=	arrElements[i];
		if(oRegExp.test(oElement.className))
			arrReturnElements.push(oElement);
	}

	return (arrReturnElements)
}
Object.prototype.$$	=	$$;


/**
 * In array
 *  Hunts for a value in the specified array
 * @author Aaron Gustafson <aaron at easy-designs dot net>
 * @param string needle
 * @return bool
 */
function inArray(needle)
{
	for (var i=0; i < this.length; i++)
	{
		if (this[i] === needle)
		{
			return i;
		}
	}
	return false;
}
Array.prototype.inArray = inArray;

 /**
 * Each
 *
 * Applies a function to each element in an array
 * @param function func
 */
function each(func)
{
       for(var i = 0, l = this.length; i < l; i++)
       {
               func(this[i]);
       }
}
Array.prototype.each   =       each;
Object.prototype.each  =       each;

/**
 * Is array
 *
 * Verifies if something is an array
 * @author Aaron Gustafson <aaron at easy-designs dot net>
 * @return bool
 */
function isArray()
{
	return (typeof(this.length)=="undefined") ? false : true;
}
Array.prototype.isArray = isArray;


/**
 * Add class
 *
 * Appends the specified class to the object
 * @author Aaron Gustafson <aaron at easy-designs dot net>
 * @return void;
 */
function addClass(theClass)
{
	if (this.className != '')
	{
		if (!this.hasClass(theClass))
		{
			this.className += ' ' + theClass;
		}
	}
	else
	{
		this.className = theClass;
	}
}
Object.prototype.addClass = addClass;


/**
 * Remove class
 *
 * Removes the specified class to the object
 * @author Aaron Gustafson <aaron at easy-designs dot net>
 * @return void
 */
function removeClass(theClass) {
	var oldClass = this.className;
	var regExp = new RegExp('\\s?'+theClass+'\\b');

	if (oldClass.indexOf(theClass) != -1)
	{
		this.className = oldClass.replace(regExp,'');
	}
}
Object.prototype.removeClass	=	removeClass;

/**
 * Has class
 *
 * Checks if an object has a specific class
 * @param string class
 * @return bool
 */
function hasClass(strClass)
{
	if(strClass)
	{
		return (this.className.indexOf(strClass) != -1) ? true : false;
	}
	else
	{
		return (this.getAttributeNode("class") != null) ? true : false;
	}
}
Object.prototype.hasClass	=	hasClass;
 
/**
 * Toggle
 *
 * Toggles the visibility of an element
 */
function toggle()
{
	if(this.hasClass('hide'))
	{
		this.removeClass('hide');
	}
	else
	{
		this.addClass('hide');
	}
}
Object.prototype.toggle	=	toggle;

/**
 * Read cookie
 * 
 * Reads a cookie and returns the content
 * @param string cookiename
 * @return string
 */
function readCookie(strCookie)
{
	// Cookies, yummy :D
	var cookies	=	document.cookie.split(';'), c, content = '';

	for(var i = 0, l = cookies.length; i < l; i++)
	{
		c	=	cookies[i];

		// Remove spaces
		while (c.charAt(0) == ' ')
		{
			c	=	c.substring(1, c.length);
		}

		// Is it the right cookie?
		if(c.indexOf(strCookie) == 0)
		{
			// Yes it was, put the content in a variable
			content	=	c.substring(strCookie.length + 1, c.length);
			break;
		}
	}
	return content;
}

/**
 * Write cookie
 *
 * @param string strCookie
 * @param string strContent
 */
function writeCookie(strCookie, strContent, intSeconds)
{
	var expire	=	'';
	if(intSeconds)
	{
		var date	=	new Date();
		date.setTime(date.getTime()+intSeconds);
		expire	=	'; expires=' + date.toGMTString();
	}

	document.cookie	=	strCookie + '=' + strContent + expire;
}


/**
 * Delete cookie
 *
 * @param string strCookie
 */
function deleteCookie(strCookie)
{
	document.cookie	=	strCookie + '=; expires=-3600';
}


/**
 * Add load event
 *
 * @author Simon Willison <http://simon.incutio.com/archive/2004/05/26/addLoadEvent>
 * @param function func
 */
function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload	=	func;
	}
	else
	{
		window.onload	=	function()
		{
			if (oldonload)
			{
				oldonload();
			}
			func();
		}
	}
}


/**
 * Table stripes
 *
 * Adds nice stripes to tables
 * @param string strTable
 */
function tableStripes(strTable, evenClass, oddClass, even)
{
	var objTable, arrTableTr, arrRowTd;
	if (!evenClass)
		evenClass	=	'even';
	if (!oddClass)
		oddClass	=	'odd';

	if (strTable == '')
	{
		objTable	=	$(strTable);
		arrTableTr	=	objTable.getElementsByTagName('tr');
		arrTableTd	=	objTable.getElementsByTagName('td');
	}
	else
	{
		arrTableTr	=	document.getElementsByTagName('tr');
		arrTableTd	=	document.getElementsByTagName('td');
	}

	arrTableTr.each(function()
	{
		arrRowTd	=	arguments[0].getElementsByTagName('td');
		arrRowTd.each(function()
		{
			if (even)
			{
				arguments[0].addClass(oddClass);
			}
			else
			{
				arguments[0].addClass(evenClass);
			}
		});
		even	=	!even;
	});
}


/**
 * Toggle multiquote
 *
 * Adds or removes multiquote cookie and CSS class
 * @param int intPost
 */
function toggleMultiquote(intPost)
{
	var strCookie		=	'Vikingboard_multiquote';
	var strQuoted		=	readCookie(strCookie);
	var objPost			=	$('post-' + intPost);
	var objQuoteLink	=	objPost.$$('a', 'multiquote')[0];

	if (strQuoted.match('_' + intPost))
	{
		strQuoted	=	strQuoted.replace('_' + intPost, '');
		objQuoteLink.removeClass('format-quote-multi-remove');
		objQuoteLink.removeClass('quoted');
		objQuoteLink.addClass('format-quote-multi');
		objQuoteLink.getElementsByTagName('span')[0].innerHTML	=	'+';
	}
	else
	{
		strQuoted	+=	'_' + intPost;
		objQuoteLink.removeClass('format-quote-multi');
		objQuoteLink.addClass('quoted');
		objQuoteLink.addClass('format-quote-multi-remove');
		objQuoteLink.getElementsByTagName('span')[0].innerHTML	=	'-';
	}

	writeCookie(strCookie, strQuoted);
}


/**
 * Multiquote handler
 */
function multiquoteHandler(ev)
{
	strParentId = ev.target.parentNode.parentNode.parentNode.id.replace('post-', '');
	toggleMultiquote(strParentId)
	return false;
}


/**
 * Multiquote
 *
 * Adds multiquote onclick to a-elements with the 'multiquote'-class
 */
function multiquote()
{
	var posts		=	$('thread');
	if(posts)
	{
		var objQuotes	=	posts.$$('a', 'multiquote');
		var q, strParentId;
		for(i = 0, l = objQuotes.length; i < l; i++)
		{
			objQuotes[i].onclick	=	multiquoteHandler;
		}
	}
}

/**
 * Clear a text field
 * Only clears the field if the text in it is the same as the default
 * @param string clear
 * @return void
 */
function clear_default_input(objInput)
{
	if (objInput.value == objInput.defaultValue)
	{
		objInput.value	=	'';
	}
}


function quick_search()
{
	var objQuick	=	$('global-search');
	if(objQuick)
	{
		var objInput			=	objQuick.$('query');
		objInput.defaultValue	=	'Search ...';
		objInput.onclick		=	function () { clear_default_input(objInput); };
	}
}

function onClicks()
{
	multiquote();
	quick_search();
}


addLoadEvent(onClicks);
addLoadEvent(tableStripes);
