var pageName = getPageName(window.location.pathname);
//If we want to use fading images on more than one page add another condition here with the images and (if applicable) their links
if (pageName == "index.php" || pageName == "")
{
	var images = new Array('Images/componenta_logo_fade.png','Images/thermostats1.jpg','Images/fasteners1.jpg','Images/radcaps2.jpg','Images/hoses1.jpg','Images/steering1.jpg','Images/keyspark1.jpg','Images/thermostats2.jpg','Images/fasteners2.jpg','Images/radcaps1.jpg','Images/hoses2.jpg');
	var links = new Array('about.php','thermostats.php','fasteners.php','radiator_caps.php','hoses.php','steering.php','about.php','thermostats.php','fasteners.php','radiator_caps.php','hoses.php');
	var alts = new Array('Componenta Logo','Thermostats','Industrial Fasteners','Radiator Caps','Rubber Hoses','Steering and Suspension','Componenta UK, Head Office','Thermostats','Industrial Fasteners','Radiator Caps','Rubber Hoses');
}
else if (pageName == "products.php")
{
	var images = new Array('Images/fasteners1.jpg','Images/thermostats1.jpg','Images/radcaps2.jpg','Images/hoses1.jpg','Images/steering1.jpg','Images/thermostats2.jpg','Images/fasteners2.jpg','Images/radcaps1.jpg','Images/hoses2.jpg');
	var links = new Array('thermostats.php','fasteners.php','radiator_caps.php','hoses.php','steering.php','thermostats.php','fasteners.php','radiator_caps.php','hoses.php');
	var alts = new Array('Industrial Fasteners','Thermostats','Radiator Caps','Rubber Hoses','Steering and Suspension','Thermostats','Industrial Fasteners','Radiator Caps','Rubber Hoses');
}

var nextImage = 1;
var imageWidth = 300;
var imageHeight = 225;
var imageTimeoutTime = 1800;
var imageTimeout, fadeTimeout, fadeTimeout2;

function setOpacity(el, opacity)
{
	opacity /= 100;
	el.style.opacity = opacity;
	el.style.MozOpacity = opacity;
	el.style.filter = "alpha(opacity=" + (opacity * 100) + ")";

}

function fadeImage(el, currentOpacity) {

	currentOpacity += 5;

	if (currentOpacity > 100)
	{
		setOpacity(el, 100);
		var prevEl = el.previousSibling ? el.previousSibling : el.parentNode.lastChild;
		prevEl.style.visibility = 'hidden';
		el.style.zIndex = 1;
		fadeTimeout = window.setTimeout(startFading, imageTimeoutTime);
	}
	else 
	{
		setOpacity(el, currentOpacity);
		fadeTimeout2 = window.setTimeout(function() { fadeImage(el, currentOpacity); }, 50);
	}

}


function startFading() 
{

	var el = document.getElementById('fadingImageContainer').childNodes[nextImage];

	el.style.visibility = 'visible';
	el.style.zIndex = 2;
	setOpacity(el, 0);
	fadeImage(el,0);

	nextImage = (nextImage < images.length-1) ? nextImage + 1 : 0;

}

/*function fadePrevImage()
{
	nextImage = (nextImage == 0) ? image.length-1 : nextImage - 1;
	clearTimeout(imageTimeout);
	clearTimeout(fadeTimeout);
	clearTimeout(fadeTimeout2);
	pageLoad('prev');
}*/

/*function fadeNextImage()
{
	nextImage = (nextImage < images.length-1) ? nextImage + 1 : 0;
	clearTimeout(imageTimeout);
	clearTimeout(fadeTimeout);
	clearTimeout(fadeTimeout2);
	pageLoad('next');
}*/

function pageLoad() 
{

	var el = document.getElementById('fadingImageContainer');

	while (el.firstChild) { el.removeChild(el.firstChild); }

	el.style.width = imageWidth + 'px';
	el.style.height = imageHeight + 'px';

	for(var i=0; i<images.length; i++)
	{
		var t = document.createElement('IMG');
		t.setAttribute('src',images[i]);
		t.setAttribute('width',imageWidth);
		t.setAttribute('height',imageHeight);
		t.setAttribute('alt',alts[i]);
		if (links[i] != "")
		{
			t.onmouseover = function() { setCursorStyle('pointer'); };
			t.onmouseout = function() { setCursorStyle('default'); };
			t.onclick = function() { followImageLink(); };
		}
		t.style.position = 'absolute';
		t.style.visibility = 'hidden';		
		el.appendChild(t);

	}
	
	el.firstChild.style.visibility = 'visible';

	imageTimeout = window.setTimeout(startFading, imageTimeoutTime);

}

function setCursorStyle(styleToSet)
{
	document.body.style.cursor=styleToSet;
}

function followImageLink()
{	
	var linkText="";
	var fetchImage = (nextImage == 0) ? nextImage=images.length-1 : nextImage-1;
	
	linkText=links[fetchImage];
	window.location=linkText;
}

function getPageName(locationText)
{
	return locationText.substring(locationText.lastIndexOf("/") + 1,locationText.length);
}

/*
	This function was stolen from:
	http://simon.incutio.com/archive/2004/05/26/addLoadEvent
*/
function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function')
  {
    window.onload = func;
  } 
  else 
  {
    window.onload = function() {if (oldonload) {oldonload();} func(); }
  }
}

addLoadEvent(pageLoad);
