(function($) {   
        
	var	$cc = null,
	    $cf = null,
	    $ct = null,
	    $ca = null,
	    $cr = null,
	    $cl = null,
	    $url = null;
	
	$.fn.CurrencyConverter = function(opts) {
	    
	    $cc = $('#currency-converter');
	    $cf = $('#currencyFrom');
	    $ct = $('#currencyTo');
	    $ca = $('#currencyAmount');
	    $cr = $('#conversionResult');
	    $cl = $('#cc-close');
	    $url = $('#sid').val() + '_xhr-proxy.html';
	    
	    var defaults = {
            handle: 'cc-handle',
            cfIndex: 15,
            ctIndex: 4,
            leftOffset: -90,
            topOffset: -190
	    },
	    
	    options = $.extend(true, {}, defaults, opts);
		
		return this.each(function() {
		    var pos = $(this).position();
		    
		    $(this).click(function() {
		        $cc.css({
		                left: pos.left + options.leftOffset + 'px',
		                top: pos.top + options.topOffset + 'px'
		            })
		            .draggable({handle: options.handle})
		            .fadeIn(600);
		        
		        $cf.attr('selectedIndex', options.cfIndex);
                $ct.attr('selectedIndex', options.ctIndex);
                setFlag('currencyFrom'); 
                setFlag('currencyTo');
                $ca.val('').focus();
                $cr.html('');              
		        
		        return false; 
		    }); 
		    
		    var timer = null;
		        
		    $ca
		        .keydown(function(e) {
		            return isValidKey(e.keyCode);
		        })
		        
		        .keyup(function(e) {
		        
    		        if(timer) {
                        window.clearTimeout(timer); 
                        e.stopPropagation();
                    } 
    		    
    		        timer = window.setTimeout(function() {
                        convert(e);
                    }, 300);		        
		        });

		    $cf.change(convert);
		    $ct.change(convert);		    
		    
		    $cl.click(function() {
		        $cc.hide(); 
		        return false;    
		    });
		});
    };
    
    var isValidKey = function(c) {
        var keyCodes = [
            8, 48, 49, 50, 51, 52, 
            53, 54, 55, 56, 57, 96, 
            97, 98, 99, 100, 101, 102, 
            103, 104, 105, 127, 188, 190
        ]; 
        
        if(c == 0 || typeof c == 'undefined') {
            return true;
        }
        return Boolean($.inArray(c, keyCodes) != -1)      
    };
    
    var convert = function(e) { 
        var amt = $ca.val(), 
            el = e.target,               
            id = $(el).attr('id');           
      
        if(!isValidKey(e.keyCode)) { 
            return;    
        }

        if(amt.length == 0 || amt <= 0) { 
            $cr.html('');
            return;    
        } 
        
        if(id == 'currencyFrom' || id == 'currencyTo') {
            setFlag(id); 
        }

        sendRequest(amt);    
    },
    
    sendRequest = function(amt) {       
        var spin = $('#loadingIndicator');
        
        spin.ajaxSend(function() {
            $(this).show();
        });
      
        $.ajax({
            url: $url,
            type: 'POST',
            data: {
                serviceType: 'currency',
                currencyFrom: $cf.val(),
                currencyTo: $ct.val(),
                currencyAmount: amt                
            },
            success: function(amt) {  
                spin.hide()
                $cr.html(amt);    
            }    
        });             
    },
    
    setFlag = function(id) {
        var cc = $('#' + id).val();  

        $('#' + id + 'Flag').css({
            background: 'url(/images/_default/flags/' + cc + '.png) no-repeat top left'    
        });   
    }; 
       
})(jQuery);
