function PrimaryCarousel() {
	var data;
	var detailedView = new Array();

	/**
	 * Called by the carousel code when it wants to have its pictures filled in. Uses carousel.add to actually
	 * add them.
	 * 
	 * @param carousel The carousel that called the callback. Images will be filled in there.
	 * @param state The state in which the carousel is. This function will only work when state == 'init'
	 */
	this.itemLoadCallbackFunction = function(carousel, state) {
		if (state != 'init')
			return;
			
		data.getEntries().each(function(i, entryData) {
		  var img = entryData.images.first();
		  var summary = entryData.summary;
		  
		  carousel.add(i, createImgWithSummary("#entry_" + (i + 1), entryData));		
		});		
	}
	
	/**
	 * Function to be called when link is clicked to open a dialog with the detailed view.
	 *  
	 * @param elementName jQuery selector identifying the DOM element to show inside the dialog
	 */
	var openDetails = function(elementName, data) {
		$(elementName).dialog({ modal: true, width: 750, height: 530, draggable: false, resizable: false });
  	    detailedView.push(new DetailedView($(elementName), data));  
	}
	
	/**
	 * Takes an img node and wrapps it into a link (<a>). The <a> gets the additional class of "thickbox" and
	 *  gets a specially constructed link that will open a thickbox window.
	 * 
	 * @param img Img DOM element to be wrapped. Element is cloned so it will still be visible on its old position.
	 * @param linkaddress Address where the link should point to.
	 * @returns A link surround and img.
	 */
	var createImgLink = function(img, linkaddress, data) {
		var link = $('<a></a>').attr("href", "#").click(function() {openDetails(linkaddress, data);});		                       
		var imgClone = $(img).clone();
		imgClone.addClass("softenOnHover");
		link.append(imgClone);
		
		return link;
	}	
	
	/**
	 * Creates a div with class  "summary" and wraps the given html with it.
	 * 
	 * @param html HTML to be wrapped by the newly created <div>. The HTML is moved, not copied.
	 * @returns The div including the wrapped html.
	 */
	var createSummaryDiv = function(html) {
		var div = $('<div></div>').addClass("summary");
		
		div.append(html);
		
		return div;
	}
	
	/**
	 * Takes an img, a link and some HTML and uses createImgLink() and createSummaryDiv() to wrap those. At the end
	 *  everything is combined into a div with class "entry".
	 *  
	 * @param img Img that will then be wrapped into a link.
	 * @param linkaddress Address where the link will point to.
	 * @param summary Text to be shown above the picture on mouse over
	 * @returns A div wrapping all three of the above.
	 */
	var createImgWithSummary = function(linkaddress, data) {
		var div = $('<div class="entry"></div>');
		
		div.append(createSummaryDiv(data.summary))
		   .append(createImgLink(data.images.first(), linkaddress, data));
		   
		return div;
	}			

	/**
	 * Initializes jCarousel to use the specified DOM element to display the thumbnail picture carousel.
	 * 
	 * @param  carouselElement jQuery selector identifying the DOM element where to append the carousel.
	 * @param  dataElement jQuery selector identifying the DOM element where to find the images. 
	 */
	this.init = function(carouselElement, dataElement) {
	  data = new SliderData();
	  data.read($(dataElement));	
	  
	  jQuery(carouselElement).jcarousel({
	    scroll: 1,
	    // auto: 1,
	    //wrap: 'last',
	    size: data.getEntries().size() - 1, 
	    // visible: 5,   
	    itemLoadCallback: this.itemLoadCallbackFunction
	  });	
	} 	
}
