var TopStory = Class.create(tabModule, {
	
	initialize : function($super, top_story) {
		top_story.top_story = this;
	
		$super(top_story);
	},

	setOptions : function($super) {
		$super();
		
		this.options.wrap = this.base.getAttribute("wrap");
		this.options.wrap = (this.options.wrap == "false") ? false : true;
		this.options.scrollerSelector = '.scrollerModule';
	},

	setProperties : function($super) {
		
		this.loaded_top_story_videos = [];
		this.scroller = this.base.down(this.options.scrollerSelector);
		this.scroll_count = this.scroller != null ? parseInt(this.scroller.getAttribute("scroll")) : 0;
		this.top_story_items = this.base.down(".top_story_items");
		this.videos = this.base.select("iframe.video");
		this.video_names = [];
		
		for (var i = 0; i < this.videos.length; i++) {
			this.video_names.push(this.videos[i].getAttribute("name"));
		}
		
		$super();
	},
	
	setEvents : function($super) {
		if (!this.options.navEnabled) { return; }
		
		$super();
		
		// Play video if preview image is clicked
		this.base.select(".video_preview").each(function(n, i) {
			n.observe('click', function() {
				var top_story_item = this.up(".top_story_item");
				
				if (top_story_item) {
					top_story_item.addClassName("play_video");
					
					var video = top_story_item.down("iframe.video");
					if (video) {
						
						var video_name = video.getAttribute("name");
					
						frames[video_name].playVideo();
					}
				}
			});
		});
		
		// Stop Auto Rotate on click of all nav elements and main viewing area
		
		if (this.top_story_items) {
			this.top_story_items.observe('click', function() { this.stopAutoChange(); }.bind(this));
		}
		
		for (var i = 0; i < this.nav.length; i++) {
			var n = this.nav[i];
			
			n.observe('click', function() {
			
				for (var j = 0; j < this.video_names.length; j++) {
					var video = frames[this.video_names[j]];
					
					if (video.pauseVideo) { video.pauseVideo(); }
				}
				
				this.stopAutoChange();
			}.bind(this));			
		}
		
		if (this.scroller) {
		
			for (var i = 0; i < this.scroller.scrollerModule.prevNavs.length; i++) {
				var n = this.scroller.scrollerModule.prevNavs[i];
				
				n.observe('click', function() { this.stopAutoChange(); }.bind(this));
			}
			
			for (var i = 0; i < this.scroller.scrollerModule.nextNavs.length; i++) {
				var n = this.scroller.scrollerModule.nextNavs[i];
				
				n.observe('click', function() { this.stopAutoChange(); }.bind(this));
			}
			
			for (var i = 0; i < this.scroller.scrollerModule.dots; i++) {
				var n = this.scroller.scrollerModule.dots[i];
				
				n.observe('click', function() { this.stopAutoChange(); }.bind(this));
			}
		}
		
	},
	
	onAutoChange : function($super) {
		
		if (this.options.wrap == false && (this.currIndex + 1) >= this.nav.length) { this.stopAutoChange(); return; }
		
		if ((this.scroll_count > 0 && ((this.currIndex + 1) % this.scroll_count) == 0) || (this.currIndex + 1) >= this.nav.length) {
			this.scroller.scrollerModule.navigate(this.scroller.scrollerModule.currentPanel + 1);
		}
		
		$super();
	}

});

NBAUtil.Events.addDomLoadedHandler(function() { $$(".top_story").each(function(n) { new TopStory(n); }); });