/* JS for Babette Waterman, created by Barcroft Digital Ltd on 29/10/2010 */

/*
 * bWasserman Javascript
*/
function bWass()
{
/*
 * jquery on document ready functions
*/
$(document).ready(function(){
	
	//declare the image sources array
	var imageSources = [];
	//declare the index of the image in current use
	var i = 0;
	//loop through the images and gather the source of each image
	$('#home_image_container img').each(function(){
		imageSources.push($(this).attr('src'));
		if($(this).attr('id') != 'homeimg')
			{
			$(this).remove();
			}
		});
	
	//declare the links array
	var linkSources = [];
	//loop through the links and gather the source of each link
	$('#home_image_container a').each(function(){
		linkSources.push($(this).attr('href'));
		});
	
	//set an image object for the first image
	var homeImg = new Image();
	$(homeImg).load(function(){
		setTimeout(function(){
			$('#homeimg').fadeIn('slow',function()
				{
				timeImage();
				});
			},300);
		}).attr('src',imageSources[0]);
	
	/*
	 * sets a time gap before fading the image
	*/
	function timeImage()
	{
	setTimeout(fadeOutIn,5000);
	}
		
	/*
	 * fades the image out
	*/
	function fadeOutIn()
	{
	$('#homeimg').fadeOut('slow',function()
		{
		//chnage the source of the image //////////////////////////////////////////////
		i++;
		//check if i is the length of the array (4) at the time of build, then reset i back to 0 to show the first image again
		i = ((i == imageSources.length) ? 0 : i);
		//create an object of the new image
		var newImg = new Image();
		$(newImg).load(function(){
			//set the new image source of the homeimg tag
			$('#homeimg').attr('src',imageSources[i]);
		
			//fade the image in slowly
			$('#homeimg').fadeIn('slow',function()
				{
				//set teh new href for the link
				$('#homelink').attr('href',linkSources[i]);
				//set the timer to start over
				timeImage();
				});
			}).attr('src',imageSources[i]);
		});
	}
	
	
	
	});
////////////////////////////////////////////
// end of jquery on document ready code ////
////////////////////////////////////////////
	

}
var bWass = new bWass();

