(function($){
	$.fn.tinycircleslider=function(options){
		var defaults={
			interval:false,intervaltime:1000,snaptodots:false,hidedots:true,radius:140,lightbox:false,callback:null};var options=$.extend(defaults,options);var oCircle=$(this);var oCircleX=oCircle.outerWidth();var oCircleY=oCircle.outerHeight();var oThumb=$('.thumb',oCircle)[0];var oThumbX=$(oThumb).outerWidth();var oThumbY=$(oThumb).outerHeight();var oOverview=$('.overview',oCircle);var oDot={};var oTimer,oTimer2,oTimer3;var oChildren=oOverview.children();var oLinks=$('a',oChildren);var iPageX=$(oChildren[0]).outerWidth(true);var iChildsLength=oChildren.length,iOrginalAngle=0,iCounter=0,iCurrent=0,iFramerate=1;return this.each(function(){																																																																																																																																																																																																																																																																																																																														initialize();});function initialize(){
				setCircular();oOverview[0].style.width=iPageX*oChildren.length+'px';if(options.snaptodots){setDots()}
				;gotoSlide(0);iOrginalAngle=0;setEvents();if(options.interval){setTimer(true)}
	};
	
	function setEvents(){
		//oThumb.onmousedown=start;
		if(oLinks.length>0){
			$('.overlay',oCircle).css({'cursor':'pointer'}).click(function(){
				if(options.lightbox){$(oLinks[iCurrent]).trigger('click');}
				else{location.href=oLinks[iCurrent].href;}
				return false;
			});
		}
		
		if(options.snaptodots){
			oDot.click(function(){
				if(iCounter==0)gotoSlide($(this).text()-1)
				
				if ($(this).text() == 2) {	
					$('div.dot-2').fadeOut(500);
					setTimeout("$('div.dot-1').fadeIn(400);",500);
					setTimeout("$('div.cambia').fadeIn(200);",500);
				} else {
					$('div.cambia').fadeOut(300);
					$('div.dot-1').fadeOut(500);
					setTimeout("$('div.dot-2').fadeIn(200);",600);
				}	
			});
		}
	};
	
	function start(oEvent){
		clearTimeout(oTimer3);
		//$(document).mousemove(drag);
		document.onmouseup=end;
		oThumb.onmouseup=end;
		if(options.snaptodots&&options.hidedots){
			oDot.stop(true,true).fadeIn('slow');
		}
		return false;
	};
	
	function end(oEvent){
		$(document).unbind('mousemove');
		document.onmouseup=null;
		oThumb.mouseup=null;
		clearTimeout(oTimer2);
		if(options.snaptodots){
			if(options.hidedots){
				oDot.stop(true,true).fadeOut('slow');
			}
			calculateMove();
		};
		if(options.interval){setTimer();}
		return false;
	};
	
	function setCircular(){
		oOverview.append($(oChildren[0]).clone());
		oChildren=oOverview.children();
	};
	
	function setTimer(bFirst){
		oTimer3=setTimeout(function(){
			gotoSlide(iChildsLength*Math.random(),true)
		},(bFirst?50:options.intervaltime));
	};
	
	function setDots(){
		oDot=$('.dot',oCircle);
		var posX,posY;
		var oDotX=oDot.outerWidth(),oDotY=oDot.outerHeight();
		var iLength=(oChildren.length-1);
		var angle=(360/iLength)*(Math.PI/180);
		for(var i=1;i<=iLength;i++){
			posY=Math.round(-Math.cos(i*angle-angle)*options.radius+(oCircleY/2-oDotY/2));
			posX=Math.round(Math.sin(i*angle-angle)*options.radius+(oCircleX/2-oDotX/2));
			oDot.clone().addClass('dot-'+i).css({left:posX,top:posY}).html('<span>'+i+'</span>').insertAfter(oDot);
		}
		oDot.remove();
		oDot=$('.dot',oCircle);
	};
	
	function gotoSlide(iNum,bTimed){
		
		// Definisco il framerate in base al browser utilizzato
		if (navigator.appName == 'Microsoft Internet Explorer')
			speed = 35;
		else
			speed = 50;
		
		var angle=Math.ceil(iNum)*(360/iChildsLength)*(Math.PI/180);
		
		// Ridefinisco l'angolo iniziale per l'area Comunicazione
		if (document.getElementById('area').value == 'comunicazione') {
			iOriginalDegree = Math.round(iOrginalAngle*180/Math.PI) ;
			if (iOriginalDegree == -360)
				iOrginalAngle = 0;
		}
		
		iFramerate=Math.max(1,Math.round(Math.abs((angle*speed)-(iOrginalAngle*speed))/10));
		var iRatio=((angle*100)-(iOrginalAngle*100))/iFramerate;
		stepMove(iRatio,bTimed);
	};
	
	function drag(oEvent){;
		var oPos={x:oEvent.pageX-oCircle.offset().left-(oCircleX/2),y:oEvent.pageY-oCircle.offset().top-(oCircleY/2)}
		iOrginalAngle=Math.atan2(oPos.x,-oPos.y);setCSS(iOrginalAngle);
		return false;};
		
	function calculateMove(){
		var iDropped=(iOrginalAngle*180/Math.PI)/(360/iChildsLength)*100;
		var iDot=Math.round((iOrginalAngle*180/Math.PI)/(360/iChildsLength))*100;iFramerate=Math.max(1,Math.round(Math.abs(iDot-iDropped)/10));
		var iStep=((iOrginalAngle*iDot/iDropped*100)-(iOrginalAngle*100))/iFramerate;
		oTimer2=setTimeout(function(){stepMove(iStep,false);},100);
	};
	
	function stepMove(iStep,bInterval){
		iCounter++
		
		// Differenzio il numero di step in base al browser
		if (navigator.appName == 'Microsoft Internet Explorer')
			maxCounter = 11;
		else
			maxCounter = 16;
		
		if (iCounter >= maxCounter)
			setSection();
		
		// Differenzio la direzione del thumb in base alla sezione in cui mi trovo
		if (document.getElementById('area').value == 'web')
			var angle=iOrginalAngle+((iCounter*iStep)/100);
		else {
			if ((document.getElementById('section').value == 'c') || ((document.getElementById('section').value == 'w') && (iCounter == maxCounter))) 
				var angle=iOrginalAngle-((iCounter*iStep)/100);
			else
				var angle=iOrginalAngle+((iCounter*iStep)/100);
		}
		
		setCSS(angle,iCounter==iFramerate);
		if(iCounter<iFramerate){
			oTimer2=setTimeout(function(){
				stepMove(iStep,bInterval);
			},50);
		} else {
			iCounter=0;
			iOrginalAngle=angle;
			if(bInterval){setTimer();}
		}
	};
	
	function setCSS(angle,bFireCallback){
		var iDegrees=Math.round(angle*180/Math.PI)
		iDegrees=iDegrees<0?iDegrees+360:iDegrees;oOverview[0].style.left=-(iDegrees/360*((iPageX*(iChildsLength))))+'px';
		oThumb.style.top=Math.round(-Math.cos(angle)*options.radius+(oCircleY/2-oThumbY/2))+'px';
		oThumb.style.left=Math.round(Math.sin(angle)*options.radius+(oCircleX/2-oThumbX/2))+'px';
		iCurrent=(Math.min(iChildsLength-1,Math.round(iDegrees/360*iChildsLength)));
		if(typeof options.callback=='function'&&bFireCallback)options.callback.call(this,oChildren[iCurrent],iCurrent);
	};
};
})(jQuery);


// Funzione che specifica la sezione indicata dall'oggetto rotation slider
function setSection() {
	if (document.getElementById('section').value == 'w')
		document.getElementById('section').value = 'c';
	else if (document.getElementById('section').value == 'c')
		document.getElementById('section').value = 'w';
}
