﻿

$(function() {
	$(".ask_doctor_link").click(function(e) {
		e.preventDefault();
		var scope = $(this);
		$("#ask_doctor_group_action, .question_trigger_container").hide();
		var replyForm = scope.closest("form");
		scope.addClass("ajax_loading");

		$.ajax({
			type: "GET",
			url: replyForm.attr("action"),
			customData: {
				button: scope
			},
			success: function(data, status, xhr) {
				this.customData.button.removeClass("ajax_loading");

				$("#ask_doctor_group_panel").hide();
				$("#ask_doctor_group_panel").html(data);
				$(".create_discussion_container").hide();
				$(".right_part").addClass("expanded");
				$("#ask_doctor_group_panel").slideDown();
			},
			error: function(xhr, status, error) {
				this.customData.button.removeClass("ajax_loading");
				// model exceptions are returned with HTTP status code = 400
				if (xhr.status == 400) {
					Viva.Notify.show(Viva.Notify.InformationType.Error, "Vprašanje strokovnjaku", xhr.responseText);
				}
				else {
					Viva.Notify.show(Viva.Notify.InformationType.Error, "Vprašanje strokovnjaku", "Prišlo je do napake. Prosimo, poskusite kasneje.");
				}
			}
		});
	});

	var showQuestion = getParameterByName("q");

	if (showQuestion == "true") {
		$(".ask_doctor_link").click();
	}
});


function getParameterByName(name) {
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null)
		return "";
	else
		return results[1];
}


