var currentTab = 1;
var xSlides = 10;
var totalSlides = 0;
//var currentSlide = 1;
var currentSlide = Math.round(Math.random() * (9)) + 1;
var pauseDuration = 5;
var sh_timer;
//these are the ids of the slideshow containers / needed to determine if whether or not they contain an image
var slides_cb = new Array("tmpl_cbss_img_1_cb","tmpl_cbss_img_2_cb","tmpl_cbss_img_3_cb","tmpl_cbss_img_4_cb","tmpl_cbss_img_5_cb","tmpl_cbss_img_6_cb","tmpl_cbss_img_7_cb","tmpl_cbss_img_8_cb","tmpl_cbss_img_9_cb","tmpl_cbss_img_10_cb");
var activeSlides = new Array();

function setCursor(btn) {
  btn.style.cursor = "pointer";
}


function ss_playSlide(thisBtn) {
	if (totalSlides > 1) {
		if (thisBtn) {
			$get("sh_play").className = "current";
			$get("sh_pause").className = "";
		}
		ss_nextSlide();
		sh_timer = setTimeout("ss_playSlide()", pauseDuration*1000);
	} else {
		return;
	}
}

function ss_pauseSlide(thisBtn) {
	if (thisBtn) {
		$get("sh_play").className = "";
		$get("sh_pause").className = "current";
	}
  clearTimeout(sh_timer);
}

function ss_prevSlide() {
  var sec = $get("slideshow");
  var cap = $get("ss_captions");
  //ss_pauseSlide();
  currentSlide > 1 ? currentSlide-- : currentSlide = xSlides;
 	if (sec != null) {
		if (activeSlides[currentSlide-1]==true) {
			sec.className = "ss_slide_" + currentSlide;
			cap.className = "ss_label_" + currentSlide;
		} else {
			ss_prevSlide();	
		}
	}
}

function ss_nextSlide() {
  var sec = $get("slideshow");
  var cap = $get("ss_captions");
  //ss_pauseSlide();
  currentSlide < xSlides ? currentSlide++ : currentSlide = 1;
  if (sec != null) {
 		if (activeSlides[currentSlide-1]==true) {
			sec.className = "ss_slide_" + currentSlide;
			cap.className = "ss_label_" + currentSlide;
		} else {
			ss_nextSlide();
		}
  }
}

//check for empty image content blocks to romove them from the slideshow
function getNumSlides() {
	for (var i=0; i<slides_cb.length; i++) {
		var thisSlide = $get(""+ slides_cb[i] +"");
		if (thisSlide != null) {
			var hasSlide = thisSlide.getElementsByTagName('img')[0];
			if (hasSlide) {
				activeSlides[i] = true;
				totalSlides++;
			} else {
				activeSlides[i] = false;
			}
		}
	}
}

function ss_initContainer() {
	var sec = $get("slideshow");
	var cap = $get("ss_captions");
	if(sec != null) {
	    if (typeof OneWeb.Admin === "undefined") {
			getNumSlides();
	        if (activeSlides[currentSlide-1]==true) {
				sec.className = "ss_slide_" + currentSlide;
				cap.className = "ss_label_" + currentSlide;
			} else {
				sec.className = "ss_slide_1";
				cap.className = "ss_label_1";
			}
		} else {
	        //show all image and caption blocks when in Admin
			sec.className = " ss_slide_1 ss_slide_2 ss_slide_3 ss_slide_4 ss_slide_5 ss_slide_6 ss_slide_7 ss_slide_8 ss_slide_9 ss_slide_10";
	    	cap.className = " ss_label_1 ss_label_2 ss_label_3 ss_label_4 ss_label_5 ss_label_6 ss_label_7 ss_label_8 ss_label_9 ss_label_10";
	    }    
	}
}
function ss_initSlides() {
	if (typeof OneWeb.Admin === "undefined") {
		
		var sh_play = $get("sh_play");
		var sh_pause = $get("sh_pause");
		var sh_prev = $get("sh_prev");
		var sh_next = $get("sh_next");
		$get("sh_play").className = "current";
		sh_play.onmouseover = function() { setCursor(sh_play); return false; }
		sh_pause.onmouseover = function() { setCursor(sh_pause); return false; }
		sh_prev.onmouseover = function() { setCursor(sh_prev); return false; }
		sh_next.onmouseover = function() { setCursor(sh_next); return false; }

		sh_play.onclick = function() { ss_playSlide(true); return false; }
		sh_pause.onclick = function() { ss_pauseSlide(true); return false; }
		sh_prev.onclick = function() { ss_prevSlide(); return false; }
		sh_next.onclick = function() { ss_nextSlide(); return false; }

		ss_playSlide();
	}
}
OneWeb.Util.appendInitEvent(ss_initContainer);
OneWeb.Util.appendLoadEvent(ss_initSlides);