// 

if (document.getElementById) {
  var text="";
  var slideshow_interval = 10000;
  //var slideshow_start_delay = 0;
  var slideshow_fade_out = 1000;
  var ssArray = new Array([], [], [], [], [], [], [], [], [], []);
  //Array FORMAT: [0,2000,1000,0,1,3,"id_holder","id_slide_1","id_slide_2"]
  var slideshowcounter=0;
  var total_slideshows = 0;
  var sscounter = 0;
  var debug_text = "";
  var slideshowstagger = 0;

  function rand(highestRandomNumber) {
    var now=new Date()
    var rnum=(now.getSeconds())%highestRandomNumber
    var rnum=rnum+1
	return rnum;
  }

  function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;
    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			
            timer++;
        }
		document.getElementById(id).style.zIndex=1;
		setTimeout("document.getElementById('" + id + "').style.visibility='hidden'",(timer * speed)+10);
    } else if(opacStart < opacEnd) {
         document.getElementById(id).style.visibility="visible"; 
		for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)+1000);
			
            timer++;
        }
		document.getElementById(id).style.zIndex=100; 
    }
  }

  //change the opacity for different browsers
  function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
  } 
  
  function currentOpacity(id) {
    var currentOpac = 100;
	//var temp_opac="";
    //if(document.getElementById(id).style.opacity < 100) {
	
	if(document.getElementById(id).style.filter) {
	    currentOpac=document.getElementById(id).style.filter.substr(14,(document.getElementById(id).style.filter.length-15));
		//alert(currentOpac);
        //currentOpac = document.getElementById(id).style.opacity * 1;
    }
	else if(document.getElementById(id).style.opacity < 100) {
        currentOpac = document.getElementById(id).style.opacity * 100;
    }
	return currentOpac;
  }

  function startSlideShows() {
	alldivs=document.body.getElementsByTagName("div");
    for (i=0; i<(alldivs.length);i++) {
	  if (alldivs[i].className) {
	    text=alldivs[i].className;
	  }
	  else if (alldivs[i].getAttribute("class")) {
	    text=alldivs[i].getAttribute("class");
	  }
	  else {
	    text="";
	  }
	  var temparray=text.split(" ");
	  //loop for all classes
	  for (var j=0; j<(temparray.length);j++) {
		if (temparray[j]=="slideShow") {
		  ssArray[0+sscounter][6]=alldivs[i].getAttribute("id");
          var holderchildrendivs=document.getElementById(ssArray[0+sscounter][6]).getElementsByTagName("div");
		  debug_text=debug_text+"  "+ssArray[0+sscounter][6]+": ";
		  //var ihold=0;
		  //while (ihold<holderchildrendivs.length) {
		  var ihold2=0;
		  for (var ihold=0; ihold<(holderchildrendivs.length);ihold++) {
		    //forloop (ADD FILTER ONLY SLIDES) problems HAPPEN when there are nested DIVS: match CLASS="*slide*"
			
			var text2="";
			
			if (holderchildrendivs[ihold].className) {
	          text2=holderchildrendivs[ihold].className;
	        }
	        else if (holderchildrendivs[ihold].getAttribute("class")) {
	          text2=holderchildrendivs[ihold].getAttribute("class");
	        }
	        else {
	          //text2="Slide"; //cannot test class
	        }
			//text2="nope";
		    if ((text2.indexOf("firstSlide")!=-1)||(text2.indexOf("subsequentSlides")!=-1)) {
			if (ihold==0) {
				ssArray[0+sscounter][3]=0;
			}
			if (ihold==1) {
				ssArray[0+sscounter][4]=1;
			}
			ssArray[0+sscounter][(7+ihold)]=holderchildrendivs[ihold];
			//alert ("good here");
			debug_text=debug_text+"  "+ssArray[0+sscounter][(7+ihold)].getAttribute("id");
			//total_slides++;
			//ihold2++;
			}
			else {
			  //ihold2=ihold;	
			}
		  }

		  debug_text=debug_text+"\n";
		  
		  ssArray[0+sscounter][5]=holderchildrendivs.length;
		  if (ssArray[0+sscounter][4]>(holderchildrendivs.length-1)){
            ssArray[0+sscounter][4]=holderchildrendivs.length-1;
          }
		  ssArray[0+sscounter][6]=alldivs[i].getAttribute("id");
		  
		  //alert (rand(10));
          /*ssArray[0+sscounter][3]=rand(holderchildrendivs.length)-1;
		  if (ssArray[0+sscounter][3]=(holderchildrendivs.length-1)){
            ssArray[0+sscounter][4]=0;
          }
		  else {
		    ssArray[0+sscounter][4]=ssArray[0+sscounter][3]+1;  
		  }*/
		  
		  total_slideshows++;
		  sscounter++;
	    }
	  }
	  //end loop for all classes
    }	
	//end loop for all divs
	//alert();
	//debug_text="";
	//alert(debug_text+": "+ssArray.toString());
	setInterval("ccontent(slideshowcounter)",slideshow_interval);

  }
  function ccontent(indexno) {
	//for (indexno=0; indexno<total_slideshows; indexno++) {
      indexno=slideshowstagger;
      if (ssArray[indexno][4]!=ssArray[indexno][3]) {
        opacity(ssArray[indexno][(ssArray[indexno][3]+7)].getAttribute("id"),100,0,slideshow_fade_out); 
	    opacity(ssArray[indexno][(ssArray[indexno][4]+7)].getAttribute("id"),0,100,slideshow_fade_out);
      }
      ssArray[indexno][3]=ssArray[indexno][3].valueOf()+1;
      ssArray[indexno][4]=ssArray[indexno][4].valueOf()+1;
      if (ssArray[indexno][4]>(ssArray[indexno][5]-1)){
        ssArray[indexno][4]=0;
      }
      if (ssArray[indexno][3]>(ssArray[indexno][5]-1)){
        ssArray[indexno][3]=0;
      }
	  slideshowstagger++;
	  if (slideshowstagger==total_slideshows) {
		  slideshowstagger=0;
	  }
	//}
  }
  window.onload=startSlideShows
}

