//Declare a bunch of things that can be changed
var UrlArray = new Array("JP_magsale_ad","JP_optics_ad","JP_ez_trigger_ad","HuntingLRP","JP_HG-6M_ad","JP_module_ad","MOR-LRP-snowshot","JPweb_BRM3G-4","JPweb_BRM3G-6");
var linkUrlArray = new Array("1.5.1.4_mags.php","1.6.3.php","1.4.8.1_ezt.php","1.2.5_LRP07H.php","1.1_feature.php","1.2.14_trigger_module.php","1.2.6_MOR07.php","1.2.5_LRP07.php","1.2.1_CTR02.php");
var ForegroundID="foreground";
var BackgroundID="background";
var linkID="bannerlink";
var Path = "photos_new/";
var Dt_change=5000.00;//time inbetween fades
var Dt_fade=2000.00;//length of fade
//declare a bunch of constants
var tpassed = 0;
var max_index = UrlArray.length - 1;
var imagecache = new Array();
var index = 1;
if (index > max_index){
index=0;
}

function slideshow()
{
	//Initialize Slide Show
	var foreground=document.getElementById(ForegroundID);
	var background=document.getElementById(BackgroundID);
	var bannerlink=document.getElementById(linkID);
	foreground.src = Path+UrlArray[0]+".jpg";
	background.src = Path+UrlArray[index]+".jpg";
	background.style.backgroundImage = 'url('+Path+UrlArray[index]+'.jpg)';
	bannerlink.href= linkUrlArray[0];
	
	//run slideshow
	if (document.images){//Make sure browser supports image objects
		slideshowfader((new Date()).getTime());
	}
}

function slideshowfader(tlast)
{
	//get variable for every element to be changed
  	var foreground=document.getElementById(ForegroundID);
	var background=document.getElementById(BackgroundID);
	var bannerlink=document.getElementById(linkID);
	
  	//Update time elapsed 
  	tcurrent = (new Date()).getTime();
  	tpassed = tpassed + tcurrent - tlast;
  	tlast = tcurrent;
  	//Check if tpassed is at a critical value
  	if (tpassed>=Dt_change && tpassed < Dt_change+Dt_fade){
		//Find out what percent of the fade time has passed
  		opacity = 1 - ((tpassed - Dt_change)/(Dt_fade));
		
  		//set the opacity of the foreground to the inverse percent of fade time passed
  		foreground.style.filter = 'alpha(opacity=' + opacity*100 + ')';
  		foreground.style.MozOpacity = opacity;
  		foreground.style.opacity = opacity;
  		setTimeout("slideshowfader("+tcurrent+")",10);
  } else if (tpassed>=Dt_change+Dt_fade){
	  
  		//set foreground opacity to zero
  		foreground.style.filter = 'alpha(opacity=' + 0 + ')';
  		foreground.style.MozOpacity = 0;
  		foreground.style.opacity = 0;
		
	  	//set foreground to match background
  		foreground.src = Path+UrlArray[index]+".jpg";
		
  		//set foreground opactity to 100
  		foreground.style.filter = 'alpha(opacity=' + 100 + ')';
  		foreground.style.MozOpacity = 1;
  		foreground.style.opacity = 1;
		
		//undate link
		bannerlink.href= linkUrlArray[index];

		//update index
		index = index+1;
		if (index > max_index){
  			index = 0;
		}

		//update image cache
		if (imagecache[index] == null){
  			imagecache[index] = new Image;
  			imagecache[index].src = Path+UrlArray[index]+".jpg";
		
			//check if image loaded
			if (imagecache[index].width==0){
				imagecache[index].src = Path+UrlArray[index]+".jpg";
			}
  		
			//Make sure the image loaded if not go to the next image until a image successfully loads
  			while (imagecache[index].width==0){
  				index = index+1;
				if (index > max_index){
		  			index = 0;
				}
				imagecache[index] = new Image;
				imagecache[index].src = Path+UrlArray[index]+".jpg";
  			}
		}

    	//set background to next image
		background.src = Path+UrlArray[index]+".jpg";
		background.style.backgroundImage = 'url('+Path+UrlArray[index]+'.jpg)';

  		//reset timer
  		tpassed=0;
 		setTimeout("slideshowfader("+tcurrent+")",10);
  } else {
		//set timeout to call self again
		setTimeout("slideshowfader("+tcurrent+")",10);
  }
}