var slidecount = 0;
var currentslide = 0;
var nextslide = 1;
var $ss;
var $slides;
var $counter = "";
var timer;
var delay = 6000;

function slidepause(){ // pause show 
//	$("#splayer").css({'display':'block'});
//	$("#spauser").css({'display':'none'});
	$("#spauser").css({'position':'absolute','left':'10000px'});
	$("#splayer").css({'position':'relative','left':'0'});
	clearTimeout(timer);
}

function slideplay(){ // set the show running on auto 
//	$("#splayer").css({'display':'none'});
//	$("#spauser").css({'display':'block'});
	$("#splayer").css({'position':'absolute','left':'10000px'});
	$("#spauser").css({'position':'relative','left':'0'});
	shownext();
}

function slideselect(pauseon){ //jump to a slide and pause  (slide index)
	showslide(pauseon);
	slidepause();
}

function shownext(){ // auto move to next slide
	showslide(nextslide);
	timer=setTimeout("shownext()",delay);
}

function showslide(num){ // show a particular slide (slide index)
	$slides.eq(num).css({'left':'0'});
  	$slides.eq(num).children("div").hide();//hide the current slide's contents
	$slides.not(":eq("+num+"),:hidden").fadeOut(1000);//hide any other visible slides
	$slides.eq(num).fadeIn(1000, function (){ //show the current slide
		$slides.eq(num).children("div").animate({'left':'+=30','opacity':0},0);//offset the contents
		$slides.eq(num).children("div").show(0); // show the contents
		$slides.eq(num).children("div:eq(0)").animate({'opacity':1},{queue:false,duration:800})// animate the contents
		.animate({'left':'-=30'},400);	
		$slides.eq(num).children("div:eq(1)").animate({'opacity':1},{queue:false,duration:1600})// animate the contents
		.animate({'left':'-=30'},800);	
	});
	currentslide = num; // increment the numbers
	if(currentslide == slidecount-1){
		nextslide = 0;
	}else{
		nextslide = currentslide + 1;
	}
	$("#slidecontroller li a").removeClass("selected");// highlight the counter
	$("#slidecontroller li a").eq(currentslide).addClass("selected");
}

function jslide(sid,classes,init,slidedelay){
	if(slidedelay){delay = slidedelay;} // overwrite default delay 
	if ($('#'+sid).length == 0){ // test to see if the slideshow container exists
		alert ("Couldn't find: #"+ sid);
		return false;
	};
	$ss = $('#'+sid);	// grab the container 
	$ss.css({'position':'relative'});
	if ($ss.children("."+classes).length == 0){ // test to see if we have any slides in the container
		alert ("Couldn't find any: ."+ classes);
		return false;
	};
	$slides = $ss.children("."+classes);	// grab the slides 
	//$slides.children("div").animate({'opacity':0},0);//hide the contents of all the slides
	$slides.css({'position':'absolute','top':'0px'});
	slidecount = $slides.length;	//count the slides 
	if(init){currentslide = init}; // overwrite default initial slide
	//$slides.not(":eq("+currentslide+"),:hidden").fadeOut(0);//hide any other visible slides
	if (slidecount > 1){
		$counter = "<ul id='slidecontroller'>";	// create the counter 
		var x=0;
		var summary = "";
		for (x=0; x<slidecount; x++){ // create each controller item
			summary = $slides.eq(x).attr("title");// get the summary from the title attribute of the slide
			$counter = $counter + "<li style='display: block' id=s"+ x +" class='si'><a title='"+ summary + "' href='javascript:slideselect(" + x + ")'>" + (Number(x+1)) + "<\/a><\/li> ";
		}
		$counter = $counter + "<li style='display: block' id='spauser'><a href='javascript:slidepause()'><img src='"+ relpath("docroot/act-on-co2/imgs/promo-pause.png") +"' alt='pause'\/><\/a><\/li>";
		$counter = $counter + "<li style='display: block' id='splayer'><a href='javascript:slideplay()'><img src='"+ relpath("docroot/act-on-co2/imgs/promo-play.png") +"' alt='play'\/><\/a><\/li>";
		$counter = $counter + "<\/ul>";
		//$("#splayer").css({'display':'none'}); // hide the play button
		//showslide(currentslide);// show the initial slide
		//timer=setTimeout("shownext()",delay);	// set the show running
	//	testforsifrload();// see if SIFR is finished loading
		$ss.append($counter);	// add the counter
		$("#splayer").css({'position':'absolute','left':'10000px'}); // hide the play button
		$("#slidecontroller li a").eq(currentslide).addClass("selected");	
		timer=setTimeout("shownext()",delay);	// set the show running
	}
}
