var delayedExec =
{
	"handlers": [],
	"add": function(h)
	{
		if (!this.handlers[h.flag])
		{
			this.handlers[h.flag] = [];
		}
		this.handlers[h.flag].push(h);
	},
	"exec": function(flag)
	{
//SWFHACK alert(flag);
 		var i, c, handlers = this.handlers[flag];
		if (handlers)
		{
			for (i = 0, c = handlers.length; i < c; i++)
			{
				handlers[i].exec();
			}
		}
	}
};

ssc = new Object();

/*
	START: Utility funcs
*/
	ssc.communityAbsURL = function(href)
	{
		var m;
		if (m = href.match(/^https?:\/\/connect/i))
		{
			// Do nothing
		}
		else if (m = href.match(/^(https?:\/\/[^\/]+)(\/.*)$/i))
		{
			href =
				"http://connect.sierraclub.org"+
				m[2];
		}
		else
		{
			href =
				"http://connect.sierraclub.org"+
				href;
		}
		return href;
	}
	
	ssc.truncator = 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);
		}
	}
/*
	END: Utility funcs
*/

/*
	START: Home recently joined
*/
	ssc.fixRecentlyJoinedHP = function()
	{
		delayedExec.add
		(
			{
				"flag": "tpBulkLoaded",
				"exec": function()
				{
					var jqc = jQuery("div#homeRight div.recently_joined div.tp_module");
					var a, img, i, c, html = "", itemsPerRow = 6, col = 0;
					a = jQuery("a", jqc);
					img = jQuery("img", jqc);
			
					html += '<ul class="reJoin">';
					for (i = 0, c = a.length; i < c; i++)
					{
						html +=
						 '<li><a href="'+a.eq(i).attr("href")+'"><img src="'+img.eq(i).attr("src")+'" alt="'+img.eq(i).attr("alt")+'" width="28" height="28" /></a></li>';
						if (++col == itemsPerRow)
						{
							html += '</ul><ul class="reJoin">';
							col = 0;
						}
					}
					html += '</ul>';
					
					jqc.after(html);
					jqc.css("display", "none");
				}
			}
		);
	}
/*
	END: Home recently joined
*/


/*
	START: Home groups box
*/
	ssc.fixHPGroupsBox = function()
	{
		var fixer = function()
		{
			var left = jQuery("div#"+this.divId+" div.tp_module div div").eq(0);
			var right = left.next(), col, i, c, a;
			
			/* Need to do this because the Port uses inline styles */
			left.addClass("clearfix").css("width", "108px").css("height", "auto").css("margin", "0px 6px 10px 0px");
			right.addClass("clearfix").css("width", "108px").css("height", "auto").css("margin", "0px 0px 10px 0px");

			jQuery("div", left).css("float", "none").css("width", "auto").css("height", "auto").css("text-align", "center");
			jQuery("div", right).css("float", "none").css("width", "auto").css("height", "auto").css("text-align", "center");

			var fixa = function(n)
			{
				n.attr("href", ssc.communityAbsURL(n.attr("href")));
			}

			a = jQuery("a", left);
			for (i = 0, c = a.length; i < c; i++)
			{
				fixa(a.eq(i));
			}

			a = jQuery("a", right);
			for (i = 0, c = a.length; i < c; i++)
			{
				fixa(a.eq(i));
			}
			
			if (this.heightAdj)
			{
				this.heightAdj(0);
			}
		}

		delayedExec.add
		(
			{
				"flag": "tpBulkLoaded",
				"divId": "st1Div",
				"heightAdj": null,
				"exec": fixer
			}
		);

		var heightAdj = function(iter)
		{
			if (iter > 5)
			{
				return;
			}

			var leftBox = jQuery("div#st1Div").css("display", "block"), rightBox = jQuery("div#st2Div").css("display", "block");
			var left = leftBox.get(0).offsetHeight, right = rightBox.get(0).offsetHeight;
			if (left > right)
			{
				rightBox.parent().css("height", left+"px");
			}
			else
			{
				rightBox.parent().css("height", right+"px");
			}

			if (leftBox.hasClass("switchContentOn"))
			{
				leftBox.css("display", "block");
				rightBox.css("display", "none");
			}
			else
			{
				leftBox.css("display", "none");
				rightBox.css("display", "block");
			}
			
			setTimeout(function() { heightAdj(iter + 1); }, 50);
		};

		delayedExec.add
		(
			{
				"flag": "tpBulkLoaded",
				"divId": "st2Div",
				"heightAdj": heightAdj,
				"exec": fixer
			}
		);
	}
/*
	END: Home groups box
*/

