// $Id: modalframe_example.js,v 1.1.2.6 2009/06/17 12:54:57 markuspetrux Exp $

(function ($) {

Drupal.behaviors.modalFramePrimaPage = function() {
  $('.modalframe-page-child:not(.modalframe-page-processed)').addClass('modalframe-page-processed').click(function() {
    var element = this;

    // This is our onSubmit callback that will be called from the child window
    // when it is requested by a call to modalframe_close_dialog() performed
    // from server-side submit handlers.
    function onSubmitCallbackPrimaPage(args, statusMessages) {
      // Display status messages generated during submit processing.
      if (statusMessages) {
        $('.modalframe-page-messages').hide().html(statusMessages).show('slow');
      }

      if (args && args.message) {
        // Provide a simple feedback alert deferred a little.
        setTimeout(function() { alert(args.message); }, 500);
      }
    }

    // Hide the messages are before opening a new dialog.
    $('.modalframe-page-messages').hide('fast');

    // Build modal frame options.
    var modalOptions = {
      url: $(element).attr('href'),
      autoResize: false,
      autoFit: true,
      draggable: false,
      onSubmit: onSubmitCallbackPrimaPage
    };

    // Try to obtain the dialog size from the className of the element.
    var regExp = /^.*modalframe-page-size\[\s*([0-9]*\s*,\s*[0-9]*)\s*\].*$/;
    if (typeof element.className == 'string' && regExp.test(element.className)) {
      var size = element.className.replace(regExp, '$1').split(',');
      modalOptions.width = parseInt(size[0].replace(/ /g, ''));
      modalOptions.height = parseInt(size[1].replace(/ /g, ''));
    }
    modalOptions.width = 1020; 
      modalOptions.height = 611;

    // Open the modal frame dialog.
    Drupal.modalFrame.open(modalOptions);

    // Prevent default action of the link click event.
    return false;
  });
};

})(jQuery);

