// JavaScript Document
	//finds element in target array.  If nothing found, returns -1, else, returns array ordinal of last elemement found
	function search_array (target,element) { 
		var result = -1;
		
		for ( var i=0;i<=target.length;i++) {
			if (target[i] == element) {
				result = i;
			}
		}
		
		return result;
	}


	//removes items from target_array that are in suppress_array.  Returns suppressed array
	function suppress_slides (target_array,suppress_array) { 
	
		var result_array = new Array ();
		
		result_array = target_array;
		
			for (var i=0;i<=suppress_array.length;i++) { 
				x = search_array(result_array,suppress_array[i]);
				if (x!=-1) {
					result_array.splice(x,1);  //remove suppressed item
				}
			}
		
		return result_array;
		
	} //suppress_slides
	
	function get_width(img_location) {
		var my_width;	
		if (img_location.search("/wide/") != -1) {
			my_width="wide";
		}
		
		if (img_location.search("/1third/") != -1) {
			my_width="onethird";
		}
		
		if (img_location.search("/2thirds/") != -1) {
			my_width="twothirds";
		}
		
		return my_width;

	} // get_width
	
	
	Array.prototype.width_map = function(i,width) {  //gives a map (array) of all instances in mother array of width up to i
		var my_map = new Array();
		
		for (j=0;j<i;j++) {
			if (get_width(this[j])==width) {
				my_map.push(j);
			}
		}
		
		return my_map;
	}
	
	
	Array.prototype.rand_pick = function(i,width) { //returns a random index in mother array of elements of width before i.  returns -1 if there are none.
		var width_map = this.width_map();
		if (width_map.length == 0 ) {
			return -1;
		}else {
			return Math.floor(Math.random()*width_map.length-1);
		}
	}
	
	
	Array.prototype.copy_past=function(i,width) {
		
		var my_rand_past;
		my_rand_past = this.rand_pick();
		
		if (my_rand_past ==-1) {
			return -1;
		}else{
			this.splice(i,0,this[my_rand_past]);
		}
		
	}
	
	Array.prototype.move_future=function(i,width) { 
		var x; 
		for (j=i;j<this.length;j++) { 
			if (get_width(this[j])==width){
				x=this[j]; 
				this.splice(j,1);
				this.splice(i,0,x);
				return 0;
			}
		}
		return -1;
	}
	
	
	function pair_slides (the_slides) {
		
		var all_paired=true;
		
		for (i=0;i<the_slides.length;i++) {
			
			switch (get_width(the_slides[i])) {
				case "wide":
					//do nothing
					break;


				case "onethird":
					if (i==0) {  //this is the first one
						
						if (the_slides.move_future(i,"wide") == -1) { //find the next w to delete then insert at 0
							alert ("Slide show must contain a wide photo");
							return;
						}
						all_paired = false;
					}
					else if (get_width(the_slides[i-1]) == "wide") {  // if prev is wide

						if (i==the_slides.length-1) { // if this is the last one
						
							if (the_slides.copy_past(i,"twothirds") == -1) {  //get a previous 2/3 from before to copy at the end
								the_slides.splice(i,1);
							}
							//leave all_paired as done
						}
						else if (get_width(the_slides[i+1]) != "twothirds") {  // next is not 2/3
							
							if (the_slides.move_future(i+1,"twothirds") == -1) { //get a 2/3 from future
								if (the_slides.copy_past(i+1,"twothirds") == -1) { //or copy from past
									the_slides.splice(i,1); // or delete this 1/3
								}
							}
							all_paired = false;
						}
					}
					break;
					
				case "twothirds": 
					if (i==0) {
						if (the_slides.move_future(i,"wide") == -1) { //find the next w to delete then insert at 0
							alert ("Slide show must contain a wide photo");
							return;
						}
						all_paired = false;
					}
					else if (get_width(the_slides[i-1]) == "wide") {  // if prev is wide
						if (i==the_slides.length-1) { // if this is the last one
							if (the_slides.copy_past(i,"onethird") == -1) {  //get a previous 1/3 from before to copy at the end
								the_slides.splice(i,1);  // no match... remove last element
							}
							//leave all_paired as done
						}
						else if (get_width(the_slides[i+1]) != "onethird") {  // next is not 1/3
							
							if (the_slides.move_future(i+1,"onethird") == -1) { //get a 1/3 from future
								if (the_slides.copy_past(i+1,"onethird") == -1) { //or copy from past
									the_slides.splice(i,1); // or delete this 2/3
								}
							}
							all_paired = false;
						}
					}
					break;

			}  //switch
			
		}  //for

		if (!all_paired) { 
			the_slides = pair_slides(the_slides);
		}
		
		return the_slides ;
	}  //pair slides
	

	function set_opacity(my_object,opacity) {  //opacity as 0-1
		my_object.style.opacity=opacity;
		my_object.style.filter="alpha(opacity=" + opacity*100 + ")";
	}
	
	var killMeFade;  //used to kill a non-passing fade_part
	
	function fade_part(img_i,total_fade,started,fps_jump,to_i,pass_on,hold) {
			//img_i holds this index to holder_img
			//total_fade is how long the fade will be
			//started is timestamp to when fade started
			//fps_jump is how long before initiating next fade_part
			//from_i is the index of  we are fading from
			//pass_on is true if we are passing to next_fade when done.  We don't if another image will be doing it.  W>LR will not do it for one of the LR
			//hold is how long to hold before next_fade
		var date= new Date();
		var to_opacity;
		
		to_opacity = (date.getTime()-started)/total_fade;
		if (to_opacity >=1) { //all done fading!
			set_opacity(holder_imgs[img_i],1);
			if (pass_on) {
				clearTimeout(killMeFade);
				setTimeout("next_fade(" + to_i + ")",hold);
			}
			return;
		}
		
		set_opacity(holder_imgs[img_i],to_opacity);
		var nextFadePart = "fade_part(" + img_i + "," + total_fade + "," + started + "," + fps_jump + "," + to_i + "," + pass_on + "," + hold + ")";
		if (pass_on) {
			setTimeout(nextFadePart,fps_jump);
		}
		else {
			killMeFade=setTimeout(nextFadePart,fps_jump);
		}
	}
	
	
	var onDeck1Loaded = false;
	var onDeck2Loaded = false;
	
	function next_fade(from_i) {
		
// -------------------------------------------------------------------
// Use these to adjust the feel of the slides
		var fade_time = .75;  //time to fade in seconds
		var fade_fudge = .25;  //time fade may fudge + or - fade time
		
		//NOTE - There is an extra 1/10 of a second added to each hold
		var hold_time = 2.5;  //time to hold an image
		var hold_fudge = .5; //time hold may fudge + or - hold_time
		
		var fps = 24  // frames per second... may be less if processor can't handle it.
// -------------------------------------------------------------------
		
		var true_fade = 1000*(fade_time + Math.random()*fade_fudge*2-fade_fudge);
		var true_hold = 1000*(hold_time + Math.random()*hold_fudge*2-hold_fudge);
		var fps_jump = 1000/fps;
		
		var d= new Date();

		
		
		if (from_i == -1) { //this is the first time

			document.getElementById("on_deck_img1").onload=function x() {
				onDeck1Loaded=true;
			};
			document.getElementById("on_deck_img2").onload=function x() {
				onDeck2Loaded=true;
			};
			ondeck1Loaded=false;
			onDeck2loaded=false;
			document.getElementById("on_deck_img1").src=null;
			document.getElementById("on_deck_img2").src=null;
			document.getElementById("on_deck_img1").src=all_slides[1];
			document.getElementById("on_deck_img2").src=all_slides[2];
			setTimeout("next_fade(0)",true_hold);
			return;
		}
		
/*		
		if (!((document.getElementById("on_deck_img1").complete==true) && (document.getElementById("on_deck_img2").complete==true))) { //next images not yet loaded
			alert('stopped');setTimeout(next_fade(from_i),500);//try again in 1/2 second
			return;
		}


		if (!(document.getElementById("on_deck_img1").naturalHeight==216 && document.getElementById("on_deck_img2").naturalHeight==216)) { //next images not yet loaded
			var cacheWait="next_fade(" + from_i + ")";
			setTimeout(cacheWait,500);//try again in 1/2 second
			return;
		}
*/

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
var is_safari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;
if (is_chrome || is_safari) {
	onDeck1Loaded=true;
	onDeck2Loaded=true;
}

		if (!(onDeck1Loaded && onDeck2Loaded)) { //next images not yet loaded
			var cacheWait="next_fade(" + from_i + ")";
			setTimeout(cacheWait,500);//try again in 1/2 second
			return;
		}

		if (from_i>all_slides.length+1) {
			from_i=0;
		}
		
		// now we can do the fade
		
		
		// clear out all background images
		for (i=0;i<=holder_divs.length-1;i++) {
			holder_divs[i].style.backgroundImage="";
		}
		
		
		var to_i;
		if (from_i==all_slides.length-1) { //we are at the end
			to_i = 0;
		}
		else {
			to_i = from_i + 1;
		}
		//preloads
		
		onDeck1Loaded=false;
		document.getElementById("on_deck_img1").src=null; 
		document.getElementById("on_deck_img1").src=all_slides[to_i];
		if (to_i+1<all_slides.length) {
			onDeck2Loaded=false;
			document.getElementById("on_deck_img2").src=null;
			document.getElementById("on_deck_img2").src=all_slides[to_i+1];
		}
		
		
		var from_width = get_width(all_slides[from_i]);
		var to_width = get_width(all_slides[to_i]);
		
		
		var from_holder_i;
		var to_holder_a_i;
		var to_holder_b_i;
		
		var first_fade_part;
		
		
		//reset holder_divs z-index that are not 1001 to 0
		for (i=0;i<=holder_divs.length-1;i++) {
			if (holder_divs[i].style.zIndex!=1001) {
				holder_divs[i].style.zIndex=0;
				holder_imgs[i].src="images/spacer.gif";
			}
		}
		
		//move border left or right of two thirds from img to div or div to img
		if (from_width!="wide" && to_width=="twothirds") {
			document.getElementById("twothirds_l_img").style.borderRightWidth="0px";
			document.getElementById("twothirds_r_img").style.borderLeftWidth="0px";
			
			document.getElementById("twothirds_l_div").style.borderRightWidth="2px";
			document.getElementById("twothirds_l_div").style.width="317px";
			
			document.getElementById("twothirds_r_div").style.borderLeftWidth="2px";
			document.getElementById("twothirds_r_div").style.width="317px";
		}
		else {
			document.getElementById("twothirds_l_div").style.borderRightWidth="0px";
			document.getElementById("twothirds_l_div").style.width="319px";
			
			document.getElementById("twothirds_r_div").style.borderLeftWidth="0px";
			document.getElementById("twothirds_r_div").style.width="319px";
			
			document.getElementById("twothirds_l_img").style.borderRightWidth="2px";
			document.getElementById("twothirds_r_img").style.borderLeftWidth="2px";
		}
		
		if ((from_width==to_width) || ((from_width!="wide") && (to_width!="wide"))) {  //same to same  or  going from !W to !W
		
			//find first holder_div that is z-index 1001 and is same width.  Put it in from_div_i
			for (i=0;i<=holder_divs.length-1;i++) {
				if (holder_divs[i].style.zIndex==1001) {  // div is currently displaying
					if (holder_divs[i].id.search(to_width) == 0) {  //div is the correct width
						from_holder_i=i;
						break;
					}
				}
			}
		
//bb added to deal with browsers that can not keep up with loading background image
holder_divs[from_holder_i].style.backgroundColor = "#000000";


			holder_divs[from_holder_i].style.backgroundImage = "url("+holder_imgs[from_holder_i].src+")"; //copy the current image to the background of the div
			set_opacity(holder_imgs[from_holder_i],0);

			holder_imgs[from_holder_i].src = document.getElementById("on_deck_img1").src;  // copy the on deck image to the working img
			
			first_fade_part = "fade_part(" + from_holder_i + "," + true_fade + "," + d.getTime() + "," + fps_jump + "," + to_i + ",true," + true_hold + ")";
			setTimeout(first_fade_part, 100);
			
		}
		else {			
			var no_b = false;
			if (to_width=="wide") {
				var no_b = true;
			}

//bb added to clear all backgroud colors we may have added

for (i=0;i<=holder_divs.length-1;i++) {
	holder_divs[i].style.backgroundColor = "";
}
			
			var compare=to_width+"_l";
			if (to_width=="wide"){
				compare=to_width;
			}
			for (i=0;i<=holder_divs.length-1;i++) {
				if (holder_divs[i].id.search(compare) ==0) { // correct width on the left
					to_holder_a_i=i;
					to_holder_b_i=i+1;
					break;
				}
			}
			
			set_opacity(holder_imgs[to_holder_a_i],0);
			holder_divs[to_holder_a_i].style.backgroundImage="";
			holder_imgs[to_holder_a_i].src=document.getElementById("on_deck_img1").src
			
			//send any 1000 divs to 0
			for (i=0;i<=holder_divs.length-1;i++) {
				if (holder_divs[i]==1000) {
					holder_divs[i]=0;
				}
			}
			
			
			holder_divs[to_holder_a_i].style.zIndex=1001;

			//send any forground div to the background
			for (i=0;i<=holder_divs.length-1;i++) {
				if (holder_divs[i].style.zIndex==1001 && i!=to_holder_a_i) {
					holder_divs[i].style.zIndex=1000;
				}
			}
			
			first_fade_part = "fade_part(" + to_holder_a_i + "," + true_fade + "," + d.getTime() + "," + fps_jump + "," + to_i + "," + no_b + "," +true_hold + ")";  // note that no_b determines if fade_part passes
			setTimeout(first_fade_part,100);
			
			if (!no_b) {
				set_opacity(holder_imgs[to_holder_b_i],0);
				holder_divs[to_holder_b_i].style.backgroundImage="";
				holder_imgs[to_holder_b_i].src=document.getElementById("on_deck_img2").src
				holder_divs[to_holder_b_i].style.zIndex=1001;
				first_fade_part = "fade_part(" + to_holder_b_i + "," + true_fade + "," + d.getTime() + "," + fps_jump + "," + (to_i+1) +",true," + true_hold + ")";
			setTimeout(first_fade_part,100);
			}
			
			
			
		}
	
		
	} // next_fade


	var all_slides = new Array();  //leave global so I can get to it from all functions without passing it around.

