	var TwirlBox = {
		list: [],
		total: 0,
		target: 0,
		stun: false,
		add: function(html){	
			this.list.push(new this.item(html));
		},
		
		item: function(html){
			this.id = TwirlBox.list.length+1;
			this.elem = jQuery("<div class='item' rel='"+(TwirlBox.list.length+1)+"'><a href='##'>"+html+"</a></div>").appendTo("#scene");
			TwirlBox.total++;
		},
		
		init: function(){		
			for(var i in TwirlBox.list){
				var item = TwirlBox.list[i];
					item.s_rotation = item.rotation =  ((Math.PI / 3) * 2 ) * i;
					item.elem.show();
			}
			TwirlBox.target = TwirlBox.total;
		},
		
		tween: function(){
			TwirlBox.stun = true;
			var stop = false;
			for(var i in TwirlBox.list){
				var item = TwirlBox.list[i];
				item.rotation += 0.09;
				if(Math.PI * 2 < item.rotation) item.rotation -= Math.PI * 2;
				
				var f_rotation = item.rotation - 2.6;
				if(Math.PI * 2 < f_rotation) f_rotation -= Math.PI * 2;				
				var scale = (Math.cos(f_rotation)+1) * ((1-0.5) * 0.5)+0.5;	
				var width = height = 170 * scale;				
				jQuery("img",item.elem).css({width:width+'px', height:height+'px'})			
				var x = 215 + ( (Math.cos(item.rotation) * (80*(scale*2))) - (width*0.5) );
				var y = 188 + ( (Math.sin(item.rotation) * (80*(scale*2))) - (height*0.5) );	
				jQuery(item.elem).css({left:x+"px", top:y+"px"});
				if(item.id == TwirlBox.target){
					//jQuery(item.elem).css({zIndex: 1});
				}
				
				if(item.id == TwirlBox.target && item.rotation.toFixed(1) == 2.6 ){ 
					var id = jQuery("img", item.elem).attr("rel");
					jQuery("#twirl-content div.item").fadeOut(300);
					jQuery("#twirl-content div.item[rel="+id+"]").fadeIn(300);
					/*
					jQuery.get("/workcase", {id:id}, function(data){
						
					});
					*/
					stop = true;
				}
			
			}
			if(stop){
				TwirlBox.stun = false;
				TwirlBox.stop();

			}
			
			
		},
		
		

		stop: function(){
			clearInterval(TwirlBox.tt);
		},
		start: function(){
			this.stop();
			
				var ta = TwirlBox.target;
				if(ta >= TwirlBox.total) ta = 0
				var TW = TwirlBox.target-3;
				if(TW < 0) TW += TwirlBox.total;

				jQuery(TwirlBox.list[TW].elem).fadeIn(1000);
				jQuery(TwirlBox.list[ta].elem).fadeOut(600);
			
			TwirlBox.tt = setInterval(function(){ TwirlBox.tween() }, 10);
		},
		rotate_: function(id){
			//jQuery("#scene div.item").css({zIndex: 0});
			TwirlBox.target = id;
			this.start();
		},
		rotate: function(){
			//if(TwirlBox.stun) return false;
			var target = TwirlBox.total;
			if(TwirlBox.target){
				if(TwirlBox.list[TwirlBox.target-2]){
					var target = TwirlBox.target-1;
				}else{
					var target = TwirlBox.total;
				}
			}
			TwirlBox.rotate_(target);
		}
		
	}

jQuery(function(){
	
	TwirlBox.init();
	TwirlBox.rotate();
	//TwirlBox.start();
	jQuery("#scene a.rotate").bind("click", function(){
		TwirlBox.rotate();
		return false;
	});

	jQuery("#scene div.item").bind("click", function(){
		TwirlBox.rotate_(jQuery(this).attr("rel"));
	});
	
	/*
	jQuery("#scene").bind("mouseover", function(){
		TwirlBox.start();
	});
	jQuery("#scene").bind("mouseout", function(){
		TwirlBox.stop();
	});
	*/
});
