/* Initialize all three slidehows */
var loaded = new Array();
loaded["large"] = false;
loaded["small1"] = false;
loaded["small2"] = false;
function image_loaded(id) {
   loaded[id] = true;
}
var interval = new Array();
var loading ;
var slideshow_obj;
function slideshow_init( int_large, int_small1, int_small2 ) {
   slideshow_obj = document.getElementById( "slideshow" );
   slideshow_obj.innerHTML += '<div id="loading">Loading</div>';
   interval["large"] = int_large;
   interval["small1"] = int_small1;
   interval["small2"] = int_small2;
   loading = document.getElementById( "loading" );
   loading.xOpacity = 99;
   setOpacity(loading); 
   wait_for_loaded( ) ;
}
var switch_loading = false;
function wait_for_loaded( ) {
   switch_loading = throb( loading, switch_loading ) ;
   if( loaded["large"] && loaded["small1"] && loaded["small2"] ) {
      slideshow_obj.removeChild(loading);
      slideshow( 'large', interval["large"], 100 );
      slideshow( 'small1', interval["small1"], 300 );
      slideshow( 'small2', interval["small2"], 550 );
   } else {
      setTimeout(wait_for_loaded, 100);
   }

}
/* Creates a new slideshow */
function slideshow(objid, inter, delay) {
   /* Get image array */
   var imgs = document.getElementById( objid ).getElementsByTagName("img");
   /* Set opacity variable */
   for(i=0;i<imgs.length;i++) {
      imgs[i].xOpacity = 0;
      setOpacity(imgs[i]); 
      imgs[i].style.display = "none";
   }
   /* Set current, and interval */
   var current = 0;
   imgs[current].style.display = "block";
   var interval = inter;
   /* Call init to fade in after a initial delay */
   setTimeout(init, delay);
   /* This fades the image in, then sets up the interval */
   function init( ) {
   	imgs[current].xOpacity = imgs[current].xOpacity + .05;
   	setOpacity(imgs[current]); 
   	if(imgs[current].xOpacity==.99)
   		setInterval(fade,interval);
   	else
   	   setTimeout(init,25);
   }
   /* This is the transition from one image to another */
   function fade( ) {
   	var cOpacity = imgs[current].xOpacity;
   	var nIndex = imgs[current+1]?current+1:0;
   	var nOpacity = imgs[nIndex].xOpacity;
   	cOpacity-=.05; 
   	nOpacity+=.05;
   	imgs[nIndex].style.display = "block";
   	imgs[current].xOpacity = cOpacity;
   	imgs[nIndex].xOpacity = nOpacity;
   	setOpacity(imgs[current]); 
   	setOpacity(imgs[nIndex]);
   	if(cOpacity<=0) {
   		imgs[current].style.display = "none";
   		current = nIndex;
   	} else {
   		setTimeout(fade,25);
      }   
   }

}
   /* Throb effect used by loading */
   function throb( load_obj, dir ) {
   	var Opacity = load_obj.xOpacity;
      if( dir == true )
   	  Opacity+=.05; 
      else
   	  Opacity-=.05;
      load_obj.xOpacity = Opacity;
   	setOpacity(load_obj);
      if( dir == true ) {
         if( Opacity>=.99 ) {
            return false ;
         } else return true ;
      } else {
         if( Opacity<=.15 ) {
            return true ;
         } else return false ;
      }
   }
   /* Sets opacity of an object */
   function setOpacity(obj) {
      if(obj.xOpacity>.99) {
         obj.xOpacity = .99;
         return;
      }
      obj.style.opacity = obj.xOpacity;
      obj.style.MozOpacity = obj.xOpacity;
      obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
   }