var Featured_Controller = new Class({
    options: {
		id: null,
		container: null,
		featured_text_container: "featured-text-container",
		//docontrols: true,
		autoplay: false,
		fx_duration: 250,
		transition_interval: 5000,
		sifr: null
    },

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

		this.featured_items = this.container.getElements("div.featured");
		this.featured_titles = this.container.getElements("div.featured-title");
		this.sifr = [];
		this.featured_text_items = this.container.getElements("div.featured-text");

		this.current_featured = 0;

		var controller = this;

		/*if ($chk(this.options.sifr)) { // bit of a hack, but it gets sifr working
			if(typeof sIFR == "function"){
				sIFR.replaceElement(named({
					sSelector: this.options.sifr.selector,
					sFlashSrc: application_webroot + this.options.sifr.font_path,
					sColor: this.options.sifr.sColor,
					sLinkColor: this.options.sifr.sLinkColor,
					nPaddingTop: this.options.sifr.nPaddingTop,
					nPaddingBottom: this.options.sifr.nPaddingBottom,
					nPaddingLeft: this.options.sifr.nPaddingLeft,
					sWmode: this.options.sifr.sWmode
				}));
			}
		}*/
		
		this.tallest_title = 0;
		$each(this.featured_titles, function(item, index) {
			if ($chk(controller.options.sifr)) {	
				if(typeof sIFR == "function"){
					item.getElement("span").setProperty("id", "sifr-span-" + index);

					if (index > 0) {
						item.setStyles({
							"width": item.getSize().x + "px",
							"position": "absolute",
							"left": "-9999px"
						})
					}

					controller.sifr.push({
						"container": item,
						"initialized": false
					});

					sIFR.replaceElement(named({
						sSelector: "#sifr-span-" + index,
						sFlashSrc: application_webroot + controller.options.sifr.font_path,
						sColor: controller.options.sifr.sColor,
						sLinkColor: controller.options.sifr.sLinkColor,
						nPaddingTop: controller.options.sifr.nPaddingTop,
						nPaddingBottom: controller.options.sifr.nPaddingBottom,
						nPaddingLeft: controller.options.sifr.nPaddingLeft,
						sWmode: controller.options.sifr.sWmode
					}));
				}
				
				if (!Browser.Engine.webkit && index > 0) {
					item.setStyles({
						"display": "none",
						"position": "static",
						"left": "0"
					});
				}
			}

			var size = item.measure(function() { return this.getSize(); });
			
			if (size.y > controller.tallest_title) {
				controller.tallest_title = size.y;
			}
		});
		//$("featured-titles-container").setStyle("height", this.tallest_title + "px");
		
		this.tallest_feature = 0;
		$each(this.featured_text_items, function(item, index) {
			var size = item.measure(function() { return this.getSize(); });

			if (size.y > controller.tallest_feature) {
				controller.tallest_feature = size.y;
			}
			
			if (index > 0) {
				controller.featured_items[index].setStyle("display", "none");
				item.setStyle("display", "none");
			}
		});
		this.featured_text_container.setStyle("height", this.tallest_feature + "px");
		
		var ul = $("topstories-list");
		this.links = ul.getElements("a");
		this.links[0].addClass("highlighted");

		//var ul_width = 0;
		$each(this.links, function(link, index) {
			//ul_width += link.getSize().x + 1; // one pixel margin that isn't measured
			link.setStyle("cursor", "pointer");
			link.addEvent("click", function() {
				controller.do_featured(index);
				$each(controller.links, function(anchor) { anchor.removeClass("highlighted"); })
				this.addClass("highlighted");
			});
		});
		//ul.setStyle("width", ul_width + "px");
		
		if (this.options.autoplay === true) { this.play(); }
		//this.fx = new Fx.Tween(this.featured_items[0], { property: "opacity", duration: this.options.fx_duration }).start(0, 1);
	},
	
	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 });
		
		var controller = this;

		f.start(1, 0).chain(
			function() {
				$(to).setStyles({ opacity: 0, display:"block" });
				t.start(0, 1).chain(function() {
					if (Browser.Engine.webkit) {
						if (controller.current_featured > 0 && !controller.sifr[controller.current_featured].initialized) {
							/*var embed = controller.sifr[controller.current_featured].header.getElement("embed");
							embed.set("width", controller.sifr[controller.current_featured].width);
							embed.set("height", controller.sifr[controller.current_featured].height);
							embed.setStyles({
								"width": controller.sifr[controller.current_featured].width + "px",
								"height": controller.sifr[controller.current_featured].height + "px"
							});
							
							sIFR.replaceElement(named({
								sSelector: "#" + controller.sifr[controller.current_featured].header.id,
								sFlashSrc: application_webroot + controller.options.sifr.font_path,
								sColor: controller.options.sifr.sColor,
								sLinkColor: controller.options.sifr.sLinkColor,
								nPaddingTop: controller.options.sifr.nPaddingTop,
								nPaddingBottom: controller.options.sifr.nPaddingBottom,
								nPaddingLeft: controller.options.sifr.nPaddingLeft,
								sWmode: controller.options.sifr.sWmode
							}));*/
							
							//controller.sifr[controller.current_featured].header.innerHTML = controller.sifr[controller.current_featured].text;
							
							controller.sifr[controller.current_featured].container.setStyles({
								"position": "static",
								"left": "0"
							});
							
							controller.sifr[controller.current_featured].initialized = true;
						}
					}
				});
			}
		);
	},
	
	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 from = this.current_featured;
			this.current_featured = which;
			this.transition(this.featured_titles[from], this.featured_titles[which]);
			this.transition(this.featured_items[from], this.featured_items[which]);
			this.transition(this.featured_text_items[from], this.featured_text_items[which]);
		}
	},

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