$(document).ready(function() {
	
	$("body").addClass("js");
	
	DD_roundies.addRule("#bottom", "5px", true);
	DD_roundies.addRule("#courses", "5px", true);
	DD_roundies.addRule(".popup-members li a", "5px", true);
	
	$(".member #content li:last-child").addClass("last");
	
	$(".button-ten").click(function() {
		$(this).blur();
		$("html").animate({ scrollTop: 0 }, 500, function() {
			$("#popup-controls li.about a").trigger("click");
		});
		return false;
	});

	// homepage map cycle
	$("#map-images").cycle({
		speed: 500,
		pause: true,
		before: function() {
			index = $(this).attr("rel");
			offset = 326 * index * -1;
			$("#map-home").css("background-position", "0 " + offset + "px");
		}
	});
	
	// on interaction with map, cancel auto cycling and move to manual
	$("#map-home div").mouseover(function() {
		$("#map-images").cycle("pause");
		index = $(this).attr("rel");
		offset = 326 * index * -1;
		$("#map-home").css("background-position", "0 " + offset + "px");
		$("#map-images img").each(function() {
			if($(this).css("opacity")>0) {
				$(this).animate({ opacity: 0 }, 250);
			}
		});
		$("#map-images img[rel='" + index + "']").show().animate({ opacity: 1 }, { queue: false, duration: 250 });
	});
	
	// map point click
	$("#map-home div").click(function() {
		window.location.href = $("#nav li." + $(this).attr("id")).find("a").attr("href");
	});
	
	// member info switching
	var menuChange = {
		over: function() {
			if(!$(this).hasClass("on")) { 
				// switch panel
				index = $(this).parent().prevAll("li").length + 1;
				$("#content div.content-panel:visible").fadeOut(250);
				$("#content div.content-panel").eq(index).fadeIn(250);
				$(this).parent().parent().find("a").removeClass("on").find("span").hide();
				$(this).addClass("on");
				// show close button
				$(this).find("span").fadeIn(200);
				// pause home cycling
				$("#map-images").cycle("pause");
				// ajax load image and fade when loaded	
				iContainer = $("#content div.content-panel").eq(index).find("div.imgContainer");
				if(iContainer.hasClass("loading")) {
					src = "/media/web/img/home/" + $(this).parent().attr("class") + ".jpg";
					var i = new Image();
					$(i).load(function() {
						iContainer.css("opacity", 0);
						iContainer.removeClass("loading").html(this);
						iContainer.animate({ "opacity": 1 }, 250);
					}).attr("src", src);
				}
			}
		},
		out: function() { 
			return false;
		},
		interval: 150
	};
	$("#nav li a").hoverIntent(menuChange);
	
	$("#nav li a span").click(function(event) {
		// switch panel
		$("#content div.content-panel:visible").fadeOut(200);
		$("#content-home").fadeIn(200);
		// remove hover state
		$(this).parent().removeClass("on").addClass("nohover").find("span").hide();
		// resume home cycling
		$("#map-images").cycle("resume");
		event.stopPropagation();
		return false;
	});
	
	$("#nav li a").mouseenter(function() {
		$(this).parent().parent().find("a").removeClass("nohover");
	});
	
	// popups
	$("#popup-controls li:not(.home) a").hover(function() {
		if($("#popup:animated").length==0) {
			if(!$("#popup").hasClass("popup-open")) {
				$("#popup").toggleClass("popup-" + $(this).parent().attr("class") + "-hover");
			}
		}
	});
	
	var popupOpen = function(popup, height) {
		$("#popup").attr("class", "popup-" + popup).css("margin-top", (height+20)*-1+"px").animate({
			marginTop: 0
		}, 1000, "easeOutExpo", function() {
			$(this).addClass("popup-open");
		});
	}
	
	var popupClose = function(next, height) {
		currentHeight = $(".popup:visible").height();
		speed = 1000;
		if(next) { speed = 200; }
		$("#popup").animate({
			marginTop: (currentHeight+30)*-1
		}, speed, "easeOutExpo", function() {
			$(this).attr("class", "").css("margin-top", 0);
			if(next) {
				popupOpen(next, height);
			}
		});
		
	}
	
	$("#popup-controls li:not(.home) a").click(function() {
		if($("#popup:animated").length==0) {	
			p = $(this).parent().attr("class");
			height = $("#popup-" + p).height();
			if($("#popup").hasClass("popup-open")) {
				if(!$("#popup").hasClass("popup-" + p)) {
					// swap
					popupClose(p, height);
				} else {
					// close
					popupClose(false, height);
				}
			} else {
				// open
				popupOpen(p, height);
			}		
		}
		return false;
	});
	
});
