/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2007 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 39 2007-10-26 20:46:32Z emartin24 $
 *
 */

$(document).ready(function () {
	
	$('#postForm a[name="new-post"]').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		$.get("data/new-post.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				overlayId: 'postModalOverlay',
				containerId: 'postModalContainer',
				iframeId: 'postModalIframe',
				onOpen: post.open,
				onShow: post.show,
				onClose: post.close
			});
		});
	});
});

var post = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
												   
				dialog.content.fadeIn(200, function () {
					$('#postModalContainer #username').focus();
				});
				
				
				
			});
		});
	},
	show: function (dialog) {
		$('#postModalContainer .send').click(function (e) {
			e.preventDefault();
			// validate form
			if (post.validate()) {
				$('#postModalContainer .message').fadeOut(function () {
					$('#postModalContainer .message').removeClass('error').empty();
				});
				$('#postModalContainer .title').html('Sende...');
				$('#postModalContainer form').fadeOut(200);
				$('#postModalContainer .content').animate({
					height: '80px'
				}, function () {
					$('#postModalContainer .loading').fadeIn(200, function () {
						$.ajax({
							url: 'data/new-post.php',
							data: $('#postModalContainer form').serialize() + '&action=send',
							dataType: 'html',
							complete: function (xhr) {
								$('#postModalContainer .loading').fadeOut(200, function () {
									$('#postModalContainer .title').html('Vielen Dank!');
									$('#postModalContainer .message').html(xhr.responseText).fadeIn(200);
								});
							},
							error: post.error
						});
					});
				});
			}
			else {
				if ($('#postModalContainer .message:visible').length > 0) {
					$('#postModalContainer .message div').fadeOut(200, function () {
						$('#postModalContainer .message div').empty();
						post.showError();
						$('#postModalContainer .message div').fadeIn(200);
					});
				}
				else {
					$('#postModalContainer .message').animate({
						height: '30px'
					}, post.showError);
				}
				
			}
		});
	},
	close: function (dialog) {
		dialog.content.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.remove(dialog);
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		post.message = '';
		/*if (!$('#postModalContainer #name').val()) {
			tell.message += 'Name is required. ';
		}*/

		var comment = $('#postModalContainer #comment').val();
		var head = $('#postModalContainer #head').val();
		//var yourname = $('#postModalContainer #yourname').val();
		//var theirname = $('#postModalContainer #theirname').val();
		
		 if (!comment) {
			
			post.message = 'Bitte geben Sie eine Überschrift ein.';
			$('#postModalContainer #head').focus();
		}
		
		if (!comment) {
			
			post.message = 'Bitte geben Sie einen Kommentar ein.';
			$('#postModalContainer #comment').focus();
		}
		
		/*if (!$('#postModalContainer #message').val()) {
			tell.message += 'Message is required.';
		}*/

		if (post.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	showError: function () {
		$('#postModalContainer .message').html($('<div class="error"></div>').append(post.message)).fadeIn(200);
	}
};
