// JavaScript Document
function CSTslideshow(instance, imagecountIn, intervalIn, memory) {
	makeHttpObject(); 					// create an object to send http requests (see function below)
	imagecount = +imagecountIn;			// PHP sends strings - convert to numbers
	interval = +intervalIn;				// PHP sends strings - convert to numbers
	firstSlide = getStart(instance);			// get first slide of sequence
	imagecount = checkCount(imagecount, instance);	// double check that the image count is accurate(Drupal issue)
	MM_effectAppearFade('slideshow'+firstSlide+instance, 500, 0, 100, false);
	MM_changeProp('slideshow'+firstSlide+instance,'','zIndex','10','DIV');
	setCookie('CSTss'+instance,firstSlide,'','','','/','','');
	if (imagecount > 1) {
		rotate(instance, imagecount, interval, firstSlide);
		rotateInterval = interval * imagecount;
		setInterval('rotate("'+instance+'", "'+imagecount+'", "'+interval+'", "'+firstSlide+'")',rotateInterval);
	}
}

function checkCount(imagecount, instance) {
	newImageCount = 0;
	for (i=0; i<=imagecount; i++) {
		if (document.getElementById('slideshow'+i+instance)) {
			newImageCount++;
		}
	}
	return newImageCount;
}

function getStart (instance){
	cval = getCookie('CSTss'+instance)
	if (cval) {
		return +cval;
	}
	else {
		return 0;
	}
}

function rotate(instance, imagecount, interval, firstSlide) {
	
	next = +firstSlide;
	currentSlide = +firstSlide;
	for (i=0; i<=imagecount-1; i++) {
		intrvl = (i+1) * interval;
		next++;
		if (next > (imagecount - 1)) {next = 0;}
/*		var request = makeHttpObject();					// force an update for the image being faded into
		var nextslide = 'slideshow'+next+instance+'img';
		var source = document.getElementById('slideshow'+next+instance+'img').src;
		request.open("GET", source+'?' + (new Date()).getTime(), false);
		request.send(null); */
		setTimeout(	'MM_effectAppearFade(\'slideshow'+currentSlide+instance+'\', 500, 100, 0, false);MM_effectAppearFade(\'slideshow'+next+instance+'\', 500, 0, 100, false);MM_changeProp(\'slideshow'+currentSlide+instance+'\',\'\',\'zIndex\',\'1\',\'DIV\');MM_changeProp(\'slideshow'+next+instance+'\',\'\',\'zIndex\',\'10\',\'DIV\');setCookie(\'CSTss'+instance+'\',\''+next+'\',\'\',\'\',\'\',\'/\',\'\',\'\')',intrvl);
		currentSlide++;
		if (currentSlide > +imagecount-1) {currentSlide = 0};
	}
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function setCookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y ) {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}
function makeHttpObject() {					// This object will be used to issue http requests to reload fresh copies of the images on each rotation - needed for unruly animated gifs
  try {return new XMLHttpRequest();}
  catch (error) {}
  try {return new ActiveXObject("Msxml2.XMLHTTP");}
  catch (error) {}
  try {return new ActiveXObject("Microsoft.XMLHTTP");}
  catch (error) {}

  throw new Error("Could not create HTTP request object.");
}

