(function($){
		
    $.fn.extend({
        dmlcarusel: function(options) {

            var defaults = {
            			startautoscrolling : true,/*true*/
            			autoscrolling : true,/*true*/
                	time : 6000,
                	fadetime : 3000,
			            tipId :'carouselPrice',
			            tipContentClass :'prodprice'
            };
            var options = $.extend(defaults, options);
            function repeat(str, num) {
        				return new Array( num + 1 ).join( str );
    				}
    				function startanimationn(el){
    					var zoom = 1.4;
    					var velo = 500;
    					var corrtop = 4;
    					
    						var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
								var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
								if (jQuery.browser.msie && (ie55 || ie6)) {
    							corrtop = 12;
    						}
    					
    					var nwidth = (el.width() * zoom)+'px';
    					var nheight = (el.height() * zoom)+'px';
    					var xleft = (((el.width() * zoom) - el.width()) /2 );
    					var xtop = (((el.height() * zoom) - el.height()) );
    					var nleft = el.offset().left - xleft +'px';
    					var ntop = el.offset().top  - xtop - corrtop +'px';
    					
    					//alert($('div',el).eq(0).css('font-size').replace('px',''));
    					el.css({'background-image': 'none','background-color':'#FFF','border':'2px solid #DEDEDE'}).animate({ width: nwidth, height: nheight, top: ntop, left: nleft }, velo);		
    					//alert(el.find('a').html());
    					$('a',el).eq(2).animate({ left: '150px' }, velo);		
    					$('div',el).eq(1).animate({ 'margin': '20px' }, velo);
    					
    					$('img',el).eq(0).animate({ width: parseInt($('img',el).eq(0).width()*zoom)+'px' }, velo);		
    					//$('a',el).eq(1).animate({ 'font-size': ($('a',el).eq(1).css('font-size').replace('px','')*zoom*100)+'px' }, velo);		
    							
    					//alert($('div',el).eq(1).html());
    					//animate({ width: parseInt($('img',el).eq(0).width()*zoom)+'px' }, 1000);		
    					
    				}
            return this.each(
            		function () {
					            // magic!
					            var startautoscrolling = options.startautoscrolling;
					            var autoscrolling = options.autoscrolling;
					            var time = options.time;
					            var fadetime = options.fadetime;
					            var tipId = options.tipId;
					            var tipContentClass = options.tipContentClass;
					            
					            var overContainer = null;
					            var overLi = null;
					            var onmove = false; //quando si è in movimento
					            
					            var $carusel = $(this);
					            var $wrapper = $('> div', this).css({'overflow': 'hidden','z-index':'20','position':'absolute','top':'-96px'}),
					                $slider = $wrapper.find('> ul'),
					                $items = $slider.find('> li'),
					                $single = $items.filter(':first'),
					                singleWidth = $single.outerWidth(true),
					                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
					                currentPage = 0;
					            
					            if($items.length >visible){ 
					                		$slider.width(9999);
					                		pages = Math.ceil($items.length / visible);
									            /* TASKS */
									            // 1. pad the pages with empty element if required
									            if ($items.length % visible != 0) {
									                // pad
									                $slider.append($items.slice(0, visible - ($items.length % visible)).clone().each(function(){ $('img',this).attr("title",' '+$('img',this).attr("title"));}).addClass('cloned'));
									                
									                $items = $slider.find('> li');
									            }
									            
									            // 2. create the carousel padding on left and right (cloned)
									            $items.filter(':first').before($items.slice(-visible).clone().each(function(){ $('img',this).attr("title",' '+$('img',this).attr("title"));}).addClass('cloned'));
									            $items.filter(':last').after($items.slice(0, visible).clone().each(function(){ $('img',this).attr("title",' '+$('img',this).attr("title"));}).addClass('cloned'));
									            $items = $slider.find('> li');
									            
									            var $wrapper1 = $wrapper.clone().appendTo($carusel);    
									    		$wrapper1.css({'z-index':'10'});
									            
									            // 3. reset scroll
									            $wrapper.scrollLeft(singleWidth * visible);
									            $wrapper1.scrollLeft(singleWidth * visible);
									            // 4. paging function
									            function gotoPage(page) {
									            		onmove = true;
									            		if(overContainer) overContainer.remove();
										        			if(overLi) overLi.css({visibility: 'visible'});
									                var dir = page < currentPage ? -1 : 1,
									                    n = Math.abs(currentPage - page),
									                    left = singleWidth * dir * visible * n;
									                
									            
									                if (page > pages) {
									            			$wrapper1.scrollLeft(singleWidth * visible);
									            		}else{
									                	$wrapper1.scrollLeft($wrapper1.scrollLeft()+left);
									            		}
									                $wrapper.fadeOut(fadetime,function(){
									                		$wrapper.show().scrollLeft($wrapper1.scrollLeft());

									                		if (page > pages) {
									                       $wrapper.scrollLeft(singleWidth * visible);
									                       $wrapper1.scrollLeft(singleWidth * visible);
									                        page = 1;
									                    } else if (page == 0) {
									                        page = pages;
									                        $wrapper.scrollLeft(singleWidth * visible * pages);
									                        $wrapper1.scrollLeft(singleWidth * visible * pages);
									                    }
									                    //alert(page);
									                    onmove = false;
									                    currentPage = page;
									                	
									                });
									                
									            }
									            
									            // 5. insert the back and forward link
									            $wrapper.after('<a href="#" class="arrow back">&lt;</a><a href="#" class="arrow forward">&gt;</a>');
									            $('a.back', this).css({position:'absolute'});
									            $('a.forward', this).css({position:'absolute'});
									            // 6. bind the back and forward links
									            $('a.back', this).click(function () {
									                gotoPage(currentPage - 1);
									                $(this).blur();
									                return false;
									            });
									            
									            $('a.forward', this).click(function () {
									                gotoPage(currentPage + 1);
									                $(this).blur();
									                return false;
									            });
									            
									            $(this).bind('goto', function (event, page) {
									                gotoPage(page);
									            });
									            
									            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
									            $(this).bind('next', function () {
									                gotoPage(currentPage + 1);
									            });
									  					$carusel.mouseover(function () {
													        autoscrolling = false;
													    }).mouseout(function () {
													        autoscrolling = true;
													    });
									  					setInterval(function () {
													        if (autoscrolling && startautoscrolling) {
													           $carusel.trigger('next');
													        }
													    }, time);
									    
									  		}else{
									  				//$wrapper.width(singleWidth*$items.length);
									  				//$slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
									  				//$slider.prepend(repeat('<li class="empty" style="width: '+singleWidth+'px" />',1));
									  		}
									  		
									  		//hover per visualizzare il prezzo
									            
						            $('li', this).hover(
						            	function () {
						            		if(onmove) return;
						            		$mLi = $(this);
										    		$carusel.find('#'+tipId).html($mLi.find('.'+tipContentClass).html());
										        //$(this).animate({ border:"3px solid transparent" }, 10 );
										        
										        //$(this).animate({ opacity: 1.0}, 500 );
										        return; //disabilito animazione
										        if(overContainer) overContainer.remove();
										        if(overLi) overLi.css({visibility: 'visible'});
										        overContainer =$(this).find("> div").clone().css({position:'absolute',left:$(this).offset().left,top:$(this).offset().top}).appendTo('body');
										        overLi = $mLi.css({visibility: 'hidden'});
										        overContainer.hover(function () {
													        			autoscrolling = false;
													  						//$(this).animate({ border :"2px solid red" , padding: '10px', width: ($(this).width()*2)+'px', height: ($(this).height()*2)+'px' }, 1000);		
													  						$(this).data('wrapper',$wrapper);
													  						startanimationn($(this));
															    },function () {
															        	autoscrolling = true;
															        	$mLi.css({visibility: 'visible'});
															        	$(this).remove();
															        	$carusel.find('#'+tipId).html('&nbsp;');
															        	//$(this).animate({ opacity: 0.6}, 10 )
															    })
										        },
										    	function () {
										        $carusel.find('#'+tipId).html('&nbsp;');
										        //$(this).animate({ opacity: 0.6}, 10 );
										    	}
										    );
									              
									    
					        }		
					  
            );
        }
    });
})(jQuery);
