/*jslint eqeqeq: true, browser: true */
/*global window, $, jQuery */
/*JP 17/07/09*/
/*  JS Linted - All problems Solved before Minifying*/
/*JP END*/
/*	
	Title : Expanding Elements 
	Author : Tom Coote
	Date : 24 March 2007
	Wedsite : http://www.tomcoote.co.uk

	Description : This can be used to allow elements such as div's to expand and contract on the page as a nice
	way of showing and hiding them.

	Instructions : First off include this file on your page. In order to make an element expandable it must be
	given a class name of 'Expanding' and it must have a unique id. You then need to specify if the element starts off
	hidden or visible, to do this you must use the style attribute 'visibility' and set it to 'hidden' or 'visible'.
	Then it is just a matter of calling the ShowHideMe function passing in the unique id of the element you wish to animate.
	Lastly but not to be missed is to make sure you have the class 'Expanding' in your style sheet with overflow set to hidden.
*/

var Height = 0;
var MaxHeight = 0;
var HeightChange = 0;
var inter;
var arrExpandingElements = [];
var Animating;

// Need to initiate all elements that have been set to expanding elements.
function GetExpanding() {
	var eExpanders = [];
	var elements = document.getElementsByTagName("*");
	var classes;

	for (var i = 0; i < elements.length; i++) {
		if (elements[i].className.indexOf(" ") >= 0) {
			classes = elements[i].className.split(" ");
			for (j = 0; j < classes.length; j++) {
				if (classes[j] === 'Expanding') {
					eExpanders.push(elements[i]);
				}
			}
		}
		else if (elements[i].className === 'Expanding') {
			eExpanders.push(elements[i]);
		}
	}

	for (i = 0; i < eExpanders.length; i++) {
		arrExpandingElements[i] = eExpanders[i].id + '=' + eExpanders[i].offsetHeight;

		if (eExpanders[i].style.visibility === 'hidden') {
			eExpanders[i].style.height = '0.1em';
		}
	}
}

if (window.addEventListener) {
	window.addEventListener('load', GetExpanding, false);
}
else if (window.attachEvent) {
	window.attachEvent('onload', GetExpanding);
}

function ShowBox(obj) {
	if (Height >= MaxHeight) {
		clearInterval(inter);
		Animating = false;
		return;
	}

	obj.style.visibility = 'visible';
	Height += HeightChange;
	obj.style.height = Height + 'px';
}
function HideBox(obj) {
	if (Height <= 2 || Height < HeightChange) {
		obj.style.visibility = 'hidden';
		obj.style.height = '0.1em';
		window.clearInterval(inter);
		Animating = false;
		return;
	}

	Height -= HeightChange;
	obj.style.height = Height + 'px';
}

function ShowHideMe(eName) {
	// Can't animate more than one object at a time
	if (Animating) {
		return;
	}

	// Set variables needed.
	eObj = document.getElementById(eName);
	Animating = true;
	Height = eObj.offsetHeight;
	MaxHeight = 0;
	HeightChange = 0;
	var sData = '';

	for (i = 0; i < arrExpandingElements.length; i++) {
		sData = arrExpandingElements[i].split('=');

		if (sData[0] === eName) {
			MaxHeight = sData[1];
		}
	}

	// Nice constriants to stop flickering
	HeightChange = MaxHeight / 15;
	if (HeightChange < 2) {
		HeightChange = 2;
	}
	if (HeightChange > 17) {
		HeightChange = 17;
	}

	// Start the required animation
	if (eObj.style.visibility === 'hidden') {
		inter = setInterval(function () {
			ShowBox(eObj);
		},
		1);
		return;
	}
	else if (eObj.style.visibility === 'visible') {
		inter = setInterval(function () {
			HideBox(eObj);
		},
		1);
		return;
	}
}