Cufon.replace('h3.trade', {
	fontFamily: 'Trade Gothic LT Std'
});

Cufon.now();

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';

		jQuery('#'+id)
		.clone()
		.attr('id', id+'-'+helpKey)
		.appendTo("body");

		if (mode =='file') {
			jQuery('#helpContent', '#'+id+'-'+helpKey).load(path + ' #pcon > *');

		} else {
			jQuery.ajax({
				url: "/help/get/"+helpKey,
				success: function(html){
					jQuery('#helpContent', '#'+id+'-'+helpKey).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: true,
		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'
		}
	};
	if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) <= 6){
		dialogOptions.bgiframe = true;
	}


	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');
		if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) <= 6){
			jQuery('#'+id+'-'+helpKey).dialog('option', 'bgiframe', true);
		}
		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;
	});


	// IE7 buttons -- missing margin-bottom
	if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) <= 7){
		jQuery('div.bt-lg-tn').wrap('<div class="ie-button"></div>');
		jQuery('div.bt-lg-wh').wrap('<div class="ie-button"></div>');
	}

});
