var calendarDate = new Date();
var calendarCurrentYear = calendarDate.getFullYear();
var calendarCurrentMonth = calendarDate.getMonth();
var calendarCurrentDay = calendarDate.getDate();
var calendarSelectedDay = 0;
var rollOverMaxDisplay = 4;

var daySelected = 0;

setCalendar = function() {
  setCalendarYear();
  setCalendarMonth();
}

setCalendarYear = function(year) {
  year = calendarCurrentYear.toString();
  var ypos = 0;
  for (i = 1; i <= 4; i++) {
    ypos = year.charAt(i - 1) * -30 - 1;
    $("#cln_left_title_year" + i).css("background-position","0px " + ypos + "px");
  }
}

setCalendarMonth = function() {
  var ypos = calendarCurrentMonth * -30 - 1;
  var width = [201, 228, 150, 128, 87, 111, 104, 177, 259, 206, 239, 239];
  $("#cln_left_title_month").css("background-position","0px " + ypos + "px");
  $("#cln_left_title_month").css("width",width[calendarCurrentMonth] + "px");
}

$(document).ready(function(){
  if ($.browser.safari) {
    $("#cln_subscribe_ical").css("width", "47px");
  }
  $("#cln_events").html("Loading..."); // helps with ie6 css issue for footer
  var year = parseInt($("#cln_year").html(),10);
  var month = parseInt($("#cln_month").html(),10);
  var day = parseInt($("#cln_day").html(),10);
  if (year != calendarCurrentYear || (year == calendarCurrentYear && month != calendarCurrentMonth)) {
    $("#cln_left_calendar_date_today").hide();
  }
  if (year >= 1950) { calendarCurrentYear = year; }
  if (month >= 0) { calendarCurrentMonth = month; }
  if (day > 0) { calendarCurrentDay = day; }
  setCalendar();
  upcomingEvents();
  $("#cln_previous").click(function(event) {
    var prevMonth = calendarCurrentMonth;
    var prevYear = calendarCurrentYear;
    if (prevMonth < 1) {
      prevMonth = 12; // in javascript month always starts with 0
      prevYear--;
    }
    document.location = "/calendar/" + prevYear + "-" + prevMonth + "/";
  });
  $("#cln_next").click(function(event) {
    var nextMonth = calendarCurrentMonth + 2;
    var nextYear = calendarCurrentYear;
    if (nextMonth > 12) {
      nextMonth = 1;
      nextYear++;
    }
    document.location = "/calendar/" + nextYear + "-" + nextMonth + "/";
  });
  $("#cln_subscribe_ical").click(function(event) {
    // switch http to webcal
    var currentHost = document.location.host.toString();
    document.location = "webcal://" + currentHost + "/ical/";
  });
  if (calendarSelectedDay > 0) {
    setTimeout("presetCalendarDayRequestedByUser()", 100);
  }
});

presetCalendarDayRequestedByUser = function() {
  selectEvent(calendarSelectedDay);
  selectDay(calendarSelectedDay, true);
  getDayEvents(calendarSelectedDay);
}

showRolloverDate = function(day) {
  // show month
  month = calendarCurrentMonth + 1; // javascript is one month behind
  month = month.toString();
  for (i = 1; i <= month.length; i++) {
    ypos = month.charAt(i - 1) * -30 - 1;
    $("#cln_rollover_date_month" + i).css("background-position","0px " + ypos + "px");
    $("#cln_rollover_date_month" + i).show();
  }
  // show slash
  $(".slash").show();
  // show month
  day = day.toString();
  for (i = 1; i <= day.length; i++) {
    ypos = day.charAt(i - 1) * -30 - 1;
    $("#cln_rollover_date_day" + i).css("background-position","0px " + ypos + "px");
    $("#cln_rollover_date_day" + i).show();
  }
}

setRollOver = function(day, div) {
  hideAllRollOver(false);
  showRolloverDate(day);
  // now display the div
  $("#cln_rollover_event_general").hide();
  $("#cln_rollover_date").show();
  $("#cln_rollover_" + div).show();
  $("#cln_rollover_" + div).find(":hidden").show();
}

hideAllRollOver = function(isMouseOut) {
  $("#cln_rollover").find(":visible").hide();
  if (!isMouseOut || daySelected < 1) {
    $("#cln_rollover_event_general").show();
    $("#cln_rollover_event_general").find(":hidden").show();
  } else if (isMouseOut) {
    getDayEvents(daySelected);
  }
}

