$(document).ready(function () {
	$('.frm_modal').click(function (e) {
		var url = $(this).attr('href');
		e.preventDefault();
		e.stopPropagation();
		// load the frm_modal form using ajax
		$.get(url, function(data){
			// create a modal dialog with the data
			$(data).modal({
				onOpen: frm_modal.open,
				onShow: frm_modal.show,
				onClose: frm_modal.close,
				minWidth: 950,
				minHeight: '85%',
				maxHeight: '95%',
				overlayCss:{
					backgroundColor:"#000000"
				},
				containerCss:{
					backgroundColor:"#FFFFFF",
					border: '4px solid #F0F0F0',
					padding: '0px',
					overflow: 'auto',
					overflowX: 'hidden'
				},
				closeClass: 'cerrar'
			});
		});
	});
});

var frm_modal = {
	open: function (dialog) {
		dialog.overlay.fadeIn('slow', function () {
			dialog.data.fadeIn(0);
			dialog.container.fadeIn('slow', function () {
			});
		});
	},
	show: function (dialog) {
		$('#simplemodal-container form').validate({
			submitHandler: function(form) {
				var formulario = $(form);
				var div_contenedor = $("#frm-container");
				var div_titulo = $("#frm-title");
				var div_loading = $("#frm-loading");
				
				formulario.fadeOut(200, function () { 
					div_titulo.html('Enviando formulario...'); 
					div_loading.fadeIn(200, function() {
						$.ajax({
							url: formulario.attr('target'),
							data: formulario.serialize() + '&ajax=true&Submit=true',
							type: 'post',
							cache: true,
							dataType: 'html',
							success: function (data) {
								div_loading.fadeOut(200, function () {
									div_titulo.html('Envío completado');
									formulario.html(data).fadeIn(200);
								});
							},
							error: function (data) {
								div_loading.fadeOut(200, function () {
									div_titulo.html('<span style="color: #FF0000">Error al enviar el formulario</span>');
									formulario.fadeIn(200);
								});
							}
						});
					});
				});
			}
		});
	},
	close: function (dialog) {
		$("#slider-fotos").unbind().find('a').unbind();
		dialog.container.fadeOut('slow', function () {
			dialog.overlay.fadeOut('slow', function () {
			$.modal.impl.unbindEvents();
			
			// remove the remaining elements
			$.modal.impl.dialog.container.hide().remove();
			$.modal.impl.dialog.overlay.hide().remove();
			$.modal.impl.dialog.iframe && this.dialog.iframe.hide().remove();

			// reset the dialog object
			$.modal.impl.dialog = {};
			});
		});
	}
};