var numberOfCarouselItems = jQuery('.marktimage ul li').size();
for(i = 0; i < numberOfCarouselItems; i++) {
jQuery('#dots').append('<img class="dot" id="dot' + i + '" src="/wp-content/themes/comet/images/becadotgrey.png" onclick="animateToImage(' + i + '); _gaq.push([\'_trackEvent\', \'Carousel\', \'Switched item\', \'Image ' + (i+1) + '\']);" />');
}
var animationtime = 5000;
var progressbartime = 1000; //in ms
var speed = 600;
var currentCarouselItem = 0;
var animationTimer;
var progressTimer;
toggleDot(currentCarouselItem);
function toggleDot(index) {
jQuery('.dot').attr('src', '/wp-content/themes/comet/images/becadotblue.png');
jQuery('#dot' + index).attr('src', '/wp-content/themes/comet/images/becadotwhite.png');
}
function animateToImage(imageIndex) {
toggleDot(imageIndex);
var animateTo = 338*imageIndex;
jQuery('.marktimage ul').animate({marginLeft: -animateTo}, speed);
clearTimeout(animationTimer);
clearTimeout(progressTimer);
jQuery('#progressbar').width('0px');
progressTimer = setTimeout('animateProgressbar()', progressbartime);
currentCarouselItem = imageIndex;
animationTimer = setTimeout('showNextImage()',animationtime);
}
function showNextImage() {
animationTimer = setTimeout('showNextImage()',animationtime);
currentCarouselItem++;
if(currentCarouselItem >= numberOfCarouselItems){
jQuery('.marktimage ul').animate({marginLeft: '0'}, speed);
currentCarouselItem = 0;
} else {
jQuery('.marktimage ul').animate({marginLeft: '-=338'}, speed);
}
toggleDot(currentCarouselItem);
setTimeout('animateProgressbar()', progressbartime);
}
var stepsize = Math.ceil(jQuery('#progressbarcontainer').width() / (Math.round(animationtime / progressbartime)-1));
function animateProgressbar() {
  if(jQuery('#progressbar').width() < jQuery('#progressbarcontainer').width()) {
    jQuery('#progressbar').animate({width: '+='+stepsize}, 50, 'linear');
    progressTimer = setTimeout('animateProgressbar()', progressbartime);
    jQuery('#progressbarcontainer').height(jQuery('#dots').height()+4 + 'px');
  } else {
    jQuery('#progressbar').width('0px');
  }
}
jQuery(document).ready(function() {
animationTimer = setTimeout('showNextImage()',animationtime);
progressTimer = setTimeout('animateProgressbar()', progressbartime);
//jQuery('#progressbar').animate({width: '338'}, animationtime, 'linear');
});

