var Featured_Controller = new Class({
    options: {
		id: null,
		container: null,
		featured_top_container: "featured-top",
		status_container: "featured-status",
		controls_container: "featured-controls",
		prev_button: "prev",
		play_button: "play",
		pause_button: "pause",
		next_button: "next",
		autoplay: false,
		fx_duration: 250,
		transition_interval: 5000
    },

    initialize: function(options) {
		this.setOptions(options);
		
		this.interval = null;
		this.container = $(this.options.container);
		this.featured_top_container = $(this.options.featured_top_container);
		this.status_container = $(this.options.status_container);
		this.controls_container = $(this.options.controls_container);

		this.featured_top_items = this.container.getElements("div.featured-top-container");
		this.featured_status_items = [];
		this.featured_text_items = this.container.getElements("div.featured-text-container");

		this.count = this.featured_top_items.length;
		this.current_featured = 0;

		var controller = this;

		this.tallest_top = 0;
		$each(this.featured_top_items, function(item, index) {
			var size = item.measure(function() { return this.getSize(); });

			if (size.y > controller.tallest_top) {
				controller.tallest_top = size.y;
			}
		});
		this.featured_top_container.setStyle("height", this.tallest_top + "px");

		$("status-right").innerHTML = this.count;
		var status_left = $("status-left");

		for(var x = 0; x < this.count; x++) {
			var current = new Element("div", {
				"class": "cufon-replace",
				"text": x + 1
			}).inject(status_left);
			
			if (x > 0) {
				current.setStyle("display", "none");
			}
			
			this.featured_status_items.push(current);
		}
		
		//Cufon.replace("div.cufon-replace");
		
		$(this.options.prev_button).addEvent("click", function() {
			controller.previous();
		});
		
		$(this.options.play_button).addEvent("click", function() {
			controller.play();
		});
		
		$(this.options.pause_button).addEvent("click", function() {
			controller.pause();
		});
		
		$(this.options.next_button).addEvent("click", function() {
			controller.next();
		});
		
		if (this.options.autoplay === true) { this.play(); }
	},
	
	transition: function(from, to) {		
		var f = new Fx.Tween(from, { property: "opacity", duration: this.options.fx_duration, onComplete:function(elem) { elem.style.display = "none"; } });
		var t = new Fx.Tween(to, { property: "opacity", duration: this.options.fx_duration });
		
		f.start(1, 0).chain(
			function() {
				$(to).setStyles({ opacity: 0, display:"block" });
				t.start(0, 1);
			}
		);
	},
	
	do_featured: function(which) {
		if (this.current_featured !== which) {
			if (Browser.Engine.trident) {
				var movie = $("flash" + this.current_featured);
				if ($chk(movie)) { movie.pauseVideo(); }
			}

			this.transition(this.featured_top_items[this.current_featured], this.featured_top_items[which]);
			
			//this.transition(this.featured_status_items[this.current_featured], this.featured_status_items[which]);
			
			this.transition(this.featured_text_items[this.current_featured], this.featured_text_items[which]);
			
			this.featured_status_items[this.current_featured].setStyle("display", "none");
			this.featured_status_items[which].setStyle("display", "block");
			
			this.current_featured = which;
		}
	},

	next: function() {
		if ($defined(this.interval)) { // reset the timer
			this.clear_interval();
			this.set_interval() 
		}
		
		if (this.current_featured == (this.count - 1)) {
			this.do_featured(0);
		} else {
			this.do_featured(this.current_featured + 1);
		}
	},
	
	previous: function() {
		if ($defined(this.interval)) { // reset the timer
			this.clear_interval();
			this.set_interval() 
		}
		
		if (this.current_featured == 0) {
			this.do_featured(this.count - 1);
		} else {
			this.do_featured(this.current_featured - 1);
		}
	},
	
	play: function() {
		if (this.paused === true) { 
			this.next();
			this.paused = false;
		}
		this.controls_container.removeClass("paused");
		this.controls_container.addClass("playing");
		this.set_interval();
	},
	
	pause: function() {
		this.clear_interval();
		this.controls_container.removeClass("playing");
		this.controls_container.addClass("paused");
		this.paused = true;
	},
	
	set_interval: function () {
		this.interval = setInterval(this.options.id + ".next()", this.options.transition_interval);
	},
	
	clear_interval: function() {
		clearInterval(this.interval);
		this.interval = null;
	}/*,

	pause_movie: function(movie) {
		var player = (Browser.Engine.trident) ? window[movie] : document[movie];
		player.pauseVideo();
	}*/
});

Featured_Controller.implement(new Options);
