// Universal call to create dialog boxes

function create_dialog(url, userOpts)
{
	var defaultOpts = {autoOpen: false};
	
  var opts = $.extend({}, defaultOpts, userOpts);
  
  var dialogBox = $('#dialog_box');
  
  // $('#dialog_box').load(url, page_load);
  // Close the dialog box if it is open
  dialogBox.dialog('close').dialog('option', opts).load(url, function(){
  	$(this).HFInit();
  	var e = $(this).find('input:visible, select:visible, textarea:visible').get(0);
  	if (e){
  	 e.focus();
  	}
  });
}

function create_data(form_name)
{
  var formSelector = "#"+form_name+", form[name="+form_name+"]";
  var form = $(formSelector);
  
  // This is to make sure that the wysiwyg textareas are up to date before the form is submitted
  form.find('textarea.wysiwyg').each(function(){
  
    try {
      // This was causing a javascript error of some sort, so it has been wrapped in a try statement.
      // It would be nice to determine what the actual problem was and do a proper fix.
      $(this).htmlarea('updateTextArea');
    }
    catch(err) {}
  });
  
/*
  var post_data = {};
  form.find('input[type=text], input[type=password], input[type=hidden], select, textarea').each(function() {
    if($(this).val() != null)
    {
      if(post_data[this.name] == undefined)
      {
        post_data[this.name] = new Array();
      }
      if($(this).hasClass('tinymce'))
      {
        post_data[this.name].push(tinyMCE.get($(this).attr('id')).getContent());
      }
      else
      {
        post_data[this.name].push($(this).val());
      }
    }
  });
  form.find('input[type=checkbox], input[type=radio]').each(function() {
    if($(this).is(':checked'))
    {
      if($(this).val() != null)
      {
        if(post_data[this.name] == undefined)
        {
          post_data[this.name] = new Array();
        }
        post_data[this.name].push($(this).val());
      }
    }
  });
*/

  var post_data =   form.serializeArray();

  return post_data;
}

function post_data(url, form_name, target)
{
  // Now we support just passing the form name, and the url will be read from the <form action='...'> attribute
  if(form_name == undefined || form_name == '')
  {
    form_name = url;
    var formSelector = "#"+form_name+", form[name="+form_name+"]";
    var f = $(formSelector);
    url       = $(formSelector).attr('action');
  }
  
  // If we don't pass a target, then set the target to be #dialog_box
  if(target == undefined || target == '')
  {
    target = '#dialog_box';
  }
  
  // Make sure jHtml textfields have their values up to date before being submitted
  
  // Do the AJAX call
  $.ajax({
    type:	"POST",
    url:	url,
    data: create_data(form_name),
    success: function(data) {
      $(target).html(data);
      $(target).HFInit();
    }
  });
}

(function($){
  $.fn.HFRoundCorners = function(){
/*
    this.find('.dark').corner('round top');
    this.find('.body, .round_bottom').corner('round bottom');
    this.find('.widget_header .controls').corner('round top');
*/
    
    return this;
  }
})(jQuery)

function round_corners()
{
  $('.dark').corner('round top');
  $('.body').corner('round bottom');
  $('.round_bottom').corner('round bottom');
}


