var rates = {
  "al":{
    "label":"Alabama",
    "rate":5,
    "notes":["AL implements local taxes, check with your city for any additional taxes"],
    "top":-100,
    "height":100
  },
  "ak":{
    "label":"Alaska",
    "rate":0,
    "notes":["There is no state income tax in AK"],
    "top":-100,"height":265
  },
  "az":{"label":"Arizona","rate":4.24},
  "ar":{
    "label":"Arkansas",
    "rate":7,
    "notes":["AR implements local taxes, check with your city for any additional taxes"],
    "top":-60,
    "height":166
  },
  "ca":{
    "label":"California",
    "rate":9.55,
    "notes":["CA implements local taxes, check with your city for any additional taxes"]
  },
  "co":{"label":"Colorado","rate":4.63},
  "ct":{
    "label":"Connecticut",
    "rate":5,
    "top":-80,
    "height":100
  },
  "de":{
    "label":"Delaware",
    "rate":5.55,
    "notes":["DE implements local taxes, check with your city for any additional taxes"],
    "top":-60,
    "height":200
  },
  "dc":{"label":"Washington, D.C.","rate":8.5},
  "fl":{
    "label":"Florida",
    "rate":0,
    "notes":["There is no state income tax in FL"],
    "top":-140,
    "height":331
  },
  "ga":{"label":"Georgia","rate":6},
  "hi":{"label":"Hawaii","rate":8.25},
  "id":{"label":"Idaho","rate":7.8},
  "il":{"label":"Illinois","rate":3},
  "in":{
    "label":"Indiana",
    "rate":3.4,
    "notes":["IN implements local taxes, check with your city for any additional taxes"]
  },
  "ia":{
    "label":"Iowa",
    "rate":7.92,
    "notes":["IA implements local taxes, check with your city for any additional taxes"]
  },
  "ks":{"label":"Kansas","rate":6.45},
  "ky":{
    "label":"Kentucky",
    "rate":5.8,
    "notes":["KY implements local taxes, check with your city for any additional taxes"],
    "top":-40,
    "height":166
  },
  "la":{"label":"Louisiana","rate":6},
  "me":{"label":"Maine","rate":8.5},
  "md":{"label":"Maryland","rate":4.75,"top":-40,"height":166},
  "ma":{"label":"Massachusetts","rate":5.3},
  "mi":{"label":"Michigan","rate":4.35,"adjusted":4.36},
  "mn":{"label":"Minnesota","rate":7.05,"adjusted":7.16},
  "ms":{"label":"Mississippi","rate":5,"top":-60,"height":100},
  "mo":{"label":"Missouri","rate":6},
  "mt":{"label":"Montana","rate":6.9,"adjusted":6.86,"top":-60,"height":200},
  "ne":{"label":"Nebraska","rate":6.84,"adjusted":6.85,"top":-40},
  "nv":{
    "label":"Nevada",
    "rate":0,
    "notes":["There is no state income tax in NV"],
    "top":-80,
    "height":232
  },
  "nh":{
    "label":"New Hampshire",
    "rate":0,
    "notes":["There is no state income tax in NH"]
  },
  "nj":{
    "label":"New Jersey",
    "rate":5.525,
    "top":-40,
    "height":166,
    "notes":["NJ implements local taxes, check with your city for any additional taxes"]
  },
  "nm":{"label":"New Mexico","rate":4.9,"adjusted":4.87},
  "ny":{
    "label":"New York",
    "rate":6.85,
    "notes":["NY implements local taxes, check with your city for any additional taxes"]
  },
  "nc":{"label":"North Carolina","rate":7},
  "nd":{"label":"North Dakota","rate":3.44,"top":-40,"height":166},
  "oh":{
    "label":"Ohio",
    "rate":4.109,
    "notes":["OH implements local taxes, check with your city for any additional taxes"]
  },
  "ok":{"label":"Oklahoma","rate":5.5},
  "or":{
    "label":"Oregon",
    "rate":9,
    "notes":["OR implements local taxes, check with your city for any additional taxes"]
  },
  "pa":{
    "label":"Pennsylvania",
    "rate":3.07,
    "notes":["PA implements local taxes, check with your city for any additional taxes"],
    "top":-40,
    "height":166
  },
  "ri":{"label":"Rhode Island","rate":7,"top":-100,"height":166},
  "sc":{"label":"South Carolina","rate":7,"top":-40,"height":166},
  "sd":{
    "label":"South Dakota",
    "rate":0,
    "notes":["There is no state income tax in SD"],
    "top":-60,
    "height":200
  },
  "tn":{"label":"Tennessee","rate":6},
  "tx":{
    "label":"Texas",
    "rate":0,
    "notes":["There is no state income tax in TX"],
    "top":-120,
    "height":298
  },
  "ut":{"label":"Utah","rate":5},
  "vt":{"label":"Vermont","rate":7,"top":-80,"height":100},
  "va":{"label":"Virginia","rate":5.75},
  "wa":{
    "label":"Washington",
    "rate":0,
    "notes":["There is no state income tax in WA"],
    "top":-40,
    "height":166
  },
  "wv":{"label":"West Virginia","rate":6},
  "wi":{"label":"Wisconsin","rate":6.5,"top":-40,"height":166},
  "wy":{
    "label":"Wyoming",
    "rate":0,
    "notes":["There is no state income tax in WY"],
    "top":-20,
    "height":133
  },
};

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 );