var Featured_Controller = new Class({
    options: {
		id: null,
		container: null,
		collection: null,
		/*
			collection should be passed in as follows:
			
			{
				container: "[id of the container element]", // element that will be resized as we tween from item to item
				elems_selector: "[selector of the child elements of the container to be tweened]" // e.g. 'div.featured-item or whatever'
			}
		*/
		startwith: 0, // 0 indexed; first element that's visible and that we animate from
		autoplay: true,
		animate_height: true,
		fx_duration: 250,
		transition_interval: 5000,
		pause_on_link_click: true
    },

    initialize: function(options) {
		this.setOptions(options);
		
		var controller = this;
		this.interval = null;
		this.container = $(this.options.container);
		
		this.collection = [];
		
		$each(this.options.collection, function(collection) {
			var container = $(collection.container);
			var elems = container.getElements(collection.elems_selector);
			
			if (controller.options.animate_height) {
				var size = elems[0].measure(function() { return this.getComputedSize(); });
				container.setStyle("height", size.height + "px");
			}

			controller.collection.push({
				container: container,
				elems: elems
			});
		});

		this.current_featured = this.options.startwith;
		this.collection[0].elems[this.options.startwith].setStyle("display", "block");
		this.count = this.collection[0].elems.length;

		var links_container = $("featured-links");
		this.links = links_container.getElements("a");
		if (this.links.length > 0) {
			this.links[0].addClass("selected");
		}

		$each(this.links, function(link, index) {
			link.setStyle("cursor", "pointer");
			link.addEvent("mouseenter", function() {
				if (controller.options.pause_on_link_click) { // stop the autoplay when a link is clicked
					controller.clear_interval();
				}
				controller.do_featured(index);
				$each(controller.links, function(anchor) { anchor.removeClass("selected"); })
				this.addClass("selected");
			});
		});
		
		if (this.options.autoplay === true) { this.play(); }
	},
	
	transition: function(container, 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 });

		if (this.options.animate_height) {
			var controller = this;
			// get the sizes dynamically in order to play nice with font resizing
			var fsize = from.measure(function() { return this.getComputedSize(); });
			var tsize = to.measure(function() { return this.getComputedSize(); });
			
			f.start(1, 0).chain(
				function() {
					new Fx.Tween(container, { property: "height", duration: controller.options.fx_duration }).start(fsize.height, tsize.height).chain(
						function() {
							to.setStyles({ opacity: 0, display:"block" });
							t.start(0, 1);
						}
					);
				}
			);
		} else {
			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(); }
			}

			var controller = this;
			$each(this.collection, function(set) {
				controller.transition(set.container, set.elems[controller.current_featured], set.elems[which]);
			});
			
			$each(this.links, function(anchor, index) {
				if (index == which) {
					anchor.addClass("selected");
				} else {
					anchor.removeClass("selected");
				}
			});

			this.current_featured = which;
		}
	},

	next: function() {
		if ($defined(this.interval)) {
			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)) {
			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.set_interval();
		if (this.options.docontrols) {
			this.transition(this.play_link, this.pause_link);
		}
	},
	
	pause: function() {
		this.clear_interval();
		if (this.options.docontrols) {
			this.transition(this.pause_link, this.play_link);
		}
		this.paused = true;
	},
	
	set_interval: function () {
		this.interval = setInterval(this.options.id + ".next()", this.options.transition_interval + (this.options.fx_duration * 2));
	},
	
	clear_interval: function() {
		clearInterval(this.interval);
		this.interval = null;
	}
});

Featured_Controller.implement(new Options);