getEventsRollOver = function(day) {
  var i = 0;
  var txt = "";
  var counter = 0;
  while (ev = events[day][i]) {
    if (++counter > rollOverMaxDisplay) {
      txt += "<div class=\"cln_event\">";
      txt += "<a href=\"#cln_event_" + rollOverMaxDisplay + "\" class=\"more_link\">&raquo; More</a>";
      txt += "</div>";
      break;
    }
    txt += "<div class=\"cln_rollover_event\"><div class=\"cln_rollover_event_title\">" + ev.title + "</div><div class=\"cln_rollover_event_description\">" + ev.blurb + "</div></div>";
    i++;
  }
  return txt;
}

getEvents = function(day) {
  if (!events[day]) {
    return;
  }
  var i = 0;
  var txt = "";
  while (ev = events[day][i]) {
    txt += "<div class=\"cln_event\" id=\"cln_event_" + i + "\">";
    for (var key in ev) {
      /*if (ev[key] == "") {
        continue;
      }*/
      if (key == "image") {
        txt += "<img src=\"" + ev[key] + "\">";
        month = calendarCurrentMonth + 1; // javascript is one month behind
        month = month.toString();
        for (j = 1; j <= month.length; j++) {
          ypos = month.charAt(j - 1) * -30 - 1;
          txt += "<div class=\"number\" style=\"background-position: 0px " + ypos + "px; display: block;\"></div>";
        }
        txt += "<div class=\"slash\"></div>";
        d = day.toString();
        for (j = 1; j <= d.length; j++) {
          ypos = d.charAt(j - 1) * -30 - 1;
          txt += "<div class=\"number\" style=\"background-position: 0px " + ypos + "px; display: block;\"></div>";
        }
      } else {
        txt += "<div class=\"cln_event_" + key + "\">";
        txt += ev[key];
        txt += "</div>";
      }
    }
    txt += "</div>";
    i++;
  }
  return txt;
}

selectDay = function(day, do_select) {
  // change color of selected day
  if (day >=1 && day <= 31) {
    imax = 1;
    if (day >= 10) {
      imax = 2;
    }
    for (i = 0; i < imax; i++) {
      $ypos = day.toString().charAt(i) * 30 + 1;
      $xpos = (do_select ? -45 : (events[day] ? 0 : -24));
      $bg_pos = $("#cln_left_calendar_date_number_" + day + "_" + i).css("background-position", $xpos + "px -" + $ypos + "px");
    }
  }
  // end change color of selected day
}

getDayEvents = function(day) {
  if (!events[day]) {
    setRollOver(day, "event_none");
    return;
  }
  var txt = getEventsRollOver(day);
  $("#cln_rollover_events").html(txt);
  setRollOver(day, "events");
}

selectEvent = function(day) {
  if (!events[day]) {
    return;
  }
  daySelected = day;
  $("#cln_left_calendar :visible").css("border-color", "#e5e5e5");
  $("#cln_left_calendar_date_" + day).css("border-color", "black");
  var txt = getEvents(daySelected);
  $("#cln_events").html(txt);
  $("#cln_left_bottom_upcoming").hide();
  $("#cln_events > :last").css("border-bottom", "none");
}

upcomingEvents = function() {
  var txt = "";
  var counter = 1;
  var numDaysToShow = 3;
  for (var day in events) {
    if (day < calendarCurrentDay) { // only show upcoming events
      continue;
    }
    txt += getEvents(day);
    if(counter++ >= numDaysToShow){
      break;
    }
  }
  $("#cln_events").html(txt);
  $("#cln_events > :last").css("border-bottom", "none");
}

function event(title, blurb, image, cost, cost_action, cost_action_link, location, location_lookup_address, time, source, source_link, description) {
  this.image = image;
  this.title = title;
  this.blurb = blurb;
  this.location = location;
  this.time = time;
  this.cost = cost;
  this.source = source;
  this.description = description;

  if (this.location != "") {
    this.location = "<b>WHERE:</b> " + this.location;
    if (location_lookup_address != "") {
      this.location += "<a href=\"http://maps.google.com/?q=" + location_lookup_address + "\" target=\"event_map\">MAP</a>";
    }
  }
  if (this.time != "") {
    this.time = "<b>WHEN:</b> " + this.time;
  }
  if (this.cost != "") {
    this.cost = "<b>COST:</b> " + this.cost;
    if (cost_action != "") {
      if (cost_action_link != "") {
        cost_action = "<a href=\"" + cost_action_link + "\" target=\"event_cost\">" + cost_action + "</a>";
      }
      this.cost += cost_action;
    }
  }
  if (this.source != "") {
    if (source_link != "") {
      source_link = "<a href=\"" + source_link + "\" target=\"event_source\">";
    }
    this.source = "<b>SOURCE:</b> " + source_link + this.source;
    if (source_link != "") {
      this.source += "</a>";
    }
  }
}

