jQuery(document).ready(function() {

	// sub menus
	jQuery("ul.menu > li").hover(function() {
		var $currentSub = jQuery(this).find("ul.sub-menu:first");
		jQuery(this).closest("ul.menu").find("ul.sub-menu:visible").hide();
		$currentSub.show();
	}, function() {
	});
	
	jQuery(document).bind("click", function() {
		jQuery("ul.sub-menu:visible").hide();
	});
	
	// horaires
	var $select = jQuery("select.club-select");
	if ($select.length > 0) {
		$select.appendTo("p.form");
		$select.bind("change", function() {
			jQuery(this).attr("disabled", true).after("<img src='/wp-content/themes/letsgo/images/indicator.gif' />");
			var clubIndex = window.location.href.indexOf("?club=");
			if (clubIndex > 0) {
				window.location.href = window.location.href.substring(0, clubIndex + 6) + $select.val();
			} else {
				window.location.href = window.location.href + "?club=" + $select.val();
			}
		});
		
		var $map = jQuery("div.wpgmappity_container");
		if ($map.length > 0) {
			jQuery("#horaire-shortcut").insertAfter($map);
		}

		var $ical = jQuery("p.ical");
		if ($ical.length > 0) {
			jQuery("#horaire-shortcut").insertAfter($ical);
		}
		
		var $firstLine = jQuery("#timetable > tbody > tr:first");
		$firstLine.find("th:first").text("");
		var colCount = $firstLine.find("th").length;
		
		jQuery("#timetable td").each(function(i, td) {
			var $td = jQuery(this);
			var content = $td.text();
			if (jQuery.trim(content).length > 0) {
				$td.html("");
				content = content.split("/");
				for (var i = 0; i < content.length; i++) {
					var $a = jQuery("<a href='/cours-fitness/"+content[i]+"'></a>");
					var $img = jQuery("<img src='/wp-content/themes/letsgo/images/" + content[i] + ".jpg' title='" + content[i] + "' alt='" + content[i] + "' width='" + (colCount == 8 ? "80" : "90") + "' />");
					$td.append($a.html($img));
					$img.error(function() {
						jQuery(this).parent().replaceWith(jQuery(this).attr("title"));
					});
				}
			}
		}).filter("td[title]").tipsy({gravity: 's'});
	}

	// input placeholders
	jQuery("input[placeholder]").placeholder({blankSubmit:true});

	// home page
	var $homePage = jQuery("#home");
	if ($homePage.length == 1) {
		/*$homePage.find("div.large-hook a").hover(function(event) {
			var $a = jQuery(this);
			if (event.type === "mouseenter") {
				$a.siblings("img").hide().parent().find("img." + $a.attr("rel")).show();
			}
			$a.siblings("a").removeClass("on").end().addClass("on");
		});*/
		setInterval(function() {
			var $hooks = $homePage.find("div.large-hook a");
			var $current = $hooks.filter("a.on");
			var currentIndex = $hooks.index($current);
			var $next = $hooks.eq((currentIndex + 1) % 3);
			$next.siblings("img").hide().parent().find("img." + $next.attr("rel")).show();
			$next.siblings("a").removeClass("on").end().addClass("on");
		}, 2000);
	}

});
