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'
			}
		*/
		autoplay: false,
		animate_height: true,
		fx_duration: 250,
		transition_interval: 5000,
		pause_on_link_click: true
    },

    initialize: function(options) {
		this.setOptions(options);
		
		var controller = this;
		this.initialized = false;
		this.interval = null;
		this.container = $(this.options.container);
		
		this.collection = [];
		
		$each(this.options.collection, function(set) {
			var container = $(set.container);
			var elems = container.getElements(set.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 = 0;
		this.count = this.collection[0].elems.length;

		var links_container = $("caption-right");
		this.links = links_container.getElements("a");
		if (this.links.length > 0) {
			this.links[0].addClass("selected");
		}

		this.play_pause = null;;
		$each(this.links, function(link, index) {
			link.setStyle("cursor", "pointer");
			
			if (link.id == "play-pause") {
				controller.play_pause = link;
				link.addEvent("click", function() {
					if (controller.paused == true) {
						controller.play();
					} else {
						controller.pause();
					}
				});
			} else {
				link.addEvent("click", function() {
					if (controller.options.pause_on_link_click) { // stop the autoplay when a link is clicked
						controller.pause();
					}
					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.initialized) {
			this.initialized = true;
			
			var controller = this;
			var flash = $("flash-container");
			var featured = $("featured-container");

			var f = new Fx.Tween(flash, {
				property: "opacity",
				duration: this.options.fx_duration,
				onComplete:function(elem) {
					var elem = $(elem);
					elem.setStyle("display", "none");
				}
			});

			f.start(1, 0).chain(
				function() {
					featured.setStyles({ opacity: 0, display: "block" });
					var t = new Fx.Tween(featured, { property: "opacity", duration: controller.options.fx_duration });
					t.start(0, 1).chain(
						function() {
							if (controller.paused === true) {
								controller.next();
								controller.paused = false;
							}
							controller.set_interval();
							controller.play_pause.innerHTML = "pause";
							var cookie  = Cookie.write("INTRO", true, { duration: 0 });
						}
					);
				}
			);
		} else {
			if (this.paused === true) { this.next(); this.paused = false; }
			this.set_interval();
			this.play_pause.innerHTML = "pause";
		}
	},
	
	pause: function() {
		this.clear_interval();
		if (this.options.docontrols) {
			this.transition(this.pause_link, this.play_link);
		}
		this.paused = true;
		this.play_pause.innerHTML = "play";
	},
	
	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);

