var rates = {
  "al":{
    "label":"Alabama",
    "rate":4,
    "notes":["Subject to local sales tax adjustments"],
  },
  "ak":{
    "label":"Alaska",
    "rate":0,
    "notes":["There is no statewide sales tax in AK","Subject to local sales tax adjustments"]
  },
  "az":{
    "label":"Arizona",
    "rate":6.3,
    "notes":["Subject to local sales tax adjustments"]
  },
  "ar":{
    "label":"Arkansas",
    "rate":6,
    "notes":["Subject to local sales tax adjustments"],
  },
  "ca":{
    "label":"California",
    "rate":8.25,
    "notes":["Subject to local sales tax adjustments"]
  },
  "co":{
    "label":"Colorado",
    "rate":2.9,
    "notes":["Subject to local sales tax adjustments"]
  },
  "ct":{
    "label":"Connecticut",
    "rate":6
  },
  "de":{
    "label":"Delaware",
    "rate":0,
    "notes":["There is no sales tax in Delaware"]
  },
  "dc":{
    "label":"Washington, D.C.",
    "rate":6
  },
  "fl":{
    "label":"Florida",
    "rate":6,
    "notes":["Subject to local sales tax adjustments"]
  },
  "ga":{
    "label":"Georgia",
    "rate":4,
    "notes":["Subject to local sales tax adjustments"]
  },
  "hi":{
    "label":"Hawaii",
    "rate":4,
    "notes":["Subject to local sales tax adjustments"]
  },
  "id":{
    "label":"Idaho",
    "rate":6,
    "notes":["Subject to local sales tax adjustments"]
  },
  "il":{
    "label":"Illinois",
    "rate":6.25,
    "notes":["Subject to local sales tax adjustments"]
  },
  "in":{
    "label":"Indiana",
    "rate":7,
    "notes":["Subject to local sales tax adjustments"]
  },
  "ia":{
    "label":"Iowa",
    "rate":6,
    "notes":["Subject to local sales tax adjustments"]
  },
  "ks":{
    "label":"Kansas",
    "rate":5.3,
    "notes":["Subject to local sales tax adjustments"]
  },
  "ky":{
    "label":"Kentucky",
    "rate":6
  },
  "la":{
    "label":"Louisiana",
    "rate":4,
    "notes":["Subject to local sales tax adjustments"]
  },
  "me":{
    "label":"Maine",
    "rate":5
  },
  "md":{
    "label":"Maryland",
    "rate":6
  },
  "ma":{
    "label":"Massachusetts",
    "rate":6.25,
    "notes":["Numerous exception categories"]
  },
  "mi":{
    "label":"Michigan","rate":6
  },
  "mn":{
    "label":"Minnesota",
    "rate":6.875,
    "notes":["Subject to local sales tax adjustments"]
  },
  "ms":{
    "label":"Mississippi",
    "rate":7
  },
  "mo":{
    "label":"Missouri",
    "rate":4.225,
    "notes":["Subject to local sales tax adjustments"]
  },
  "mt":{
    "label":"Montana",
    "rate":0,
    "notes":["Subject to local sales tax adjustments"]
  },
  "ne":{
    "label":"Nebraska",
    "rate":5.5,
    "notes":["Subject to local sales tax adjustments"]
  },
  "nv":{
    "label":"Nevada",
    "rate":6.85,
    "notes":["Subject to local sales tax adjustments"]
  },
  "nh":{
    "label":"New Hampshire",
    "rate":0,
    "notes":["There is no sales tax in New Hampshire"]
  },
  "nj":{
    "label":"New Jersey",
    "rate":7,
    "notes":["Subject to local sales tax adjustments"]
  },
  "nm":{
    "label":"New Mexico",
    "rate":0,
    "notes":["A tax of 5% may be collected at the discresion of the retailer"]
  },
  "ny":{
    "label":"New York",
    "rate":4,
    "notes":["Subject to local sales tax adjustments"]
  },
  "nc":{
    "label":"North Carolina",
    "rate":5.5,
    "notes":["Often 7.75%, subject to local sales tax adjustments"]
  },
  "nd":{
    "label":"North Dakota",
    "rate":5
  },
  "oh":{
    "label":"Ohio",
    "rate":5,
    "notes":["Subject to local sales tax adjustments"]
  },
  "ok":{
    "label":"Oklahoma",
    "rate":4.5,
    "notes":["Subject to local sales tax adjustments"]
  },
  "or":{
    "label":"Oregon",
    "rate":0,
    "notes":["There is no statewide sales tax in Delaware","Subject to local sales tax adjustments"]
  },
  "pa":{
    "label":"Pennsylvania",
    "rate":6,
    "notes":["Subject to local sales tax adjustments"]
  },
  "ri":{
    "label":"Rhode Island",
    "rate":7
  },
  "sc":{
    "label":"South Carolina",
    "rate":6,
    "notes":["Subject to local sales tax adjustments"]
  },
  "sd":{
    "label":"South Dakota",
    "rate":4,
    "notes":["Subject to local sales tax adjustments"]
  },
  "tn":{
    "label":"Tennessee",
    "rate":7,
    "notes":["Subject to local sales tax adjustments"]
  },
  "tx":{
    "label":"Texas",
    "rate":6.25,
    "notes":["Subject to local sales tax adjustments"]
  },
  "ut":{
    "label":"Utah",
    "rate":4.75,
    "notes":["Subject to local sales tax adjustments"]
  },
  "vt":{
    "label":"Vermont",
    "rate":6
  },
  "va":{
    "label":"Virginia",
    "rate":5
  },
  "wa":{
    "label":"Washington",
    "rate":6.5,
    "notes":["Subject to local sales tax adjustments"]
  },
  "wv":{
    "label":"West Virginia",
    "rate":6
  },
  "wi":{
    "label":"Wisconsin",
    "rate":5,
    "notes":["Subject to local sales tax adjustments"]
  },
  "wy":{
    "label":"Wyoming",
    "rate":4,
    "notes":["Subject to local sales tax adjustments"]
  },
};