//---------------------------------- GLOBAL HOLDER ARRAYS	
	var holder_divs = new Array();
	var holder_imgs = new Array();
//----------------------------------------------------------




	function slideShow() { 
		
		var slides_lead = new Array();  //template and page top slides combined
		var slides_rand = new Array(); //template and page rand slides combined
		
		holder_divs[0]=document.getElementById("wide_div");
		holder_divs[1]=document.getElementById("onethird_l_div");
		holder_divs[2]=document.getElementById("twothirds_r_div");
		holder_divs[3]=document.getElementById("twothirds_l_div");
		holder_divs[4]=document.getElementById("onethird_r_div");
		
		holder_imgs[0]=document.getElementById("wide_img");
		holder_imgs[1]=document.getElementById("onethird_l_img");
		holder_imgs[2]=document.getElementById("twothirds_r_img");
		holder_imgs[3]=document.getElementById("twothirds_l_img");
		holder_imgs[4]=document.getElementById("onethird_r_img");
		

		if (slides_fresh_lead) { 
			slides_lead = slides_page_lead;
		}
		else {
			slides_lead = suppress_slides(slides_template_lead, slides_template_suppress); 

			if (page_lead_on_top) {
				slides_lead = slides_page_lead.concat(slides_lead);
			}
			else {
				slides_lead = slides_lead.concat(slides_page_lead);

			}
		}

		slides_rand = suppress_slides(slides_template_rand, slides_template_suppress);
		slides_rand = slides_rand.concat(slides_page_rand);
		
		for (var i=0;i<=4;i++) {
			slides_rand.sort( function() {return 0.5 - Math.random()})   //randomizes slides_rand
			slides_rand.reverse();
		}

		all_slides = slides_lead;
		all_slides = all_slides.concat(slides_rand);
		all_slides = pair_slides(all_slides);

		//all_slides now holds what we want to display

		next_fade(-1)

	}