/**
  Modal
*/
function modal() {}
modal.prototype = {
  ownModal: null,
  initialize: function() {
    var msg=jQuery('#customModal');
    if (msg.length == 0){
      msg = jQuery('<div/>', {
        'id': "customModal"
      });
      jQuery('body').append(msg);
    };
    
    var loader = jQuery('#myModal-loader');
    if(loader.length == 0) {
      loader = jQuery('<div/>', {
        'id': "myModal-loader"
      });
      jQuery('body').append(loader);
    }
    
    this.ownModal = msg;   
    
    jQuery('.ui-widget-overlay').live('click', function(){
      modal.prototype.hideModal();
    });    
     
  },
  
  showModal: function(url, modiframe) {
    var msg = jQuery('#customModal');
    
    var loader = jQuery('#myModal-loader');
    loader.show();    
    
    jQuery.post(url, function(response)
    {
      msg.html(response);
      options = {
        width: 'auto',
        dialogClass: modiframe? 'modalDefault modalIframe':'modalDefault',
        modal: true,
        closeOnEscape: true,
        resizable: false,
        draggable: false,
        zIndex:99990,
        autoReposition: true
      };

      loader.hide();
      msg.dialog(options);
    });
  },
  
  hideModal: function() {
    this.ownModal.dialog("close");
  }
};

/**
 * Retro compatibility with old plateform function used to display modal.
 * @param {String} url Url of the ajax login system
 * @returns void
 */
function viewModalSocial(url, modiframe) {
  viewModal(url, modiframe);
}

function viewModal(url, modiframe) {
  if(modiframe == undefined){
    modiframe = false;
  }
  modal.prototype.initialize();
  modal.prototype.showModal(url.replace('url_site/', ''), modiframe);
};

function resizeIframe(sizeClass){
  jQuery('.iframeSocial').attr('class', sizeClass).addClass('iframeSocial');
  var dialog = jQuery('#customModal').data("dialog");
  if (dialog.options.autoReposition) {
    dialog.option("position", dialog.options.position);
  }
}

function callbackIframe(action){
  switch(action) {
    case 'closeIframe': {
        doHideModal();
        break;
    }
    case 'closeGuestCreation': {
        membreIdentifie = 1;
        doHideModal();
        break;
    }
  }
}

BOM.Utils.socialReloadPage = true;

function doHideModal(){
  modal.prototype.hideModal();  
  if(BOM.Utils.socialReloadPage){
    top.location.reload();
  }  
}
