
var currentSlide=0;
var the_slides=new Array();

var hangTime = 8000; // How long before the next promo is shown automatically (5 seconds) 
var fadeTime = 40; // How long to fade in each promo
var the_slideshow_play = false;
var image_location = '/themes/cosmos/img/';

function the_slideshow_getElementByClass(classname){
	var inc=0;
	var alltags=document.all? document.all : document.getElementsByTagName("*");
	for (i=0; i<alltags.length; i++){
		if (alltags[i].className==classname) {
			the_slides[inc++]=alltags[i];
		}
	}
}

function the_slideshow_setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function the_slideshow_fadeIn(objId,opacity,fadeTime) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      the_slideshow_setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("the_slideshow_fadeIn('"+objId+"',"+opacity+")", fadeTime);
    }
  }
}

function the_slideshow_hideAll() { // Hide all the promos 
	for (i=0; i<the_slides.length; i++) {
		the_slides[i].style.display="none";
	}
}

function the_slideshow_stop() {
	the_slideshow_play = false;
	imageController.src = image_location+'image-play.png';
	imageController.title = 'Play';
	imageController.alt = 'Play';
	imageController.onclick = function() {
		the_slideshow_start();
	};
	if (typeof(rotateTime) != "undefined") {
		clearTimeout(rotateTime);
	}
}

function the_slideshow_start() {
	the_slideshow_play = true;
	imageController.src =  image_location+'image-pause.png';
	imageController.title = 'Pause';
	imageController.alt = 'Pause';
	imageController.onclick = function() {
		the_slideshow_stop();
	};
	rotateTime = setInterval("the_slideshow_next(true)", hangTime);
}

// Return the next section (or loop back to the first one if the end is reached)
function the_slideshow_next(autoPlay) {
	currentSlide++;
	if (currentSlide == the_slides.length) currentSlide = 0;
	the_slideshow_hideAll();
	the_slides[currentSlide].style.display="block"; //show next panel
	imageNumberSpan.innerHTML = "Viewing image "+(currentSlide+1)+" of "+the_slides.length+" ";
	var fadeId = "the_slide" + currentSlide;
	//the_slideshow_fadeIn(fadeId,0);
	if (!autoPlay) {
		the_slideshow_stop();
	}
}

// Return the next section (or loop back to the last one if the first is reached)
function the_slideshow_previous(autoPlay) {
	currentSlide--;
	if (currentSlide < 0) currentSlide = the_slides.length-1;
	the_slideshow_hideAll();
	the_slides[currentSlide].style.display="block"; //show prev panel
	imageNumberSpan.innerHTML = "Viewing image "+(currentSlide+1)+" of "+the_slides.length+" ";
	var fadeId = "the_slide" + currentSlide;
	//the_slideshow_fadeIn(fadeId,0);
	if (!autoPlay) {
		the_slideshow_stop();
	}
}

function the_slideshow_Init() {

	the_slideshow_getElementByClass("the_slide");
	
	for (i=1; i<the_slides.length; i++) {
		the_slides[i].style.display="none";
	}
	
	currentSlide = 0;	
	imageTabs = document.createElement('div');
	imageTabs.id = 'the_slideshow_controls';
	lastTabTitle = '';
	
	
	// Add previous button
	imagePrevious = document.createElement('img');
	imagePrevious.id = 'the_slideshow_previous';
	imagePrevious.src =  image_location+'image-previous.png';
	imagePrevious.title = 'Previous';
	imagePrevious.alt = 'Previous';
	imagePrevious.onclick = function() {
		the_slideshow_previous(false);
	};
	imageTabs.appendChild(imagePrevious);
	
	// Add Play/pause controller button
	imageController = document.createElement('img');
	imageController.id = 'the_slideshow_play';
	imageController.src =  image_location+'image-play.png';
	imageController.onclick = function() {
		the_slideshow_start();
	};
	imageTabs.appendChild(imageController);
	
	// Add next button
	imageNext = document.createElement('img');
	imageNext.id = 'the_slideshow_next';
	imageNext.src =  image_location+'image-next.png';
	imageNext.title = 'Next';
	imageNext.alt = 'Next';
	imageNext.onclick = function() {
		the_slideshow_next(false);
	};
	imageTabs.appendChild(imageNext);
	
	//Add img x of x
	imageNumberSpan = document.createElement('span');
	imageNumberSpan.id = 'the_slideshow_imagecount';
	imageNumberSpan.innerHTML = "Viewing image "+(currentSlide+1)+" of "+the_slides.length+" ";
	imageTabs.appendChild(imageNumberSpan);
	
	document.getElementById('the_slideshow').insertBefore(imageTabs,document.getElementById('the_slideshow').childNodes[0]);

}
