$(document).ready(function() {
	
	// analytics tracking of external links (starting with http://)
	$("a[href^='http://']").click(function() {
		pageTracker._trackPageview('/external/'+ $(this).attr('href'));
	});
	$("a.brochure").click(function() {
		pageTracker._trackPageview('/external/brochure/');
	});
	
	
	// contact form highlighting
	$("table#contact-form-table tr:odd").addClass("altrow");
	
	
	// home hero
	$("ul#hero-switch li a").mouseover(function() {
		clearTimeout(heroTimer); // user has interacted so cancel automatic hero switching
		startWhenLoaded = false; // prevent images from automatically switching once they've all loaded
		if(!$(this).parent().hasClass("switch-active")) {
			clicked = $(this).parent().attr("id").substring(7, $(this).parent().attr("id").length);
			$("div.hero-active").fadeOut("medium").removeClass("hero-active");
			$("div#hero-" + clicked).css("visibility", "visible").css("display", "none").fadeIn("medium").addClass("hero-active"); // visibility is originally hidden, so widths of captions can be calculated for right-alighing (can't do this with display:none set)
			$("ul#hero-switch li.switch-active").removeClass("switch-active");
			$(this).parent().addClass("switch-active");
		}
	});
	
	
	// centre hero switches
	hero_width = 0;
	$("ul#hero-switch li").each(function() {
		hero_width += $(this).outerWidth();
	});
	hero_push  = (960 - hero_width) / 2;
	if($.browser.msie && $.browser.version.substring(0,1)==6) {
		hero_push = hero_push / 2;
	}
	hero_push = Math.floor(hero_push)
	$("ul#hero-switch li:first").css("margin-left", hero_push + "px");
	
	
	// course filtering
	$("ul#course-filter li a").click(function() {
		if(!$(this).parent().hasClass("disabled")) {
			// get types of courses to show
			filter = $(this).parent().attr("id").substring(14, $(this).parent().attr("id").length);
			if(filter=="all") {
				$("ul#course-filter").find("li").removeClass("current");
				$("li#course-filter-all").addClass("current");
				$("ul.course-listing").find("li.grouper").show();
			} else {
				$("ul#course-filter").find("li").removeClass("current");
				$("li#course-filter-" + filter).addClass("current");
				$("ul.course-listing").find("li.grouper").show();
				// loop through members
				$("ul.course-listing").find("li.grouper").each(function() {
					if(!$(this).find("a:first").hasClass(filter)) {
						$(this).hide();
					}
				});
			}
		}
		return false;
	});


	// signup form (if no JS then form submitted normally without modal window)
	$("form#signup-form").submit(function() {
		// post results over to django, load response into results div
		$("div#signup-result").load("/action/signup/", {
			"signup-language": $("#signup-language").val(), 
			"signup-email": $("#signup-email").val(), 
			"signup-firstname": $("#signup-firstname").val(), 
			"signup-familyname": $("#signup-familyname").val(), 
			"signup-age": $("#signup-age").val(), 
			"signup-country": $("#signup-country").val(), 
			"signup-level": $("#signup-level").val()
		}, function() {
			// when done, show results div in thickbox
			tb_show(null, "#TB_inline?height=125&width=300&inlineId=signup-result", false)
			this.blur();
		});
		return false;
	});
	
	
	// signup
	$("a#signup-close").click(function() { closeSignup(); return false; });
	$("input#signup-email").focus(function() {
		if($(this).attr("value")=="Enter your e-mail address" || $(this).attr("value")=="Introduce tu dirección de correo electrónico") {
			$(this).attr("value", "");
		}
		$("a#signup-close").fadeIn("slow");
		$("div#signup-fields").show();
		$("div#signup").animate({
			height: 120 + $("div#signup-fields").height() + "px"
		}, 500, "easeOutBack");
	});
	
	
	// member slideshows
	$("div#member-slideshow").cycle({
		fx: "fade",
		speed: 2000,
		timeout: 4000
	});

	
	// course search map
	$("div#course-search-maps div").mouseover(function() {
		c = $(this).attr("class");
		id = c.substring(4, c.length);
		changeLocationMap(id);
		changeLocationSelect(id);
	});
	$("select#course-search-location").change(function() {
		c = $(this).find("option:selected").attr("class");
		id = c.substring(4, c.length);
		changeLocationMap(id);
	});
	
	// course details
	var current_courseid = 0;
	var reset_courseid;
	//$("div.course-details-left").css("height", "0px"); // prevents slideDown going too far
	$("a.course-link").click(function() {
		speed = 500;
		courseid = $(this).attr("id").substring(12, $(this).attr("id").length);
		if(courseid == current_courseid) {
			// toggle open details window closed
			$("div#course-details-" + courseid).slideUp(speed);
			$(this).removeClass("course-link-active");
			$(this).parent().parent().removeClass("course-row-active");
			reset_courseid = true;
		} else {
			// close open details
			$("div.course-details-active").slideUp(speed).removeClass("course-details-active");
			$("a.course-link-active").removeClass("course-link-active");
			// change highlighed LI
			$("ul.course-listing").find("li").removeClass("course-row-active");
			$(this).parent().parent().addClass("course-row-active");
			// open new details
			$("div#course-details-" + courseid).slideDown(speed).addClass("course-details-active");
			$(this).addClass("course-link-active");
			reset_courseid = false;
		}
		if(reset_courseid) {
			current_courseid = 0;
		} else {
			current_courseid = courseid;	
		}
		return false;
	});
	
});


function closeSignup() {
	$("a#signup-close").fadeOut("slow");
	if($("input#signup-email").attr("value")=="") {
		if(!$("body").hasClass("es")) {
			$("input#signup-email").attr("value", "Enter your e-mail address");
		} else {
			$("input#signup-email").attr("value", "Introduce tu dirección de correo electrónico");
		}
	}
	$("div#signup").animate({
		height: "120px"
	}, 500, "easeOutBack", function() {
		$("div#signup-fields").hide();
	});
}


function changeLocationMap(id) {
	o = "-" + (id * 116) + "px";
	$("div#course-search-maps").css("background-position", o);
}


function changeLocationSelect(id) {
	$("select#course-search-location").find("option").each(function() {
		c = $(this).attr("class");
		t = c.substring(4, c.length);
		if(t==id) {
			v = $(this).val();
		}
	});
	$("select#course-search-location").val(v);
}


function heroSwitch(to) {
	$("ul#hero-switch li a").eq(to).trigger("mouseover");
	next = to+1;
	if(next == 12) { next = 0; }
	heroTimer = setTimeout("heroSwitch("+next+")", 5000);
}