$(document).ready(function() {
	var printPressed = false;
	$(".question").fancybox({
		'titleShow'	: false,
		'onClosed'	:function() {
			$(".otherQuestions").css("display","none");
			$("#mainQuestion").css("display","block");
			$(".morehelp").css("display","block");
			$("#helpheaderorange").css("display","none");
		}
	});
	
	$(".question").click(function() {
		var split = $(this).attr("id").split("_");
		var indibs = false;
		if($(this).hasClass("indibs")) {
			indibs = true;
		}
		getAnswer(split[1],indibs);
		return false;
	});
	
	if(window.location.hash == "#conditions") {
		$("#question_49").trigger("click");
	}
	
	$(".printer").click(function() {
		if(!printPressed) {
			$("body").append("<h2 style='display:none' id='questionprintheader'>"+$("#mainQuestion #questionheader").html()+"</h2>");
			$("body").append("<p style='display:none' id='answerprintheader'>"+$("#mainQuestion #answertextholder").html()+"</p>");
			//$("body").append("<br /><br /><img src='/files/design/images/logo.gif' width='170' />");
			printPressed = true;
		}
		window.print();
		return false;
	});
	
	$(".btn-luk").click(function() {
		$.fancybox.close();
		return false;
	});
	
	$(".morehelp").click(function() {
		printPressed = false;
		$(".printer").css("display","none");
		$("#mainQuestion").fadeOut("fast", function() {
			$(".otherQuestions").fadeIn("fast");
		});
		$(".morehelp").fadeOut("fast", function() {
			$("#helpheaderorange").fadeIn("fast");
		});
		return false;
	});
	
	//Links from within help categories window
	$(".questionShowAnswer").click(function() {
		$(".printer").css("display","inline");
		
		var split = $(this).attr("id").split("_");
		var indibs = false;
		if($(this).hasClass("indibs")) {
			indibs = true;
		}
		getAnswer(split[1],indibs);
		
		$(".otherQuestions").fadeOut("fast", function() {
			$("#mainQuestion").fadeIn("fast");
		});
		$("#helpheaderorange").fadeOut("fast", function() {
			$(".morehelp").fadeIn("fast");
		});
		
		return false;
	});
	
	//From help link in top of pages
	$(".questionFromHelp").click(function(){
		$("#mainQuestion").hide();
		$(".morehelp").hide();
		$("#helpheaderorange").show();
		$(".otherQuestions").show();
		return false;
	});
	
});

function getAnswer(id,indibs) {
	if(indibs) {
		$("#mainQuestion #questionheader").html($("#popupcontent_"+id).find(".popupcontentheader").html());
		$("#mainQuestion #answertextholder").html($("#popupcontent_"+id).find(".popupcontentanswer").html());
	}
	else {
		$.ajax({
			type: "POST",
			url: "/cms/modules/ehandel.question/php/getAnswer.ajax.php",
			data: {
				id: id
			},
			success: function(response){
				var obj = $.parseJSON(response);
				$("#mainQuestion #questionheader").html(obj.head);
				$("#mainQuestion #answertextholder").html(obj.text);
			}
		 });
	}
}
