//	==	rotator.js	==
;(function($) {
	$.fn.extend({
		rotator: function(options) {
			return this.each(function() {
				new $.myRotator(this, options);
			});
		}
	});
	$.myRotator = function(element, options) {
		var defaults = {
			picPath: false,
			startIndx: 0
		};
		this.options = $.extend({}, defaults, options || {});
		this.element = $(element);
		this.len = $(element).find(".rotator_info_unit").length;
		this.lastIndx = this.len - 1;
		this.picPath = this.options.picPath;
		this.rtrIndx = this.options.startIndx;
		this.controller = '<div class="rotate_control row"><ul class="counter"><li class="arrow" vector="-1"></li><li class="rtr_indx">'+this.rtrIndx+'</li><li>of</li><li class="rtr_len">'+this.len+'</li><li class="arrow rt" vector="1"></li></ul></div>';
		this.setup();
	};
	$.extend($.myRotator.prototype, {
		setup: function() {
			$rotatorObj = this.element;
			$rotatorObj.find(".rotator_info").after(this.controller);
			if (this.element.hasClass("article_img")) {
				$rotatorObj.find(".rotator_info").before(this.controller);
			}
			var self = this;
			$rotatorObj.find(".rotate_control .arrow").css("cursor", "pointer").bind("click", function(){
				self.rotate($(this).attr("vector"));
			});
			$rotatorObj.find(".rotator_info_unit").eq(this.rtrIndx).show();
			$rotatorObj.find(".rtr_indx").html(this.rtrIndx+1);
			if (this.picPath) {
				//$("." + this.picPath).find(".rotator_pic").eq(this.rtrIndx).show();	// 'inline' don't work in Safari
				$("." + this.picPath).find(".rotator_pic").eq(this.rtrIndx).css('display','block');
			}
		},
		 rotate: function(dir) {
			var self = this;
			$rotatorObj = this.element;
			$rotatorObj.find(".rotate_control .arrow").css("cursor", "default").unbind( "click" );
			if (this.picPath) {
				$("." + this.picPath).find(".rotator_pic").eq(this.rtrIndx).hide();
			}
			$rotatorObj.find(".rotator_info_unit").eq(this.rtrIndx).hide();
			this.rtrIndx += parseInt(dir);
			if (this.rtrIndx >= this.len) {this.rtrIndx = 0;}
			if (this.rtrIndx < 0) {this.rtrIndx = this.lastIndx;}
			$rotatorObj.find(".rtr_indx").html(this.rtrIndx+1);
			$rotatorObj.find(".rotator_info_unit").eq(this.rtrIndx).show();
			if (this.picPath) {
				$("." + this.picPath).find(".rotator_pic").eq(this.rtrIndx).show();
			}
			//if (!this.options.spotlight) {
				self.stopRotate();
			//}
		},
		 stopRotate: function(dir) {
			var self = this;
			$rotatorObj.find(".rotate_control .arrow").css("cursor", "pointer").bind("click", function(){
				self.rotate($(this).attr("vector"));
			});
		}
	});
})(jQuery);

