/* Nicolas « Neovov » Le Gall pour Neoma Interactive - http://neoma-interactive.com */
var utopies = function() {

	function init() {
		var config = {
			expire: 1,
			schemes: [
				{
					name: "blue",
					banners: 3
				},
				{
					name: "brown",
					banners: 3
				},
				{
					name: "green",
					banners: 3
				},
				{
					name: "orange",
					banners: 4
				},
				{
					name: "purple",
					banners: 1
				},
			]
		},
		$body = $("body"),
		$visual = $("#visual");


		if (!$body.hasClass("back")) {
			// Changing the scheme (or not?)
			var
				cookie = $.cookie("scheme"),
				scheme = null,
				current = $body.attr("class").replace(/scheme-/, "");

			if (cookie === null) {
				// Randomize the scheme
				scheme = config.schemes[Math.round(Math.random() * (config.schemes.length - 1))];
			} else {
				// Searching the scheme from the cookie
				for (var key in config.schemes) {
					if (config.schemes[key].name === cookie) {
						// Found, break!
						scheme = config.schemes[key];
						break;
					}
				}
			}

			// Setting the scheme
			if (scheme.name !== current) {
				$body.removeClass("scheme-" + current);
				$body.addClass("scheme-" + scheme.name);
			}

			// Setting a cookie
			if (cookie === null) {
				$.cookie("scheme", scheme.name, { expire: config.expire });
			}

			// Changing the banner
			var
				banner = Math.round(Math.random() * scheme.banners) || 1,
				current = $visual.attr("class").replace(/banner-/, "");

			// Setting the banner
			if (banner !== current) {
				$visual.removeClass("banner-" + current);
				$visual.addClass("banner-" + banner);
			}
		}


		// Adding a JS class
		$body.addClass("js");


		// Enabling the click on "ESPACE MEMBRE"
		$(".member a").click(function() {
			$(".pop-hover").toggleClass("visible");
			return false;
		});


		// Opening the pop-hover after submitting wrong login/pass
		if ($("#header .pop-hover .erreur_message").length) {
			$("#header .pop-hover").addClass("visible");
		}


		// Featuring .questionnaire
		$(".questionnaire .question").each(function(i) {
			var question = $(this);
			var choices  = question.next(".choices"); // Getting the "choices" who follow this question
			if (choices.length) { // This question have multiple choices
				question.children("input").each(function(i) {
					if (i == 0) {
						$(this).change(function() {
							if (this.checked) {
								choices.hide();
							} else {
								choices.show();
							}
						});
					} else {
						$(this).change(function() {
							if (this.checked) {
								choices.show();
							} else {
								choices.hide();
							}
						});
					}
				});
			}
		});

		$(".questionnaire .choices").hide();
	}


	return {
		init: init
	}
}();

$(document).ready(function() {
	utopies.init();
});