function setupBlocks(){
  var block;
  for( var key in rates ){
    if( rates[ key ].rate >= 9 )
      block = $(".nine").get(0);
    else if( rates[ key ].rate >= 8 )
      block = $(".eight").get(0);
    else if( rates[ key ].rate >= 7 )
      block = $(".seven").get(0);
    else if( rates[ key ].rate >= 6 )
      block = $(".six").get(0);
    else if( rates[ key ].rate >= 5 )
      block = $(".five").get(0);
    else if( rates[ key ].rate >= 4 )
      block = $(".four").get(0);
    else if( rates[ key ].rate >= 3 )
      block = $(".three").get(0);
    else if( rates[ key ].rate >= 2 )
      block = $(".two").get(0);
    else if( rates[ key ].rate >= 1 )
      block = $(".one").get(0);
    else
      block = $(".zero").get(0);
    
    var rate;
    if( rates[key].adjusted )
      rate = rates[key].adjusted;
    else
      rate = rates[key].rate;

    var leftpos;
    if( rate.toString().indexOf(".") != -1 )
      leftpos = Math.round(250 - (250 * rate.toString().substr( rate.toString().indexOf(".") )));
    else
      leftpos = 250;
      
    var topmod = '';
    if( rates[ key ].top )
      topmod = "top:"+rates[ key ].top+"px;height:"+rates[ key ].height+"%;";
      
    $(block).append( $("<div class=\""+key+" rate\" style=\"left:"+leftpos+"px;"+topmod+"\"><a class=\"covera\" href=\"#\">&nbsp;</a><span><a title=\""+rates[key].label+"\" href=\"#\">"+key.toUpperCase()+"</a></span></div>") );
  }
}

function selectState(state){
  // reset the hash
  document.location.hash = state;
  // set big rate
  $("#bigrate").html( rates[state].rate + "%" );
  // set state label
  $("#selected").html( rates[state].label );
  // set detail list
  $(".details").html('');
  if( rates[state].notes ){
    for( var i=0; i < rates[state].notes.length; i++){
      $(".details").append( $("<li>"+rates[state].notes[ i ]+"</li>") );
    }
  }
  
  // compute and set salary estimate
  salaryEstimate(state);
  
  // TODO set selected in dropdown
  
  // clear and set selected in the graph
  $(".selected").each( function(){ $(this).removeClass('selected') } );
  $("."+state).each( function(){ $(this).addClass('selected') } );
}

function salaryEstimate(state){
  var salary = $("form .salary").val();
  salary = salary.replace(/\$/,'').replace(/,/,'').replace(/ /,'');
  var estimate = Math.round( salary * (rates[state].rate * 0.01 ) );
  if( estimate != NaN )
    $("#estimate").html( "$" + estimate );
  else
    // TODO show estimate error
    console.log("Oops:",estimate);
}

// given any state element, return the state
function stateFromClasses( el ){
  var classes = el.className;
  classes = classes.replace(/rate/i,'').replace(/selected/i,'').replace(/\s+/g,'');
  return classes;
}

// find the currently selected state
function selectedState(){
  return stateFromClasses( $(".rate.selected").get(0) );
}

function init(){
  // if we already have a hash, select that state
  if( document.location.hash && rates[document.location.hash.substr(1)] )
    selectState(document.location.hash.substr(1));
  // otherwise, select ca
  else
    selectState('ca');
  
  $("area").each( function(){ $(this).bind("click",function(){
    if( this.href.split('#')[1] && rates[this.href.split('#')[1]] )
      selectState(this.href.split('#')[1]);
  }) });
  // 
  // // bind click events to rate anchors
  // $(".rate a").bind("click",function(e){ e.preventDefault(); selectState( stateFromClasses( $(this).parents(".rate").get(0) ) ) });
  // // bind click event to state dropdown
  // // TODO scroll to the state
  // $("#select").bind("click",function(){ selectState( $("select.states").val() ) });
  // // bind change event to salary input
  // $("form .salary").bind("change",function(){ salaryEstimate( selectedState() ) });
  
  // never fire form submits
  $("form").each( function(){ $(this).bind("submit",function(e){ e.preventDefault() }) } );
}

jQuery( init );
