var callPoll = null;
var orgName = '';
var org = 0;
var default_area_code = '';

function clicktocall_set_org(name, atom, area)
{
	orgName = name;
	org = atom;
	default_area_code = area;
}

$(function(){
	$('.clicktocall_start').click( function() {
		setButtons(1);
		$('#clicktocall_form').show(1);
		$('#call_status').hide(0);
		$('#call_status #call_message').html('');
		$('#clicktocall_dialog').dialog('open');
	});
	$('#clicktocall_dialog').dialog({
		autoOpen: false,
		buttons: { "Connect": function() { clicktocall(); }, "Cancel": function() { $(this).dialog('close'); } },
		resizable: false,
		closeOnEscape: false,
		height: 250,
		width: 450,
		stack: false
	});
	$('#clicktocall_dialog').find('input').keypress(function(e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			$(this).parent().parent().parent().parent().find('.ui-dialog-buttonpane').find('button:first').click(); /* Assuming the first one is the action button */
			return false;
		}
	});
});

function checkcallStatus()
{
	$.getJSON('util.ajax', {action: 'callhandler', org: org},
	function(json) {
		if (json.result)
		{
			if (json.result == 2)
				$('#call_status #call_message').html('Contacting ' + orgName + '...');
			else if (json.result == 3)
				$('#call_status #call_message').html('Call completed. Thank you!');
			else if (json.result == 4)
				$('#call_status #call_message').html('You did not accept our call. Please try again.');
			else if (json.result == 5)
				$('#call_status #call_message').html('You did not answer your phone. Please try again.');
			if (json.result > 2)
			{
				$('#loading').hide();
				clearInterval(callPoll);
			}
		}
	});
}

function setButtons(state)
{
	if (state == 1)
		$('#clicktocall_dialog').dialog( "option", "buttons", [ { text: "Connect", click: function() { clicktocall(); }}, { text: "Cancel", click: function() { $(this).dialog('close'); } } ] );
	else if (state == 2)
		$('#clicktocall_dialog').dialog( "option", "buttons", [{ text: "Close", click: function() { $(this).dialog("close"); }}] );
}

function clicktocall()
{
	var error = '';
	var stripped = $('#clicktocall_phone').val().replace(/[\(\)\.\-\ ]/g, '');

	if (stripped.length == 7)
		stripped = default_area_code + stripped;
	if ($('#clicktocall_phone').val() == '')
		error = "Please enter your phone number.\n";
	else if (isNaN(parseInt(stripped)))
		error = "I'm sorry, the phone number contains characters I don't recognize.\n";
	else if (!(stripped.length == 10))
		error = "I'm sorry, the phone number is the wrong length. Make sure you included your area code.\n";

	if (error)
	{
		$('#call_status').show(1);
		$('#call_status #call_message').html("<span class='msg'>" + error + "</span>");
	}
	else
	{
		setButtons(2);
		$('#loading').unbind('ajaxSend');
		$.getJSON('util.ajax', {action: 'callhandler', org: org, phone: stripped},
		function(json) {
			if (json.result)
			{
				if (json.result == 1)
				{
					$('#call_status #call_message').html('Calling you...');
					callPoll = setInterval( "checkcallStatus()", 1000 );
				}
				else
					$('#call_status #call_message').html(json.message);
				$('#clicktocall_form').hide(0);
				$('#call_status').show(1);
			}
		});
	}
}
