var slideheight = 450;
var originalheight = 51;
var mailLoaded = false;
var spaceState = false;
var spaceReady = true;

function spaceToggle()
{
	if (!mailLoaded)
	{
		if (!!window.postMessage) {
			$('#mailbox').load("fragments/mail.txt");
		} else {
			var ifr = $("<iframe src=\"http://mail.kulmblick.net\" height=\"400\" frameBorder=\"0\" marginheight=\"0\" marginwidth=\"0\"/>");
			$('#mailbox').append(ifr);
		}
		mailLoaded = true;
	}
	if(spaceReady)
	{
		spaceReady = false;
		if (!spaceState) {
			spaceOpen();
			spaceState=true;
		} else {
			spaceClose();
			spaceState=false;
		}
	}
}

function setReady() {
	spaceReady = true;
}

function spaceOpen() {
    $('#mailbox').show();
    $('#footer').animate({
        "height": slideheight + originalheight + "px"
    }, "slow",setReady);
    $('#footerclose').click(spaceToggle);    
    $('#footerclose').show();
    _gaq.push(['_trackEvent', 'Mail', 'Reopen']);
}

function spaceClose() {
    $('#footer').animate({
        "height": originalheight + "px"
    }, "slow",setReady);
    $('#mailbox').hide();
    $('#footerclose').hide();
    _gaq.push(['_trackEvent', 'Mail', 'Close']);
}

function adjustsize() {
    var complete = $(window).height();
    if ((originalheight + slideheight) > complete) {
        $('#footer').hide();
    }
    else {
        $('#footer').show();
    }
}
$(window).resize(adjustsize);

function sendmessage() {
    var mname = $('input[name="name"]').val();
    var mmail = $('input[name="email"]').val();
    var mtime = $('input[name="timeframe"]').val();
    var mtext = $('textarea[name="text"]').val();

    var msg = "Zeitraum: " + mtime + "\n\n" + mtext;
    var sub = mname + " - " + mtime;

    var mail = {
        'ReplyTo': mmail,
        'Text': msg,
        'Subject': sub
    };

    var mailmsg = {
        'data': JSON.stringify(mail)
    };

    var xhr = new easyXDM.Rpc({
        remote: "https://kulmmail.appspot.com/static/index.html"
    }, {
        remote: {
            request: {}
        }
    });

    xhr.request({
        url: "/json",
        method: "POST",
        data: mailmsg
    }, function(response) {
        if (JSON.parse(response.data).Code == 'ok') {
            alert("Ihre Mail wurde versandt!");
            mailclose();
        }
        else {
            alert("Fehler: Bitter versuchen Sie es erneut oder senden sie Ihre Mail direkt an info@kulmblic"+String.fromCharCode(107)+String.fromCharCode(46)+String.fromCharCode(110)+String.fromCharCode(101)+String.fromCharCode(116));
        }

    });
}

