
var fixDialogWidth = function(dialog) {
    // reduce overall dialog width by width of the padding
    jQuery(dialog).css('width', jQuery(dialog).width() - (2*parseInt(jQuery(dialog).css('padding-left'))) + 'px');
    jQuery(dialog).css('height', 'auto');
    jQuery(dialog).css('visibility', 'visible');
}

var confirm = function(message, callback, el) {
     jQuery('#confirmContent').html(message);
     jQuery('#confirmDialog')
        .dialog('open')
        .find('a.confirmOk')
            .unbind('click')
            .click(function(){
                callback.apply(el);
                return false;
            });
}

jQuery(document).ready(function() {

    jQuery('a.submitForm').click(function(){
        var el = jQuery(this);
        // check for nextStep class -- set flag if found
        if (el.hasClass('nextStep')) {
            el.parents('form').append('<input type="hidden" name="data[redirect_next_step]" value="1" />');
        }

        // check for addAnother class -- set flag if found
        if (el.hasClass('addAnother')) {
            el.parents('form').append('<input type="hidden" name="data[add_another]" value="1" />');
        }

        // finds the nearest form ancestor and submits it
        el.parents('form:first').submit();
        return false;
    })



    var generateDialogHtml = function(link, mode) {
        var path = link.attr('href');
        var helpKey = mode == 'file' ? path.replace(new RegExp('/',"g"), "_") : path.substring(1);
        var id = link.hasClass('large') ? 'lgHelpDialog' : 'helpDialog';
		  // Check for bogus "notfirst" attribute, which allows us to avoid
		  // creating another dialog if one with the same ID already exists.
		  if (!link.attr('notfirst')) {
			  jQuery('#'+id)
					.clone()
					.attr('id', id+'-'+helpKey)
					.appendTo("body");

			  jQuery.ajax({
					url: mode == 'file' ? path : "/help/get/"+helpKey,
					success: function(html){
						 jQuery('#'+id+'-'+helpKey+' .helpContent').html(html);
					}
			  });
		  }
    }


    // HELP DIALOG
    // generate all help dialogs
    jQuery('a.help').each(function(){
        generateDialogHtml(jQuery(this));
    });

    // POPUP DIALOG
    // generate all popup dialogs
    jQuery('a.popup').each(function(){
        generateDialogHtml(jQuery(this), 'file');
    });




	var dialogOptions = {
		  modal: false,
		  height: 'auto',
          width: 400,
		  open : function() { fixDialogWidth(jQuery(this)); },
		  resize : function() { fixDialogWidth(jQuery(this)); },
		  resizeStop : function() { fixDialogWidth(jQuery(this)); },
          resizable: false,
          draggable: false,
		  autoOpen: false,
		  overlay: {
				opacity: 0.5,
				background: 'black'
		  }
	};


	jQuery('div[id^=helpDialog]').dialog(dialogOptions);
    jQuery('div[id^=lgHelpDialog]').dialog(jQuery.extend({}, dialogOptions, {width:800}));

    jQuery('a.help, a.popup').click(function(){
        var el = jQuery(this);
        var mode = el.hasClass('popup') ? 'file' : '';
        var path = el.attr('href');
        var helpKey = mode == 'file' ? path.replace(new RegExp('/',"g"), "_") : path.substring(1);
		var id = el.hasClass('large') ? 'lgHelpDialog' : 'helpDialog';
        jQuery('#'+id+'-'+helpKey).dialog('open');
        return false;
    });



    // CONFIRM DIALOG
    jQuery('#confirmDialog').dialog(jQuery.extend({}, dialogOptions, {modal:true}));


    // GLOBAL DIALOG CLOSE LISTENER
    jQuery('a.dialogClose').click(function(){
        jQuery(this).parents('div[id*=Dialog]:first').dialog('close');
        return false;
    });



});
