var carouselImages=[];
var rotateDelay=3; //seconds
var rotateInterval;
var playing=false;

function cd_carousel(params,id) {
	$.get('/_ssp/carousel/',params,function(data) { imageHandler(data,id); }, 'json');
}

function cd_carousel_scroll(params,id) {
	$.get('/_ssp/carousel/',params,function(data) { imageHandler(data,id); }, 'json');
}


var rollTimeout;
var appearTimeout;


function initScrollButtons(theDiv,id) {
	$(theDiv).find('a.previous').click(function() {
	
	   var imgIndex = $(theDiv).find('.image_holder').attr( 'title' );
	   var imgPadding = $(carouselImages[id][imgIndex].newImg).css("padding-right");
	   imgPadding = imgPadding == null ? 0 : imgPadding.substring( 0, imgPadding.indexOf( 'px' ) );
	   
	   var imgWidth = $(carouselImages[id][imgIndex].newImg).attr("width");
	   
	   var scrollWidth = imgPadding*2 + imgWidth;
	   
		if (imgIndex>0) {
			imgIndex--;
			$(theDiv).find('.image_holder').animate({
			    left: '+='+scrollWidth
	  		}, 300);
	  		$(theDiv).find('.caption').html(carouselImages[id][imgIndex].caption);
	  	} else {
	  		imgIndex = carouselImages[id].length-4;
	  		$(theDiv).find('.image_holder').animate({
			    left: (0-scrollWidth)*imgIndex
	  		}, 300);
	  		$(theDiv).find('.caption').html(carouselImages[id][imgIndex].caption);
	  	}
  		$(theDiv).find('.image_holder').attr( 'title', imgIndex );
		return false;
	});


	$(theDiv).find('.image_holder').hover(function() {
		clearTimeout(appearTimeout);
		rollTimeout=setTimeout(function() {
		$(theDiv).find('.caption').animate({
			    opacity: 1
	  		}, 300);},500);
	},function() {
		clearTimeout(rollTimeout);
		appearTimeout=setTimeout(function() {
		$(theDiv).find('.caption').animate({
			    opacity: 0
	  		}, 300);
		},500);
	});


	$(theDiv).find('a.next').click(function() {
	
	   var imgIndex = $(theDiv).find('.image_holder').attr( 'title' );
	   var imgPadding = $(carouselImages[id][imgIndex].newImg).css("padding-right");
	   imgPadding = imgPadding == null ? 0 : imgPadding.substring( 0, imgPadding.indexOf( 'px' ) );
	   
	   var imgWidth = $(carouselImages[id][imgIndex].newImg).attr("width");
	   
	   var scrollWidth = imgPadding*2 + imgWidth;
	   
		if (imgIndex<carouselImages[id].length-4) {
			imgIndex++;
			$(theDiv).find('.image_holder').animate({
		   	 left: '-='+scrollWidth
  			}, 300);
	  		$(theDiv).find('.caption').html(carouselImages[id][imgIndex].caption);
  		} else {
  			imgIndex=0;
	  		$(theDiv).find('.image_holder').animate({
			    left: 0
	  		}, 300);
	  		$(theDiv).find('.caption').html(carouselImages[id][imgIndex].caption);
  		}
  		$(theDiv).find('.image_holder').attr( 'title', imgIndex );
		return false;
	});
}

