
  /* Main function
  - - - - - - - - - - - - - - - - */
  $.euroas.currency = function() {
  
    // Expand default args
    args = $.extend($.euroas.currency.defaults, arguments[0] || {});
  
    // Don't act on absent elements
    if (!$(args.element).length) return this;
    
    $.get(args.xmlUrl, $.euroas.parseHTML);
    
    $(args.element).find('.currencies a').click($.euroas.convertCurrency);
  };
  
  
  /* Default arguments & variables
  - - - - - - - - - - - - - - - - */
  $.euroas.currency.defaults = {
    element : '.convert',
    xmlUrl  : 'http://cw.money.pl/kursy_nbp.html'
  };
  
  
  /* Parse XML data
  - - - - - - - - - - - - - - - - */
  $.euroas.parseHTML = function(html) {
  
    $(html.responseText).find('tr').each(function() {
      
      $row = $(this);
      
      if ($row.index() > 1 && $row.index() != 4) {
        
        var code  = $.trim($(this).find('td:nth-child(1)').text());
        var rate  = $.trim($(this).find('td:nth-child(2)').text());
        
        $('.currencies a.' + code.toLowerCase()).data('code', code).data('rate', rate);
        
        //console.log($('.currencies a.' + code.toLowerCase()).data('price'));
      }
    });
  };
  
  
  /* Convert currency to specific value
  - - - - - - - - - - - - - - - - */
  $.euroas.convertCurrency = function() {
  
    code      = $(this).data('code');
    rate      = $(this).data('rate');
    price     = $('#price').data('price').toFixed(2);
    newPrice  = (price / rate).toFixed(2);
    
    $('#price').html(newPrice + ' ' + code);
    
    if ($('#net_price').length) {
      netPrice = $('#net_price').data('price').toFixed(2);
      $('#net_price').html((netPrice / rate).toFixed(2) + ' ' + code);
    } 
  
    return false;
  };
