var gets = extractUrlParams();

/**
 * Gets the params in the URL and returns a table key <-> vaalue.
 */
function extractUrlParams(){	
	var t = location.search.substring(1).split('&');
	var f = [];
	for (var i=0; i<t.length; i++){
		var x = t[ i ].split('=');
		f[x[0]]=x[1];
	}
	return f;
}

function submitForm() {
	var action = '/support/contact.do?theme=' + gets['theme'] + '&subject=' + gets['subject'] + '&question=' + gets['question'];
	var option = document.getElementById('sOption');
	
	if (typeof(option.value) != 'undefined') {
		action = action + '&option=' + option.value;
	}
	document.getElementById('supportForm').action = action;
	
	return true;
}
$('html').addClass('js');

function pageload(hash) {
	//cas d'une question
	if (hash.indexOf("qlist") == 0) {
		$("#top-qanda, .qlist").hide();
		$("#formulaire").empty();
		$("#"+hash).show();
		$("#themes-list a[href='#"+hash+"']").addClass("active");
		return false;
	}
	else if(hash.indexOf("q") == 0){
		$("#top-qanda, .qlist").hide();
		loadForm($('a[id$="' + hash + '"]').attr('href'));
		return false;
	}
	else {	
		$('#formulaire').hide();
		$('#supportThemes').show();
	}
}

function loadForm(hash) {
	$('#supportWrapper #formulaire').load(hash,function(){
		var qId = $("#formulaire input[name='question']").attr("value");
		var theId = $("#qanda a#q"+qId).parent("p").parent("li").parent("ul").parent("div").attr("id");
		$("#themes-list a[href='#"+theId+"']").addClass("active");
	});
	$('#formulaire').show();
	$('#supportThemes').hide();
	
}

$(function(){
	
	/* Filters menu*/
	$("#themes-list ul li a").click(function(e){
	
		$(".qlist").hide();
		$('#formulaire').hide();
		e.preventDefault();
		var thisHref = $(this).attr("href");
		$("#themes-list ul li a").removeClass("active");
		$(this).addClass("active");

		if($("#top-qanda:visible")){
			$("#top-qanda").hide();
		}
		
		
		$("#qanda > "+thisHref).show();
	});
	
	$('#supportThemes ul h3').click(function(e){
		location.hash = this.id;
		console.log(e);
		e.preventDefault();
	});

	$('#qanda ul li p a').click(function(e){
		e.preventDefault();
		$('#formulaire').empty();
		$(".qlist").hide();
		loadForm(this.href);
		var hash = this.id;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
		$('#formulaire').show();
		$('#supportThemes').hide();
		return false;
	});	
	
	$.historyInit(pageload);

});