function initButtons(theDiv,id) {
	$(theDiv).find('a.previous').click(function() {
	
	   var imgIndex = $(theDiv).find('.image_holder').attr( 'title' );
	   var imgPadding = $(carouselImages[id][imgIndex].newImg).css("padding-right");
	   imgPadding = imgPadding == null ? 0 : imgPadding.substring( 0, imgPadding.indexOf( 'px' ) );
	   
	   var imgWidth = $(carouselImages[id][imgIndex].newImg).attr("width");
	   
	   var scrollWidth = imgPadding*2 + imgWidth;
	   
		if (imgIndex>0) {
			imgIndex--;
			$(theDiv).find('.image_holder').animate({
			    left: '+='+scrollWidth
	  		}, 300);
	  		$(theDiv).find('.caption').html(carouselImages[id][imgIndex].caption);
	  	} else {
	  		imgIndex=carouselImages[id].length-1;
	  		$(theDiv).find('.image_holder').animate({
			    left: (0-scrollWidth)*(imgIndex)
	  		}, 300);
	  		$(theDiv).find('.caption').html(carouselImages[id][imgIndex].caption);
	  	}
  		$(theDiv).find('.image_holder').attr( 'title', imgIndex );
		return false;
	});


	$(theDiv).find('.image_holder').hover(function() {
		clearTimeout(appearTimeout);
		rollTimeout=setTimeout(function() {
		$(theDiv).find('.caption').animate({
			    opacity: 1
	  		}, 300);},500);
	},function() {
		clearTimeout(rollTimeout);
		appearTimeout=setTimeout(function() {
		$(theDiv).find('.caption').animate({
			    opacity: 0
	  		}, 300);
		},500);
	});


	$(theDiv).find('a.next').click(function() {
	   var imgIndex = $(theDiv).find('.image_holder').attr( 'title' );
	   var imgPadding = $(carouselImages[id][imgIndex].newImg).css("padding-right");
	   imgPadding = imgPadding == null ? 0 : imgPadding.substring( 0, imgPadding.indexOf( 'px' ) );
	   
	   var imgWidth = $(carouselImages[id][imgIndex].newImg).attr("width");
	   
	   var scrollWidth = imgPadding*2 + imgWidth;
		
		if (imgIndex<carouselImages[id].length-1) {
			imgIndex++;
			$(theDiv).find('.image_holder').animate({
		   	 left: '-='+scrollWidth
  			}, 300);
	  		$(theDiv).find('.caption').html(carouselImages[id][imgIndex].caption);
  		} else {
  			imgIndex=0;
	  		$(theDiv).find('.image_holder').animate({
			    left: 0
	  		}, 300);
	  		$(theDiv).find('.caption').html(carouselImages[id][imgIndex].caption);
  		}
  		$(theDiv).find('.image_holder').attr( 'title', imgIndex );
		return false;
	});
}

function imageScrollHandler(item,id) {
	carouselImages[id]=item;
	for (var i in carouselImages[id]) {
		carouselImages[id][i].newImg= new Image();
		carouselImages[id][i].newImg.src=carouselImages[id][i].src;
		carouselImages[id][i].newImg.title=carouselImages[id][i].title;
		carouselImages[id][i].newImg.alt=carouselImages[id][i].title;
		carouselImages[id][i].caption=carouselImages[id][i].caption;		
		/*
if (carouselImages[id][i].link[0]!='') {
			$(carouselImages[id][i].newImg).css('cursor','pointer');
			$(carouselImages[id][i].newImg).click((function(i) { return function() {
				document.location.href=carouselImages[id][i].link[0];
			}})(i));
		}
*/
		var hrefWrapper = $("<a title='"+ carouselImages[id][i].title +"' href='"+ carouselImages[id][i].largeSrc + "' rel='shadowbox[" + id + "]' ></a>")
		$(hrefWrapper).append(carouselImages[id][i].newImg);
		$('#'+id).find('.image_holder').append( hrefWrapper );
	}
	$('#'+id).find('.caption').html(carouselImages[id][0].caption);
	
        Shadowbox.init({ modal: true, animate: true }); 
	return false;
}

function imageHandler(item,id) {
	carouselImages[id]=item;
	
	for (var i in carouselImages[id]) {
		carouselImages[id][i].newImg= new Image();
		carouselImages[id][i].newImg.src=carouselImages[id][i].src;
		carouselImages[id][i].newImg.title=carouselImages[id][i].title;
		carouselImages[id][i].newImg.alt=carouselImages[id][i].title;
		carouselImages[id][i].caption=carouselImages[id][i].caption;	
		/*
if (carouselImages[id][i].link[0]!='') {
			$(carouselImages[id][i].newImg).css('cursor','pointer');
			$(carouselImages[id][i].newImg).click((function(i) { return function() {
				document.location.href=carouselImages[id][i].link[0];
			}})(i));
		}
		
*/
		var hrefWrapper = $("<a title='"+ carouselImages[id][i].title +"' href='"+ carouselImages[id][i].largeSrc + "' rel='shadowbox[" + id + "]' ></a>")
		$(hrefWrapper).append(carouselImages[id][i].newImg);
		$('#'+id).find('.image_holder').append( hrefWrapper );

	}
	$('#'+id).find('.caption').html(carouselImages[id][0].caption);
	Shadowbox.init({ modal: true, animate: true });	
	return false;
}
