var calendarDate = new Date();
var calendarCurrentYear = calendarDate.getFullYear();
var calendarCurrentMonth = calendarDate.getMonth();
var calendarCurrentDay = calendarDate.getDate();
var calendarSelectedDay = 0;
var ongoing = false;
var rollOverMaxDisplay = 4;
//calendarCurrentDay = 3; // for test

var daySelected = 0;

setOngoing = function() { // will set for ongoing calendar
  if (!ongoing) {
    return;
  }
  $("#cln_left_title_ongoing").show();
  $("#cln_left_title_ongoing_calendar_back").click(function(event) {
    document.location = "/calendar/";
  });
}

setRegular = function() { // will set for regular (not ongoing) calendar
  if (ongoing) {
    return;
  }
  $("#cln_left_title_calendar").show();
  $("#cln_weekdays").show();
  $("#cln_left_calendar").show();
  $("#cln_left_bottom_upcoming").show();
  $("#cln_right").show();
  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();
    day = 1;
  }
  if (year >= 1950) { calendarCurrentYear = year; }
  if (month >= 0) { calendarCurrentMonth = month; }
  if (day > 0) { calendarCurrentDay = day; }
  $("#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);
}

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

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

setCalendarMonth = function() {
  var ypos = calendarCurrentMonth * -20;
  var width = [121, 141, 96, 77, 58, 65, 62, 107, 163, 126, 150, 149];
  $("#cln_left_title_month").css("background-position","0px " + ypos + "px");
  $("#cln_left_title_month").css("width",width[calendarCurrentMonth] + "px");
}

$(document).ready(function() {
  setTimeout("setOngoing()", 100); // safari bug
  setTimeout("setRegular()", 100); // safari bug
  setTimeout("upcomingEvents()", 100);
  setTimeout("setCalendar()", 100);
});

showRolloverDate = function(day) {
  // show day
  $("#cln_rollover_date_day").text(day);
  $("#cln_rollover_date").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_container").children().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) {
//$("#cln_rollover_container").append("outside ");
//return "inside";

  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_event\">";
    if (ev.title != "") {
      txt += "<div class=\"cln_event_title\">" + ev.title + "</div>";
    }
    if (ev.blurb != "") {
      txt += "<div class=\"cln_event_blurb\">" + ev.blurb + "</div>";
    }
    if (ev.time != "") {
      txt += "<div class=\"cln_event_time\">" + ev.time + "</div>";
    }
    if (ev.location != "") {
      txt += "<div class=\"cln_event_location\">" + ev.location + "</div>";
    }
    txt += "</div>";
    i++;
  }
  return txt;
}

getEvents = function(day) {
  if (!events[day]) {
    return;
  }
  var i = 0;
  var txt = "";
  var val = "";
  var month = parseInt($("#cln_month").html(),10)+1;
  while (ev = events[day][i]) {
    txt += "<div class=\"cln_event\" id=\"cln_event_" + i + "\">";
    var imageDisplayed = false;
    for (var key in ev) {
      /*if (ev[key] == "") {
        continue;
      }*/
      if (key == "location_lookup_address" || key == "ongoing_start" || key == "ongoing_end") {
        continue;
      }
      val = ev[key];
      if (key == "location" && ev["location_lookup_address"] != "") {
        val += " (<a href=\"http://maps.google.com/?q=" + ev["location_lookup_address"] + "\" target=\"event_map\">map</a>)";
      }
      if (key == "description" && imageDisplayed) {
        txt += "</div>";
      }
      if (key == "image") {
        if (ev['ongoing_end'].substring(0, 1) == "0") {
          ev['ongoing_end'] = ev['ongoing_end'].substring(1);
        }
		if(val != ""){
        	txt += "<img src=\"" + val + "\">";
        }
		txt += "<div class=\"cln_event_txt_holder\">";
        txt += "<div class=\"cln_event_date\">" + month + "/" + day + (ev['ongoing_end'] ? " - " + ev['ongoing_end'] : "") + "</div>";
        imageDisplayed = true;
      } else {
        txt += "<div class=\"cln_event_" + key + "\">";
        txt += val;
        txt += "</div>";
      }
    }
    txt += "</div>";
    i++;
  }
  return txt;
}

selectDay = function(day, do_select) {
  // change color of selected day
  if (day < 1 || day > 31) {
    return;
  }
  if (day == daySelected && !do_select) {
    return;
  }
  imax = 1;
  if (day >= 10) {
    imax = 2;
  }
  for (i = 0; i < imax; i++) {
    $ypos = day.toString().charAt(i) * 20;
    $xpos = (do_select ? -17 : 0);
    $bg_pos = $("#cln_left_calendar_date_number_" + day + "_" + i).css("background-position", $xpos + "px -" + $ypos + "px");
  }
}

getDayEvents = function(day) {
  if (!events[day]) {
    setRollOver(day, "event_none");
    return;
  }
  var txt = getEventsRollOver(day);
  $("#cln_rollover_events").html(txt);
  $("#cln_rollover_events > .cln_event").css("border-bottom", "none");
  $("#cln_rollover_events > .cln_event").css("padding-bottom", "0px");
  $("#cln_rollover_events > .cln_event").css("margin-bottom", "20px");
  setRollOver(day, "events");
}

selectEvent = function(day) {
  if (!events[day]) {
    return;
  }
  var prevDaySelected = daySelected;
  daySelected = day;
  // use previous day to update color of numbers
  if (prevDaySelected > 0) {
    selectDay(prevDaySelected, false);
  }
  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 (!ongoing && day < calendarCurrentDay) { // only show upcoming events for regular calendar and all events for ongoing
      continue;
    }
    txt += getEvents(day);
    if(counter++ >= numDaysToShow){
      break;
    }
  }
  if (txt == "") {
    txt = "Sorry, there are no events found.";
  }
  $("#cln_events").html(txt);
  $("#cln_events > :last").css("border-bottom", "none");
}

function event(title, blurb, image, location, location_lookup_address, time, source, source_link, description, ongoing_start, ongoing_end) {
  // the order in which these are declared is the order in which they will appear on the page
  this.image = image;
  this.title = title;
  this.blurb = blurb;
  this.time = time;
  this.location = location;
  this.source = source;
  this.description = description;
  this.location_lookup_address = location_lookup_address;
  this.ongoing_start = ongoing_start;
  this.ongoing_end = ongoing_end;
  if (this.source != "") {
    if (source_link != "") {
      source_link = "<a href=\"" + source_link + "\" target=\"event_source\">";
    }
    this.source = "<b>&raquo; WEBSITE</b> " + source_link + this.source;
    if (source_link != "") {
      this.source += "</a>";
    }
  }
}

