var helpText_posX = -1;
var helpText_posY = -1;

$(document).ready(function() {

	// helpText
	$(".helpText").live("click",function(e){
		if(!$(this).data("subject")){ 
			return; 
		}
		var target = $("#help_"+$(this).data("subject"));
		var url = $(this).data("url");
		var iframe = $(this).data("iframe");
		if($(this).hasClass("big")){
			$(".helpText_content").dialog("destroy");
    	target.dialog({
				modal: true, 
				resizable: false, 
				height: $(window).height() * 0.9, width: 700
			});
		}
		else {
			if(!$(".helpText_content").is(":visible") || helpText_posX == -1 || helpText_posY == -1) { // id: 123d8
				helpText_posX = e.clientX + 15;
				helpText_posY = e.clientY + 15;
			}
			$(".helpText_content").dialog("destroy"); // do not move this above line with id: 123d8
			target.dialog({
				modal: true, 
				minHeight: 0,
				resizable: false, 
				position: [helpText_posX, helpText_posY]
			});
		}
		if(url) { // load content via ajax request
			target.load(url);
		}
		else if(iframe) { // load url into iframe
			target.css("overflow","hidden").html('<iframe style="margin: 0; padding: 0; border: 0px; " src="' + iframe + '" width="100%" height="100%"></iframe>');
		}
	});

	$(".ui-widget-overlay").live("click",function () {
		$(".helpText_content").dialog( "close" );
	});
	// helpText END
	
	$(".hoverChangeImg").hover(
		function() {var fn = $(this).attr("src").replace(/(\-?[0-9]*)\./,"_hover$1."); $(this).attr("src",fn);},
		function() { var fn = $(this).attr("src").replace(/_hover/,""); $(this).attr("src",fn);}
		);

	$(".pagination > select").change(function() {
		window.location = $(this).val();
	});

	$("#askus").dialog({ 
		autoOpen: false,
		open: function(event, ui) { refresh_responsetime = setInterval("getSupportResponsetime()", 10000); }, 
		close: function(event, ui) { clearInterval(refresh_responsetime); }, 
		resizable: false, 
		modal: true, 
		width: 450, 
		title: 'Spørg MADLOG' 
	});

	$(".askus")
		.click(function() {
			if($("#askus").html() == '') { drawAskUsDiv(1);}
			else { $("#askus").dialog('open');}
		});

	$(".defbutton").click(function(){
		window.location=$(this).data("href");
	});

	$(".confirm").live("click", function(){
		if(typeof $(this).data("confirm_msg") == 'undefined'){
			alert("Error: Missing confirm message");
			return false;
		}
		var answer = confirm($(this).data("confirm_msg"));
		if(!answer){
			return false;
		}
		if(!$(this).is("a,button:submit")){
			if(typeof $(this).data("href") == "string"){
				window.location = $(this).data("href");
			}
			else{
				alert("Error: Unknown type && missing href");
				return false;
			}
		}
	});

});

function u_unit(value){
	// NOTE: If we get countries that don't use dot as decimal delimiter, uncomment the sections below
//	var host = window.location.host;
//	if(host.match('madlog.dk')){
		return value.toString().replace(".",",");
//	}
//	else{
//		return value; // keep . as decimal seperator
//	}
}

function drawAskUsDiv(doOpen)
{
	$.getJSON("/api/sec/support/draw_askus_div.php", { }, function(data) {
		$("#askus").html(data.html);
		$("#askus_select")
			.change(function() {
				$(".askus_options").hide();
				var option = "#askus_option"+$(this).val();
				$(option).show();
			});
		$("#askus").dialog('option','position',{of: $(".askus"), at: 'left', my: 'right top'});
		$("#askus_submit")
			.click(function() {
				if($("#askus_message").val() == '')
				{$("#askus_message").focus(); return alert("Hvad vil du gerne spørge om, eller sige?"); }
				var option = $("#askus_select").val();
				if(option == 2 && $("#askus_email").val().length < 1)
					{ return alert("Indtast en email adresse."); }
				if(option == 3 && $("#askus_phone").val().length < 1)
					{ return alert("Indtast et telefonnummer."); }
				sendSupportMessage();
			});
		$("#askus_message").focus();
		if(doOpen == 1){ $("#askus").dialog('open'); }
	});
}

function getSupportResponsetime()
{
	$.getJSON("/api/support/get_responsetime.php", { }, function(data) {
		$("#closed_reason").html('<p>'+data.closed_reason+'</p>');
		$("#responsetime").html(data.text);
	});
}

function sendSupportMessage()
{
	$("#askus_submit").attr("disabled",true);
	$.getJSON("/api/sec/support/send_support_message.php", 
			{ "msg":$("#askus_message").val(), "contact_method":$("#askus_select option:selected").val(),"phone":$("#askus_phone").val(),"email":$("#askus_email").val() }, 
			function(data) {
				alert(data.status_text);
				if(data.status == 'ok'){ drawAskUsDiv(); $("#askus").dialog('close'); }
				else { $("#askus_submit").attr("disabled",false); }
	});
}

