/*********************
//* jQuery Price - created by mck: http://www.daesso.com/ | http://www.bomb.pl/
*********************/

(function($) {
	$.fn.Price = function(options) {
		
		var defaults = {
			amount_txt: 'Specified amount',
			nofee_txt: 'No fee',
			noinfo_txt: 'No information'
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function()
		{
			var count = 0;
			
			var item = jQuery(this);
			var item_name = item.attr('name');
			var value = parseFloat(item.val()) || 0;
			var last_mode = '';
			
			var replacement = jQuery('<div/>').attr('id', item_name + '_replacement').attr('class', 'form_element').attr('style', 'float:left;');
			var container = jQuery('<div/>').attr('id', item_name + '_container').attr('class', 'form_element');
			
			item.wrap('<div class="form_element"></div>');
			replacement.css('height', item.height()).hide().insertAfter(item);
			container.insertAfter(replacement);
			
			refresh(value > 0 ? 'amount' : (value < 0 ? 'noinfo' : 'nofee'));
			
			function refresh(mode)
			{
				if(mode != last_mode)
				{
					if(last_mode == 'amount')
					{
						value = parseFloat(item.val()) || 0;
					}
					
					last_mode = mode;
				}
			
				var amount_link = jQuery('<a/>').html(options.amount_txt).attr('id', item_name + '_amount').bind('click', function() { refresh('amount') });
				var nofee_link = jQuery('<a/>').html(options.nofee_txt).attr('id', item_name + '_nofee').bind('click', function() { refresh('nofee') });
				var noinfo_link = jQuery('<a/>').html(options.noinfo_txt).attr('id', item_name + '_noinfo').bind('click', function() { refresh('noinfo') });
				
				if(mode == 'amount')
				{
					container.html('').append(nofee_link).append(' | ').append(noinfo_link);
					replacement.hide();
					item.val(value > 0 ? value : '').show();
				}
				else if(mode == 'nofee')
				{
					container.html('').append(amount_link).append(' | ').append(noinfo_link);
					item.val(0).hide();
					replacement.html(options.nofee_txt).show();
				}
				else if(mode == 'noinfo')
				{
					container.html('').append(amount_link).append(' | ').append(nofee_link);
					item.val(-1).hide();
					replacement.html(options.noinfo_txt).show();
				}
			}
		});
	}
})(jQuery);
