(function(d){var c=function(g,f){return(g<<f)|(g>>>(32-f))};var b=function(j){var f="";var g;var k;var h;for(g=0;g<=6;g+=2){k=(j>>>(g*4+4))&15;h=(j>>>(g*4))&15;f+=k.toString(16)+h.toString(16)}return f};var a=function(j){var g="";var h;var f;for(h=7;h>=0;h--){f=(j>>>(h*4))&15;g+=f.toString(16)}return g};var e=function(g){g=g.replace(/\x0d\x0a/g,"\x0a");var f="";for(var i=0;i<g.length;i++){var h=g.charCodeAt(i);if(h<128){f+=String.fromCharCode(h)}else{if((h>127)&&(h<2048)){f+=String.fromCharCode((h>>6)|192);f+=String.fromCharCode((h&63)|128)}else{f+=String.fromCharCode((h>>12)|224);f+=String.fromCharCode(((h>>6)&63)|128);f+=String.fromCharCode((h&63)|128)}}}return f};d.extend({sha1:function(f){var l;var x,w;var g=new Array(80);var o=1732584193;var n=4023233417;var m=2562383102;var k=271733878;var h=3285377520;var v,t,s,r,q;var y;f=e(f);var p=f.length;var u=new Array();for(x=0;x<p-3;x+=4){w=f.charCodeAt(x)<<24|f.charCodeAt(x+1)<<16|f.charCodeAt(x+2)<<8|f.charCodeAt(x+3);u.push(w)}switch(p%4){case 0:x=2147483648;break;case 1:x=f.charCodeAt(p-1)<<24|8388608;break;case 2:x=f.charCodeAt(p-2)<<24|f.charCodeAt(p-1)<<16|32768;break;case 3:x=f.charCodeAt(p-3)<<24|f.charCodeAt(p-2)<<16|f.charCodeAt(p-1)<<8|128;break}u.push(x);while((u.length%16)!=14){u.push(0)}u.push(p>>>29);u.push((p<<3)&4294967295);for(l=0;l<u.length;l+=16){for(x=0;x<16;x++){g[x]=u[l+x]}for(x=16;x<=79;x++){g[x]=c(g[x-3]^g[x-8]^g[x-14]^g[x-16],1)}v=o;t=n;s=m;r=k;q=h;for(x=0;x<=19;x++){y=(c(v,5)+((t&s)|(~t&r))+q+g[x]+1518500249)&4294967295;q=r;r=s;s=c(t,30);t=v;v=y}for(x=20;x<=39;x++){y=(c(v,5)+(t^s^r)+q+g[x]+1859775393)&4294967295;q=r;r=s;s=c(t,30);t=v;v=y}for(x=40;x<=59;x++){y=(c(v,5)+((t&s)|(t&r)|(s&r))+q+g[x]+2400959708)&4294967295;q=r;r=s;s=c(t,30);t=v;v=y}for(x=60;x<=79;x++){y=(c(v,5)+(t^s^r)+q+g[x]+3395469782)&4294967295;q=r;r=s;s=c(t,30);t=v;v=y}o=(o+v)&4294967295;n=(n+t)&4294967295;m=(m+s)&4294967295;k=(k+r)&4294967295;h=(h+q)&4294967295}var y=a(o)+a(n)+a(m)+a(k)+a(h);return y.toLowerCase()}})})(jQuery);