/*
	START: Share save box
*/
	jQuery(document).ready
	(
		function()
		{
			var jqc = jQuery("div#share_save_box");
			
			/* Face book */
			jQuery("a.facebook", jqc).click
			(
				function ()
				{
					var u=location.href;
					var t=document.title;
					window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
					return false;
				}
			);
			
			/* Myspace */
			jQuery("a.myspace", jqc).click
			(
				function ()
				{
					var u=location.href;
					var t=document.title;
					window.location = 'http://www.myspace.com/Modules/PostTo/Pages/?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t);
					return false;
				}
			);

			/* Digg */
			jQuery("a.digg", jqc).click
			(
				function ()
				{
					var u=location.href;
					var t=document.title;
					window.location = 'http://digg.com/submit?phase=2&url='+encodeURIComponent(u)+'&t='+encodeURIComponent(t);
					return false;
				}
			);

			/* Delicious */
			jQuery("a.delicious", jqc).click
			(
				function ()
				{
					var u=location.href;
					var t=document.title;
					window.location = 'http://del.icio.us/post?url='+encodeURIComponent(u)+'&title='+encodeURIComponent(t);
					return false;
				}
			);

			/* Reddit */
			jQuery("a.reddit", jqc).click
			(
				function ()
				{
					var u=location.href;
					var t=document.title;
					window.location = 'http://reddit.com/submit?url='+encodeURIComponent(u)+'&title='+encodeURIComponent(t);
					return false;
				}
			);

			/* Newsvine */
			jQuery("a.newsvine", jqc).click
			(
				function ()
				{
					var u=location.href;
					var t=document.title;
					window.location = 'http://www.newsvine.com/_tools/seed&save?url='+encodeURIComponent(u)+';title='+encodeURIComponent(t);
					return false;
				}
			);

			/* Yahoo */
			jQuery("a.yahoo", jqc).click
			(
				function ()
				{
					var u=location.href;
					var t=document.title;
					window.location = 'http://buzz.yahoo.com/submit?submitUrl='+encodeURIComponent(u)+'&submitHeadline='+encodeURIComponent(t);
					return false;
				}
			);

			/* Yahoo */
			jQuery("a.email", jqc).click
			(
				function ()
				{
					var body = encodeURIComponent("Check out this page from Sierra Student Coalition:\n"+window.location.href+"\nThanks");
					window.location = "mailto:?subject="+encodeURIComponent("Sierra Student Coalition")+"&body="+body;
					return false;
				}
			);
		}		
	);
/*
	END: Share save box
*/


/*
	START: Port widget fixers
*/
	ssc.clearPortInlineStyle = function(ctxt)
	{
		jQuery("*", ctxt).attr("style", "");
	}

	ssc.fixRelatedDiscussionOLD = function()
	{
		var jqc = jQuery("div#related_port_discussions");
		var tpmod = jQuery("div.tp_module", jqc);
		ssc.clearPortInlineStyle(tpmod);
		jQuery("div *", tpmod).addClass("hidden");
		jQuery("a", tpmod).removeClass("hidden").css("display", "block");
	}

	ssc.fixRelatedDiscussion = function()
	{
	}

	ssc.fixCommunityTips = function()
	{
		delayedExec.add
		(
			{
				"flag": "tpBulkLoaded",
				"exec": function()
				{
					var i = 0, div, jqc = jQuery("div#rcol_community_tips_comments");
			
					div = jQuery("div.tp_module div div", jqc).eq(0);
					while ((i++ < 3) && (div.length))
					{
						div
							.css("float", "none")
							.css("width", "179px")
							.css("height", "auto")
							.addClass("clearfix");
						jQuery("div", div).eq(0)
							.css("width", "85px")
							.css("height", "auto")
							.next()
							.css("width", "85px")
							.css("height", "auto");
						div = div.next();
					}
			
					jQuery("a", jqc).each
					(
						function(i)
						{
							jQuery(this).attr("href", ssc.communityAbsURL(this.href));
						}
					);
					//this.clearPortInlineStyle(jQuery("div.tp_module", jqc));
					//jQuery("a", jqc).attr("class", "");
				}
			}
		);
	}
/*
	END: Port widget fixers
*/


