$(document).ready(function(){
	rotator.init();
	stats.init();
});
var stats = {
	current_stat:0,
	interval_id:false,
	init:function(){
		this.set_selectors();
		this.set_events();
	},
	set_selectors:function(){
		this.$stats = $("#stats").find("li");
		this.total_stats = this.$stats.length;
	},
	set_events:function(){
		this.show_next_stat();
	},
	show_next_stat:function(){
		var $current_stat = $(stats.$stats[stats.current_stat]);
		stats.$stats.hide();
		$current_stat.show().css({left:"100%"});
		$current_stat.animate({left:"12%"}, 3000, function(){
			$current_stat.delay(5000).fadeOut('slow', function(){
				stats.current_stat++;
				if (stats.current_stat == stats.total_stats){
					stats.current_stat = 0;
				}
				stats.show_next_stat();
			});
		});
	}
}

var rotator = {
	init:function(){
		this.set_selectors();
		this.set_events();
		this.show_selected_rotator(false, this.$rotators.filter(":first").data("section"));
	},
	set_selectors:function(){
		this.$rotators_wrap = $("#rotator_wrap");
		this.$rotators = $(".rotator");
		this.$selector = $("#rotator_selector");
		
		this.rotator_width = this.$rotators_wrap.width();
	},
	set_events:function(){
		this.$selector.find("li").live("click", this.show_selected_rotator);
	},
	show_selected_rotator:function(e, rotator_id){
		var _this = rotator;
		if (typeof(e) !== 'undefined' && e != false){
			e.preventDefault();
			$target = $(e.currentTarget);
			rotator_id = $target.data("section");
		}
		_this.$rotators.fadeOut();
		_this.$rotators.cycle('destroy');
		$("#"+rotator_id+"_rotator").cycle({
			startingSlide:0,
			pager:"#pager_nav",
			end:this.go_to_next_rotator,
			nowrap:true,
			timeout:6000
		}).fadeIn();
		_this.set_active_rotator_selector(rotator_id);
	},
	set_active_rotator_selector:function(rotator_selector_class){
		this.$selector.find("li").removeClass("active").end().find("li").filter("."+rotator_selector_class).addClass("active");
	},
	go_to_next_rotator:function(){
		var _this = rotator;
		var current_rotator = _this.get_active_rotator();
		var next_rotator_check = _this.$selector.find("li").filter("."+current_rotator).next();
		var next_rotator = ''
		if (next_rotator_check.length > 0){
			next_rotator = next_rotator_check.data("section");
		}
		else{
			next_rotator = _this.$selector.find("li").filter(":first").data("section");
		}		
		_this.show_selected_rotator(false, next_rotator);
	},
	get_active_rotator:function(){
		return this.$selector.find("li").filter(".active").data("section");
	}
}