$("#currency a").live("click", function(event) {
	event.preventDefault();
	$body.removeClass("american canadian").addClass($(this).text().match(/\w+$/).toString().toLowerCase());
	if ($body.data("firstname"))
		$.post("action.php", {action: "canadian", firstname: $body.data("firstname"), lastname: $body.data("lastname"), canadian: Number($body.hasClass("canadian"))});
});
$("div.people>a").live("click", function(event) {
	event.preventDefault();
	if (($friend = $("input[name=friend]").val()) && $friend.search(/\S\s+\S/) == -1)
		alert("Your last name is required.");
	else
		$.post("action.php", {action: ($link = $(this)).text().toLowerCase(), day: ($day = $link.closest("li")).data("day"), ride: $day.data("ride"), room: ($room = $day.parent().parent()).index() + 1, conid: ($con = $room.closest("section")).data("conid"), conname: $con.find("h2").text(), userid: $con.data("userid"), firstname: $body.data("firstname"), lastname: $body.data("lastname"), canadian: Number($body.hasClass("canadian")), friend: $friend}, function(data) {
			$room.closest("div").replaceWith(data);
			if (!$con.data("userid"))
				$con.data("userid", $con.find("div.people>div.user").data("userid"));
			else if (!$con.find("div.people>div.user").length)
				$con.removeData("userid");
		});
});
$("div.people>a[title^=M]").live("hover", function() {
	$(this).parents("ul").eq(1).find("li[data-day=" + $(this).closest("li").data("day") + "]").children().toggleClass("hover");
});
$("div[data-userid], li[data-userid]").live("hover", function() {
	($this = $(this)).closest("section").find('[data-userid=' + $this.data("userid") + "]").toggleClass("hover");
}).live("mousedown", function() {
	$(this).parents("ul").eq(1).parent().find("div.hover, li.hover").removeClass("hover");
});
buttons = ["Okay", "Sure", "Got it", "Roger"];
function error(event, error) {
	event.preventDefault();
	$('<div id="error"><p>' + error + '</p><a href="#">' + buttons[Math.floor(Math.random() * buttons.length)] + '</a></div>').appendTo($body).children().last().click(function(event) {
		event.preventDefault();
		$(this).parent().remove();
	});
}
function confirmation(event, error) {
	event.preventDefault();
	$('<div id="error"><p>' + error + '</p><a href="#">Okay</a><a href="#">Nope</a></div>').appendTo($body).children().last().click(function(event) {
		event.preventDefault();
		$(this).parent().remove();
		$("form").addClass("clear").submit();
	}).prev().click(function(event) {
		event.preventDefault();
		$(this).parent().remove();
	});
}
$("form:not(.clear)").live("submit", function(event) {
	$inputs = $(this).parent().find("input");
	if (!($val = $inputs.filter("[name=login]").val()))
		error(event, "A name is required.");
	else if ($val.search(/\S\s+\S/) == -1)
		error(event, "A last name is required.");
	else if ($val.search(/\S\s+\S\.?$/) != -1)
		confirmation(event, "A complete last name is recommended. It isn't displayed on the site. Would you like to enter one?");
	else if ($.inArray($.sha1($.trim($val.toLowerCase())), blacklist) != -1)
		error(event, "Sorry, " + $val + " is not permitted to sign up for any rooms. If you would like to resolve a deficit or believe this is in error, contact Nikolai.");
	else if (($val = $inputs.filter("[name=friend]").val()) && $val.search(/\S\s+\S/) == -1)
		error(event, "Your last name is required.");
})
$("aside>h2>a").toggle(function(event) {
	event.preventDefault();
	$(this).closest("aside").css({height: "auto"});
});
$(document).ready(function() {
	($header = $("body>header")).data("marginTop", parseInt($header.css("marginTop")));
	$body = $("body");
	$containerwidth = $("#container>div").outerWidth();
	($window = $(window)).bind("scroll resize", function() {
		$body.toggleClass("thin", $containerwidth > $window.width()).removeClass("fixed").toggleClass("fixed", $window.scrollTop() > $header.offset().top - $header.data("marginTop"));
	}).triggerHandler("scroll");
	if (location.href.substr(-7) == "nikolai")
		$("div.rooms div.people").droppable({drop: function(event, ui) {
			$origin = ($person = ui.draggable).parents("li").eq(1);
			$destination = $(this).parents("li").eq(1);
			$person.appendTo(this).css({left: 0, top: 0});
			$.post("action.php", {action: "redistribute", userid: $userid = $person.data("userid"), day: ($day = $person.closest("li")).data("day"), room: $destination.index() + 1});
			if (!$destination.find("li[data-userid=" + $userid + "]").length)
				$destination.find("div.list>ul").append('<li data-userid="' + $userid + '">' + $origin.find("li[data-userid=" + $userid + "]").text() + "</li>");
			if (!$origin.find("div[data-userid=" + $userid + "]").length)
				$origin.find("li[data-userid=" + $userid + "]").remove();
		}}).children("div").draggable({revert: "invalid", revertDuration: 0});
	($aside = $("aside")).toggle(function() {
		end = $aside.css({height: "auto"}).height();
		$aside.removeAttr("style").animate({height: end});
	}, function() {
		start = $aside.removeAttr("style").height();
		$aside.css({height: end}).animate({height: start});
	}).find("a").click(function(event) {
		event.preventDefault();
	});
});

