//declare gallery object
var fp_gallery = new fp_gallery();

//establish gallery object
function fp_gallery(){
	//source variables
	this.foreground 	= "foreground";
	this.background 	= "background";
	this.bannerlink 	= "bannerlink";
	this.Initialize 	= InitializeGallery;
	this.Run 			= RunGallery;
	this.Pause 			= Pause;
	this.Fade			= Fade;
	this.Change 		= Change_Image;
	this.Select 		= Select_Image;
	this.Update_Index 	= Update_Index;
	this.Stop			= Stop;
	this.Play			= Play;
}

function InitializeGallery(){
	//source variables
	var foreground = document.getElementById(this.foreground);
	var background = document.getElementById(this.background);
	var bannerlink = document.getElementById(this.bannerlink);
	//menu
	var play_button = document.getElementById("play_button");
	play_button.style.filter = 'alpha(opacity=' + 100 + ')';
  	play_button.style.MozOpacity = 1;
  	play_button.style.opacity = 1;
	var stop_button = document.getElementById("stop_button");
	stop_button.style.filter = 'alpha(opacity=' + 50 + ')';
  	stop_button.style.MozOpacity = 0.5;
  	stop_button.style.opacity = 0.5;
	//basics
	this.Path 		= "jp_galleries/" //path to where images are stored
	this.Url 		= linkUrlArray[0];
	//time variables
	this.Dt_Pause 	= 4000.00; //length of pause
	this.Dt_Fade 	= 2000.00; //length of fade
	this.tpassed 	= 0;
	this.t			= (new Date()).getTime();
	this.interval	= 16;
	//indexing variables
	this.index		= 0;
	this.max_index 	= UrlArray.length - 1;
	this.direction  = 1;
	//action variable
	this.action		= "Pause";
	//set up foreground, background and link
	if (this.max_index < 0) {
		//of no images than don't do anything
		return;
	} else {
		if (this.max_index == 0) {
		//if only one image than don't run slide show and just show the image
			foreground.src 		= this.Path+UrlArray[this.index]+".jpg";
			this.foregroundsrc 	= foreground.src;
			background.src 		= this.Path+UrlArray[this.index]+".jpg";
			this.backgroundsrc 	= background.src;
		} else {
		//This is normal case of 2 or more images in which slide show runs
			foreground.src 						= this.Path+UrlArray[this.index]+".jpg";
			bannerlink.href						= linkUrlArray[this.index];
			this.index = ++this.index;
			background.src 						= this.Path+UrlArray[this.index]+".jpg";
			background.style.backgroundImage 	= 'url('+background.src+')';
			this.intervalID = setInterval("fp_gallery.Run()",this.interval);
		}
	}
}
//Run slide show method
function RunGallery(){
	//Update time elapsed 
  	var t_new = (new Date()).getTime();
  	this.tpassed = this.tpassed + t_new - this.t;
  	this.t = t_new;
	//figure out what action to take
	if (this.action == "Fade"){
		this[this.action]();
	} else if (this.action == "Change"){
		this[this.action]();
	} else {  //probably action = pause
		this[this.action]();
	}
}

function Update_Index(direction){
	this.index = this.index + direction;
	if (this.index > this.max_index){
  		this.index = 0;
	} else if (this.index < 0){
		this.index = this.max_index;
	}
}

function Pause(){
	if(this.tpassed  >= this.Dt_Pause){
		this.action = "Fade";
		this.tpassed = 0;
	}
}

function Fade(){
	//source variables
	var foreground = document.getElementById(this.foreground);
	var background = document.getElementById(this.background);
	var bannerlink = document.getElementById(this.bannerlink);
	//If fade has come up then start fade
	//Find out what percent of the fade time has passed
  	this.opacity = 1 - ((this.tpassed)/(this.Dt_Fade));
  	//set the opacity of the foreground to the inverse percent of fade time passed
  	foreground.style.filter = 'alpha(opacity=' + this.opacity*100 + ')';
  	foreground.style.MozOpacity = this.opacity;
  	foreground.style.opacity = this.opacity;
	if(this.tpassed  >= this.Dt_Fade){
		this.action = "Change";
		this.tpassed = 0;
	}
}

