/*
* File: 		ajax.js
* Author: 		Matt Skelton
* Company: 		Broda Seating LTD.
* Date:		August 01, 2008		: Created
*			December 04, 2008 	: Updated
* Description:	This file contains mutliple functions/methods that assist in Ajax implementations of the seatingisbelieving.com website.
* License:		You are free to use any code within this file as you see fit, as long as you include the header of this file. Cheers!
*/

function switchSource(imageName, newFileName)
{
	document[imageName].src = "system/application/images/padding/chairs/Resized/" + [newFileName];
}

function ot_star_slider_down()
{
	var container = document.getElementById("ot_star_slider");
	
	Effect.SlideDown(container, { duration: 1.5 });
	
	return false;
}

function ot_star_slider_up()
{
	var container = document.getElementById("ot_star_slider");
	
	Effect.SlideUp(container, { duration: 1.5 });
		
	return false;
}


function homePageImageCycler()
{
	var xmlHttp 	= getTransport();
	var firstImage 	= document.getElementById('homePageImage');
	var secondImage	= document.getElementById('homePageImage2');
	var imageFolder = '/system/application/images/';
	var duration 	= 1500;  	/* 1000 millisecond fade = 1 sec */
	var steps 		= 50; 		/* number of opacity intervals   */
	var delay 		= 5000;     /* 5 sec delay before fading out */
	var _st 		= window.setTimeout;
	
		
	// This function allows setTimeout to pass multiple parameters to the dest function.
	// Without it. passing multiple params is a HUGE pain in the arse!
	// Courtesy of: http://www.hedgerwow.com/360/dhtml/setTimeout/demo.php
	window.setTimeout = function(fRef, mDelay)
	{
		if (typeof fRef == "function")
		{
			var argu 	= Array.prototype.slice.call(arguments,2);
			var f 		= (function(){ fRef.apply(null, argu); });
			
			return _st(f, mDelay);
		}

		return _st(fRef,mDelay);
	}

	if ((firstImage != null) && (secondImage != null))
	{
		setTimeout(loadImage, delay, 0);
	}

	function loadImage(iteration)
	{
		if (xmlHttp == null)
		{
			return;
		}

		var url = "/image/getRandomAjax/" + Math.random();

		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4)
			{			
				if (iteration % 2 == 0)
				{
					fadeOut(firstImage);
					secondImage.src = imageFolder + xmlHttp.responseText;
					fadeIn(secondImage);
				}
				else
				{
					fadeOut(secondImage);
					firstImage.src = imageFolder + xmlHttp.responseText;
					fadeIn(firstImage);
				}
				
				iteration++;
				
				// Load next image after delay period.
				setTimeout(loadImage, delay, iteration);
			}
		}
	}

	// Changes the Opacity of an element to the specified level.
	// Level must be between 0.0 & 1.0
	function setOpacity(element, level)
	{
		element.style.opacity 	= level;
		element.style.filter 	= "alpha(opacity=" + level * 100 + ");";
	}

	// Fades an element IN completely in the number of steps specified
	function fadeIn(element)
	{	
		for (i = 0; i <= 1; i += (1 / steps))
		{
			setTimeout(setOpacity, i * duration, element, i);
		}
	}

	// Fades an element OUT completely in the number of steps specified
	function fadeOut(element)
	{
		for (i = 0; i <= 1; i += (1 / steps))
		{
			setTimeout(setOpacity, i * duration, element, 1 - i);
		}
	}
}