var Content_Controller = new Class({
    options: {
		id: null,
		webroot: null,
		datasource: null,
		type: null,
		container: null,
		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
		thumb_width: 48,
		no_results_text: "<p>We're sorry, there are no results.</p>",
		topics_restrict: "tags" // last minute hack for the switch to issues instead of tags; will default to tags but set to "issues" to restrict by those instead
    },

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

		this.count = 0;
		this.parent = null;
		this.loaded = false;
		this.loading = false;
		this.datasource = this.options.datasource;
		this.current_page = 1;
		this.limit = this.options.limit;
		this.results_per_page = $chk(this.limit) ? this.limit : 10;
		this.container = this.options.container;
		this.content_container = new Element("div").inject(this.container);
		this.issues_uids = this.options.issues_uids;

		if (!$chk(this.limit)) {
			this.more_container = new Element("div", {
				"class": "more-container",
				"styles": {
					"display": "none"
				}
			}).inject(this.container);

			var controller = this;
			var more_btn = new Element("a", {
				"class": "more",
				"html": "<span>more</span>",
				"events": {
					"click": function() {
						controller.more();
					}
				}
			}).inject(this.more_container);
			
			var clear_div = new Element("div", { "class": "clear" }).inject(this.more_container);
		}

		// not currently used, but the backend is there to support 'em; see the multimedia Content_Controller for examples
		this.date = null;
		this.tag = null;
		this.controls = null;
	},

	get_content: function() {
		if (!this.loading) {
			this.loading = true;
			var height = this.container.getSize().y;
			if (height == 0) {
				this.container.setStyle("height", "200px");
			} else {
				this.container.setStyle("height", height + "px");
			}

			//this.container.innerHTML = "";
			if (this.count == 0) this.container.addClass("loading");
			if (!$chk(this.limit)) { this.more_container.setStyle("display", "none"); }

			var pars = "method=get&type=" + encodeURIComponent(this.options.type) + "&current_page=" + encodeURIComponent(this.current_page) + "&results_per_page=" + encodeURIComponent(this.results_per_page);

			if ($chk(this.issues_uids)) { pars += "&issues_uids=" + encodeURIComponent(this.issues_uids); }
			if ($chk(this.options.exclude_uid)) { pars += "&exclude_uid=" + encodeURIComponent(this.options.exclude_uid); }

			/*if ($chk(this.date)) {
				pars += "&start_date=" + encodeURIComponent(this.date.start_date) + "&end_date=" + encodeURIComponent(this.date.end_date);
			}

			if ($chk(this.tag)) {
				pars += "&" + this.options.topics_restrict + "=" + encodeURIComponent(this.tag);
			}*/

			var success_fn = this.got_content.bind(this);
			var req = new Request({
				url: this.datasource,
				data: pars,
				onSuccess: success_fn
			}).send();
		}
	},
	
	got_content: function(request) {
		r = JSON.decode(request);

		this.container.removeClass("loading");

		if (r.RESULTS.ROWCOUNT > 0) {
			this.count += r.RESULTS.ROWCOUNT;	
				
			this.container.setStyle("height", "auto");
			
			var feed_item = null;
			for (var x = 0; x < r.RESULTS.ROWCOUNT; x++) {
				feed_item = new Element("div", { "class": "feed-item" }).inject(this.content_container);
				//if (x == (r.RESULTS.ROWCOUNT - 1)) { feed_item.addClass("last"); }
				
				if (this.options.do_images == true) {
					var feed_item_left = new Element("div", { "class": "feed-item-left" }).inject(feed_item);

					if ($chk(r.RESULTS.DATA.LIST_IMAGE[x])) { 
						src = this.options.webroot + r.RESULTS.DATA.LIST_IMAGE[x];
					} else {
						var img_num = Math.floor(Math.random() * 7) + 1;
						src = this.options.webroot + "images/faux/feed/" + img_num + ".jpg";
					}

					var img = new Element("img", {
						"src": src,
						"alt": r.RESULTS.DATA.TITLE[x],
						"styles": {
							"position": "absolute",
							"visibility": "hidden"
						}
					}).inject(feed_item_left);

					var size = img.getSize();
					if (size.x > this.options.thumb_width) {
						img.set("width", this.options.thumb_width);
						img.setStyle("width", this.options.thumb_width + "px");
						if (Browser.Engine.trident) {
							var height = this.options.thumb_width * size.y / size.x;
							img.set("height", height);
							img.setStyle("height", height + "px");
						}
					}
					
					img.setStyles({
						"position": "static",
						"visibility": "visible"
					});
				}

				var feed_item_right = new Element("div", { "class": "feed-item-right" }).inject(feed_item);
				
				var type_data = this.parent.get_type_data(r.RESULTS.DATA.TYPE[x]);
				if (!$chk(type_data)) alert(r.RESULTS.DATA.TYPE[x]);
				
				var p_date = new Element("p", {
					"class": "date",
					"text": Date.parse(r.RESULTS.DATA.DATE[x]).format(this.options.date_format) + " in " + type_data.label
				}).inject(feed_item_right);
				
				var p = new Element("p").inject(feed_item_right);
				
				var a = new Element("a", { "href": this.options.webroot + type_data.url + r.RESULTS.DATA.UID[x], "text": r.RESULTS.DATA.TITLE[x] }).inject(p);
				
				
				var clear_div = new Element("div", { "class": "clear", "html": "<!-- // -->" }).inject(feed_item);
			}
			
			if (this.count < r.COUNT) {
				if (!$chk(this.limit)) { this.more_container.setStyle("display", "block"); }
			} else {
				feed_item.addClass("last");
			}
			
			if (!this.loaded) { this.loaded = true; }
		} else {
			var div = new Element("div", {
				"class": "no-results",
				"html": this.options.no_results_text
			}).inject(this.container);
			
			this.container.setStyle("height", "auto");
		}
		
		this.loading = false;
		this.parent.fireEvent("onLoad", this);
	},
	
	more: function() {
		this.current_page++;
		this.get_content();
	}
});

Content_Controller.implement(new Options);

