$(function() {
  // Load in ISG stuff
  $("head").append('<sc' + 'ript type="text/javascript" src="http://staging.iseekgolf.com/teetimes/ga.js"></sc' + 'ript>');
  var states = [];
  var states_regions = {};
  var found_regions = []
  $("#isg_courses").html("<option value=''>Please Choose a Course</option>");
  $("#isg_regions").html("<option value=''>Please Choose a Region</option>");
  
  $(isg_data).each(function() {
    $("#isg_courses").append("<option value='" + this.id + "'>" + this.title + "</option>");
    states.push(this.region.state);
    if (states_regions[this.region.state] == null) {
      states_regions[this.region.state] = new Array
    }
    if (jQuery.inArray(found_regions,this.region.id) == -1) {
      found_regions.push(this.region.id);
      states_regions[this.region.state].push(this.region);
    }
  });
  $(states.unique().sort()).each(function() {
    regions = "";
    $(states_regions[this].unique_by_key("title").sort(function(a,b){return a.title > b.title})).each(function() {
      regions += "<option value='" + this.id + "'>" + this.title + "</option>";
    });
    $("#isg_regions").append("<optgroup label='" + this + "'>" + regions + "</optgroup>");
  });
  $("#isg_regions").bind("change",function() {
    $("#isg_courses").val('');
    $("#isg_holes").html("<option>18</option><option>9</option>");
    $("#isg_holes").val("18");
    $("#isg_cart").html("<option value='true'>Yes</option><option value='false'>No</option>");
    $("#isg_cart").val('true');
  });
  $("#isg_courses").bind("change",function() {
    $("#isg_regions").val('');
    // Update the holes and regions
    course_id = $(this).val();
    $.each(isg_data,function(i) {
      if (isg_data[i].id == course_id) {
        course = isg_data[i];
      }
    });
    if(course.holes == "18") {
      $("#isg_holes").html("<option>18</option>");
      $("#isg_holes").val("18");
    }
    if(course.holes == "9") {
      $("#isg_holes").html("<option>9</option>");
      $("#isg_holes").val("9");
    }
    if(course.holes == "both") {
      $("#isg_holes").html("<option>18</option><option>9</option>");
      $("#isg_holes").val("18");
    }
    if(course.carts == "optional") {
      $("#isg_cart").html("<option value='true'>Yes</option><option value='false'>No</option>");
      $("#isg_cart").val('true');
    }
    if(course.carts == "compulsory") {
      $("#isg_cart").html("<option value='true'>Yes</option>");
      $("#isg_cart").val('true');
    }
    if(course.carts == "notused") {
      $("#isg_cart").html("<option value='false'>No</option>");
      $("#isg_cart").val('false');
    }
  });
  // Load in 6 months forward
  $("#isg_month_year").html("");
  for (i=0;i<7;i++) {
    d = new Date;
    d.setMonth(d.getMonth() + i);
    $("#isg_month_year").append("<option value='" + (d.getMonth() + 1) + "," + d.getFullYear() + "'>" + d.getMonthName() + " " + d.getFullYear() +"</option>")
  }
  $("#isg_month_year").bind("change",function() {
    load_days();
  });
  load_days();
})



Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };
Array.prototype.unique_by_key =
  function(key) {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        eval("x = this[i]." + key + ";");
        eval("y = this[j]." + key + ";");
        if (x === y)
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };
Date.prototype.getMonthName = function() {
  var m = ['January','February','March','April','May','June','July','August','September','October','November','December'];
  return m[this.getMonth()];
}
Date.prototype.getDayName = function() {
  var d = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
  return d[this.getDay()];
}
function daysInMonth(month,year) {
  var m = [31,28,31,30,31,30,31,31,30,31,30,31];
  if (month != 2) return m[month - 1];
  if (year%4 != 0) return m[1];
  if (year%100 == 0 && year%400 != 0) return m[1];
  return m[1] + 1;
} 
function sort_by_value(keyArray, valueMap) {
  return keyArray.sort(function(a,b){return valueMap[a]-valueMap[b];});
}
var day_names = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
function load_days() {
  date_bits = $("#isg_month_year").val().split(",");
  today = new Date;
  if (date_bits[0] == (today.getMonth() + 1) && date_bits[1] == today.getFullYear()) {
    start = today.getDate();
  } else {
    start = 1
  }
  $("#isg_day").html("");
  for (i=start;i<=daysInMonth(date_bits[0],date_bits[1]);i++) {
    d = new Date;
    d.setDate(i);
    d.setMonth(date_bits[0] - 1);
    d.setYear(date_bits[1]);
    $("#isg_day").append("<option value='" + i + "'>" + d.getDayName() + " " + i + "</option>");
  }
}


