var RainboMenu = function(){};

RainboMenu.prototype = {
	index : 0,
	centerX : 0,
	radius : 1,
	b : 0,
	angleArr : [],
	itemLength : 0,
	targetClassName : "",
	prevClassName : "#rainbow_prev",
	nextClassName : "#rainbow_next",
	
	timerId:null,
	
	init : function(targetClassName, options){
		$("#caption").hide();
		
		if("index" in options)this.index = options.index;
		if("centerX" in options)this.centerX = options.centerX;
		if("radius" in options)this.radius = options.radius;
		if("b" in options)this.b = options.b;
		if("angleArr" in options){
			this.angleArr = options.angleArr;
			this.itemLength = options.angleArr.length;
		}else if("itemLength" in options){
			this.itemLength = options.itemLength;
			this.angleArr = new Array(this.itemLength);
			
			var w = 180 / this.itemLength;
			for(var i = 0; i < this.itemLength; i++){
				this.angleArr[i] = i * w;
			}
		}
		
		this.targetClassName = targetClassName;
		
		var index = this.index;
		var angleArr = this.angleArr;
		var len = this.itemLength;
		var radius = this.radius;
		var centerX = this.centerX;
		var b = this.b;
		
		$.each($(this.targetClassName), function(i){
			var idx = Math.floor((i + index) % len);
			var x0 = Math.cos(angleArr[idx] *  Math.PI / 180);
			var x = Math.floor((x0 * radius * 2 + centerX));
			//var y = Math.floor(Math.pow(x0, 2) * (radius * 2)) + b;
			var y = Math.floor(Math.pow(x0, 2) * (radius * 2.5)) + b;
			
			$(this).css({left:x, top:y});
			
			
			$(this).bind("mouseover", function(){
				var $img = $( $(this).find("img").get(0) );
				$("#rec_title").hide();
				$("#caption").show();
				$("#caption").html($img.attr("alt"));
			});
			$(this).bind("mouseout", function(){
				var $img = $( $(this).find("img").get(0) );
				$("#rec_title").show();
				$("#caption").hide();
				$("#caption").html("");
			});
			
		});
		
		//前へ
		$(this.prevClassName).bind("click", {obj:this}, function(evt){
			var obj = evt.data.obj;
			obj.prev();
			return false;
			
			/*
			var obj = evt.data.obj;
			
			$.each($(obj.targetClassName), function(i){
				var idx = Math.floor((i + obj.index + 1) % obj.itemLength);
				
				var x0 = Math.cos(obj.angleArr[idx] *  Math.PI / 180);
				var x = Math.floor((x0 * obj.radius * 2 + obj.centerX));
				//var y = Math.floor(Math.pow(x0, 2) * (obj.radius * 2)) + obj.b;
				var y = Math.floor(Math.pow(x0, 2) * (obj.radius * 2.5)) + obj.b;
				$(this).animate({left: x, top: y},
							 "1000",
							  (i == obj.itemLength - 1)?function(){
							  	obj.index = Math.floor((obj.index + 1) % obj.itemLength);
							 } : undefined
				);
				
			});
			
			return false;
			*/
		});
		
		//次へ
		$(this.nextClassName).bind("click", {obj:this}, function(evt){
			var obj = evt.data.obj;
			obj.next()
			return false;
			
			/*
			var obj = evt.data.obj;
			
			$.each($(obj.targetClassName), function(i){
				var idx = obj.index - 1;
				if(idx < 0)idx += obj.itemLength;
				
				idx = Math.floor((idx + i) % obj.itemLength);
				
				var x0 = Math.cos(obj.angleArr[idx] *  Math.PI / 180);
				var x = Math.floor((x0 * obj.radius * 2 + obj.centerX));
				//var y = Math.floor(Math.pow(x0, 2) * (obj.radius * 2)) + obj.b;
				var y = Math.floor(Math.pow(x0, 2) * (obj.radius * 2.5)) + obj.b;
				
				$(this).animate({left: x, top: y},
							 "1000",
							 (i == obj.itemLength - 1)?function(){
							 	obj.index = obj.index - 1;
								if(obj.index < 0)obj.index += obj.itemLength;
							 } : undefined
				);
				
			});
			
			return false;
			*/
		});
		
		this.autoRotate();
	},
	
	next:function(){
		var obj = this;
		
		if(obj.timerId != null)clearInterval(obj.timerId);
		
		$.each($(obj.targetClassName), function(i){
			var idx = obj.index - 1;
			if(idx < 0)idx += obj.itemLength;
			
			idx = Math.floor((idx + i) % obj.itemLength);
			
			var x0 = Math.cos(obj.angleArr[idx] *  Math.PI / 180);
			var x = Math.floor((x0 * obj.radius * 2 + obj.centerX));
			//var y = Math.floor(Math.pow(x0, 2) * (obj.radius * 2)) + obj.b;
			var y = Math.floor(Math.pow(x0, 2) * (obj.radius * 2.5)) + obj.b;
			
			$(this).animate({left: x, top: y},
						 "1000",
						 (i == obj.itemLength - 1)?function(){
						 	obj.index = obj.index - 1;
							if(obj.index < 0)obj.index += obj.itemLength;
							
							obj.autoRotate();
						 } : undefined
			);
			
		});
	},
	
	prev:function(){
		var obj = this;
		
		if(obj.timerId != null)clearInterval(obj.timerId);
		
		$.each($(obj.targetClassName), function(i){
			var idx = Math.floor((i + obj.index + 1) % obj.itemLength);
			
			var x0 = Math.cos(obj.angleArr[idx] *  Math.PI / 180);
			var x = Math.floor((x0 * obj.radius * 2 + obj.centerX));
			//var y = Math.floor(Math.pow(x0, 2) * (obj.radius * 2)) + obj.b;
			var y = Math.floor(Math.pow(x0, 2) * (obj.radius * 2.5)) + obj.b;
			$(this).animate({left: x, top: y},
						 "1000",
						  (i == obj.itemLength - 1)?function(){
						  	obj.index = Math.floor((obj.index + 1) % obj.itemLength);
							
							obj.autoRotate();
						 } : undefined
			);
			
		});
	},
	
	autoRotate:function(){
		if(this.timerId != null)clearInterval(this.timerId);
		
		var obj = this;
		this.timerId = setInterval(function(){obj.autoRotateFunction()}, 5000);
	},
	autoRotateFunction:function(){
		this.next();
	}
}
