var $loginDialog, $signupDialog;
function initDialogs()
{
    initLoginDialog();
    initMeetlieLoginDialog();
    initSignupDialog();
    initFeedbackDialog();
    initEmailDialog();
}

function initLoginDialog()
{
    $loginDialog=$('<div></div>')
        .html($('#loginDialog').html())
        .dialog({
                bgiframe: true,
                autoOpen: false,
                height: "350",
                width: "300",
                modal: true,
                buttons: {
                    'Login': function() {
                        sendMeetlieLoginData();
                        $(this).dialog('close');
                    },
                    Cancel: function() {
                        $(this).dialog('close');
                    }
                },
                close: function() {
                }
	    });
        $("#loginDialog").html("");  //clear the original, hidden loginDialog html, as it causes duplicates in jquery's 'serialize'
	$loginDialog.keypress(function(e){
	    if(e.keyCode==13)
		{
		    sendMeetlieLoginData();
		    $loginDialog.dialog('close');
		}
		});
}

function initMeetlieLoginDialog()
{
    $meetlieLoginDialog=$('<div></div>')
        .html($('#meetlieLoginDialog').html())
        .dialog({
                bgiframe: true,
                autoOpen: false,
                height: "350",
                width: "300",
                modal: true,
                buttons: {
                    'Login': function() {
                        sendMeetlieLoginData();
                        $(this).dialog('close');
                    },
                    Cancel: function() {
                        $(this).dialog('close');
                    }
                },
                close: function() {
                }
	    });
        $("#meetlieLoginDialog").html("");  //clear the original, hidden loginDialog html, as it causes duplicates in jquery's 'serialize'
	$meetlieLoginDialog.keypress(function(e){
	    if(e.keyCode==13)
		{
		    sendMeetlieLoginData();
		    $meetlieLoginDialog.dialog('close');
		}
		});
}

function sendMeetlieLoginData()
{
    $("form#meetlieLoginForm > input#password").val(sha1($("form#meetlieLoginForm > input#password").val()));
    var parameters=$("form#meetlieLoginForm").serialize();
    parameters+="&remember=1";
    sendLoginRequest(parameters);
}

function initSignupDialog()
{
    $signupDialog=$('<div></div>')
	.html($('#signupDialog').html())
	.dialog({
		bgiframe: true,
		autoOpen: false,
		height: "350",
		width: "300",
		modal: true,
		buttons: {
		    'Meet!': function() {
			sendSignupData();
			$(this).dialog('close');
		    },
		    Cancel: function() {
			$(this).dialog('close');
		    }
		},
		close: function() {
		}
	    });
    $signupDialog.keypress(function(e){
	    if(e.keyCode==13)
		{
		    sendSignupData();
		    $signupDialog.dialog('close');
		}
		});
        $("#signupDialog").html("");  //clear the original, hidden signupDialog html, as it causes duplicates in jquery's 'serialize'
}

function sendSignupData()
{
    variables=$("form#signupForm").serialize();
    variables+="&action=signup";
	$.post("php/siteActions.php", variables, function(response){
		var responseVars=response.split(",");
		var host=responseVars[1];
		var id=responseVars[0];
		var slot=responseVars[2];
		host=jQuery.trim(host);
		id=jQuery.trim(id);
		slot=jQuery.trim(slot);
		var mySignup="div#signup"+slot;		
    		$(mySignup).removeClass("free");
    		$(mySignup).addClass("taken");  //style the signup as 'taken'
		$(mySignup+" > .signupButton").remove(); //get rid of the signup button
		$("div#notifications").html("You signed up for "+host+"'s meeting!");
		$("div#notifications").show();
		$("form#signupForm > input[type='hidden']").remove();
		loadSignupsPage(); //refresh 'my Signups'
	    });
}

function initFeedbackDialog()
{
    $feedbackDialog=$('<div></div>')
	.html($('#feedbackDialog').html())
	.dialog({
		bgiframe: true,
		autoOpen: false,
		height: "350",
		width: "300",
		title: "feedback",
		modal: true,
		buttons: {
		    'Submit': function() {
			sendFeedbackData();
			$(this).dialog('close');
		    },
		    Cancel: function() {
			$(this).dialog('close');
		    }
		},
		close: function() {
		}
	});
    $("#feedbackDialog").html(""); //clear the initial html so we don't get double variables in the post 
    $feedbackDialog.keypress(function(e){
	    if(e.keyCode==13)
		{
		    sendFeedbackData();
		    $feedbackDialog.dialog('close');
		}
		});
}

function sendFeedbackData()
{
    var parameters=$("form#feedbackForm").serialize();
    $.post("php/feedback.php", parameters, function(response){
	    $("div#notifications").html("Thanks for the feedback!");
	    $("div#notifications").show();				
	});
}

function initEmailDialog()
{
    $emailDialog=$('<div></div>')
	.html($('#emailDialog').html())
	.dialog({
		bgiframe: true,
		autoOpen: false,
		height: "350",
		width: "300",
		title: "join our email list",
		modal: true,
		buttons: {
		    'Submit': function() {
			sendEmailData();
			$(this).dialog('close');
		    },
		    Cancel: function() {
			$(this).dialog('close');
		    }
		},
		close: function() {
		}
	});
    $("#emailDialog").html(""); //clear the initial html so we don't get double variables in the post 
    $emailDialog.keypress(function(e){
	    if(e.keyCode==13)
		{
		    sendEmailData();
		    $emailDialog.dialog('close');
		}
	});
}

function sendEmailData()
{
    var parameters=$("form#emailForm").serialize();
    $.post("php/email.php", parameters, function(response){
	    $("div#notifications").html("Thanks for joining our email list!");
	    $("div#notifications").show();				
	});
}

function showSignupDialog()
{
    $signupDialog.dialog('open');
}

function showLoginForm(){
    $loginDialog.dialog("open");
}

function showMeetlieLoginForm()
{
    $meetlieLoginDialog.dialog('open');
}

function showFeedback()
{
    $("input#feedbackUser").val(user);
    $feedbackDialog.dialog('open');
}

function showEmailDialog()
{
    $("input#emailUser").val(user);
    $emailDialog.dialog('open');
}