var defaultZip = '64747';

$(document).ready(function() {

  /* Weather ----------------------------------------- */

  function getWeather(zip) {

    $('#weather .results').hide();
    $('#weather .waiting').show();

    var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20location%3D" + zip + "&format=json&callback=?";
  
    $.getJSON( url, function( data ) {
          
      if(data.error) {
        $('#weather .error').show();
        $('#weather .waiting').hide();
        return false;
      }
      
      if(data.query.results.channel.location) {
        $('#weather .error').hide();
        $('#weather .waiting').hide();
        $('#weather .desc').html(data.query.results.channel.item.condition.text);
        var cond = data.query.results.channel.item.condition.code * 61;
        $('#weather .image').css('background-position', '-' + cond + 'px top');
        $('#weather .current').html(data.query.results.channel.item.condition.temp);
        $('#weather .location').html(data.query.results.channel.location.city + ', ' + data.query.results.channel.location.region);
        $('#weather .high').html(data.query.results.channel.item.forecast[0].high);
        $('#weather .low').html(data.query.results.channel.item.forecast[0].low);
        $('#weather .results').fadeIn();
        createCookie('zip', zip, 1000);
        
      } else {
        $('#weather .waiting').hide();
        $('#weather .error').show();
      }
    });
    
  }
  
  var storedZip = readCookie('zip');
  if (storedZip) {
    $('#weather .zip').val(storedZip);
    getWeather(storedZip);
  } else {
    $('#weather .zip').val(defaultZip);
    getWeather(defaultZip);
  }

  $('#weather .go').click(function() {
    var goZip = $('#weather .zip').val();
    getWeather(goZip);
    return false;
  });

  $('#weather .form').submit(function() {
    var goZip = $('#weather .zip').val();
    getWeather(goZip);
    return false;
  });


  /* Commodities ----------------------------------------- */
  
    $('#commodities .header .date').html(getDate());
  
    var s_corn = 'C' + getMonth('corn') + getYear() + '.CBT';
    var s_soy = 'S' + getMonth('soy') + getYear() + '.CBT';
    var s_wheat = 'W' + getMonth('wheat') + getYear() + '.CBT';
    var s_hogs = 'LH' + getMonth('hogs') + getYear() + '.CME';
    var s_cattle = 'LC' + getMonth('cattle') + getYear() + '.CME';
    
    var symbols = "'" + s_corn + "', '" + s_soy + "', '" + s_wheat + "', '" + s_hogs + "', '" + s_cattle + "'";
  
    var surl = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(" + symbols + ")&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=?";
  
    $.getJSON( surl, function( data ) {

      var symbols = ["corn", "soy", "wheat", "hogs", "cattle"];
      
      for(var i=0; i<symbols.length; i++) {
        var price = data.query.results.quote[i].LastTradePriceOnly;
        var change = data.query.results.quote[i].Change || '';
        
        $('#commodities .' + symbols[i] + ' .price').html(price);
        $('#commodities .' + symbols[i] + ' .change').html(change);

        if(change.slice(0,1) == '-') {
          $('#commodities .' + symbols[i] + ' .image').addClass('down');
          $('#commodities .' + symbols[i] + ' .price').addClass('down');
          $('#commodities .' + symbols[i] + ' .change').addClass('down');
        } else {
          $('#commodities .' + symbols[i] + ' .image').addClass('up');
        }
      }
      
      $('#commodities .waiting').hide();
      $('#commodities .items').fadeIn();
      
    });


});

function createCookie(name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name, "", -1);
}

function getDate() {
  var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
  var d = new Date();
  var date = d.getDate();
  var month = d.getMonth();
  var year = d.getFullYear();
  
  return date + '-' + months[month] + '-' + (year-2000);
}

function getMonth(type) {
  var corn = ['H', 'H', 'H', 'K', 'K', 'N', 'N', 'U', 'U', 'Z', 'Z', 'Z'];  //C_YY.CBT
  var soy = ['F', 'H', 'H', 'K', 'K', 'N', 'N', 'Q', 'U', 'X', 'X', 'F'];   //S_YY.CBT
  var wheat = ['H', 'H', 'H', 'K', 'K', 'N', 'N', 'U', 'U', 'Z', 'Z', 'Z']; //W_YY.CBT
  var hogs = ['G', 'G', 'J', 'J', 'K', 'M', 'N', 'Q', 'V', 'V', 'Z', 'Z'];  //LH_YY.CME
  var cattle = ['G', 'G', 'J', 'J', 'M', 'M', 'Q', 'Q', 'V', 'V', 'Z', 'Z'];  //LC_YY.CME
  
  var array;
  switch(type) {
    case 'corn':
      array = corn;
      break;
    case 'soy':
      array = soy;
      break;
    case 'wheat':
      array = wheat;
      break;
    case 'hogs':
      array = hogs;
      break;
    case 'cattle':
      array = cattle;
      break;
  }      

  var d = new Date();
  var month = d.getMonth();
  
  return array[month];
}

function getYear() {
  var d = new Date();
  var year = d.getFullYear();
  return new String(year - 2000);
}





