/**************************************
	
	Author: Michael Turnwall
	Created: 11.27.2006
	Modified: 01.05.2007
	Description: Various functions and classes for turnwall.net
	Copyright 2006 Michael Turnwall - Some Rights Reserved
	If you are going to use any part of this library please give
	credit to me or the original author of the function that you use

**************************************/

//onload function written by Simon Willison - http://simon.incutio.com
// use this function instead of window.onload - usage addLoadEvent(functionName) or addLoadEvent(function(){ some code;} )
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
// end addLoadEvent()
/*----------------------*/

// function that allows getting elements by their class name. Returns the found elements in an array
// arguments: className - class name to search for, element - a starting point to start the search
// if no element is specified, all the nodes on the page are searched
document.getElementsByClassName = function(className,element)
{
	if(element)
	{
		var el = document.getElementById(element);
		var children = el.getElementsByTagName('*');
	}
	else
	{
		var children = document.getElementsByTagName('*') || document.all;
	}
	
	var elements = [];
	var regexp = new RegExp("\\b"+className+"\\b","g");
	for(i = 0; i < children.length; i++)
	{
		if(children[i].className.match(regexp))
			elements.push(children[i]);
	}
	if(elements.length > 0)
		return elements;
	else
		return false;
}
/*----------------------*/


function setOpacity(obj, opacity)
{
	// check first if the object even exists
	if(!obj)
		return false;
	opacity = (opacity == 100)?99.999:opacity;
	
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	
	// Safari < 1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}
/*----------------------*/

// opacity is the starting opacity
var fadeInTimer;
function fadeIn(objId,opacity,target,speed,callback)
{
	clearTimeout(fadeInTimer);
	obj = document.getElementById(objId);
	if (document.getElementById && obj)
	{
		if (opacity <= target)
		{
			setOpacity(obj, opacity);
			opacity += 10;
			fadeInTimer = window.setTimeout("fadeIn('"+objId+"',"+opacity+","+target+","+speed+","+callback+")", speed);
		}
		else
		{
			// time to remove the loading graphic from the background
			// do this because in Firefox, the loading graphic eats up a lot of cpu time when something covers it *shrug*
			
			var parent = obj.parentNode;
			if(parent && parent.nodeType == 1)
			{
				if(parent.style.backgroundImage != "url()")
					parent.style.backgroundImage = "url()";
			}
			if(typeof callback == "function")
				callback();
		}
	}
}
/*----------------------*/


// id of element to fade, final opacity, speed in milliseconds, finished - what to do when fadeout is finished (hide/delete/fadeIn)
var fadeOutTimer;
function fadeOut(objId,opacity,target,speed,finished,callback)
{
	clearTimeout(fadeOutTimer);
	obj = document.getElementById(objId);
	if (document.getElementById && obj)
	{
		if (opacity >= target)
		{
			setOpacity(obj, opacity);
			opacity -= 10;
			fadeOutTimer = window.setTimeout("fadeOut('"+objId+"',"+opacity+","+target+","+speed+",'"+finished+"',"+callback+")", speed);
		}
		else
		{
			if(finished == "hide")
				obj.style.display = "none";
			else if(finished == "delete")
			{
				var parentNode = obj.parentNode;
				parentNode.removeChild(obj);
				return true;
			}
			else if(finished == "fadeIn")
			{
				fadeIn(objId,opacity,100,speed);
				if(ajax.responseText)
				{
					obj.innerHTML = ajax.responseText;
					exeJavascript(ajax.responseText);
				}
			}

			if(typeof callback == "function")
				callback();
		}
	}
}
/*----------------------*/

// stripes alt rows, className is the element class to look for
function initStripe(className,elID,table,hover,oddRow,evenRow)
{
	if(oddRow == null)
		oddRow = "oddRow";
	if(evenRow == null)
		evenRow = "evenRow";

	if(elID)
	{
		var element = document.getElementById(elID);
		if(table == true)
		{
			var tables = element.getElementsByTagName("table");
			for(var i = 0; i < tables.length; i++)
			{
				var regexp = new RegExp(className, "i");
				if(tables[i].className.match(regexp))
				{
					stripeRows(tables[i].getElementsByTagName("tr"),oddRow,evenRow);
				}
			}
		}
		else
		{
			var elements = element.getElementsByClassName(className);
			stripeRows(elements,oddRow,evenRow);
		}
	}
	else
	{
		var elements = document.getElementsByClassName(className);
		stripeRows(elements,oddRow,evenRow);
	}
}
/*----------------------*/

function stripeRows(elements,oddRow,evenRow)
{
	for(var i = 0; i < elements.length; i++)
	{
		regexp = new RegExp("(\\s"+oddRow+"|\\s"+evenRow+"|"+oddRow+"\\s|"+evenRow+"\\s)");
		elements[i].className = elements[i].className.replace(regexp,"");
		//alert(elements[i].className);
		regexp = new RegExp("(\\s"+oddRow+"|\\s"+evenRow+"|"+elements[i].className+"(?!\\S))");
		//alert(regexp);
		if(i%2)
		{
			//elements[i].className += " " + evenRow;
			elements[i].className = elements[i].className.replace(regexp,elements[i].className + " " + evenRow);
		}
		else
		{
			//elements[i].className += " " + oddRow;
			elements[i].className = elements[i].className.replace(regexp,elements[i].className + " " + oddRow);
		}
	}
	delete regexp;
}
/*----------------------*/

function createCookie(name,value,days)
{
	if(days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
		var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
/*----------------------*/

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i < ca.length; i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length,c.length);
	}
	return false;
}
/*----------------------*/

