function JCarouselContainer()
{
	/* Closure for this */
	var thisObject = this;
	
	this.init = function (containerDiv, selectedItem, start)
	{
		this.containerDiv = document.getElementById(containerDiv);
		
		if(this.containerDiv)
		{
			this.selectedItem =  selectedItem;		
			this.start = (start != undefined) ? start : selectedItem * 2 -1 ;  
			/* Create captions div container */
			this.captionDiv = document.createElement('div');
			this.captionDiv.setAttribute('id',containerDiv+'_caption');
			this.captionDiv.setAttribute('class','caption');
			this.captionDiv.setAttribute('className','caption');
			this.captionDiv.innerHTML = '';
			this.containerDiv.appendChild(this.captionDiv);
			for (var i = 0 ; i < selectedItem; i++)
			{
				var liBefore = document.createElement('li');
				var liAfter = document.createElement('li');
				jQuery('#'+containerDiv+' ul').append(liBefore); 
				jQuery('#'+containerDiv+' ul').prepend(liAfter);
			} 
			
			var options = {
							scroll:1, 
							animation : '150', 
							start: this.start, 	
							itemLoadCallback: { 	 		
							onBeforeAnimation: thisObject.onLoadImage
						}
					};
			
			if(containerDiv == 'mySelectionImageFlow')
			{
				options.initCallback = mySelectionLoaded;
			}
			
			jQuery('#'+containerDiv+' ul').jcarousel(options);
		}
		
		return this;						
		
	};
	
	this.onLoadImage = function(carousel, item, idx, state)
	{		 
		/* Display new caption */
		if (carousel != undefined)
			thisObject.carousel = carousel;
		var img = thisObject.carousel.get(thisObject.carousel.first + thisObject.selectedItem);
		var j = 0;
		for(j = thisObject.carousel.first -1 ; j <= thisObject.carousel.last +1; j++)
		{	
			var tmp = thisObject.carousel.get(j);
			if (tmp.children().length > 0)
				thisObject.setOpacity(thisObject.carousel.get(j),(j == thisObject.carousel.first + thisObject.selectedItem) ? 10: 3);
		}
		if (img.children()[0] != undefined)
		{
			var tmp_href = img.children()[0].href;
			var tmp_alt = img.children()[0].lastChild.alt;
				
			var caption = '<a href="' + tmp_href + '">'
						 + tmp_alt
						 + '</a>';					 		
			thisObject.captionDiv.innerHTML = caption;
		}
		else
			thisObject.captionDiv.innerHTML = '';
				
	};
	
	this.setOpacity = function(object, value)
	{					
			object.css('opacity',value/10);			
			if (value == 10)
				jQuery('img.image', object).css('border','1px solid #ccff00');
			if (value == 3)
				jQuery('img.image', object).css('border','0px solid #ccff00');
			object.css('filter','alpha(opacity=' + value*10 + ')');
			object.trigger('change');	
	};
	
	this.removeItemInTheMiddle = function()
	{
		/*var j = thisObject.carousel.first;
		var last = thisObject.carousel.last;
		do 
		{
			if (thisObject.carousel.get(j) == item)
			{*/				
				thisObject.carousel.remove( thisObject.carousel.first + thisObject.selectedItem);
				if (thisObject.carousel.first + thisObject.selectedItem == thisObject.carousel.last - thisObject.selectedItem)				            
				{
					thisObject.carousel.animate(parseInt(thisObject.carousel.list.css(thisObject.carousel.lt)) + 95,false);
					thisObject.carousel.first += -1; 				    
					thisObject.carousel.last += -1;
				}
				thisObject.carousel.buttons();
				thisObject.onLoadImage();
				/*break;
			}
			j++;
		}while (j < (last + 1))*/
				
	}
		
}

function initializeJCarousel(containerDiv, selectedItem, start)
{
	var container = new JCarouselContainer();
	return container.init(containerDiv, selectedItem, start);
}

