/**
 * @author DNAInfo
 */
var pause = new Array();
var shows = new Array();
var currentSlide = 0;
var opts = '';

var startStop = function(num){
	if(pause[num] == 0 || pause[num] == undefined){
		pause[num] = 1;
		$('#startSlideshow'+num).attr('className', 'slideshow-button slideshow-button-on');
		$('#slideshow'+num).cycle("pause");
	}else {
		pause[num] = 0;
		$('#startSlideshow'+num).attr('className', 'slideshow-button slideshow-button-off');
		$('#slideshow'+num).cycle("resume");	  	
	}
};

var previousSlide = function(num){
	var nextSlide = ((currentSlide - 1) < 0) ? $("#jcarouselSlideshow" + num + " li").length - 1: parseInt(currentSlide) - 1;
	$('#slideshow'+num).cycle(nextSlide, 'scrollRight');
	currentSlide = nextSlide; // reset currentSlide
	
	/*var currentSlide = 0;
	currentSlide = ((opts.currSlide - 1) < 0) ? opts.slideCount - 1: opts.currSlide - 1; // opts is set in constructor
	$('.slideshow-panel').cycle(currentSlide, 'scrollRight');*/
};

var nextSlide = function(num){
	$('#slideshow'+num).cycle('next');
	
};

var gotoSlide = function(slideNum, slideshowNum){
	currentSlide = slideshowNum;
	$('#slideshow'+slideshowNum).cycle(slideNum, 'scrollLeft');
}

var onBefore = function() {
	var imgId = this.getElementsByTagName('img');
	var id = imgId[0].id;
	var num = id.slice(10); // reads character from html id which is printed as [id]slide[index]
	var indexNum = id.slice(10); // reads the last two characters of the id string to arrive at the slide index
	currentSlide = num;
	var storyId = id.slice(0,id.length-6);
	var url = document.location.href;
	var pos = url.search(/.com/i);
	var path = url.slice(pos+4, url.length);
	
	var showId = $(this).parent().attr("id"); // grabs parent id of slideshow which is firing the onBefore function
	var showNum = showId.charAt(showId.length-1); // truncates the id to arrive at the actual slideshow array value
	slideCounter(showNum, indexNum);

	//AMB ad server project

	
	try
	  {
		/*var element = document.getElementById('doubleclicklink');
		var src = element.getAttribute("src");
		//alert("old src=" + src);
		var randnum = Math.floor(Math.random()*1000000);
		var srcnew = src.replace(/ord=\d+/g, "ord=" + randnum );*/
		
		/*document.write
		('<script language="JavaScript1.1" src=' + srcnew + ' type="text/javascript"><\/script>');*/
		//alert("new src=" + srcnew);
		//document.getElementById('doubleclicklink').setAttribute("src", srcnew);
	
		/* var pn = element.parentNode;
		 element.parentNode.removeChild(element);
		 
		 var new_script = document.createElement('script');
		 new_script.src = srcnew;
		 pn.appendChild(new_script);*/
		
	  }
	catch(err)
	  {
	  txt="There was an error on this page.\n\n";
	  txt+="Error description: " + err.description + "\n\n";
	  txt+="Click OK to continue.\n\n";
	  alert(txt);
	  }
	

	
	try {
		if(pageTracker)
			pageTracker._trackPageview(path + '/slideshow/' + storyId);
		} catch(err) {}	
	
};

var onAfter = function() {
	var nodes = this.childNodes;
	var headline = this.getElementsByTagName('h3');
	var desc = this.getElementsByTagName('p');
};

var startSlideshow = function(num){
	opts = $('#slideshow'+num).cycle({
			fx: 'scrollLeft', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			pause: 0, 
			timeout: 7000,
			speed: 1500,
			before: onBefore 
	}).data('cycle.opts');
	
	//The state object contains numerous properties, such as:
	//opts.currSlide // zero based slide index
	//opts.slideCount // total number of slides
	
};

var stopSlideshow = function(num){
	$('#slideshow'+num).cycle({before: null});	
	$('#slideshow'+num).cycle('stop');
};

var setupSlideshow = function(){
	for(var i = 0; i < $(".slideshow-container").length; i++){
		jQuery('#jcarouselSlideshow'+i).jcarousel();
		startSlideshow(i);
		if (i == 0){
			pause[i] = 1;
		} else {
			pause[i] = 0;
		}
		startStop(i);		
	}
	$(".slideshow-container img").css("cursor", "pointer"); 
};

var killSlideshow = function(){
	for(var i = 0; i < $(".slideshow-container").length; i++){
		stopSlideshow(i);
	}
};	

var slideCounter = function(slideshowNum, slideIndex)
{		var check;
		//calling changeAd() on Slideounter to By pass the double call issue with onBefore 	changeAd();
		if( adblock === null){
		changeAd(); }
		slideIndex = parseInt(slideIndex)+1; //parse the slidenumber from text to base 10 number and add one to it.	
		document.getElementById('slideshow_index_'+slideshowNum).innerHTML = slideIndex + ' ';
};
