if (!window.liltype)
{
	var liltype = {};
}

liltype.FeedGetter = function (url, callback)
{
	if (callback && url)
	{
		this.url = url;
		this.callback = callback;
		this.format = google.feeds.Feed.JSON_FORMAT;
	}
}
new liltype.FeedGetter();
liltype.FeedGetter.prototype = 
{
	"url": "",
	"format": null,
	"entries": 3,
	"onLoad": function(result)
	{
		this.callback(result);
	},
	"get": function()
	{
		var feed, obj = this;
		feed = new google.feeds.Feed(this.url);
		feed.setResultFormat(this.format);
		feed.setNumEntries(this.entries);
		feed.load(function(result) { obj.onLoad(result); });
	}
};

liltype.DOMTpl = function()
{
	if (arguments.length)
	{
		this.ns = arguments[1] ? arguments[1] : ["liltype", "http://liltype.com/DOMTpl"];
		this.stack = null;
		this.tpls = {};
		this.nsRegExp = new RegExp("^"+this.ns[0]+":");
		this.parse(arguments[0]);
	}
}
new liltype.DOMTpl();

liltype.DOMTpl.prototype =
{
	"valRegExp": /\{([a-z0-9\-_\.\!]+)\}/i,
	"nsRegExp": null,
	"isArray": function(obj)
	{
		return (obj[0] !== undefined) && (obj[0] !== null) && (""+obj[0]).length;
	},
	"getAtt": function(k, atts)
	{
		var i, c, v;
		for (i = 0, c = atts.length; i < c; i++)
		{
			if (((v = atts[i][k]) !== null) && (v !== undefined))
			{
				return v;
			}
		}
		return null;
	},
	"parse": function(n)
	{
		var len = n.length, atts = [], children = null, iu, nu, id, tpln, i, c;
		
		if (typeof n == "string")
		{
			if (this.stack !== null)
			{
				this.stack.node.children.push(n);
				return;
			}
		}
		else if (len == 0)
		{
			return;
		}
		else if (len == 1)
		{
			children = [];
		}
		else if (len == 2)
		{
			if (typeof n[1] == "string")
			{
				children = [n[1]];
			}
			else if (this.isArray(n[1]))
			{
				children = n[1];
			}
			else
			{
				atts = [n[1]];
				children = [];
			}
		}
		else
		{
			nu = n[(iu = len - 1)];
			if (typeof nu == "string")
			{
				atts = n.slice(1, iu);
				children = [nu];
			}
			else if (this.isArray(nu))
			{
				atts = n.slice(1, iu);
				children = nu;
			}
			else
			{
				atts = n.slice(1);
				children = [];
			}
		}
		
		tpln =
		{
			"tag": n[0],
			"atts": atts,
			"children": []
		}
		if ((id = this.getAtt(this.ns[0]+":t", atts)) !== null)
		{
			if (this.stack !== null)
			{
				this.stack.node.children.push({"subTpl": id});
			}
			this.tpls[id] = tpln;
		}
		else if (this.stack !== null)
		{
			this.stack.node.children.push(tpln);
		}
		
		this.stack =
		{
			"node": tpln,
			"next": this.stack
		};
		for (i = 0, c = children.length; i < c; i++)
		{
			this.parse(children[i]);
		}
		this.stack = this.stack.next;
	},
	"sprintStr": function(str, vals)
	{
		var m, v;
		if ((str === undefined) || (str === null) || !str.length)
		{
			return "";
		}

		while (m = str.match(this.valRegExp))
		{
			v = vals[m[1]];
			v = ""+(((v === null) || (v === undefined)) ? "" : v);
			//v = ((v === null) || (v === undefined) || !v.length) ? "" : v;
			str = str.replace(new RegExp("\\{"+m[1]+"\\}", "g"), v);
		}
		return str;
	},
	"sprintNode": function(n, vals)
	{
		var i, c, html = "", k;
		
		if (n === undefined)
		{
			alert("Foo");
			return "";
		}

		if (typeof n == "string")
		{
			return this.sprintStr(n, vals);
		}
		else if (n.subTpl)
		{
			return this.sprintStr("{"+n.subTpl+"}", vals);
		}

		html += "<"+n.tag;
		for (i = 0, c = n.atts.length; i < c; i++)
		{
			for (k in n.atts[i])
			{
				if (!k.match(this.nsRegExp))
				{
					html += (" "+k+"=\""+this.sprintStr(n.atts[i][k], vals).replace(/\"/g, "&quot;")+"\"");
				}
			}
		}
		if (c = n.children.length)
		{
			html += ">";
			for (i = 0; i < c; i++)
			{
				html += this.sprintNode(n.children[i], vals);
			}
			html += "<"+"/"+n.tag;
		}
		else
		{
			html += "/";
		}
		html += ">";
		return html;
	},
	"sprint": function(tplK)
	{
		var tpl, vals = arguments[1] ? arguments[1] : {}, html;
		if (tpl = this.tpls[tplK])
		{
			return this.sprintNode(tpl, vals);
		}
		return "";
	},
	"sprintEach": function(tplK, valsSrc, valsTgt)
	{
		var i, c, tpl;
		if (tpl = this.tpls[tplK])
		{
			if ((valsTgt[tplK] === null) || (valsTgt[tplK] === undefined))
			{
				valsTgt[tplK] = "";
			}

			if (this.isArray(valsSrc))
			{
				for (i = 0, c = valsSrc.length; i < c; i++)
				{
					valsTgt[tplK] += this.sprintNode(tpl, valsSrc[i]);
				}
			}
			else
			{
				valsTgt[tplK] += this.sprintNode(tpl, valsSrc);
			}
		}
	}
};



var oscentral =
{
	"tpls":
	{
		"featureBoxB": new liltype.DOMTpl
		(
			["div", {"liltype:t": "main"}, {"class": "feature_box_b{class}"}, [
				["div", {"class": "box_inner_a"}, [
					["div", {"class": "box_inner_b"}, [
						["div", {"class": "box_content"}]
					]]
				]]
			]]
		)
	},
	"fixBreadCrumb": function()
	{
		var jqc = jQuery("#fw_breadcrumb"), a, aLast;
		(a = jQuery("a", jqc)).eq(0).css("display", "none").next().css("display", "none");
		a.eq(1).html("Home");
		aLast = a.eq(a.length - 1);
		if ((window.location.pathname.match(/index\.html/) ||
		     window.location.pathname.match(/\/$/)) &&
		    (aLast.attr("href").match(/index\.html/) ||
		     aLast.attr("href").match(/\/$/)))
		{
			aLast.addClass("breadcrumb_last");
		}
		else
		{
			jqc.append(" > "+document.title);
		}
	},
	"encloseFeatureBoxB": function(srcsd)
	{
		var tgt, i, c, srcs, src, vals = srcsd[2] ? srcsd[2] : {};
		srcs = jQuery(srcsd[0], srcsd[1]);
		for (i = 0, c = srcs.length; i < c; i++)
		{
			src = srcs.eq(0);
			src.before(oscentral.tpls.featureBoxB.sprint("main", vals));
			tgt = jQuery("div.box_content", src.prev());
			tgt.get(0).appendChild(src.get(0));
		}
	},
	"getInvolvedPage": function()
	{
		var jqc = jQuery("#get_involved_inner"), n, bt, isButton = true, img, isImg = true;

		/* Fix photo */
		if ((img = jQuery("div.add_photo_hid img", jqc)).length)
		{
			jQuery("div.image_container", jqc).append(jQuery("div.add_photo_hid img", jqc).eq(0));
		}
		else
		{
			isImg = false;
		}
		
		/* Fix button */
		if ((bt = jQuery("div.button_type_hid span", jqc).html()) == "no_button")
		{
			jQuery("div.button_container a", jqc).css("display", "none");
			isButton = false;
		}
		else
		{
			jQuery("div.button_container a", jqc)
				.attr("href", jQuery("div.action_link_hid span", jqc).html())
				.append('<img src="/oscentral/images/buttons/'+jQuery("div.button_type_hid span", jqc).html()+'.jpg" />');
		}
		
		if (!isImg && !isButton)
		{
			jQuery("div.image_button_container", jqc).css("display", "none");
		}
	},
	"aboutPage": function()
	{
	},
	"multimediaPage": function()
	{
	},
	"multimediaPhotoPage": function()
	{
		var jqcs = jQuery("#multimedia_inner div.media_item"), i, c, jqc, n, img, w;
		for (i = 0, c = jqcs.length; i < c; i++)
		{
			jqc = jqcs.eq(i);
			jQuery("h2", jqc).html(jQuery("h2 a", jqc).html());
			(jq = jQuery("div.photo_container", jqc)).append(img = jQuery("div.media_item_hid div.photo img", jqc));
			if (img.get(0).offsetWidth && ((new Number(img.get(0).offsetWidth)) > 0))
			{
				jq.css("width", img.get(0).offsetWidth+"px");
			}
		}
	},
	"multimediaVideoPage": function()
	{
		var jqcs = jQuery("#multimedia_inner div.media_item"), i, c, jqc, n, img;
		for (i = 0, c = jqcs.length; i < c; i++)
		{
			jqc = jqcs.eq(i);
			jQuery("h2", jqc).html(jQuery("h2 a", jqc).html());
			//(jq = jQuery("div.photo_container", jqc)).append(img = jQuery("div.media_item_hid div.photo img", jqc));
			//jq.css("width", img.get(0).offsetWidth+"px");
		}
	},
	"home":
	{
		"membersShow": 5,
		"members": function()
		{
			var i, c, ps = [], jqps = jQuery("div.member_dat", jQuery("#hp_member_profiles").prev()), jqp, img, url, p, done, picks, html;
			for (i = 0, c = jqps.length; i < c; i++)
			{
				jqp = jqps.eq(i);
				if ((img = jQuery("div.img img", jqp)).length && (url = jQuery("div.url span", jqp).html()).length)
				{
					ps.push({"url": url, "img": img});
				}
			}

			picks = {};
			c = ps.length  - 1;
			html = "";
			for (i = 0; i < this.membersShow; i++)
			{
				p = Math.round(Math.random() * c);
				done = false;
//pa = "";
				while (!done)
				{
//pa += "\n"+(p+":"+picks["_"+p]);
					if (!picks["_"+p])
					{
						done = true;
						picks["_"+p] = true;
//pa += "\n"+("Picked:"+p);
					}
					else if (++p == c)
					{
						p = 0;
					}
				}
//alert(pa+"("+i+")");
				html +=
					'<a href="'+ps[p].url+'"><img class="viz" src="'+ps[p].img.attr("src")+'" width="74" height="60" border="0"/><'+'/a>'; 
			}
			jQuery("#hp_member_profiles").prepend(html);
			jQuery("#hp_member_profiles img.viz").eq(0).addClass("first");
		},
		"midRow": function()
		{
			var jqc = jQuery("#home_row_mid");
			var i, c, jqs = jQuery("div.make_missions_happen_dats div.mission_dat", jqc), jq, jqt, f, loops = 0;
			//var fg = new liltype.FeedGetter();
			c = jqs.length;
			i = Math.round(Math.random() * (c - 1));
			while (loops < 2)
			{
				jq = jqs.eq(i);
				if (jQuery("div.name span", jq).html().length)
				{
					jqt = jQuery("div.make_missions_happen div.box_content");
					jQuery(".name", jqt).html(jQuery("div.name span", jq).html());
					jQuery(".title", jqt).html(jQuery("div.title span", jq).html());
					jQuery("a.url", jqt).attr("href", jQuery("div.url span", jq).html());
					jQuery("img.image", jqt).attr("src", jQuery("div.image img", jq).attr("src"));
					jQuery(".status", jqt).html(jQuery("div.status span", jq).html());
					this.midRowOnLoad();
					return;
				}
				else
				{
					if (++i == c)
					{
						i = 0;
						loops++;
					}
				}
			}
		},
		"midRowOnLoad": function()
		{
			var jqs = jQuery("#home_row_mid div.feature_box_a"), i, c, h = 0, n,
			    borderH = 9, paddingH = 9;
			for (i = 0, c = jqs.length; i < c; i++)
			{
				n = jqs.get(i);
				h = (n.offsetHeight > h) ? n.offsetHeight : h;
			}
			h -= (borderH + paddingH); 
			for (i = 0; i < c; i++)
			{
				jqs.css("height", h+"px");
			}
			
		},
		"promoSlides":
		{
			"count": 4,
			"ind": null,
			"timeoutMs": 5000,
			"timeoutId": null,
			"items": null,
			"navs": null,
			"init": function()
			{
				var i, c, jqcSrc, jqcSrcs = jQuery("#hp_top_promo div.top_promo_dats div.promo_dat"), jqTgts = jQuery("#hp_top_promo_container a.promo_item"), jqTgt;
				for (i = 0; i < this.count; i++)
				{
					jqcSrc = jqcSrcs.eq(i);
					jqTgt = jqTgts.eq(i);
					jqTgt.attr("href", jQuery("div.url span", jqcSrc).html());
					jqTgt.append(jQuery("div.img img", jqcSrc));
				}
				jQuery("#hp_top_promo_container").append('<div class="round_corners_l"></div><div class="round_corners_r"></div>');
				this.items = jQuery("#hp_top_promo_container a.promo_item");
				this.navs = jQuery("#hp_top_promo_container div.nav_item");
				
				this.select(0);
			},
			"select": function(ind)
			{
				var nextInd, obj = this, items = this.items, navs = this.navs;
				if (this.timeoutId)
				{
					clearTimeout(this.timeoutId);
				}
				
				if (this.ind !== null)
				{
					items.eq(this.ind).fadeOut("slow");
					navs.eq(this.ind).removeClass("nav_item_selected");
				}
				items.eq(ind).fadeIn("slow");
				navs.eq(ind).addClass("nav_item_selected");

				this.ind = ind;

				nextInd = ind + 1;
				if (nextInd == this.count)
				{
					nextInd = 0;
				}

				this.timeoutId = setTimeout(function() { obj.select(nextInd); }, this.timeoutMs);
				return false;
			}
		},
		"whatMakesYouSmile": function()
		{
			var jqc, jqs, jq, i, c, img, url, loops = 0;
			jqc = jQuery("#what_makes_you_smile");
			jqs = jQuery("div.what_makes_dats div.what_makes_dat", jqc);
			c = jqs.length -1;
			i = Math.round(Math.random() * c);
			while (loops < 2)
			{
				jq = jqs.eq(i);
				if ((img = jQuery("div.image img", jq)).length && (url = jQuery("div.url span", jq).html()).length)
				{
					jQuery("a.what_makes_top", jqc).attr("href", url).html("").append(img);
					jQuery("a.what_makes_bot", jqc).attr("href", url);
					return;
				}
				else
				{
					if (++i == c)
					{
						i = 0;
						loops++;
					}
				}
			}
		}
	},
	"studentReportsPage":
	{
		"init": function()
		{
			var f;
			f = new liltype.FeedGetter("http://community.operationsmile.org/community/feeds/blogs?community=2011", function(result) { oscentral.studentReportsPage.onload(result); });
			f.format = google.feeds.Feed.MIXED_FORMAT;
			f.entries = 3;
			f.get();
		},
		"truncate": function(str, pos, append)
		{
			var len;
			
			str = str.replace(/<[^>]+>/g, " ");
			str = str.replace(/\&nbsp;/g, " ");
			
			len = str.length;
	
			if (pos >= len)
			{
				return str;
			}
			pos--;
			len--;
			
			while ((pos < len) &&
						 !str.substr(pos, 1).match(/\s/))
			{
				pos++;
			}
			
			if (pos < len)
			{
				return str.substr(0, pos)+append;
			}
			else
			{
				return str.substr(0, len + 1);
			}
		},
		"fmtEntry": function(entry, jqc, feed)
		{
			var d = new Date(entry.publishedDate), h, ampm = "AM", n, comments = "0", nc;
			jQuery("h2.title a", jqc).html(entry.title);
			jQuery("a.post_url", jqc).attr("href", entry.link.replace(/opsmile-ext.hosted.jivesoftware.com/, "community.operationsmile.org"));
			jQuery("a.comments_url", jqc).attr("href", entry.link.replace(/opsmile-ext.hosted.jivesoftware.com/, "community.operationsmile.org"));
			if (n = entry.xmlNode.getElementsByTagName("clearspace:replyCount")[0])
			{
				comments= "";
				for (i = 0, c = n.childNodes.length; i < c; i++)
				{
					if ((nc = n.childNodes[i]).nodeType == 3)
					{
						comments += nc.nodeValue;
					}
				}
			}
			jQuery(".num_comments", jqc).html(comments);
			jQuery(".blog_post", jqc).html(this.truncate(entry.content, 512, "..."));
			jQuery(".date", jqc).html((d.getMonth() + 1)+"/"+d.getDate()+"/"+d.getFullYear());
			jQuery(".name", jqc).html(feed.author);

			if ((h = d.getHours()) == 0)
			{
				h = 12;
			}
			else if (h == 12)
			{
				ampm = "PM";
			}
			else if (h > 12)
			{
				h -= 12;
				ampm = "PM";
			}
			jQuery(".time", jqc).html(h+":"+d.getMinutes()+" "+ampm);
		},
		"onload": function(results)
		{
			var entries = results.feed.entries, jqcs = jQuery("#student_reports_container div.student_report"), i;
			for (i = 0; i < 3; i++)
			{
				this.fmtEntry(entries[i], jqcs.eq(i), results.feed);
			}
		}
	},
	"learnPage": function()
	{
		var jqc = jQuery("#learn_inner"), jqd = jQuery("div.learn_dat", jqc), img = jQuery("div.main_image img", jqd).eq(0);
		jQuery("span.main_image_container", jqc).append(img);
	},
	"learnLandingPage":  function()
	{
		var jqcs = jQuery("#learn_index div.learn_item"), i, c, jqc;
		for (i = 0, c = jqcs.length; i < c; i++)
		{
			jqc = jqcs.eq(i);
			if (!i)
			{
				jqc.addClass("learn_item_first");
			}
			jQuery("span.teaser_image_container a.learn_more_link_img", jqc).append(jQuery("div.learn_dat div.teaser_image img", jqc));
			jQuery("a.learn_more_link", jqc).attr("href", jQuery("h3 a", jqc).attr("href"));
			jQuery("a.learn_more_link_img", jqc).attr("href", jQuery("h3 a", jqc).attr("href"));
		}
	},
	"learnCtrIndexPage": function()
	{
	}
};

jQuery(document).ready
(
	function()
	/* Handle Jive Pages */
	{
		var jqc, jqs, jq, jwc3;
		if ((jqc = jQuery("#jive-wrapper")).length)
		{
			/* Add feature box Bs */
			jwc3 = jQuery("#jive-widget-container_3", jqc);
			jqs =
			[
				["div.jive-widget-communityactionswidget", jwc3],
				["#jive-body-sidebarcol div.jive-sidebar-actions", jqc, {"class": " feature_box_b-sidebar"}],
				["div.jive-widget-recentcontentwidget", jwc3, {"class": " recentcontentwidget"}]
			];
			for (i = 0, c = jqs.length; i < c; i++)
			{
				oscentral.encloseFeatureBoxB(jqs[i]);
			}

			/* Fix user bar */
			jQuery("#jive-userbar-user-tgt").append(jqc = jQuery("#jive-userbar-user"));
			jQuery(".jive-avatar", jqc).css("width", "21px").css("height", "22px");
			
			/* Fix search */
			jQuery("#jive-body").prepend(jqc = jQuery("#jive-userbar-search"));
			jQuery("#jive-query").attr("value", "Search").focus
			(
				function()
				{
					if (this.value == "Search")
					{
						this.value = "";
					}
				}
			);
			
			/* Fix right col widgets*/
			jQuery("#jive-widget-container_3 div.jive-widget").eq(0).addClass("jive-widget-first");
			jQuery("#jive-body-sidebarcol div.jive-sidebar").eq(0).addClass("jive-sidebar-first");
			
			/* Fix  photos/objects in HTML formatted text widgets */
			jQuery
			(
				".jive-widget-htmltextwidget .jive-widget-body img,"+
				".jive-widget-htmltext .jive-widget-body img,"+
				".jive-widget-htmltextwidget .jive-widget-body object,"+
				".jive-widget-htmltext .jive-widget-body object,"+
				".jive-widget-formattedtextwidget .jive-widget-body img,"+
				".jive-widget-formattedtext .jive-widget-body img,"+
				".jive-widget-formattedtextwidget .jive-widget-body object,"+
				".jive-widget-formattedtext .jive-widget-body object"
			).each
			(
				function()
				{
					var f, j;
					if ((f = (j = jQuery(this)).css("float")) == "left")
					{
						j.addClass("float_l_margin");
					}
					else if (f == "right")
					{
						j.addClass("float_r_margin");
					}
				}
			);
			
			/* Fix the weird clubs & groups bug: http://opsmile-ext.hosted.jivesoftware.com/index.jspa?showhomepage=true */
			jQuery("#jive-widgetframe-body_4522 a").addClass("jive-link-community-small");
		}
	}
);



(
	function()
	{
		var alerted = false;
		var f = function()
		{
			var n;
			if (!alerted)
			{
				if ((n = jQuery("#jive-spotlight-search-container")).length && n.children().length)
				{
					alert(n.get(0).innerHTML);
					alerted = true;
				}
				else
				{
alert("*");
					setTimeout(f, 5000);
				}
			}
		}
		//f();
	}
)();