function Change_Image(){
	//source variables
	var foreground = document.getElementById(this.foreground);
	var background = document.getElementById(this.background);
	var bannerlink = document.getElementById(this.bannerlink);
	//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 = background.src;
		
  	//set foreground opactity to 100
  	foreground.style.filter = 'alpha(opacity=' + 100 + ')';
  	foreground.style.MozOpacity = 1;
  	foreground.style.opacity = 1;
		
	//undate link
	bannerlink.href	= linkUrlArray[this.index];
			
	//everything is set and now advance the counter and prepare the next image to be loaded'
	
	//update index
	this.Update_Index(1);

	//set background to next image
	var backgroundimage = new Image();
	backgroundimage.src = this.Path+UrlArray[this.index]+".jpg";
	background.src = backgroundimage.src;
	background.style.backgroundImage = 'url('+background.src+')';
	this.action = "Pause";
	this.tpassed = 0;
}

function Select_Image(direction){
	//source variables
	var foreground = document.getElementById(this.foreground);
	var background = document.getElementById(this.background);
	var bannerlink = document.getElementById(this.bannerlink);
	//stop timer
	clearInterval(this.intervalID);
	//set direction
	this.direction = direction;
	
	//there are four possiblities of transisitions foward and back and then in fade and out.
	if(this.direction > 0){  //going forward in the selection
		if (this.action == "Fade") {
			this.tpassed = this.Dt_Fade;
		} else {
			//this.action = "Change"; - old code that starts the gallery
			this.tpassed = 0;
			if(this.action != "Stop"){
				this.Change();
			} else {
				this.Change();
				this.Stop();
			}
		}
	} else { //going backwards in the selection
		if (this.action == "Fade"){
			//switch background and foreground and invert opacity
			//update index
			this.index = this.index + this.direction;
			background.src = foreground.src
			background.style.backgroundImage = 'url('+background.src+')';
			foreground.src = this.Path+UrlArray[this.index]+".jpg";
			this.tpassed = this.Dt_Fade;
		} else {
			//update index
			this.Update_Index(this.direction);
			this.Update_Index(this.direction);
			//set background to next image
			var backgroundimage = new Image();
			backgroundimage.src = this.Path+UrlArray[this.index]+".jpg";
			background.src = backgroundimage.src;
			background.style.backgroundImage = 'url('+background.src+')';
	
			//this.action = "Change"; - old code that starts the gallery
			this.tpassed = 0;
			if(this.action != "Stop"){
				this.Change();
			} else {
				this.Change();
				this.Stop();
			}
		}
	}
	if(this.action != "Stop"){
		this.intervalID = setInterval("fp_gallery.Run()",this.interval);
	}
}

function Stop(){
	//source variables
	var foreground = document.getElementById(this.foreground);
	var background = document.getElementById(this.background);
	var bannerlink = document.getElementById(this.bannerlink);
	//Stop the Gallery
	clearInterval(this.intervalID);
	if (this.action == "Fade"){
		if (this.opacity >= .5){
			this.opacity = 1;
			foreground.style.filter = 'alpha(opacity=' + this.opacity*100 + ')';
  			foreground.style.MozOpacity = this.opacity;
  			foreground.style.opacity = this.opacity;
		} else {
			this.Change()
		}
	}
	this.action = "Stop";
	var play_button = document.getElementById("play_button");
	play_button.style.filter = 'alpha(opacity=' + 50 + ')';
  	play_button.style.MozOpacity = 0.5;
  	play_button.style.opacity = 0.5;
	var stop_button = document.getElementById("stop_button");
	stop_button.style.filter = 'alpha(opacity=' + 100 + ')';
  	stop_button.style.MozOpacity = 1;
  	stop_button.style.opacity = 1;
}

function Play(){
	//stop timer
	clearInterval(this.intervalID);
	var play_button = document.getElementById("play_button");
	play_button.style.filter = 'alpha(opacity=' + 100 + ')';
  	play_button.style.MozOpacity = 1;
  	play_button.style.opacity = 1;
	var stop_button = document.getElementById("stop_button");
	stop_button.style.filter = 'alpha(opacity=' + 50 + ')';
  	stop_button.style.MozOpacity = 0.5;
  	stop_button.style.opacity = 0.5;
	this.action = "Pause";
	this.t			= (new Date()).getTime();
	this.intervalID = setInterval("fp_gallery.Run()",this.interval);
}

//menu graphics

