function addLoadEvent(func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	} 
	else 
	{
		window.onload = function() 
		{
			oldonload();
			func();
		}
	}
}

var shiftAmount = 0;
var newLeft = 0;
var maxLeft = 0;
var maxRight = 0;
var clickFunction = getBigImage;
var inTransition = false;
function tb_init() 
{
	var carousels = new Array();
	var divs = new Array();
	
	divs = document.getElementsByTagName('div');
	
	//iterate through each div on the page to see if the user wants to transform it to a carousel
	for (var i=0;i<divs.length;i++)
	{
		if (divs[i].className.indexOf("carousel") >= 0) 
		{
			carousels.push(divs[i]);
		}
	}
	
	//iterate through each carousel and transform it
	for (var i=0;i<carousels.length;i++)
	{
		
		//get the current position of the carousel
		carouselPos = findPos(carousels[i]);
		
		//remove the first child (should be an empty text node)
		if (carousels[i].firstChild.nodeType == 3) carousels[i].removeChild(carousels[i].firstChild);
		
		//get the width and height of the children
		var childWidth = carousels[i].firstChild.offsetWidth;
		var childHeight = carousels[i].firstChild.offsetHeight;
		
		//get the position of the first child and second child to work out margins
		var firstChildPos = findPos(carousels[i].firstChild);
		var secondChild = get_nextsibling(carousels[i].firstChild);
		var secondChildPos = findPos(secondChild);
		
		//work out margins of the children
		hChildMargin = (secondChildPos[0] - (firstChildPos[0] + childWidth));
		
		//get the padding of the main element
		var vPadding = (firstChildPos[1] - carouselPos[1]);
		var hPadding = (firstChildPos[0] - carouselPos[0]);
		
		//find out how many children are on one row
		var childrenPerRow = Math.floor((carousels[i].offsetWidth - hPadding) / (childWidth + hChildMargin));
		shiftAmount = (childrenPerRow * (childWidth + hChildMargin));
		
		//remove the children from the element
		var children = new Array();
		var numOfChildren = 0;
		while(carousels[i].firstChild) 
		{
			if (carousels[i].firstChild.nodeType != 3)
			{
				if (carousels[i].firstChild.className == 'thumb_container')
				{
					numOfChildren++;
					//check if the the child contains an a tag (which it should)
					if (carousels[i].firstChild.firstChild.tagName == 'A') carousels[i].firstChild.firstChild.onclick = clickFunction;
				}
				children.push(carousels[i].firstChild);
			}
			carousels[i].removeChild(carousels[i].firstChild);
		}
		
		//set the maximum slider limits
		maxLeft = shiftAmount - ((childWidth + hChildMargin) * numOfChildren);
		maxRight = 0;
		
		//set up the clipping mask
		clipper = document.createElement('div');
		clipper.style.zIndex = 2;
		clipper.style.overflow = 'hidden';
		clipper.style.position = 'relative';
		clipper.style.width = (carousels[i].offsetWidth - hPadding);
		
		//set up the container
		container = document.createElement('div');
		container.style.zIndex = 2;
		container.style.position = 'relative';
		container.style.width = ((childWidth + hChildMargin) * numOfChildren) + 'px';
		container.id = 'carousel_' + i;
		container.style.left = '0px';
		
		//add the children
		for (var n=0;n<children.length;n++)
		{
			container.appendChild(children[n]);
		}
		
		//set up the buttons
		left_button = document.createElement('div');
		left_button.style.zIndex = 3;
		left_button.style.position = 'absolute';
		left_button.style.width = '23px';
		left_button.style.height = '23px';
		left_button.style.cursor = 'pointer';
		left_button_img = document.createElement('img');
		left_button_img.id = 'left_button';
		left_button_img.src = 'images/carousel_button_left_on.gif';
		left_button_img.onclick = function()
		{
			if (!inTransition)
			{
				inTransition = true;
				element = this.parentNode.parentNode.firstChild.firstChild;
				newLeft = parseFloat(element.style.left) - shiftAmount;
				carouselLeft(element, parseFloat(element.style.left));
			}
		};
		left_button.appendChild(left_button_img);
		
		right_button = document.createElement('div');
		right_button.style.zIndex = 3;
		right_button.style.position = 'absolute';
		right_button.style.width = '23px';
		right_button.style.height = '23px';
		right_button.style.cursor = 'pointer';
		right_button_img = document.createElement('img');
		right_button_img.id = 'right_button';
		right_button_img.src = 'images/carousel_button_right_of.gif';
		right_button_img.onclick = function()
		{
			if (!inTransition)
			{
				inTransition = true;
				element = this.parentNode.parentNode.firstChild.firstChild;
				newLeft = parseFloat(element.style.left) + shiftAmount;
				carouselRight(element, parseFloat(element.style.left));
			}
		};
		right_button.appendChild(right_button_img);
		
		clipper.appendChild(container);
		carousels[i].appendChild(clipper);
		
		//move the buttons to the correct location
		currentElementPos = findPos(carousels[i])
		right_button.style.top = (currentElementPos[1] + (carousels[i].offsetHeight  / 2) - 12) + 'px';
		right_button.style.left = ((currentElementPos[0] + carousels[i].offsetWidth) - 12) + 'px';
		left_button.style.top = (currentElementPos[1] + (carousels[i].offsetHeight  / 2) - 12) + 'px';
		left_button.style.left = (currentElementPos[0] - 12) + 'px';
		
		if(numOfChildren > 4)
		{
			//attach the buttons
			carousels[i].appendChild(left_button);
			carousels[i].appendChild(right_button);
		}
		
	}
}

