$(document).ready(function () {
	$('.login_link').click(function (e) {
		e.preventDefault();
		// load the login form using ajax
		$.get(login_url, function(data){
			// create a modal dialog with the data
			$(data).modal({
				closeHTML: "<a href='#' title='Fermer' class='modal-close'>x</a>",
				position: ["15%",],
				overlayId: 'modal-overlay',
				containerId: 'modal-container',
				onOpen: modal.open,
				onShow: modal.show,
				onClose: modal.close
			});
		});
	});

	// preload images
	var img = ['cancel.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'send.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = '/images/bahunet_loginbox/' + this;
	});
});

var modal = {
	open: function (dialog) {
		// height in pixels
		var h = 180;
		var title = $('#modal-container .modal-title').html();
		$('#modal-container .modal-title').html('Chargement...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#modal-container .modal-content').animate({
						height: h
					}, function () {
						$('#modal-container .modal-title').html(title);
						$('#modal-container form').fadeIn(200, function () {
							$('#modal-container #login_nickname').focus(); // Focus on nickname input
						});
					});
				});
			});
		});
	},
	show: function (dialog) {
		$('#modal-container .modal-send').click(function (e) {
			e.preventDefault();

      var msg = $('#modal-container .modal-message');
      $('#modal-container .modal-title').html('Envoi en cours...');
      $('#modal-container form').fadeOut(200, function () {
        $('#modal-container .modal-loading').fadeIn(200, function () {
          $.ajax({
            url: login_url,
            data: $('#modal-container form').serialize(),
            type: 'post',
            cache: false,
            dataType: 'html',
            success: function (data) {
              if(data == 'OK')
              {
                $('#modal-container .modal-loading').fadeOut(200);
                $('#modal-container .modal-title').html('Connexion acceptée...');
                $('#modal-container form').fadeOut(200);
                $('#modal-container .modal-content').animate({
                  height: 40
                }, function () {
                  dialog.data.fadeOut(200, function () {
                    dialog.container.fadeOut(200, function () {
                      dialog.overlay.fadeOut(200, function () {
                        $.modal.close();
                      });
                    });
                  });
                });
                location.reload();
              }
              else // Form is not valid
              {
                $('#modal-container .modal-loading').fadeOut(200, function(){
                  $('#modal-container .modal-content').fadeIn(200, function(){
                    $('#modal-login-form-content').html(data);
                    $('#modal-container form').fadeIn(200);
                    $('#modal-container #login_nickname').focus(); // Focus on nickname input
                  });
                });
              }
            }
          });
        });
      });
		});
	},
	close: function (dialog) {
		$('#modal-container .modal-title').html('Fermeture...');
		$('#modal-container form').fadeOut(200);
		$('#modal-container .modal-content').animate({
			height: 40
		}, function () {
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
					}); 
				});
			});
		});
    
	}
};