function eraseCookie(name)
{
	createCookie(name,"",-1);
}
/*----------------------*/

// empties a text field of it's default value when a user first clicks into the text field
// when the text field loses focus, the default value is written back if no user defined value is entered
function scanInputs()
{
	var combinedFields = new Array();		// going to hold the the NodeLists for both inputs[text] and textareas
	
	// grab all inputs with a type of text and push the returned NodeList to the array. Repeat for textareas
	var inputs = document.getElementsByTagName("input");
	for(var i = 0; i < inputs.length; i++)
	{
		combinedFields.push(inputs[i]);
	}
	var textAreas = document.getElementsByTagName("textarea");
	for(var i = 0; i < textAreas.length; i++)
	{
		combinedFields.push(textAreas[i]);
	}

	inputValuesArray = new Array();		// holds the associative array [name] = value/innerHTML

	for(var i = 0; i < combinedFields.length; i++)
	{
		// check to see if it's a text field. The first part of the statement is so we don't throw an error when on textareas since they don't have type attr
		if(combinedFields[i].getAttribute("type") && combinedFields[i].getAttribute("type").toLowerCase() == "text")
		{
			inputValuesArray[inputs[i].getAttribute("name")] = inputs[i].value;
			combinedFields[i].onfocus = function()
			{
				if(this.value == inputValuesArray[this.getAttribute("name")]) 
					this.value = "";
			}
			combinedFields[i].onblur = function()
			{
				if(this.value == "")
					this.value = inputValuesArray[this.getAttribute("name")];
			}
		}
		else if(combinedFields[i].nodeName.toLowerCase() == "textarea")
		{
			inputValuesArray[combinedFields[i].getAttribute("name")] = combinedFields[i].innerHTML;
			combinedFields[i].onfocus = function()
			{
				if(this.innerHTML == inputValuesArray[this.getAttribute("name")]) 
					this.innerHTML = "";
				
			}
			combinedFields[i].onblur = function()
			{
				if(this.innerHTML == "")
					this.innerHTML = inputValuesArray[this.getAttribute("name")];
			}
		}
	}
}
/*----------------------*/

function openWin(url,wName,para)
{
	//alert(typeof(arguments[2]));
	if(typeof(arguments[2]) == "object")
		var values = _parameters(arguments[2]);
	window.open(url,wName,values);
}


function _parameters(attributes)
{
	var values = [];
	for(attribute in attributes)
	{
		values.push(attribute + "=" + attributes[attribute].toString());
	}
	return values.join(",");
}
/*----------------------*/

// takes a literal object and creates a string of attributes for elements
function _attributes(attributes)
{
	var values = [];
	for(attribute in attributes)
	{
		values.push((attribute=='className' ? 'class' : attribute) +
          '="' + attributes[attribute].toString() + '"');
	}
    return values.join(" ");
}
/*----------------------*/

function removeElement(el, pEl)
{
	if(typeof el == "object")
		el.parentNode.removeChild(el);
	else if(document.getElementById(el))
	{
		var temp = document.getElementById(el);
		temp.parentNode.removeChild(document.getElementById(el));
	}
	else
		return false;		
}
/*----------------------*/

