var first = 1;
var last = 4;
var current = 4;
var isPause = false;
var timer = 5000; //1000 per second.  5000 == 5 seconds
var slides;

var timerId=0;

function nextSlide() {
	var showNextIdx;
	if (current==last) { showNextIdx=1; }
	else { showNextIdx=current+1; }
	switchSlides(current, showNextIdx);
}

function pauseShow() {
	if (isPause) {
		startShow();
	} else {
		stopShow();
	}
	isPause=!isPause;
}

function prevSlide() {
	// Hide current picture
	if (current==first) {showNextIdx=last; }
	else { showNextIdx=current-1; }
	switchSlides(current, showNextIdx);
}

function showSlide(idx) {
	// Hide current picture
	if (idx>=first && idx<=last) { showNextIdx = idx; }
	else { showNextIdx=1; }
	switchSlides(current, showNextIdx);
}

function startShow() {
	timerId=setInterval("nextSlide()", timer);
}

function stopShow() {
	clearInterval(timerId);
}

function switchSlides(a, b) {
    stopShow();
	Effect.Fade('slide' + a);
    setTimeout("Effect.Appear('slide" + b + "');", 0);
	current=b;
	startShow();
}

function initSlideShow() {
  if (!document.getElementsByTagName){ return; }
  var divs = document.getElementsByTagName("div");
  
  var tmpfirst=0;
  var tmplast=0;

  for (var i=0; i<divs.length; i++){
    var curdiv = divs[i];
    if ( (curdiv.getAttribute("id")) && (curdiv.getAttribute("rel") == "slide") ) {
      if (tmplast>0) {
      	curdiv.style.display='none';
      } else {
      	curdiv.style.display='block';
      }
      tmplast=tmplast+1;
      
    }
  }
  
  if (tmplast>0) {
  	first=1;
  	last=tmplast;
  	current=1;
  	startShow();
  }
}

