// Internet Explorer
var ie5=(document.getElementById && document.all);
// Mozilla Firefox
var ns6=(document.getElementById && !document.all);

nPlus = 5   //the % of fading for each step
speed = 50  //the speed

nOpac = 0

function FadeImg(){
   if(document.getElementById){
        imgs = document.getElementById('img2');
		//imgs.style.MozOpacity=0;
		imgs.style.visibility="visible";

		opacity = nOpac+nPlus;
		nOpac = opacity;
		
		if(nOpac>100) {
		  nOpac=0;
    	} else {
		  setTimeout('FadeImg()',speed);

    	  if(ie5){
            imgs.style.filter="alpha(opacity=0)";
	  	    imgs.filters.alpha.opacity = opacity;
    	  }
    	  if(ns6){
            //imgs.style.MozOpacity = 0;
	  	    imgs.style.MozOpacity = nOpac/100;
    	  }
		}
    }
}

/* Determine the number of pictures (from count.txt)*/
var ss_max;
var ss_curr;
var ss_next;

var xmlHttp = false;
try {
  xmlHttp = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      xmlHttp = false;
	}
  }
}

if (!xmlHttp)
     alert("Error initializing XMLHttpRequest!");

function getSlideshowCount() {
  // Build the URL to connect to
  var url="Pictures/Slideshow/count.txt";

  // Open a connection to the server
  xmlHttp.open("GET", url, false);

  // Send the request
  xmlHttp.send(null);

  ss_max = xmlHttp.responseText;
}

function loadSlideshowPictures() {
  ss_imgs = new Array;
  for (i=0; i<ss_max; i++) {
	ss_imgs[i] = new Image(200,150);  
	ss_imgs[i].src="Pictures/Slideshow/pic" + (i+1) + ".jpg";  
  }

  ran_unrounded=Math.random()*ss_max;
  ran_number=Math.floor(ran_unrounded); 

  ss_curr=ran_number;
  ss_next=ran_number+1;
  if (ss_next == ss_max) {ss_next=0;}
}

function init_slideshow() {
	document.image1.src=ss_imgs[ss_curr].src;
}

first_time=true;

function start_slideshow(){
	if (first_time) {
	   getSlideshowCount();
	   loadSlideshowPictures();
	   init_slideshow();
	   first_time=false;
	}
	
    setTimeout ('switch_images()', 5000);
}

function switch_images() {
    document.image1.src=ss_imgs[ss_curr].src;
	document.image2.src=ss_imgs[ss_next].src;
	
	FadeImg();

	if (ns6) {
	   document.image1.style.visibility="hidden";
	   document.image1.src=document.image2.src;
	   document.image1.style.visibility="visible";
	   document.image2.style.visibility="hidden";
	}
	
	if (ss_next == ss_max-1) {
	  ss_curr=ss_next;
	  ss_next=0;
	} else {
	  ss_curr=ss_next;
	  ss_next=ss_curr+1;
	}
	start_slideshow();
}

onload=start_slideshow;
//  End -->