/*
	START: Determine Login Status
*/
	ssc.validSSOLogin = null;
	ssc.validSSOInited = false;
	ssc.loginCheckLimit = 200;
	ssc.loginCheckCount = 0;
	ssc.loginCheckTimeout = 50;
	ssc.ssoCookieKey = Math.round(Math.random() * 1000000);
	ssc.loginHandlers = [];

	ssc.getSSOLogin = function(isFromTimeout)
	{
		var m, checkAgain = true, i, c, ssoLogin;

		if ((this.validSSOLogin !== null) ||
		    (!isFromTimeout && this.validSSOInited))
		/* If already started looking or already found status, stop */
		{
			return;
		}

		this.validSSOInited = true;

		if (this.loginCheckCount++ > this.loginCheckLimit)
		/* If looked too many times, stop */
		{
			this.validSSOLogin = false;
			return;
		}

		if (m = document.cookie.match(new RegExp("(^|\\s|;)cvUserName=([^;]+)(;|$)")))
		{
			if (m[2].match(/\*/))
			{
				ssoLogin = false;
			}
			else
			{
				ssoLogin = true;
			}

			m = decodeURIComponent(m[2]).split(/:/);
			if (m && m.length && (m[0] == this.ssoCookieKey))
			{
				checkAgain = false;
				this.validSSOLogin = ssoLogin;
			}

			for (i = 0, c = this.loginHandlers.length; i < c; i++)
			{
				this.loginHandlers[i](ssoLogin);
			}
		}
		
		if (checkAgain)
		{
			setTimeout(function () { ssc.getSSOLogin(true) }, this.loginCheckTimeout);
		}
	}
	
	ssc.redirectLoginIndex = function()
	{
	}
	
	ssc.showLoginHeader = function()
	{
		ssc.loginHandlers.push
		(
			function(validSSOLogin)
			{
				var jqc, i, c, nexturl, links, loc;
				jqc = jQuery("div#fw_header_user_links");

				if (validSSOLogin)
				{
					jQuery("div.header_user_links_in", jqc).css("display", "block");
					jQuery("div.header_user_links_out", jqc).css("display", "none");
				}
				else
				{
					jQuery("div.header_user_links_in", jqc).css("display", "none");
					jQuery("div.header_user_links_out", jqc).css("display", "block");
				}
	
				links = jQuery("a.nexturl", jqc);
				if (!links.eq(0).attr("href").match(/NEXTURL/))
				{
					loc = encodeURIComponent(window.location.href);
					for (i = 0, c = links.length; i < c; i++)
					{
						links.eq(i).attr("href", links.eq(i).attr("href")+"?NEXTURL="+loc);
					}
				}
			}
		);
		ssc.getSSOLogin();
	}
	
	
	ssc.showLoginRightCol = function()
	{
		ssc.loginHandlers.push
		(
			function(validSSOLogin)
			{
				if (validSSOLogin)
				{
					jQuery("div#fw_home_join_community").css("display", "none");
					jQuery("div#fw_member_welcome").css("display", "block");
				}
				else
				{
					jQuery("div#fw_home_join_community").css("display", "block");
					jQuery("div#fw_member_welcome").css("display", "none");
				}
			}
		);
		
		ssc.getSSOLogin();
	}
/*
	END: Determine Login Status
*/


/*
	START: fix homepage community actions
*/
	ssc.fixHPCommunityActions = function()
	{
		delayedExec.add
		(
			{
				"flag": "tpBulkLoaded",
				"exec": function()
				{
					var jqc = jQuery("div#hp_community_actions div.tp_module").css("display", "none");
					var a, html = "", n, c = 2;
					for (i = 0, n = jQuery("div div", jqc).eq(0); (i < c) && (n.length); i++, n = n.next())
					{
						a = jQuery("a.boldlnk12", n);
						html +=
						(
							'<b>'+a.html()+'</b>'+
							'<br/>'+
							'<a title="Details" href="'+a.attr("href")+'">Take Action</a>'+
							'<br/><br/>'
						);
					}
					jqc.after(html);
				}
			}
		);
	}
/*
	START: fix homepage community actions
*/


/*
	START: fix community campaigns
*/
	ssc.fixCommunityCampaigns = function()
	{
		delayedExec.add
		(
			{
				"flag": "tpBulkLoaded",
				"exec": function()
				{
					var jqc = jQuery("div#rcol_community_campaigns div.tp_module").css("display", "none");
					var a, img, members, html = "", n, c = 2;
					for (i = 0, n = jQuery("div div", jqc).eq(0); (i < c) && (n.length); i++, n = n.next())
					{
						img = jQuery("img", n).eq(0);
						a = jQuery("a.boldlnk12", n);
						members = jQuery("span", a.parent()).eq(1);
						html +=
						(
							'<div class="clearfix" style="padding: 6px 0px;">'+
								'<div style="float: left; display: inline; width: 75px; padding-right: 4px;">'+
									'<a href="'+a.attr("href")+'"><img src="'+ssc.communityAbsURL(img.attr("src"))+'" width="75" height="75" alt="'+a.html()+'"/></a>'+
								'<'+'/div>'+
								'<div style="float: left; display: inline; width: 110px;">'+
									'<a href="'+a.attr("href")+'">'+a.html()+'</a>'+
									'<br/>'+
									members.html()+' Members'+
								'<'+'/div>'+
							'<'+'/div>'
						);
					}
					jqc.after(html);
				}
			}
		);
	}
