var opsmile =
{
	"firstP": function(ctxt)
	/* attach "first" className to all first <p>s */
	{
		var prev, p, ps, i, c;
		ps = jQuery("p", ctxt);
		for (i = 0, c = ps.length; i < c; i++)
		{
			prev = (p = ps.eq(i)).prev();
			if (prev.length)
			{
				if (prev.get(0).tagName.toLowerCase() != "p")
				{
					p.addClass("first");
				}
			}
			else
			{
				p.addClass("first");
			}
		}
	},
	"firstHs": function(tag, ctxt)
	/* attach "first" className to all first <h(1|2|3)>s */
	{
		var hs = jQuery(tag, ctxt), h, i, c;
		for (i = 0, c = hs.length; i < c; i++)
		{
			if (!(h = hs.eq(0)).prev().length)
			{
				h.addClass("first");
			}
		}
	},
	"firstEls": function(ctxt)
	/* attach "first" className to certain elements that are first */
	{
		ctxt = ctxt ? ctxt : jQuery(document);
		this.firstP(ctxt);
		this.firstHs("h2", ctxt);
	},
	"elementClickA": function()
	{
		window.location = jQuery("a", this).attr("href");
	},
	"fixPrimaryNav": function(selector, navAreaCode)
	{
		var jqc = jQuery(selector), i, c, ns, n, href, hrefTgt;
		jQuery("div.tab", jqc).click(this.elementClickA).
			mouseover
			(
				function()
				{
					jQuery(this).addClass("over");
				}
			).
			mouseout
			(
				function()
				{
					jQuery(this).removeClass("over");
				}
			);

		ns = jQuery("a", jqc);
		hrefTgt = window.location.pathname;
		for (i = 0, c = ns.length; i < c; i++)
		{
			n = ns.eq(0);
			href = n.get(0).href.replace(/#[^#]*$/, "").replace(/^[^:]+:\/\/[^\/]+\//, "/");
		}
	},
	"smileCounter":
	{
		"numHeight": 54,
		"count": function(jqc, smiles)
		{
			var pct, off, nextNum;
			if (smiles.length > 6)
			{
				smiles = smiles.substr(smiles.length - 6);
			}
			
			jQuery("div.digit_1000s", jqc).html(this.sprintNum(smiles.substr(0, 1)));
			jQuery("div.digit_100s", jqc).html(this.sprintNum(smiles.substr(1, 1)));
			jQuery("div.digit_10s", jqc).html(this.sprintNum(smiles.substr(2, 1)));

			jQuery("div.digit_1s", jqc).html(this.sprintNum(smiles.substr(3, 1)));
			pct = new Number(smiles.substr(5));
			if (pct != 0)
			{
				off = Math.ceil(this.numHeight * -1 * (pct / 10));
				nextNum = ""+((new Number(smiles.substr(3, 1))) + 1);
				nextNum = (nextNum == 10) ? "0" : nextNum;
				jQuery("div.digit_1s div.num", jqc).css("top", off+"px");
				jQuery("div.digit_1s", jqc).append(this.sprintNum(nextNum));
				jQuery("div.digit_1s div.num_"+nextNum, jqc).css("top", (off + this.numHeight)+"px");
			}

			if (window.correctPNGImg)
			{
				var i, c, imgs = jQuery("div.smile_counter_digits img", jqc);
				for (i = 0, c = imgs.length; i < c; i++)
				{
					correctPNGImg(imgs.get(i));
				}
			}
		},
		"sprintNum": function(n)
		{
			return '<div class="num num_'+n+'"><img src="images/smile-counter/num-'+n+'.png" alt="'+n+'" width="49" height="54" /><'+'/div>';
		}
	},
	"elNube":
	{
		"maxCollisions": 32,
		"paddingX": 2,
		"paddingY": 2,
		"dbgId": null,
		"dbg": function(msg)
		{
			var c;
			if (this.dbgId)
			{
				if ((c = jQuery("#"+this.dbgId)).length)
				{
					c.html(msg+"\n"+c.html());
				}
			}
		},
		"init": function(id)
		{
			var canvas, tags, tag, tagN, i, c, ii, cc, x, y, iii, ccc, tagsPlaced, tagPlaced, collision;
			canvas = jQuery("#"+id);
			//this.dbgId = id+"_debug";
			width = canvas.get(0).offsetWidth;
			height = canvas.get(0).offsetHeight;
			tagsPlaced = [];

			tags = jQuery(".team", canvas);

			for (i = 0, c = tags.length; i < c; i++)
			{
				tagN = (tag = tags.eq(i)).get(0);
				tag.css("width", tagN.offsetWidth + 2).css("height", tagN.offsetHeight + 2);
				//tag.html(i+":"+tag.html());
				for (ii = 0; ii < this.maxCollisions; ii++)
				{
					x = Math.round(Math.random() * (width - tagN.offsetWidth));
					y = Math.round(Math.random() * (height - tagN.offsetHeight));
					collision = false;
					for (iii = 0, ccc = tagsPlaced.length; iii < ccc; iii++)
					{
						tagPlaced = tagsPlaced[iii];
						
						if
						(
							(
								(
									(x > tagPlaced[0])
									&&
									(x < tagPlaced[2])
								)
								||
								(
									((x + tagN.offsetWidth) > tagPlaced[0])
									&&
									((x + tagN.offsetWidth) < tagPlaced[2])
								)
								||
								(
									(x < tagPlaced[0])
									&&
									((x + tagN.offsetWidth) > tagPlaced[2])
								)
							)
							&&
							(
								(
									(y > tagPlaced[1])
									&&
									(y < tagPlaced[3])
								)
								||
								(
									((y + tagN.offsetHeight) > tagPlaced[1])
									&&
									((y + tagN.offsetHeight) < tagPlaced[3])
								)
								||
								(
									(y < tagPlaced[1])
									&&
									((y + tagN.offsetHeight) > tagPlaced[3])
								)
							)
						)
						{
							this.dbg("collision("+ii+")):"+tag.html().replace(/[<>]/g, "")+":("+x+","+y+") - ("+width+" - "+tagN.offsetWidth+","+height+" - "+tagN.offsetHeight+")");
							collision = true;
							break;
						}
					}
					if (!collision)
					{
						break;
					}
				}

				if (collision)
				{
					this.dbg(i+":Placed w/ Collision");
					tag.css("display", "none");
				}
				else
				{
					tag.css("left", x+"px");
					tag.css("top", y+"px");
					tagsPlaced.push([x - this.paddingX, y - this.paddingY, x + tagN.offsetWidth + this.paddingX, y + tagN.offsetHeight + this.paddingY]);
					this.dbg(i+":Placed OK");
				}
			}
		}
	},
	"elNube2":
	{
		"dbgId": null,
		"dbg": function(msg)
		{
			var c;
			if (this.dbgId)
			{
				if ((c = jQuery("#"+this.dbgId)).length)
				{
					c.html(msg+"\n"+c.html());
				}
			}
		},
		"initOld": function(id)
		{
			var canvas, tags, tag, tagN, i, c, offsetMax = 8, offsetMax2 = offsetMax * 2;
			canvas = jQuery("#"+id);
			tags = jQuery(".team", canvas);
			for (i = 0, c = tags.length; i < c; i++)
			{
				tagN = (tag = tags.eq(i)).get(0);
				x = Math.round(Math.random() * offsetMax2) - offsetMax;
				y = Math.round(Math.random() * offsetMax2) - offsetMax;
				tag.css("left", x+"px");
				tag.css("top", y+"px");
			}
		},
		"init": function(id)
		{
			var canvas, tags, tag, tagN, i, c, offsetMaxX = 4, offsetMaxY = 5, x, y, tagDats = [];
			//this.dbgId = id+"_debug";
			canvas = jQuery("#"+id);
			canvas.css("width", canvas.get(0).offsetWidth+"px").css("height", canvas.get(0).offsetHeight+"px")
			tags = jQuery(".team", canvas);
			for (i = 0, c = tags.length; i < c; i++)
			{
				tagN = (tag = tags.eq(i)).get(0);
				x = (Math.round(Math.random() * offsetMaxX) * (Math.round(Math.random()) ? 1 : -1)) + tagN.offsetLeft;
				y = (Math.round(Math.random() * offsetMaxY) * (Math.round(Math.random()) ? 1 : -1)) + tagN.offsetTop;
				this.dbg(i+":"+tagN.offsetLeft+","+tagN.offsetTop+" - "+x+","+y);
				tagDats.push([tag, x, y]);
				//tag.css("position", "absolute").css("left", x+"px").css("top", y+"px");
			}
			
			for (i = 0, c = tagDats.length; i < c; i++)
			{
				tag = tagDats[i];
				this.dbg(i+":fug:"+tag[1]+","+tag[2]);
				tag[0].css("position", "absolute").css("left", tag[1]+"px").css("top", tag[2]+"px");
			}
		}
	},
	"embedVid": function()
	{
		var vidURL = "http://support.operationsmile.org/twitcon09/media/twitter2.mov", width = 606, height = 384;
		document.write
		(
			'<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" '+
					'width="'+width+'" height="'+height+'" '+
					'codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0" '+
					'align="middle" >'+
				'<param name="src" value="'+vidURL+'" />'+
				'<param name="autoplay" value="true" />'+
				'<embed src="'+vidURL+'" width="'+width+'" height="'+height+'" '+
						'pluginspage=http://www.apple.com/quicktime/download/ '+
						'align="middle" autoplay="true" bgcolor="black" >'+
				'</embed>'+
			'</object>'
		);
	}
};

jQuery(document).ready
(
	function()
	{
/*
		jQuery("a[href*='index.php']").each
		(
			function()
			{
				var jq = jQuery(this);
				jq.attr("href", jq.attr("href").replace(/index\.php/, "index-live.php"));
			}
		);
*/
	}
);