function convertKgToPounds(val)
{
	return Math.round(val * 2.2046);
}

function convertPoundsToKg(val)
{
	return Math.round(val * 0.4536);
}

function convertCmToInches(val)
{
	return Math.round(val * 0.3937);
}

function convertInchesToCm(val)
{
	return Math.round(val * 2.54);
}


(function( $ ){

    var methods = {
	    init : function( options ) { 
    	
    		var settings = {
    	    	'type': 'weight',
    	    	'onSet': null
    	    };
    	    
    	    if( options ) {
    	    	$.extend( settings, options );
    	    }

		    // If the plugin hasn't been initialized yet
		    if ( $('#convert-form').length == 0 ) 
		    {		    	
	        	convertform = '<div id=\"convert-form\"><table style=\"width: 20px;\"><tr><th style=\"width: 10px;\" id=\"convert-title-1\"></th><th style=\"width: 20px;\"></th><th style=\"width: 10px;\" id=\"convert-title-2\"></th></tr><tr><td style=\"width: 10px;\"><input type=\"text\" class=\"three-ch\" id=\"convert-val-1\"/></td><td style=\"width: 20px;\"><img src=\"/images/icons/conversionicon-white.png\" title=\"convert\"/></td><td style=\"width: 10px;\"><input type=\"text\" class=\"three-ch\" id=\"convert-val-2\"/></td></tr></table></div>';	
		    	$('body').append(convertform);
		    }
		    
	    	$("#convert-form").dialog({
	    		autoOpen: false,
	    		height: 160,
	    		width: 160,
	    		modal: true,
	    		resizable: false,
	    		buttons: {
	    			'Set': function() {
	    				val = $('#convert-val-1').val();
	    				if(!isNaN(val))
	    				{
		    				targ = $(this).dialog('option', 'updatetarget');
		    				$('#' + targ).val(val);		    				
		    				$(this).dialog('close');
		    				if(settings.onSet)
		    				{
		    					eval(settings.onSet + '()');
		    				}		    				
		    				$('#' + targ).trigger('change');		    				
	    				}
	    			},
	    			Cancel: function() {
	    				$(this).dialog('close');
	    			}
	    		},
	    		open: function(event, ui) {
	    			ival = $(this).dialog('option', 'initialval');
	    			if(!ival || ival == "")
	    			{
	    				ival = 0;
	    			}
	    			$('#convert-val-1').val(ival);
	    			func = $(this).dialog('option', 'function1');
	    			$('#convert-val-2').val(eval(func + '(ival)'));
	
	    			$('#convert-val-1').unbind('blur');
	    			$('#convert-val-2').unbind('blur');
	
	    			$("#convert-val-1").blur(function() {
	    				if(!isNaN($(this).val()))
	    				{
		    				cv1 = parseInt($(this).val());
		    				func = $('#convert-form').dialog('option', 'function1');	
		    				$('#convert-val-2').val(eval(func + '(' + cv1 + ')'));
	    				}
	    			});
	    			
	    			$("#convert-val-2").blur(function() {
	    				if(!isNaN($(this).val()))
	    				{
		    				cv2 = parseInt($(this).val());
		    				func = $('#convert-form').dialog('option', 'function2');	
		    				$('#convert-val-1').val(eval(func + '(' + cv2 + ')'));
	    				}
	    			});
	    					
	    		},
	    		close: function() {
	    		}
	    	});
	
	    	$(this).live('click', function() {
		    	switch(settings.type)
		    	{
		    		case 'length':
					{
			    		$('#convert-title-1').html('cm');
			    		$('#convert-title-2').html('inches');
			    		$('#convert-form').dialog('option', 'function1', 'convertCmToInches');
			    		$('#convert-form').dialog('option', 'function2', 'convertInchesToCm');
			    		$('#convert-form').dialog('option', 'title', 'Length Conversion');
						break;
					}
					default:
		    		case 'weight':
					{
			    		$('#convert-title-1').html('kg');
			    		$('#convert-title-2').html('pounds');
			    		$('#convert-form').dialog('option', 'function1', 'convertKgToPounds');
			    		$('#convert-form').dialog('option', 'function2', 'convertPoundsToKg');
			    		$('#convert-form').dialog('option', 'title', 'Weight Conversion');
						break;
					}
		    	}
	    	
	    		$('#convert-form').dialog('option', 'updatetarget', $(this).attr('rel'));
	    		$('#convert-form').dialog('option', 'initialval', $('#' + $(this).attr('rel')).val());
	    		$('#convert-form').dialog('open');
	    		return false;
	    	});
		}
    };
    
    $.fn.conversion = function( method ) {
    	if(!$.fn.dialog)
    	{
        	$.error( 'jQuery.ui.dialog is required for this plugin' );
        	return;        	
    	}
    	
        if ( methods[method] ) 
        {
        	return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        }
        else if ( typeof method === 'object' || ! method ) 
        {
        	return methods.init.apply( this, arguments );
        } 
        else 
        {
        	$.error( 'Method ' +  method + ' does not exist on jQuery.conversion' );
        } 
    };
})( jQuery );