/*
	END: fix community campaigns
*/


/*
	START: fix center col community actions
*/
	ssc.fixCColCommunityActions = function()
	{
		var maxH = 0;
		var fixBox = function(action, maxH)
		{
			var h, maxH, fixHref = function(i)
			{
				var jq = jQuery(this);
				jq.attr("href", ssc.communityAbsURL(jq.attr("href")));
			}
	
			while (action.length)
			{
				action.addClass("clearfix").css("width", "140px").css("height", "auto").css("margin", "0px 3px");
				jQuery("*", action).css("float", "none").css("text-align", "center").css("height", "auto");
				jQuery("a", action).each(fixHref);
				maxH =
					((h = action.get(0).offsetHeight) > maxH) ?
					h :
					maxH;
				action = action.next();
			}
			return maxH;
		}
		maxH = fixBox(jQuery("div#cc_com_acts_st1Div div.tp_module div").eq(0), maxH);
		maxH = fixBox(jQuery("div#cc_com_acts_st2Div div.tp_module div").eq(0), maxH);
		jQuery("div#cc_com_acts_st1Div").parent().css("height", maxH+"px");
/*
		var jqc = jQuery("div#cc_com_acts_st1Div div.tp_module").css("display", "none");
		var a, img, title, html = "", n, divs, c;
		
		var procAct = function(n, i)
		{
			var img, a, html, title;
			img = jQuery("img", n).eq(0);
			a = jQuery("a", n).eq(1);
			title = ssc.truncator(a.html(), 45, '');
			html =
			(
				'<div class="ccol_community_action'+(!i ? ' first' : '')+'">'+
					'<a href="'+a.attr("href")+'"><img src="'+ssc.communityAbsURL(img.attr("src"))+'" width="84" height="84" alt="'+title+'"/></a>'+
					'<br/>'+
					'<a href="'+a.attr("href")+'">'+title+'</a>'+
				'<'+'/div>'
			);
			return html;
		}

		n = jQuery("div", jqc).eq(0);
		html += procAct(n, 0);
		divs = n.siblings();
		for (i = 0, c = divs.length; i < c; i++)
		{
			if (jQuery("div", divs.eq(i)).length)
			{
				html += procAct(divs.eq(i), i + 1);
			}
		}
		jqc.after('<div class="clearfix">'+html+'</div>');

		jqc = jQuery("div#cc_com_acts_st2Div div.tp_module").css("display", "none")
		html = "";
		c = 3;
		for (i = 0, n = jQuery("div.tp_boldtxt10", jqc).eq(i); (i < c) && (n.length); i++, n = jQuery("div.tp_boldtxt10", jqc).eq(i))
		{
			img = jQuery("img", n).eq(0);
			a = jQuery("a", n).eq(1);
			title = this.truncator(a.html(), 45, '');
			html +=
			(
				'<div class="ccol_community_action'+(!i ? ' first' : '')+'">'+
					'<a href="'+a.attr("href")+'"><img src="'+img.attr("src")+'" width="84" height="84" alt="'+title+'"/></a>'+
					'<br/>'+
					'<a href="'+a.attr("href")+'">'+title+'</a>'+
				'<'+'/div>'
			);
		}
		jqc.after('<div class="clearfix">'+html+'</div>');
*/
	}
/*
	START: fix center col community actions
*/


/*
	START: fix homepage community created events
*/
	ssc.fixHPCommunityCreatedEvents = function()
	{
		delayedExec.add
		(
			{
				"flag": "tpBulkLoaded",
				"exec": function()
				{
					jQuery("div#hp_community_created_events div.tp_module div.eventcontainer").css("padding", "0px 0px 15px 0px");
					jQuery("div#hp_community_created_events div.tp_module div.eventcontainer div.eventlnk").each(function(i) {
							var a = jQuery(this).find("a");
							a.addClass("nl");
							a.addClass("bld");
							if (match = a.attr('href').match(/^(https?:\/\/[^\/]+\/)/)) {
									a.attr('href', 'http://connect.sierraclub.org/' + a.attr('href').substr(match[1].length));
							} else {
									a.attr('href', 'http://connect.sierraclub.org' + a.attr('href'));
							}
					});
					jQuery("div#hp_community_created_events div.tp_module div.eventcontainer div.eventtxt").each(function(i) {
							var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'], m;
							m = jQuery(this).text().match(/^(.+[0-9]+:[0-9]+)/);
							if (m)
							{
								var date = new Date(m[1]);
								jQuery(this).text(months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear());
							}
					});
				}
			}
		);
	}
/*
	START: fix homepage community created events
*/