window.addEvent("domready", function() {
	dp.SyntaxHighlighter.HighlightAll("usage");
	
	var thumbPageCounter = 0;

	var newsCarousel = new iCarousel("news_content", {
		idPrevious: "news_previous",
		idNext: "news_next",
		idToggle: "undefined",
		item: {
			klass: "news_item",
			size: 455
		},
		animation: {
			type: "scroll",
			duration: 1000,
			amount: 1
		}
	});
	
	var numNewsItems = 15;
	for (var i = 1; i <= 15; i++) {
    if ($("thumb" + i)) {   
      addEvent(i);
    }
    else {
      numNewsItems = parseInt(i) - 1;
      break;
    }
  }
  
  var startIndex = 1;
  var endIndex = startIndex + 3;
  
  j('#newsThumbsIndex').html(startIndex + " - " + endIndex + " of " + numNewsItems);
		
	//Define the animation speed for the Carousel
  var speed = 200;
  
  //Define the cell width
  var cellWidth = 114;
  j('#thumbNextLink').click(function(){
    var marginLeft = parseInt(j('#news_frame').css("marginLeft").replace("px", "")) - (cellWidth * 4)
    if ((marginLeft - (cellWidth * 4)) < (-1 * cellWidth * numNewsItems)) {
      marginLeft = (-1 * cellWidth * numNewsItems) + (cellWidth * 4);
    }
    
    startIndex = (marginLeft / (-1 * cellWidth)) + 1;
    endIndex = startIndex + 3;
    j('#newsThumbsIndex').html(startIndex + " - " + endIndex + " of " + numNewsItems);
    
    //As the rest of our carousel is hidden, lets move it's margin left until it's in view
    //We use the jQuery animate() function give this movement a nice smooth feel
    j('#news_frame').animate({marginLeft:marginLeft + 'px'}, speed);
    return false;
  });
  j('#thumbPrevLink').click(function(){
    var marginLeft = parseInt(j('#news_frame').css("marginLeft").replace("px", "")) + (cellWidth * 4);
    if (marginLeft > 0) { 
      marginLeft = 0; 
    }
    
    startIndex = (marginLeft / (-1 * cellWidth)) + 1;
    endIndex = startIndex + 3;
    j('#newsThumbsIndex').html(startIndex + " - " + endIndex + " of " + numNewsItems);
    
    //And now lets move back to the start of the Carousel
    j('#news_frame').animate({marginLeft:marginLeft + 'px'}, speed);
    return false;
  });	
		
	//$("thumbNextLink").addEvent("click", function(event){new Event(event).stop();newsThumbsCarousel.goTo(++thumbPageCounter)});
	//$("thumbPrevLink").addEvent("click", function(event){new Event(event).stop();newsThumbsCarousel.goTo(--thumbPageCounter)});
	
	function updateCellColors(index) {
    for (var j=1; j <= numNewsItems; j++) {
      updateCellColor(j, "#eeffee");
    }
    updateCellColor(index, "#aaffaa");
  }
  
  function updateCellColor(index, color) {
    $("thumb" + index + "cell").setStyle("backgroundColor", color);
  }
  
  function addEvent(index) {
    $("thumb" + index).addEvent("mouseover", function(event){new Event(event).stop();newsCarousel.goTo(parseInt(index) - 1); updateCellColors(index)});
  }

});

function truncateThumbTitle(title) {
  if (title.length > 20) {
    title = title.substr(0, 20);
    title = title.substr(0, title.lastIndexOf(' ')) + "...";
  } 
  return title;
}

