(function($){
    $.fn.extend({
        newsExpose: function(options) {
        	var defaults = {
                time: 5000,
                rotate: true,
                startIndex: 0,
                total:0,
                currentIndex: 0,
                items:null
            };
        	var timer;
        	var that = this;
            var options = $.extend(defaults, options);
            
            this.rotate = function(){
            	var next = options.currentIndex == (options.total -1) ? 0 : options.currentIndex+1;
            	that.goTo(next);
            };
            this.goTo = function(index){
				var nextItem = $(options.items[index]);
				var direction = index>options.currentIndex ? 1 : (index<options.currentIndex ? -1 : 0);
				if(direction != 0){
					//slide to position
					var topOffset = that.menu.position().top;
					topOffset += (direction>0 && nextItem.prevAll().length<2) || (direction>0 && nextItem.nextAll().length<1) ?
										0 :
										(direction<0 && nextItem.prev().length<1 ?
											-topOffset :
											direction*-nextItem.outerHeight(true)
										);
					that.menu.animate({top: topOffset});
					
					index = parseInt(index);
					that.hide(options.currentIndex);
					that.show(index);
					options.currentIndex = index;
				}
            };
            this.start = function(){
            	options.timer = setInterval(this.rotate,options.time);
            };
            this.hide = function(index){
            	var item = $(options.items[index]);
            	item.removeClass("selected");
            	$(item.children(":first").attr("href")).hide();
            };
            this.show = function(index){
            	var item = $(options.items[index]);
            	item.addClass("selected");
            	$(item.children(":first").attr("href")).show("fast");
            };
            this.init = function(){
                var obj = $(this); 
                var canvas 	= $("div.BNEnewsCanvas[:first]",obj);
                var menu 	= $("div.BNEnewsMenu[:first]",obj);  
                var pointer = $("div.Bfloater[:first]",obj);
				that.menu = menu;
                var menuItems = menu.find("li");
                options.total = menuItems.length;
                if(options.total > 1){
                	options.currentIndex = options.startIndex; 
                    options.items = menuItems;
                    canvas.children().hide();
                    for(i = 0; i<options.total; i++){                	
                    	$(menuItems[i]).attr("rotationIndex",i);
                    	$(menuItems[i]).click(function(){ 
                    		that.goTo($(this).attr("rotationIndex"));
                    		clearInterval(options.timer);
                    		options.timer = setInterval(that.rotate,options.time);
                    		return false;
                    	});
                    }
                    this.show(options.startIndex); 
                    this.start(); 
                }
                return true;
            };
            return this.init(this.itemClick);
        }
    });
    
})(jQuery);
