jQuery.fn.contentflow = function(options){
	var opts = jQuery.extend({}, jQuery.fn.contentflow.defaults, options);
	var playTimeout;
	var playFunction = function(acf){playTimeout = setTimeout(function(){acf.moveTo(acf.getActiveItem().next); playFunction(acf);}, opts.playSpeed);}
	var acf = jQuery.fn.contentflow.acf;
	return this.each(
		function(){
			var $container = $(this);
			var isFirst = true;
			var timeout;
			var stop = true;
			opts = jQuery.extend({}, opts, {
			
				'onclickInactiveItem' : function(item){return false;}, 
				'onDrawItem' : function(item) {
					clearTimeout(timeout);
					if (isFirst) {
						setTimeout(init, 200);
						isFirst = false;
					} else if(stop){
						timeout = setTimeout(function(){
								stop = false;
								complete();
							}, 2000			
						);
					}		
				}
			
			});
			acf = new ContentFlow($container.get(0), opts);
			
			if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				var imgList = {"imageList": [{"image": opts.pathToGallery+"images/contentflow/blank.jpg", "title": "", "url": opts.pathToGallery+"images/contentflow/blank.jpg", 'height' : 300, 'width' : 300}]};
				imgList.imageList = jQuery.merge(imgList.imageList, list.imageList);
			} else {
				var imgList = list;
			}
			
			$container.parent().append('<div id="contentFlowGallery_tempImages"></div>');
			$('#contentFlowGallery_tempImages').css({'visibility':'hidden', 'height':'0', 'width':'0', 'overflow':'hidden'});
			
			$container.parent().append('<div id="contentFlowGallery_loading"></div>');
			$('#contentFlowGallery_loading').css({'position':'absolute', 'top':0, 'left':0,'height':'600px', 'width':'100%', 'background':'#fff url('+opts.pathToGallery+'images/contentflow/loading2.gif) no-repeat center top', 'zIndex':10000});
			
			function init(){
				$.each(imgList.imageList, function(i, item) {
					
					$('#contentFlowGallery_tempImages').append( '<a href="'+item.url+'"  onclick="return false;" class="item"><img class="content" height="'+item.height+'" width="'+item.width+'" src="'+item.image+'" title="'+item.title+'" /><div class="caption">'+item.title+'</div></a>' );
				});
				setTimeout(function(){
					$('#contentFlowGallery_tempImages a').each(function(i, domElement) {
						acf.addItem(domElement, 'last');
					});}, 1000);
			};
			
			function complete(){
				acf.rmItem(0);
				acf.moveTo(0);
				
				if(opts.prevButton != undefined) $(opts.prevButton).click(function(){acf.moveTo(acf.getActiveItem().pre);});
				if(opts.nextButton != undefined) $(opts.nextButton).click(function(){acf.moveTo(acf.getActiveItem().next);});
				if(opts.playButton != undefined) $(opts.playButton).click(function(){
						if($(opts.playButton).hasClass('paused')){
							playFunction(acf); $(opts.playButton).removeClass('paused');
						} else {
							clearTimeout(playTimeout); $(opts.playButton).addClass('paused');
						}
					}
				);
				var tlo = setTimeout(function(){
					$('#contentFlowGallery_loading').css({'display':'none'});
				}, 1000);
				// Fix per ie6
				if ($.browser.msie && $.browser.version.substr(0,1)<7) {
					acf.rmItem(0);
					$('.slider').css({'background':'transparent url('+opts.pathToGallery+'images/contentflow/slider_white.gif) no-repeat'});
					$('.buttonWrap').css({'marginTop': 90});
				}
				$container.click(function(){clearTimeout(playTimeout); $(opts.playButton).addClass('paused');});
				
				if ($.browser.msie && $.browser.version.substr(0,1)<=7) {
					$('a.item').click(function(){
						var hrefUrl = $(this).attr('href');
						window.location.href = hrefUrl;
						if(window.location.href != hrefUrl){
							windo.open(hrefUrl);
						}
					});
				}
			}
		}
		
	);	
};

jQuery.fn.contentflow.defaults = {	
	reflectionHeight: 0.3,
	scaleFactor: 1.3,
	fixItemSize: false,
	playSpeed: 5000,
	circularFlow : true,
	//fixItemSize : true,
	maxItemHeight : 400,
	'flowDragFriction':0,
	'pathToGallery' : ''
};

// acf deve essere dichiarata come variabile jquery per evitare che il garbage collector la porti via
jQuery.fn.contentflow.acf;


// NOT HERE
// Place at page bottom
//$(document).ready(function(){
	//$('.ContentFlow').contentflow();
	//var cf = new ContentFlow('contentFlow', {scaleFactor:2, fixItemSize:false, reflectionHeight: 1.3, reflectionColor: "#FF0000"});
//});
