$(document).ready(function() {
	
	// Swap image maps
	$("area").hover(function() {
		$("#menu").attr("src","/media/onsfoundation/images/menu/"+$(this).attr("class")+".jpg");
	}, function() {
		$("#menu").attr("src","/media/onsfoundation/images/menu/inactive.jpg");
	});
	
	// Expand Donate box when clicked
	$("#tribute").click(function() {
		if ($("#donate div.tribute").css("display")=="none") {
			$("#donate div.tribute").blur();
			$("#donate div.tribute").slideDown();
		} // if ($("#donate .tribute").css("display")=="none")
	});
	
	// Format deadlines
	$("span.deadline").each(function() {
		now = new Date;
		year = now.getFullYear();
		dl = $(this).html().split("/");
		month = dl[0];
		day = dl[1];
		deadline = new Date(year,month-1,day,23,59,59,0);
		now = new Date();
		if (deadline<now) { year = year + 1; }
		$(this).html(month+"/"+day+"/"+year);
	});

	$("#donate #tribute").attr("checked",false);
	
	// Load latest tweet into footer
	var feed = "http://twitter.com/statuses/user_timeline/onsfoundation.json?count=1&callback=?";
	$.getJSON(feed,function(data) {
		$.each(data, function(i,item) {
			var txt = markupTweet(item.text);
			var url = "http://twitter.com/"+item.user.screen_name+"/status/"+item.id;
			$("#social").prepend('<h3><a href="'+url+'">Latest Tweet</a></h3><p id="tweet"></p>');
			$("#tweet").html(txt);
		});
	});
	
	// Zebra-stripe tables
	$("table tr:even").addClass("alt");
	
	// Load external links in a new window
	$("a").each(function() {
		if ($(this).attr("rel")=="external") {
			this.target = "_blank";
		} // if ($(this).attr("rel")=="external")
	});
	
	// Open YouTube links in a modal window
	$("a[rel=youtube]").click(function(e) {
		e.preventDefault();
		var classes = $(this).attr("class").split(" ");
		var ytid = "";
		for (var i=0; i<classes.length; i++) {
			if (classes[i]!="youtube") { ytid = classes[i]; }
		} // for (var i=0; i<classes.length; i++)
		if ($("#mask").length==0) {
			$("body").append('<div id="mask"></div>');
			$("#mask").css("height",$(document).height());
		} // if ($("#mask").length==0)
		$("#mask").fadeTo("fast",0.8);
		var a = [
			self.pageXOffset ||
			document.documentElement.scrollLeft ||
			document.body.scrollLeft,
			self.pageYOffset ||
			document.documentElement.scrollTop ||
			document.body.scrollTop
			];
		$(window).bind("scroll",{pos: a},scrollLock);
		$("#mask").click(function() {
			$("#ytwin").remove();
			$(this).fadeTo("fast",0, function() {
				$("#mask").remove();
			});
			$(window).unbind("scroll");
			$(".window").fadeOut();
		});
		$("body").append('<div id="ytwin"></div>');
		var url = "http://www.youtube.com/v/"+ytid+"&amp;hl=en&amp;fs=1&amp;rel=0&amp;autoplay=1";
		document.getElementById("ytwin").innerHTML = '<object id="ytwin" width="482" height="290" type="application/x-shockwave-flash" data="'+url+'"><param name="bgcolor" value="#000000"></param><param name="allowfullscreen" value="true"></param><param name="movie" value="'+url+'"></param></object>';
		$("#ytwin").css("top",($(window).height()/2)-145);
		$("#ytwin").css("left",($(window).width()/2)-241);
	});
	
	$("#find h2").click(function() {
		if ($("#find ul").is(":visible")) {
			$("#find ul").slideUp();
		} else {
			$("#find ul").slideDown();
		} // if ($("#find ul").is(":visible"))
	});
	
});

function markupTweet(tweet) {
	var t = "";
	var words = tweet.split(" ");
	for (var i=0;i<words.length;i++) {
		if (words[i].indexOf("@")==0) {
			t += '<a href="http://twitter.com/'+words[i].substr(1)+'">'+words[i]+'</a>';
		} else if (words[i].indexOf("#")==0) {
			t += '<a href="http://search.twitter.com/search?q=%23'+words[i].substr(1)+'">'+words[i]+'</a>';
		} else if (words[i].indexOf("http://")==0) {
			t += '<a href="'+words[i]+'">'+words[i]+'</a>';
		} else {
			t += words[i];
		} // if (words[i].indexOf("@")==0)
		if (i+1<words.length) {
			t += " ";
		} // if (i+1<words.length)
	} // for (var i=0;i<words.length;i++)
	return t;
} // function markupTweet(tweet)

function scrollLock(e) {
	var a = e.data.pos;
	window.scrollTo(a[0],a[1]);
	return false;
} // function scrollLock(e)
