img1 = new Image(16, 16);
img1.src="/immagini/extra/spinner.gif";
img2 = new Image(220, 19);
img2.src="/immagini/extra/ajax-loader.gif";

$(document).ready(function(){

  $("#login_link").click(function(){
    $('#login_form').modal();
  });

  // When the form is submitted
  //$("#status > form").submit(function(){
  $("#login").submit(function(){

    // Hide 'Submit' Button
    $('#submit').hide();

    // Show Gif Spinning Rotator
    $('#ajax_loading').show();

    // 'this' refers to the current submitted form
    var str = $(this).serialize();
//alert(str);
    // -- Start AJAX Call --

    $.ajax({
        type: "POST",
        url: "/index.php",  // Send the login info to this page
        data: str,
        success: function(msg){

          $("#status").ajaxComplete(function(event, request, settings){
            // Show 'Submit' Button
            $('#submit').show();
            // Hide Gif Spinning Rotator
            $('#ajax_loading').hide();
           // alert(msg.substring(0,2));
            if(msg.substring(0,2) == 'OK')   {  /* LOGIN OK? */
              var login_response = '<div id="logged_in" style="text-align:center">' +
          	    '<div>' +
          	    '<div style="margin-top: 18px;">' +
          	    '<img align="absmiddle" src="/immagini/extra/ajax-loader.gif">' +
          	    '</div>' +
          	    '<div style="margin-top: 10px">'+
          	    "Accesso avvenuto con successo! <br /> Si prega di attendere, reindirizzamento in corso...</div></div>";

              $('a.modalCloseImg').hide();

              /*$('#simplemodal-container').css("width","500px");*/
              $('#simplemodal-container').css("height","120px");

              $(this).html(login_response); // Refers to 'status'

              // After 2 seconds redirect the
              setTimeout('vai_pagina("'+msg+'")', 3000);
            } else  { /* ERROR? */
              var login_response = msg;
              $('#login_response').html(login_response);
            }

          });

        }

      });

    // -- End AJAX Call --

    return false;

  }); // end submit event

});

function vai_pagina(msg) {
  var arr = msg.split(/;/);
  window.location = '/'+arr[1]+'/'+arr[2]; // Members Area
}
