/*
jQuery common scripts for Loewy Text Rotator
- Sukiman, suki@webexcellence.net, http://www.webexcellence.net/
*/
(function($) {
	$.loewy = $.loewy || {};
	
	$.fn.loewyRotateText = function(o) {
		return this.each(function() {
			new $.loewy.rotateText(this,o);
		});
	}
	
	$.loewy.rotateText = function(ele, o) {
		this.options = $.extend({ 
			delay: 3000,
			fadeOutSpeed: 800,
			fadeInSpeed: 800
		},o);
		this.self = this;
		this.p = ele;
		
		this.start();
	}
	
	$.extend($.loewy.rotateText.prototype, {
		current: -1,
		intervalId: 0,
		start: function() {
			if (this.current == -1) {
				this.current = Math.floor((this.frameCount()-1) * Math.random());
				$(".loewyRotateTextItem:eq("+this.current+")",this.p).show();
				this.initNext();
			}
		},
		frameCount: function() {
			return $(".loewyRotateTextItem",this.p).length;
		},
		rotate: function() {
			var self = this;
			n = (this.current + 1) % this.frameCount();
			//now without effect
			/* 
			if (jQuery.browser.safari) {
				$(".loewyRotateTextItem:eq("+this.current+")",this.p).animate({opacity: 0}, this.options.fadeOutSpeed, function(){
					self.fadeInNext(self);
					});
			} else {
				$(".loewyRotateTextItem:eq("+this.current+")",this.p).fadeOut(this.options.fadeOutSpeed);
				window.setTimeout( function() {
					self.fadeInNext(self)
				}, self.options.fadeOutSpeed);
			}
			*/
			$(".loewyRotateTextItem:eq("+this.current+")",this.p).hide();
			self.fadeInNext(self)
		},
		initNext: function() {
			var s = this;
			window.setTimeout( function() {
				s.rotate();
			}, s.options.delay);
		},
		fadeInNext: function(self) {
			var n = (self.current + 1) % self.frameCount();
			//now without effect
			/*
			if (jQuery.browser.safari) {
				$(".loewyRotateTextItem:eq("+n+")",self.p).css("opacity",0).animate({opacity: 1}, self.options.fadeInSpeed, function() { } );
			} else {
				$(".loewyRotateTextItem:eq("+n+")",self.p).fadeIn(self.options.fadeInSpeed);
			}
			*/
			$(".loewyRotateTextItem:eq("+n+")",self.p).show();
			self.current = n
			self.initNext();
		}
	});
})($);