var Feed_Controller = new Class({
	Implements: Events,

    options: {
		id: null,
		webroot: null,
		datasource: "cfc_extensions/data/feed.cfc",
		issues_uids: null, // pass one or more issue uids to restrict by issues
		exclude_uid: null, // use on content so the content itself doesn't come back as a result
		limit: null, // if this option is set, no 'more' button will print
		date_format: "%B %d, %Y @ %H:%M %p %Z",
		do_images: true, // generally set to false when calling the feed controller in pods
		types: [ "all", "news", "multimedia", "blog" ],
		content_map: [
			{ "type": "press_release", "label": "Press", "url": "newsroom/press/release/?id=" },
			{ "type": "statement", "label": "Press", "url": "newsroom/press/release/?id=" },
			{ "type": "floor_statement", "label": "Press", "url": "newsroom/press/release/?id=" },
			{ "type": "committee_statement", "label": "Press", "url": "newsroom/press/release/?id=" },
			{ "type": "media_advisory", "label": "Press", "url": "newsroom/press/release/?id=" },
			{ "type": "speech", "label": "Press", "url": "newsroom/press/release/?id=" },
			{ "type": "newsletter", "label": "Press", "url": "newsroom/press/release/?id=" },
			{ "type": "transcript", "label": "Press", "url": "newsroom/press/release/?id=" },
			{ "type": "announcement", "label": "News You Can Use", "url": "newsroom/featured/item/?id=" }, // announcements can be either a press type or announcement content; the view handles both the same
			{ "type": "miscellaneous", "label": "Press", "url": "newsroom/press/release/?id=" }, // end press
			{ "type": "news_article", "label": "News", "url": "newsroom/news/article/?id=" },
			{ "type": "oped", "label": "News", "url": "newsroom/news/article/?id=" },
			{ "type": "news_article_external", "label": "News", "url": "newsroom/news/article/?id=" },
			{ "type": "oped_external", "label": "News", "url": "newsroom/news/article/?id=" },
			{ "type": "video_youtube_embed", "label": "Video", "url": "newsroom/multimedia/?id=" },
			{ "type": "video_senate_studio_real", "label": "Video", "url": "newsroom/multimedia/?id=" },
			{ "type": "video_upload", "label": "Video", "url": "newsroom/multimedia/?id=" },
			{ "type": "video_remote_url", "label": "Video", "url": "newsroom/multimedia/?id=" }, // end video
			{ "type": "audio_upload_podcast", "label": "Audio", "url": "newsroom/multimedia/?id=" },
			{ "type": "audio_senate_studio_real", "label": "Audio", "url": "newsroom/multimedia/?id=" },
			{ "type": "audio_remote_url", "label": "Audio", "url": "newsroom/multimedia/?id=" },
			{ "type": "audio_upload", "label": "Audio", "url": "newsroom/multimedia/?id=" }, // end audio
			{ "type": "gallery", "label": "Galleries", "url": "newsroom/galleries/gallery/?id=" },
			{ "type": "blog", "label": "Bob's Blog", "url": "about/blog/post.cfm?id=" }
		],
		onLoad: Class.empty
    },

    initialize: function(options) {
		this.setOptions(options);

		this.datasource = this.options.webroot + this.options.datasource;
		this.types = [];
		
		for (var x = 0; x < this.options.types.length; x++) {
			this.set_tab_handler(this.options.types[x]);
		}
		
		this.types[0].controller.get_content();
	},
	
	set_tab_handler: function(type) {
		var controller = this;
		var tab = $("tab-" + type);
		var container = $("tab-" + type + "-content");

		tab.addEvent("click", function() {
			var which = this.id;
			$each(controller.types, function(t) {
				if (t.tab.id != which) {
					t.tab.removeClass("selected");
					t.container.setStyle("display", "none");
				} else {
					t.tab.addClass("selected");
					t.container.setStyle("display", "block");
					if (!t.controller.loaded) {
						t.controller.get_content();
					}
				}
			});
			this.addClass("selected");
		});

		var content_controller = null;
		if (type == "multimedia") {
			content_controller = new Content_Controller({
				id: "content_" + type + "_controller",
				webroot: this.options.webroot,
				datasource: this.datasource,
				type: type,
				container: container,
				issues_uids: this.options.issues_uids,
				exclude_uid: this.options.exclude_uid,
				limit: this.options.limit,
				date_format: "%B %d, %Y",
				do_images: this.options.do_images,
				onLoad: this.options.onLoad
			});
		} else {
			content_controller = new Content_Controller({
				id: "content_" + type + "_controller",
				webroot: this.options.webroot,
				datasource: this.datasource,
				type: type,
				container: container,
				issues_uids: this.options.issues_uids,
				exclude_uid: this.options.exclude_uid,
				limit: this.options.limit,
				date_format: this.options.date_format,
				do_images: this.options.do_images,
				onLoad: this.options.onLoad
			});
		}
		content_controller.parent = this;

		this.types.push({
			tab: tab,
			container: container,
			controller: content_controller
		});
	},
	
	get_type_data: function(type) {
		for (var x = 0; x < this.options.content_map.length; x++) {
			if (this.options.content_map[x].type == type) {
				return this.options.content_map[x];
			}
		}
	}
});

Feed_Controller.implement(new Options);

