/*
 * SimpleModal
 * http://www.ericmmartin.com/projects/simplemodal/
 * Revision: $Id: contact.js 212 2009-09-03 05:33:44Z emartin24 $
 */
var url ='';
$(document).ready(function () {
	contact.launchSM();
	
	// preload images
	var img = ['modal_top_glow.png', 'modal_content_glow.png', 'modal_bottom_glow.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = 'http://metalship.org/images/' + this;
	});
});

var contact = {
	launchSM: function() {
		var boite_divs1 = '<div><div class="sm-top">Loading...</div><div class="sm-loading"><img src="http://metalship.org/images/icons/loader.gif" /></div><div class="sm-content"><div class="sm-message" style="display:none" />';
		var boite_divs2 = '</div><div class="sm-bottom"></div></div>';
		
		$('.modal_open').live("click",function (e) {
			url = $(this).attr('rel');
			e.preventDefault();
			$(boite_divs1+boite_divs2).modal({
				closeHTML: '<a href="#" title="Close" class="modal-close"><img src="http://metalship.org/images/cancel.png" height="16"></a>',
				position: ["15%",],
				overlayId: 'sm-overlay',
				containerId: 'simplemodal',
				onOpen: contact.open,
				//onShow: contact.show,	// à ce moment, le contenu n'est pas chargé. Regarder si on clique sur le bouton du contenu non lancé ne marchera donc pas
				onClose: contact.close,
				overlayClose:true,
				minWidth:478,
				maxWidth:478
			});
		});
	},
	message: null,
	open: function (dialog) {
		$.get(url, function(data) {
			$('.sm-loading').hide();
			$('.sm-content').html(data);
			var title = $('.modal_title').attr('title');
			$('#simplemodal .sm-top').html(title);
			contact.show();
		});
		
		dialog.overlay.fadeIn(200, function () {
			dialog.data.hide();
			dialog.container.fadeIn(200, function () {
				dialog.data.slideDown('slow', function() {
					$('.sm-close').live('click',function(){ contact.close(dialog);});	// normalement pas de "live()". Mais pbs: parfois la fenêtre ne ferme pas. live() règle ça, mais ferme trop rapidement après la 2ème fermeture de fenêtre ds la même page
					if ($('input[@=hidden].close_after').attr('title')) setTimeout(function() { contact.close(dialog);},2000);
				});
			});
		});
	},
	show: function (dialog) {
		$('#simplemodal .sm-send').click(function (e) {
			e.preventDefault();
			// validate form
			if (contact.validate()) {
				var msg = $('#simplemodal .sm-message');
				msg.fadeOut(function () {
					msg.removeClass('seriousbox').empty();
				});
				$('#simplemodal .sm-top').html('Sending...');
				$('#simplemodal .sm-loading').slideDown('slow');
				$('#simplemodal .sm-content').slideUp('slow', function () {
					$.ajax({
						url: $('#simplemodal form').attr('action'),
						data: $('#simplemodal form').serialize() + '&action=send',
						type: 'post',
						cache: false,
						success: function (data) {
							$('#simplemodal .sm-loading').slideUp(200, function () {
								$('.sm-content').empty().append(data).slideDown('slow');
								$('textarea').not('.noautogrow').autogrow().css('overflow','hidden');
								contact.show();
								ajax.launchOverlabel();
								
								var title = $('.modal_title').attr('title');
								$('#simplemodal .sm-top').html(title);
								if ($('input[@=hidden].close_after').attr('title')) setTimeout(function() { contact.close(dialog);},2000);
								$('.sm-close').click(function(){ contact.close(dialog);});
							});
						},
						error: contact.error
					});
				});
			}
			else {
				//$('#simplemodal .sm-message').show();
				//contact.showError();
				$('#simplemodal .mandatory').each(function() {
					if (!$(this).val()) $(this).wrap('<div class="seriousbox" style="margin:0;padding:0;"></div>');
				});
			}
		});
	},
	close: function (dialog) {
		$('#simplemodal .sm-content').slideUp('slow');
		$('#simplemodal .sm-top').html('Goodbye...');
		dialog.data.fadeOut('slow', function () {
			dialog.container.fadeOut(200);
			dialog.overlay.fadeOut(200, function () {
				$.modal.close();
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		contact.message = '';
		$('#simplemodal .mandatory').each(function() {
			if (!$(this).val()) contact.message = 'Fill in all the required fields.';
		});
		if (contact.message.length > 0) return false;
		else return true;
	},
	showError: function () {
		$('#simplemodal .sm-message')
			.html($('<div class="seriousbox" style="display:none;"></div>')
			.append(contact.message)).hide().fadeIn('slow');
		$('.seriousbox').slideDown(500);
	}
};
