jQuery(document).ready(function() {

  /*  Generic ajax button press.
  */
  jQuery('a.ajax').live('click', function() {
    /* Send an AJAX request to the target page. */
    revv_ajax(jQuery(this).attr('href'), null, jQuery(this).attr('callback'));

    return false;
  });

  jQuery('a.ajax-dialog').live('click', function() {
    /* Send an AJAX request to the target page. */
    var title = jQuery(this).attr('title');
    var width = jQuery(this).attr('width');
    var dialogclass = jQuery(this).attr('dialogclass');
    
    jQuery.get(jQuery(this).attr('href'), null, function(data) {
      drawDialogHTML(title, data, {
          'Close'  : function() { jQuery(this).dialog('close'); }
      }, width, dialogclass);
    });

    return false;
  });

  jQuery('a.inline-dialog').live('click', function() {
    var title = jQuery(this).attr('title');
    var html = jQuery(jQuery(this).attr('href')).html();
    drawDialogHTML(title, html, {
        'Close'  : function() { jQuery(this).dialog('close'); }
    });

    return false;
  });
	
	/* login popup */
	
	/*jQuery('.loginbtn a').click(function(){
    var title = "Mobileez Login";
    jQuery.get('/revvengine/engine/open/login', null, function(data) {
      drawDialogHTML(title, data, {
          'Close'  : function() { jQuery(this).dialog('close'); }
      });
    });
		return false;
	});*/
	

});

function revv_ajax(url, data, callback, overrideCallback)
{
  if(data === undefined) data = { };

  if(typeof data == 'string') {
    data += '&ajax=yes';
  }
  else if(data) {
    data.ajax = 'yes';
  }
  else {
    data = null;
  }

  if(callback  &&  !jQuery.isFunction(callback) &&  jQuery.isFunction(window[callback])) {
		callback = window[callback];
	}

  if(callback  &&  jQuery.isFunction(callback)) {
    var success =
      function(response, status) {
        if(overrideCallback  &&  'onsuccess' in response) delete response.onsuccess;
        if(checkAjaxResponse(response, status)) {
          return callback(response);
        }
        return false;
      }
    ;
  }
  else {
    var success = checkAjaxResponse;
  }

  jQuery.ajax({
    type: 'POST',
    url: url,
    data: data,
    dataType: 'json',
    success: success,
    error: checkAjaxResponse
  });
}

function revv_ajax_submit(form, callback, url, overrideCallback)
{
  var data = jQuery(form).serialize();
  if(!url) url = jQuery(form).attr('action');
  revv_ajax(url, data, callback);
}

function checkAjaxResponse(response, status) {
  // Check if the AJAX call was a success or not.
  if(status === 'success') {
    // Be careful about the type of result we get...
    if(response && typeof response == 'object' && response.result) {
      // Make sure the result was successful.
      if(response.result === 'success') {
        // Do something with the data...

        // Execute the callback function...
        if('onsuccess' in response && jQuery.isFunction(window[response.onsuccess])) {
          window[response.onsuccess](response);
        }

        return true;
      } else {
        // Execute the callback function...
        if('onerror' in response && jQuery.isFunction(window[response.onerror])) {
          window[response.onerror](response);
        } else {
          // Otherwise, handle the error... Standard method coming soon.
          if(response.result == 'Session Expired') {
            // Reload the window... this will take the user to the login screen.
            window.location.reload();
          }
          alert(response.result+': '+response.message);
        }
      }
    } /* If there is no result, we ignore (for now). It cant be a valid response. */
  } else {
    alert('AJAX communication error.');
  }

  // Return error.
  return false;
}

function drawDialogHTML(title, html, buttons, width, dialogclass) {
  // Make sure the dialog container exists before using it.
  if(!jQuery('#dialog').size() > 0) {
    jQuery(document.body).append('<div id="dialog" class="container_12"></div>');
  }
  if(!width) { width = jQuery(window).width() * 0.66; }
  // Create the action dialog form.
  jQuery('#dialog').html(html).dialog({
    // General dialog settings.
    'title'      : title,
    'modal'      : true,
    'resizable'  : false,
    'width'      : width,
    'dialogClass': dialogclass,

    /* Buttons that appear on the UI dialogue. */
    'buttons'    : buttons,

    // Event to run onclose.
    'close' : function() { jQuery(this).remove(); }
  });
}

function ajaxDialog(data)
{
  drawDialogHTML(data.title, data.html, {
      'Close'  : function() { jQuery(this).dialog('close'); }
  });
}


function ajaxRedirect(data)
{
  location.href = data.redirect;
}

