var Featured_Controller = new Class({
    options: {
		id: null,
		container: null,
		featured_left_container: "featured-left",
		featured_text_container: "featured-text-container",
		controls_container: "featured-controls",
		prev_button: "prev",
		play_button: "play",
		pause_button: "pause",
		next_button: "next",
		//docontrols: true,
		autoplay: false,
		fx_duration: 250,
		transition_interval: 5000
    },

    initialize: function(options) {
		this.setOptions(options);
		
		var controller = this;
		this.interval = null;
		this.container = $(this.options.container);
		this.featured_left_container = $(this.options.featured_left_container);
		this.featured_text_container = $(this.options.featured_text_container);
		this.controls_container = $(this.options.controls_container);

		this.featured_left_items = this.container.getElements("div.featured-left-content");
		this.featured_right_items = this.container.getElements("div.featured-right-content");

		this.current_featured = 0;

		this.do_size();
		this.container.setStyle("overflow", "visible");
		
		$(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(); }
	},
	
	do_size: function() {
		var controller = this;

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

			if (size.y > controller.tallest_left) {
				controller.tallest_left = size.y;
			}
			
			if (index > 0) {
				item.setStyle("display", "none");
			}
		});
		this.featured_left_container.setStyle("height", this.tallest_left + "px");
		
		this.tallest_right = 0;
		$each(this.featured_right_items, function(item, index) {
			var size = item.measure(function() { return this.getSize(); });

			if (size.y > controller.tallest_right) {
				controller.tallest_right = size.y;
			}
			
			if (index > 0) {
				item.setStyle("display", "none");
			}
		});
		this.featured_text_container.setStyle("height", this.tallest_right + "px");
	},

	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_left_items[this.current_featured], this.featured_left_items[which]);
			this.transition(this.featured_right_items[this.current_featured], this.featured_right_items[which]);
			this.current_featured = which;
		}
	},

	next: function() {
		if ($defined(this.interval)) { // reset the timer
			this.clear_interval();
			this.set_interval() 
		}
		
		if (this.current_featured == (this.featured_right_items.length - 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.featured_right_items.length - 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);
