/*
 * 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 () {
	$('#sendafriend').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		$.get("action/sendafriend.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				overlayId: 'contactModalOverlay',
				containerId: 'contactModalContainer',
				iframeId: 'contactModalIframe',
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		});
	});
});

var contact = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.content.fadeIn(200, function () {
					$('#contactModalContainer #name').focus();
				});
				// resize the textarea for safari
				if ($.browser.safari) {
					$('#contactModalContainer textarea').attr({
						cols: '37',
						rows: '8'
					});
				}
			});
		});
	},
	show: function (dialog) {
		$('#contactModalContainer .send').click(function (e) {
			e.preventDefault();
			// validate form
			if (contact.validate()) {
				$('#contactModalContainer .message').fadeOut(function () {
					$('#contactModalContainer .message').removeClass('error').empty();
				});
				$('#contactModalContainer .title').html('Wird verschickt ...');
				$('#contactModalContainer form').fadeOut(200);
				$('#contactModalContainer .content').animate({
					height: '80px'
				}, function () {
					$('#contactModalContainer .loading').fadeIn(200, function () {
						$.ajax({
							type: "POST",
							url: 'action/do_sendafriend.php',
							data: $('#contactModalContainer form').serialize() + '&action=send'+'&media_id='+$('#media_id').val(),
							dataType: 'html',
							complete: function (xhr) {
								$('#contactModalContainer .loading').fadeOut(200, function () {
									$('#contactModalContainer .title').html('Die Weiterempfehlung wurde verschickt. ');
									$('#contactModalContainer .message').html(xhr.responseText).fadeIn(200);
								});
							},
							error: contact.error
						});
					});
				});
			}
			else {
				if ($('#contactModalContainer .message:visible').length > 0) {
					$('#contactModalContainer .message div').fadeOut(200, function () {
						$('#contactModalContainer .message div').empty();
						contact.showError();
						$('#contactModalContainer .message div').fadeIn(200);
					});
				}
				else {
					$('#contactModalContainer .message').animate({
						height: '50px'
					}, contact.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 () {
		contact.message = '';
		if (!$('#contactModalContainer #namesender').val()) {
			contact.message += 'Bitte geben Sie ihren Namen an. ';
		}

        if (!$('#contactModalContainer #name1').val()) {
            contact.message += 'Bitte geben Sie den Namen des Empf�ngers an. ';
        }

		var email = $('#contactModalContainer #emailsender').val();
		if (!email) {
			contact.message += 'Bitte geben Sie ihre Email-Adresse an. ';
		}
		else {
			// Regex from: http://regexlib.com/REDetails.aspx?regexp_id=599
			var filter = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/;
			if (!filter.test(email)) {
				contact.message += 'Die Email-Adresse ist ung�ltig. ';
			}
		}

        var email = $('#contactModalContainer #email1').val();
        if (!email) {
            contact.message += 'Bitte geben Sie die Email-Adresse des Empf�ngers an. ';
        }
        else {
            // Regex from: http://regexlib.com/REDetails.aspx?regexp_id=599
            var filter = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/;
            if (!filter.test(email)) {
                contact.message += 'Die Email-Adresse ist ung�ltig. ';
            }
        }
        /*
		if (!$('#contactModalContainer #message').val()) {
			contact.message += 'Message is required.';
		}
        */
		if (contact.message.length > 0) {
			return false;
		}
		else {
			return true;
		}

	},
	showError: function () {
		$('#contactModalContainer .message').html($('<div class="error"></div>').append(contact.message)).fadeIn(200);
	}
};