var placeholder = 'Posez nous votre question, nous y apporterons une réponse dans les plus brefs délais !';
var validate = true;



if(!$.browser.webkit)
{
	
	$("#cabinet").css({marginTop:"30px"});
	
	
	
	$("#cabinet #divant textarea").html(placeholder);
	$("#cabinet #divant textarea").focusin(function(){
	
		if($(this).text() == "" || $(this).text() == placeholder)
		{
			$(this).text("");
		}
		
	});
	
	$("#cabinet #divant textarea").focusout(function(){
		if($(this).text() == "")
		{
			
			$(this).text(placeholder);
		}
	});
	
	
	
	$("#formContact input").val("Mail").css({color:"#999"});;
	$("#formContact input").focusin(function(){
	
		if($(this).val() == "" || $(this).val() == "Mail")
		{
			$(this).val("");
			$(this).css({color:"black"});
		}
		
	});
	
	$("#formContact input").focusout(function(){
		if($(this).val() == "")
		{
			$(this).val("Mail");
			$(this).css({color:"#999"});
		}
	});
	
	$(".UIButton span").css({background :'url("images/button.png") repeat-x scroll left 1px transparent'});
	
	//document.body.style.minHeight = window.innerHeight+"px";
	
	//$(window).resize(function(){
		//document.body.style.minHeight = window.innerHeight+"px";
	//});
}
else
{

	//document.body.style.minHeight = document.documentElement.clientHeight+"px";
	
	
	//$(window).resize(function(){
	//	document.body.style.minHeight = document.documentElement.clientHeight+"px";
	
	//});
}


$("#cabinet .button").click(function(){
	var content = $("#textAreaQuestion").val();
	
	if(content != "" && content != placeholder && validate)
	{
		afficherValidation();
	}
	
});

function afficherValidation()
{
	$("#validationArea").css({display:"block"}).animate({
			opacity: 1
		},
		1000,
		function(){
			$('#email').keyup(function(){
				
				
				$(this).attr("pattern","^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*[^._-]@([a-zA-Z0-9-]+([-.][a-zA-Z0-9]{2,4}))+$")
			});
			
		}
	);
	
	if(validate)
	{
		$("#cabinet .button span").first().animate({
				opacity: 0
			},
			300,
			function(){
				$(this).text("Valider").animate({
				opacity: 1
				},
				300,
				function(){
					checkForm();
				}
				);
			});
	}
}

function checkForm(){

	$("#cabinet .button").click(function(){
		if($("#email").val().match("^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*[^._-]@([a-zA-Z0-9-]+([-.][a-zA-Z0-9]{2,4}))+$") && $("#nom").val() != "")
		{
			
			validate = false;
			$("#cabinet .button span").first().html("Chargement");
			$("#patientQuestion").submit();
			return false;
		}
		else
		{
			if(!$("#email").val().match("^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*[^._-]@([a-zA-Z0-9-]+([-.][a-zA-Z0-9]{2,4}))+$") && !($("#nom").val() != ""))
			{
				$("#email").css({background:"#fbb"});
				$("#nom").css({background:"#fbb"});
			}
			else if(!$("#email").val().match("^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*[^._-]@([a-zA-Z0-9-]+([-.][a-zA-Z0-9]{2,4}))+$"))
			{
				$("#email").css({background:"#fbb"});
				$("#nom").css({background:"#fff"});
			}
			else if($("#nom").val() == "")
			{
				$("#email").css({background:"#fff"});
				$("#nom").css({background:"#fbb"});
				
			}
		}
		return false;
	});
	return false;
}




$("#liste table tr").hover(
	function(){
		$(this).css({background:"-webkit-gradient(linear, 0% 0%, 0% 20%, from(#bbb), to(RGBa(255,255,255,0))),-webkit-gradient(linear, 0% 100%, 0% 70%, from(#bbb), to(RGBa(255,255,255,0)))"});
		
		if(!$.browser.webkit)
		{
			$(this).css({background:"rgba(0,0,0,0.1)"});
		}
	},
	function(){
		$(this).css({background:"none"});
	}
);

$("#liste table tr:first").hover(
	function(){
		$(this).css({background:"-webkit-gradient(linear, left 100%, left 70%, from(#bbb), to(RGBa(255,255,255,0)))"});
	},
	function(){
		$(this).css({background:"none"});
	}
);

$("#liste table tr:last").hover(
	function(){
		$(this).css({background:"-webkit-gradient(linear, left 0%, left 10, from(#bbb), to(RGBa(255,255,255,0)))"});
	},
	function(){
		$(this).css({background:"none"});
	}
);



/*
$("#liste table tr").click(function(){
	$("table tr").each(function(){
		$(this).children(".question").children(".questionContainer").css({height:"25px"});
		$(this).children(".question").children(".questionContainer").css({cursor:"pointer"});
	});
	$(this).children(".question").children(".questionContainer").css({height:"auto",cursor:"default"});
	
	
	
});*/