function carouselLeft(element, oldPos) 
{
	if((parseFloat(element.style.left) > newLeft) && (parseFloat(element.style.left) > maxLeft)) 
	{
		var newPos = curve(0.65, parseFloat(element.style.left), newLeft - parseFloat(element.style.left), 2);
		if (((newPos - oldPos) * (newPos - oldPos)) < 0.01) 
		{
			element.style.left =  newLeft + 'px';
		}
		else
		{
			if (newPos < maxLeft)
			{
				element.style.left =  maxLeft + 'px';
			}
			else
			{
				element.style.left =  newPos + 'px';
			}
		}
		oldPos = newPos;
		setTimeout('carouselLeft(document.getElementById("'+element.id+'"), '+oldPos+')', 15);
	}
	else
	{
		//disable the button if at max
		rightButton = document.getElementById('right_button');
		leftButton = document.getElementById('left_button');
		leftStrLength = leftButton.src.length;
		rightStrLength = rightButton.src.length; 
		if (parseFloat(element.style.left) <= maxLeft)
		{
			rightButton.src = rightButton.src.substring(0,rightStrLength-7) + '_on.gif';
			leftButton.src = leftButton.src.substring(0,leftStrLength-7) + '_of.gif';
		}
		else
		{
			leftButton.src = leftButton.src.substring(0,leftStrLength-7) + '_on.gif';
			rightButton.src = rightButton.src.substring(0,rightStrLength-7) + '_on.gif';
		}
		inTransition = false;
	}
}

function carouselRight(element, oldPos) 
{
	if((parseFloat(element.style.left) < newLeft) && (parseFloat(element.style.left) < maxRight)) 
	{	
		var newPos = curve(0.65, parseFloat(element.style.left), newLeft - parseFloat(element.style.left), 2);
		if (((newPos - oldPos) * (newPos - oldPos)) < 0.01) 
		{
			element.style.left = newLeft + 'px';
		}
		else
		{
			if (newPos > maxRight)
			{
				element.style.left =  maxRight + 'px';
			}
			else
			{
				element.style.left =  newPos + 'px';
			}
		}
		oldPos = newPos;
		setTimeout('carouselRight(document.getElementById("'+element.id+'"), '+oldPos+')', 15);
	} 
	else
	{
		//disable the button if at max
		rightButton = document.getElementById('right_button');
		leftButton = document.getElementById('left_button');
		leftStrLength = leftButton.src.length;
		rightStrLength = rightButton.src.length;
		if (parseFloat(element.style.left) >= maxRight)
		{
			rightButton.src = rightButton.src.substring(0,rightStrLength-7) + '_of.gif';
			leftButton.src = leftButton.src.substring(0,leftStrLength-7) + '_on.gif';
		}
		else
		{
			rightButton.src = rightButton.src.substring(0,rightStrLength-7) + '_on.gif';
			leftButton.src = leftButton.src.substring(0,leftStrLength-7) + '_on.gif';
		}
		inTransition = false;
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function get_nextsibling(n)
{
	var x=n.nextSibling;
	while (x.nodeType!=1)
	{
		x=x.nextSibling;
	}
	return x;
}

function getBigImage() 
{
	//alert('fading In: ' + fadingIn);
	//alert('fading Out: ' + fadingOut);
	//imageSrc = 'http://farap.files.wordpress.com/2006/08/cat-in-bombay-003.jpg';
	imageSrc = this.firstChild.src.substring(0,this.firstChild.src.length-6) + '.jpg';
	if (document.getElementById('big_image').src == imageSrc) return false;
	//blank out current image
	document.getElementById('big_image').style.opacity = 0;
	//create a new image object and set up events
	image = new Image();
	image.onload = bigImageSuccess;
	image.onerror = bigImageFailure;
	image.onabort = bigImageFailure;
	image.src = imageSrc;
	return false;
}

function bigImageSuccess()
{
	document.getElementById('big_image').src = image.src;
	dec_transparency(document.getElementById('big_image'), 0.1);
}

function bigImageFailure()
{
	dec_transparency(document.getElementById('big_image'), 0.1);
	alert('uh oh!');
}

function inc_transparency(element, amount) {
	element.style.opacity = parseFloat(element.style.opacity) - amount;
	if(element.style.opacity > 0)
	{
		setTimeout('inc_transparency(document.getElementById("'+element.id+'"), '+amount+')', 60);
	}
}

function dec_transparency(element, amount) {
	element.style.opacity = parseFloat(element.style.opacity) + amount;
	if(element.style.opacity < 1)
	{
		setTimeout('dec_transparency(document.getElementById("'+element.id+'"), '+amount+')', 60);
	}
}

function curve(t, b, c, d)
{
	//quartic ease in ease out curve - taken from from Robert Pinner's AS tweens
	if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
	return -c/2 * ((t-=2)*t*t*t - 2) + b;
}

addLoadEvent(tb_init);