$(document).ready(function() {
	/* Poll validate */
	$("#voteSubmit").live("click", function() {
		if (!$("input[name='poll_choice']:checked").size()) {
			alert("Veuillez choisir une réponse avant de valider.");
			return false;
		}
		$.post(
			"index.php?action=poll_dynamic_show",
			{
				poll_id : $("#poll_id").val(),
				poll_choice : $("input[name='poll_choice']:checked").val()
			},
			function (data) {
				$("#poll_module").replaceWith(data);
			}
		);
		return false;
	});
	/* END - Poll validate */
	
	
	/* Autosubmit */
	$("#autosubmit, .autosubmit").change(function() {
		$(this).parents("form:first").submit();
	});
	/* END - Autosubmit */
	
	
	/* Games popup */
	$("a.play_game").click(function() {
		var width = 800;
		var height = 680;
		window.open('http://www.files.csfffsc.fr/lib/_swf/module_flash.php?id=' + $(this).attr("rel"), 'nom', config='top=' + (screen.height - height) / 3 + ', left=' + (screen.width - width) / 2 + ', height=' + height + ', width=' + width + ', toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no');
		return false;
	});
	/* END - Games popup */
	
	
	/* Countdown start */
	if ($("#countdown_value").size()) {
		start_countdown();
	}
	/* END - Countdown start */
	
	
	// Jeu de lettres
	$("#game_validate").live("click", function() {
		$.post(
			"index.php?action=game_show",
			{
				game_validate: 	true,
				proposal: 		$("#proposal").val(),
				answer: 		$("#answer").val(),
				id: 			$("#id").val()
			},
			function (data) {
				$("#gameContainer").html(data);
			}
		);
		return false;
	});
	
	$("#type_ods5, #type_9lettres").live("click", function() {
		var type = $(this).attr("id");
		$.post(
			"index.php?action=game_show",
			{
				game_type: 	type
			},
			function (data) {
				$("#gameContainer").html(data);
			}
		);
		return false;
	});
	
	$("#play").live("click", function() {
		$.post(
			"index.php?action=game_show",
			{
			},
			function (data) {
				$("#gameContainer").html(data);
			}
		);
		return false;
	});
	
	$("#continue").live("click", function() {
		$.post(
			"index.php?action=game_show",
			{
				next_game: true
			},
			function (data) {
				$("#gameContainer").html(data);
			}
		);
		return false;
	});
	
	$("#new_record_validate").live("click", function() {
		var player = $("#player").val();
		if (player == "") {
			alert("Veuillez saisir un pseudo pour pouvoir figurer dans le top 5.");
			return false;
		}
		$.post(
			"index.php?action=game_show",
			{
				new_record_validated: true,
				player: player
			},
			function (data) {
				$("#gameContainer").html(data);
			}
		);
		return false;
	});
	
	
	/* Licences clubs placing */
	$(".licencesListNext").css("display", "none");
	$(".licencesListNextSwitcher").click(function() {
		var self = $(this);
		self.prevAll(".licencesListNext:first").slideToggle("slow", function() {
			if ($(this).css("display") == "none") {
				self.html("Tout afficher");
			} else {
				self.html("R&eacute;duire");
			}
		});
		return false;
	});
	/* END - Licences clubs placing */
	
	
	/* New news comment */
	$("#comment_author").blur(function() {
		if ($(this).val() == "") {
			$(this).addClass("disabledText").val("Votre nom ou pseudo");
		}
	});
	$("#comment_author").focus(function() {
		if ($(this).val() == "Votre nom ou pseudo") {
			$(this).removeClass("disabledText").val("");
		}
	});
	$("#comment_text").blur(function() {
		if ($(this).val() == "") {
			$(this).addClass("disabledText").val("Votre message");
		}
	});
	$("#comment_text").focus(function() {
		if ($(this).val() == "Votre message" && $(this).hasClass("disabledText")) {
			$(this).removeClass("disabledText").val("");
		}
	});
	$("#comment_author").triggerHandler("blur");
	$("#comment_text").triggerHandler("blur");
	
	$("#commentValidate").click(function() {
		var author = $("#comment_author").val();
		if (author == "" || $("#comment_author").hasClass("disabledText")) {
			alert("Veuillez saisir un pseudo.");
			return false;
		}
		var text = $("#comment_text").val();
		if (text == "" || $("#comment_text").hasClass("disabledText")) {
			alert("Veuillez saisir un commentaire.");
			return false;
		}
		$(".newCommentContainer").find("input, textarea").attr("disabled", "disabled");
		$.post(
			"index.php?action=news_add_comment",
			{
				id : 		$("#news_id").val(),
				author: 	author,
				text: 		text
			},
			function (data) {
				var result = eval("(" + data + ")");
				if (result.result == "success") {
					var test = $("#news_id").before(
						'<div class="commentContainer" style="display: none;">' +
							'<div class="commentHeader"><span class="rightFloat">' + result.created + '</span>' + result.author + '</div>' +
							'<div class="commentText">' + result.text + '</div>' +
						'</div>');
					$("#comments_list").html(result.num_comments + " commentaire" + (result.num_comments > 1 ? "s" : ""));
					$(".commentContainer:last").fadeIn(1500);
					$("#comment_text").addClass("disabledText").val("Votre message");
					$("#comment_author").addClass("disabledText").val("Votre nom ou pseudo");
				}
				$(".newCommentContainer").find("input, textarea").removeAttr("disabled", "disabled");
			}
		);
		return false;
	});
	/* END - New news comment */
	
	
	/* Events expand/collapse */
	$("#showNextEvents").click(function() {
		if ($("#nextEvents").is(":visible")) {
			$("#nextEvents").hide();
			$(this).html("Tout afficher");
		} else {
			$("#nextEvents").show();
			$(this).html("R&eacute;duire");
		}
		return false;
	});
	$("#nextEvents").hide();
	/* END - Events expand/collapse */
	
	
	/* Games switch types */
	$(".type_festivals_federaux").click(function() {
		$("tr").filter(function () { return $(this).find(".festivals_federaux").size() }).toggle($(this).find("span[id^='type_']").hasClass("uncheckedGameType"));
	});
	$(".type_festivals_non_federaux").click(function() {
		$("tr").filter(function () { return $(this).find(".festivals_non_federaux").size() }).toggle($(this).find("span[id^='type_']").hasClass("uncheckedGameType"));
	});
	$(".type_tournois_du_comite").click(function() {
		$("tr").filter(function () { return $(this).find(".tournois_du_comite").size() }).toggle($(this).find("span[id^='type_']").hasClass("uncheckedGameType"));
	});
	$(".type_tournois_hors_comite").click(function() {
		$("tr").filter(function () { return $(this).find(".tournois_hors_comite").size() }).toggle($(this).find("span[id^='type_']").hasClass("uncheckedGameType"));
	});
	$(".type_epreuves_federales_par_centre").click(function() {
		$("tr").filter(function () { return $(this).find(".epreuves_federales_par_centre").size() }).toggle($(this).find("span[id^='type_']").hasClass("uncheckedGameType"));
	});
	$(".type_parties_club").click(function() {
		$("tr").filter(function () { return $(this).find(".parties_club").size() }).toggle($(this).find("span[id^='type_']").hasClass("uncheckedGameType"));
	});
	$(".type_championnats_du_monde").click(function() {
		$("tr").filter(function () { return $(this).find(".championnats_du_monde").size() }).toggle($(this).find("span[id^='type_']").hasClass("uncheckedGameType"));
	});
	$(".type_championnat_de_france").click(function() {
		$("tr").filter(function () { return $(this).find(".championnat_de_france").size() }).toggle($(this).find("span[id^='type_']").hasClass("uncheckedGameType"));
	});
	$(".type_simultanes_de_france").click(function() {
		$("tr").filter(function () { return $(this).find(".simultanes_de_france").size() }).toggle($(this).find("span[id^='type_']").hasClass("uncheckedGameType"));
	});
	$(".type_parties_detonantes").click(function() {
		$("tr").filter(function () { return $(this).find(".parties_detonantes").size() }).toggle($(this).find("span[id^='type_']").hasClass("uncheckedGameType"));
	});
	$(".type_simultanes_permanents").click(function() {
		$("tr").filter(function () { return $(this).find(".simultanes_permanents").size() }).toggle($(this).find("span[id^='type_']").hasClass("uncheckedGameType"));
	});
	$("span[class^='type_']").click(function () {
		$(this).find("span").toggleClass("uncheckedGameType").toggleClass("checkedGameType");
	});
	/* END - Games switch types */
	
	
	/* Newsletter add e-mail */
	$("#newsletter_form > input[type='submit']").click(function() {
		var email = $("#newsletter_email").val();
		$("#newsletter p:first").html('<p><img alt="Chargement..." src="img/loading.gif" /></p>');
		$.post(
			"index.php?action=newsletter_send_confirmation_email",
			{
				email: 		email
			},
			function (data) {
				var json = eval("(" + data + ")");
				$("#newsletter p:first").html(json['message']);
				$("#newsletter_email").val("");
			}
		);
		return false;
	});
	$("#newsletter_email").blur(function() {
		if ($(this).val() == "") {
			$(this).addClass("disabledText").val("Votre adresse e-mail");
		}
	});
	$("#newsletter_email").focus(function() {
		if ($(this).val() == "Votre adresse e-mail") {
			$(this).removeClass("disabledText").val("");
		}
	});
	$("#newsletter_email").triggerHandler("blur");
	/* END - Newsletter add e-mail */
	
	
	/* Floating modules show/hide */
	$(".floatingModuleHeader").siblings().hide();
	$(".floatingModuleHeader").click(function() {
		$(".floatingModule").removeClass("hoverFloatingModule");
		$(".floatingModuleHeader").siblings().toggle();
		return false;
	});
	$(".floatingModule").mouseover(function() {
		if (!$(this).find(".floatingModuleContent").is(":visible")) {
			$(this).addClass("hoverFloatingModule");
		}
	});
	$(".floatingModule").mouseout(function() {
		$(this).removeClass("hoverFloatingModule");
	});
	/* END - Floating modules show/hide */
	
	
	
	
	
	
	
	
	
	
	
	
	/*
	var map = new GMap2(document.getElementById("map"));
	
	var point;
	var center;
	var marker;
	var icons = [];
	var markers = [];
	
	
	$(".departmentContainer a").click(function () {
		var self = $(this);
		$("#clubContainer").hide();
		$.post(
			self.attr("href"),
			{},
			function (data) {
				var json = eval("(" + data + ")");
				$("#clubContainer").html(json.content).fadeIn();
				
				
				touyou();
				
			}
		);
		return false;
	});
	
	
	
	
	
	
	
	
	
	
	$("#map").css({ height: 500, width: 600 });
	
	$.post("index.php?action=get_clubs", { "club": $("#club_code").val() },
		function (data) {
			var json = eval("(" + data + ")");
			if (json.result == "success") {
					function displayPoint(marker, index){
						//$("#message").hide();
						var moveEnd = GEvent.addListener(map, "moveend", function() {
							var markerOffset = map.fromLatLngToDivPixel(marker.getLatLng());
							//$("#message").fadeIn().css({ top:markerOffset.y, left:markerOffset.x });
							GEvent.removeListener(moveEnd);
						});
						map.panTo(marker.getLatLng());
					}
					
					var index = 0;
					$.each(json.clubs, function () {
						var t = this;
						if (t.active == true) {
							icons[index] = new GIcon(G_DEFAULT_ICON, "../img/map_pin_active.png");
							center = new GLatLng(parseFloat(t.lat), parseFloat(t.lng));
						} else {
							icons[index] = new GIcon(G_DEFAULT_ICON, "../img/map_pin.png");
						}
						icons[index].iconSize = new GSize(15, 22);
						icons[index].shadow = "";
						icons[index].iconAnchor = new GPoint(7, 22);
						icons[index].infoWindowAnchor = new GPoint(9, 9);
						
						point = new GLatLng(parseFloat(t.lat), parseFloat(t.lng));
						marker = new GMarker(point, { draggable : false, title : t.code + " - " +  t.name, icon: icons[index] });
						markers[index] = marker;
						map.addOverlay(marker);
						index++;
					});
					$(markers).each(function(i,marker){
						displayPoint(marker, i);
					});
					map.setCenter(center, 10);
			}
		}
	);
	*/
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	$("a.photo").colorbox({
		opacity:				"0.7",
		maxWidth:			"85%",
		maxHeight:			"85%",
		rel:					"gallery",
		slideshow:			true,
		slideshowSpeed:	4000,
		slideshowAuto:		true,
		slideshowStop:		"Stopper le diaporama",
		slideshowStart:	"Lancer le diaporama"
	});
	
	$("a.pic_box").colorbox({
		opacity:				"0.7",
		maxWidth:			"85%",
		maxHeight:			"85%"
	});
	
	$(".odsList").treeview({
		collapsed: true
	});
	
	
	/* Video integration */
	$("a[href*='www.dailymotion.com'], a[href*='video.google.'], a[href*='www.youtube.com']").click(function () {
		var dailymotion = new RegExp("www.dailymotion.com", "g");
		var google_video = new RegExp("video.google.", "g");
		var youtube = new RegExp("www.youtube.com", "g");
		
		var url = $(this).attr("href");
		if (dailymotion.test(url)) {
			var video_id = url.split("/");
			video_id = video_id[video_id.length-1];
			video_id = video_id.split("_");
			video_id = video_id[0];
			var html = "<object width=\"480\" height=\"365\"><param name=\"movie\" value=\"http://www.dailymotion.com/swf/" + video_id + "&related=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowScriptAccess\" value=\"always\"></param><embed src=\"http://www.dailymotion.com/swf/" + video_id + "&related=0\" type=\"application/x-shockwave-flash\" width=\"480\" height=\"365\" allowfullscreen=\"true\" allowscriptaccess=\"always\"></embed></object>";
		} else if (google_video.test(url)) {
			var video_id = url.split("=");
			video_id = video_id[video_id.length-1];
			var html = "<embed id=\"VideoPlayback\" src=\"http://video.google.fr/googleplayer.swf?docid=" + video_id + "&hl=fr&fs=true\" style=\"width: 400px; height: 326px\" allowFullScreen=\"true\" allowScriptAccess=\"always\" type=\"application/x-shockwave-flash\"></embed>";
		} else if (youtube.test(url)) {
			var video_id = url.split("=");
			video_id = video_id[video_id.length-1];
			var html = "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/" + video_id + "&hl=fr_FR&fs=1&\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/" + video_id + "&hl=fr_FR&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object>";
		}
		$.modal(html, {
			overlayClose: true,
			focus: false
		});
		return false;
	});
	/* END - Video integration */
	
	$("marquee").marquee().mouseover(function () { $(this).trigger("stop"); }).mouseout(function () { $(this).trigger("start") });
	
	$("#check_uncheck_all_types").click(function () {
		if (($(".gamesTypeSelector").find(".checkedGameType").size() == $(".gamesTypeSelector").find("span[class^='type_']").size()) || ($(".gamesTypeSelector").find(".uncheckedGameType").size() == $(".gamesTypeSelector").find("span[class^='type_']").size())) {
			$(".gamesTypeSelector").find("span").each(function () {
				$(this).triggerHandler("click");
			});
		} else {
			$(".gamesTypeSelector").find(".uncheckedGameType").each(function () {
				$(this).parent().triggerHandler("click");
			});
		}
		return false;
	});
});