////////Interprétation de la réponse
function load(){
$("ul.path li").each(function(){
	var text = $(this).text();
	if(text == "Macintosh HD" || text == "macintosh hd" || text == "macintosh HD")
	{
		$(this).html('➤ <img src="style/images/hd.png"/>Macintosh HD');
	}
	else if(text == "Maison" || text == "maison")
	{
		$(this).html('➤ <img src="style/images/home.png"/>Maison');
	}
	else if(text == "Applications" || text == "applications" || text == "apps")
	{
		$(this).html('➤ <img src="style/images/apps.png"/>Applications');
	}
	else if(text == "Utilitaires" || text == "utilitaires" || text == "Utilitaire")
	{
		$(this).html('➤ <img src="style/images/tools.png"/>Utilitaires');
	}
	else if(text.match(".app$"))
	{
		$(this).html('➤ <img src="style/images/app.png"/>'+$(this).text());
	}
	else
	{
		$(this).html('➤ <img src="style/images/folder.png"/>'+$(this).text());
	}
});

$("ul.path li:first").css({paddingLeft:"0px"});

$("ul.menuPath").children().last().css({background:"RGBa(245,245,245,0.8)"});

$("ul.menuPath").children().last().hover(function(){
	$(this).css({background:'url("style/images/menuBackground.png") center left repeat-x'});
},function(){
	$(this).css({background:'RGBa(245,245,245,0.8)'});
});


$(".UIButton").each(function(){
	var text = $(this).text();
	$(this).html('<img src="style/images/buttonLeft.png"/><span>'+text+'</span><img src="style/images/buttonRight.png"/>');
});

$(".UIButton").mousedown(function(){
	var text = $(this).children("span").text();
	
	$(this).html('<img src="style/images/buttonHoldLeft.png"/><span>'+text+'</span><img src="style/images/buttonHoldRight.png"/>');
	if(!$.browser.webkit)
	{
		$(this).children("span").css({backgroundPosition:'left -28px'});
	}
	else
	{
		$(this).children("span").css({backgroundPosition:'-29px left'});
	}	
	
});



$(".UIButton").mouseup(function(){
	var text = $(this).children("span").text();
	
	$(this).html('<img src="style/images/buttonLeft.png"/><span>'+text+'</span><img src="style/images/buttonRight.png"/>');
	if(!$.browser.webkit)
	{
		$(this).children("span").css({backgroundPosition:'left 1px'});
	}
	else
	{
		$(this).children("span").css({backgroundPosition:'0px left'});
	}	
});

if(!$.browser.webkit)
	{
		$(this).children("span").css({backgroundPosition:'left 1px'});
	}

$("ul.terminal li").each(function(){
	//var text = $(this).text();
	//$(this).html('console:~ username$ <span class="cmd">'+text+'</span>');
});

$("ul.terminal").each(function(){
	var text = "";
	var html = '<table>';
	var cpt = 0;
	$(this).children('li').each(function(){
		text += $(this).text()+'<br/>';
		
		if(cpt == 0)
		{
			html += '<tr><td class="prefix">console:~ username$ </td><td class="cmd"></td>';
		}
		else
		{
			html += '<tr><td class="prefix">console:~ username$ </td>';
		}
		cpt++;
	});
	
	html += '</table>';
	
	$(this).html(html);
	$(this).children().children().children().children('.cmd').attr('rowspan',cpt).html(text);
	
	
	$(this).prepend('<div><h4>Terminal :</h4></div>');	
	
	//$(this).append('<textarea>'+text+'</textarea>');
	
});

$("ul.terminal").click(function(){
	
});
}





var left;
var right;
var arrow;
var arrowWhite;
var backgroundPath;

function precharge()
{
	left = new Image(11,21);
	right = new Image(11,21);
	arrow = new Image(10,13);
	arrowWhite = new Image(10,13);
	backgroundPath = new Image(21,1);
	backPressed = new Image(23,30);
	
	left.src = "style/images/buttonHoldLeft.png";
	right.src = "style/images/buttonHoldRight.png";
	arrow.src = "style/images/arrow.png";
	arrowWhite.src = "style/images/arrowWhite.png";
	backgroundPath.src = "style/images/menuBackground.png";
	backPressed.src = "style/images/back.png";
}

$(".important").each(function(){
	var text = $(this).text();
	$(this).html('<table><tr><td class="imgImportant"><div>!</div></td><td>'+text+'</td></tr></table>');
});

if(!$.browser.webkit)
{
	$(".UIButton span").css({backgroundPosition:'left 1px'});
}
else
{

}


$("#backButton").mousedown(function(){
	$(this).css({'background':'url("style/images/back.png")'});
});

$("#backButton").mouseup(function(){
	$(this).css({'background':'url("style/images/backPressed.png")'});
});

$("#backButton").click(function(){

	$(this).parent().parent().children("#content").html("");
	$("#ajax").animate({
		opacity: 0
		},
		200,
		function(){
		
		});
	$("#cache").animate({
		opacity: 0
		},
		600,
		function(){
			$(this).css({display:"none"});
			$("#ajax").css({display:"none"});
			$("footer").css({display:"block"});
			$("#content").css({opacity:0});
		});
	
});



$("#liste table tr").click(function(){
	idQuestion = $(this).children(".question").children(".id_question").text();
	$.post(
			"question.php", 
			{ id_question: idQuestion},
			function(data){
				$("#content").html(data);
				load();
				$("footer").css({display:"none"});
				
	$("#cache").css({display:"block"});
	$("#ajax").css({display:"block"});

	

	$("#cache").animate({
		opacity: 1
		},
		400,
		function(){
			$("#ajax").animate({
				opacity: 1
				},
				200,
				function(){
					var leftPosition = ($(window).width() - $("#content").width())/2;
					var topPosition = document.body.scrollTop + 100;
					
					$("#ajax").animate({
							height: $("#content").height()+50+"px",
							left:leftPosition+"px",
							top:topPosition+"px"
						},
						200,
						function(){
							$("#content").animate({
								opacity: 1
								},
								200,
								function(){
									
								});
						});
				});
		});
			}
	 	);

});


$("#cache").click(function(){
	$("#backButton").click();
});




