(function($){
	$.fn.rollingNews = function(options) {
		var isMethodCall = (typeof options == "string") || false;
		var args = arguments;
				
		$(this).each(function(){
			var rollingNews = $(this).data("rollingNews");
			if (isMethodCall && rollingNews) {
				if (rollingNews[options] && typeof rollingNews[options] == "function"){
					rollingNews[options].apply(rollingNews, $.makeArray(args).slice(1));
				}
				return rollingNews;
			}
			
			if (rollingNews == undefined && !isMethodCall) {
				rollingNews = new $.rollingNews(this, options);
				$(this).data("rollingNews", rollingNews);
			}

		});
		
		return this;
	}
	
	$.rollingNews = function(elem, options) {
		this.elem = elem;
		this.options = $.extend({}, $.rollingNews.defaults, options);
		this.events = $.extend({}, $.rollingNews.events, {});
		this.init();
	}
	
	$.extend( $.rollingNews, {
		defaults: {
			animType:1
		},
		events: {
			mouseover: function(event) {
				this.pause();
				
			},
			mouseout: function(event) {
				this.resume();
				
			}
		},
		prototype: {
			init: function() {
				this.list = $("ul", this.elem);
				this.parent = $(this.list).parent();
				this.parent_height = $(this.parent).innerHeight();
				this.parent_padding_top = parseInt($(this.parent).css("paddingTop")) || 0;
				this.parent_padding_bottom = parseInt($(this.parent).css("paddingTop")) || 0;
				
				this.items = $(">li", this.list);
				
				if (this.options.animType == 1) {
					this.tail = $(this.items[0]).clone();
					$(this.list).append(this.tail.addClass("tail"));
					this.items = $(">li", this.list);
					
					//$(this.list).css("top", -(this.parent_height - this.parent_padding_top) + "px");
					$(this.list).css("top", 0);
					
				}
				
				$(this.elem).bind("mouseover mouseout", function(event) {
					var r = $(this).data("rollingNews");
					if ( r && r.events[event.type] ) {
						if ( typeof r.events[event.type] == "function" ) {
							r.events[event.type].apply(r, [event]);
						}
					}
				});
				
				this.paused = false;
				this.current = 0;
				
				if (this.options.animType != 1) {
					$(this.items).hide();
					$(this.items[0]).show();
				}
				this.next();
			},
			pause: function() {
				this.paused = true;
				if ( this.timeout > 0 ) {
					window.clearTimeout( this.timeout );
					this.timeout = 0;
				}
			},
			resume: function() {
				this.paused = false;
				if ( this.timeout > 0 ) {
					window.clearTimeout( this.timeout );
					this.timeout = 0;
				}
				var self = this;
				this.timeout = window.setTimeout( function() {
					self.next();
				}, 50 );
			},
			next: function() {
				if ( this.timeout > 0 ) {
					window.clearTimeout( this.timeout );
					this.timeout = 0;
				}
				if (this.paused) {
					return;
				}
				
				if (this.options.animType == 1) {
					
				} else {
					//
				}
				var self = this;
				this.timeout = window.setTimeout( function() {
					self.animate();
				}, 2000 );
			},
			animate: function() {
				var self = this;
				
				if (this.options.animType == 1) {
					this.current++;
					if (this.current >= this.items.length) {
						this.current = 1;
						$(this.list).css("top", 0 + "px");
					}
					
					this.anim_top = - this.current * this.parent_height;
					
					$(this.list).animate({
						top:  this.anim_top + "px"
					}, { 
						duration: 3000, 
						easing: "easeOutExpo", 
						complete: function() {
							var p = $(this).parents(".rollingNews");
							var r = $(p).data("rollingNews");
							if (r && !r.paused) {
								r.next.apply(r, []);
							}
						}
					});
				} else {
					var toHideIdx = this.current;
					var toShowIdx = this.current + 1;
					if (toShowIdx > this.items.length - 1) {
						toShowIdx = 0;
					}
					
					this.current = toShowIdx;
					
					var toCloneHTML = $(this.items[toHideIdx]).html();
					var toClone = $(this.items[toHideIdx]);
					var toShowCloneHTML = $(this.items[toShowIdx]).html();
					var toShowClone = $(this.items[toShowIdx]);
					
					var offset = $(this.parent).offset();
					
					if ($.browser.msie && $.browser.version > 7 ) {
						offset.left -= 2;
						offset.top -= 2;
					}
					
					var height = toClone.outerHeight(true);
					var width = toClone.outerWidth(true);
					var rows = 8;
					var rowHeight =  height/rows;
					var hide_clones =[],
						show_clones = [],
						shows = [];
					
					for (var i=0, clone, html; i < rows; i++) {
						clone = $('<div></div>')
							.appendTo('body')
							.addClass('rollingNews-effects-explode')
							.css({
								position: 'absolute',
								overflow: 'hidden',
								width: width,
								height: rowHeight,
								left: offset.left,
								top: offset.top + i*(rowHeight)
							});
							
						html = $(toCloneHTML)
							.appendTo(clone)
							.css({
								position: 'absolute',
								visibility: 'visible',
								left: 0,
								top: -i*(rowHeight)
							});
							
						hide_clones.push(clone);
						
						clone = $('<div></div>')
							.appendTo('body')
							.addClass('rollingNews-effects-explode')
							.css({
								position: 'absolute',
								overflow: 'hidden',
								backgroundColor: "#999999",
								width: width,
								height: 0,
								left: offset.left,
								top: offset.top + i*(rowHeight) + rowHeight
							});
							
						html = $(toShowCloneHTML)
							.appendTo(clone)
							.css({
								position: 'absolute',
								visibility: 'visible',
								left: 0,
								top: -i*(rowHeight)
							});
							
						shows.push({
							height: rowHeight,
							top: offset.top + i*(rowHeight)
						});
						
						show_clones.push(clone);
							
						
						
						
					}
					
					//this.clone = $(this.items[0]).clone();
					//$(this.items[toHideIdx]).hide();
					//$(this.items[toShowIdx]).show();
					
					$(".rollingNews-effects-explode").bind("mouseover mouseout", function(event) {
						if ( self && self.events[event.type] ) {
							if ( typeof self.events[event.type] == "function" ) {
								self.events[event.type].apply(self, [event]);
							}
						}
					});
					
					$(hide_clones).each(function(){
						$(this).animate({opacity:0},{duration:2000});
					});
					
					
					for (var i=0; i < rows; i++) {
						$(show_clones[i]).animate({height:shows[i].height, top: shows[i].top, backgroundColor:"#cccccc"},{duration:2000});
					};
					
					this.timeout = window.setTimeout( function() {
						$(self.items[toHideIdx]).hide();
						$(self.items[toShowIdx]).show();
						$(".rollingNews-effects-explode").unbind();
						$(".rollingNews-effects-explode").remove();
						
						if (!self.paused) {
							self.animate();
						}
					}, 8000 );
					
				}
			}
		}
	});
	
	$().ready( function() {
		$(".rollingNews").rollingNews();
	});
	
})(jQuery);