﻿/*
--------------------------------------------------------------------
GM
--------------------------------------------------------------------
*/
var GM = {};

GM.init = function()
{
	$(".question img").click(this.send);
	$(".question img").css("cursor", "pointer");
	$(".error").css("display", "none");
	$(".already").css("display", "none");
	$(".end").css("display", "none");
	//$("#results").css("display", "none");
	GM.attachRollOverEvent(".question img");
	$("#results h2 img").click(this.showResults);
	$("#results h2 img").css("cursor", "pointer");
};


GM.attachRollOverEvent = function(imageId)
{
	$(imageId).mouseover( GM.over );
	$(imageId).mouseout( GM.out );
	$(imageId).mouseout();
};

GM.over = function ()
{
	var name = $(this)[0].name;
	var text;
	var color;
	if(name == "mou")
	{
		text = "Plutôt mou";
		color = "#FDF6F9"
	}
	else 
	{
		text = "Plutôt dur";
		color = "#F94D89";
	}
	
	$("#questionLabel").text(text);
	//$("#"+name).css("display", "block");
	$(this).css("border-bottom", "1px dotted #F94D89");
}
GM.out = function()
{
	$("#questionLabel").text("Ce matin, vous étiez... ?");
	var name = $(this)[0].name;
	$("#"+name).css("display", "none");
	$(this).css("border-bottom", "1px dotted #FFF");
}

GM.send = function(){
	var name = $(this)[0].name;
	var value = (name == "dur") ? "1" : "0";
	$.get("php/insert.php?t="+new Date(), { g:value },
		function(success)
		{
			switch(success)
			{
				case "1":
					GM.hideQuestion(GM.showEnd);
					break;
				case "-1":
					//GM.hideQuestion();
					GM.hideQuestion(GM.showAlready);
					//GM.showAlready();
					//GM.showResults();
					break;
				default:
					console.info("CODE ERREUR : ", success);
					GM.showError();
			}
		}
	);
};
GM.hideQuestion = function(callback)
{
	$(".question").fadeOut("slow", callback);
};
GM.showError = function()
{
	$(".error").fadeIn("slow");
};
GM.showAlready = function()
{
	$(".already").fadeIn("slow");
	GM.showResults();
};
GM.showEnd = function()
{
	$(".end").fadeIn("slow");
	GM.showResults();
};
GM.showResults = function()
{
	$.get("php/extract.php?t="+new Date(), 
		function(result)
		{
			var resultContaner = $("#resultContainer")
			resultContaner.html(result);
			resultContaner.animate({opacity:'hide'}, 10);
			//resultContaner.css("visibility", "hidden");
			//resultContaner.css("display", "none");
			resultContaner.animate({opacity:'show'}, "slow");
		}
	);
};


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