var DVAsite = {
  events : "",
  year : "",
  month : "",
  event_num : 3,
  weekdays : new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"),
  month_names : new Array("January","February","March","April","May","June","July","August","September","October","November","December"),

  init : function(event_json) {
    this.events = event_json;
  }
}

function toDollars(val) {
  var i = parseFloat(val);
	if(isNaN(i)) { return ""; }
	i = parseInt((i + .005) * 100)/100;

	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	return s;
}

function assignAttendeeOnclick(att) {
  if(Element.hasClassName(att.parentNode, "over")) {
    Element.removeClassName(att.parentNode, "over");
  } else {
    Element.addClassName(att.parentNode, "over");
  }
}

window.onload = function() {

  if($("project_category") && $("project-categories-form")) {
    $("project_category").onchange = function() {
      $("project-categories-form").submit();
    }
  }

  if($("site-logo") && $("house-logo")) {
    $("site-logo").onmouseover = function() {
      $("house-logo").style.display = 'block';
    }

    $("site-logo").onmouseout = function() {
      $("house-logo").style.display = 'none';
    }
  }

  if ($("next-link") && $("next")) {
    $("next-link").onmouseover = function() {
      $("next").style.display = 'block';
    }
    $("next-link").onmouseout = function() {
      $("next").style.display = 'none';
    }
  }

  if ($("previous-link") && $("previous")) {
    $("previous-link").onmouseover = function() {
      $("previous").style.display = 'block';
    }
    $("previous-link").onmouseout = function() {
      $("previous").style.display = 'none';
    }
  }

  if($("calendar-wrap")) {

    var popup = $("popup");

    Element.getElementsBySelector($("calendar-wrap"), "div.hasEvent").each(function(hasEvent) {
      hasEvent.onmouseover = function() {
        id = this.parentNode.id;

        if (this.parentNode.classNames().include("today")) {
          background = "#fffde3";
        } else {
          background = "transparent";
        }
        this.parentNode.style.background = background + " url(/images/td_over_bg.gif) center top no-repeat";
        Element.getElementsBySelector($("calendar-wrap"), "div.hasEvent").each(function(otherDay) {
          if(hasEvent != otherDay) {
            otherDay.parentNode.style.background = otherDay.parentNode.classNames().include("today") ? "#fffde3" : "transparent";
          }
        });

        date = new Date(DVAsite.year, DVAsite.month - 1, id);

        Element.getElementsBySelector(popup, ".date")[0].innerHTML = DVAsite.weekdays[date.getDay()] + ", " + DVAsite.month_names[date.getMonth()] + " " +  date.getDate();

        date = date - (date % 86400000)
        today = new Date;
        today = today - (today % 86400000)

        diff = date - today;
        if(diff >= 86400000) {
          days = Math.ceil(diff/1000/60/60/24);
          if(days == 1) {
            days_away = "1 day away"
          }
          else {
            days_away = days + " days away"
          }
        }
        else if(diff <= -86400000) {
          days = Math.floor(Math.abs(diff/1000/60/60/24));
          if(days == 1) {
            days_away = "1 day ago"
          }
          else {
            days_away = days + " days ago"
          }
        }
        else {
          days_away = "Today"
        }

        Element.getElementsBySelector(popup, ".days_from_now")[0].innerHTML = days_away;

        // Hide event-# elements when not needed.
        i = DVAsite.events[id].size();
        while(i <= DVAsite.event_num) {
          $("event-" + i).style.display = "none";
          i++;
        }

        for(i = 1; i <= DVAsite.events[id].size(); i++) {
          date_ele = $("event-" + i);
          date_ele.style.display = "block";

          delete attrs;
          var attrs = DVAsite.events[id][i - 1];
          Element.getElementsBySelector(date_ele, ".title")[0].innerHTML = attrs.title;

          more_info = [];
          if(attrs.aia_units) {
            more_info.push(attrs.aia_units + " AIA/CES units");
          }

          if(attrs.related_link_url) {
            more_info.push('<a href="' + attrs.related_link_url + '">more info</a>');
          }

          Element.getElementsBySelector(date_ele, ".more_info")[0].innerHTML = more_info.join(" | ");

          start_time = attrs.start_time.split(" ")[1];
          start_time = start_time.split(":");
          st_am_pm = start_time[0] < 12 || start_time[0] > 23 ? " AM" : " PM";
          end_time = attrs.end_time.split(" ")[1];
          end_time = end_time.split(":");
          et_am_pm = end_time[0] < 12 || end_time[0] > 23 ? " AM" : " PM";

          Element.getElementsBySelector(date_ele, ".time")[0].innerHTML = (start_time[0] < 13 ? start_time[0] + ":" + start_time[1] + st_am_pm : (start_time[0]-12) + ":" + start_time[1] + st_am_pm) + " - " + (end_time[0] < 13 ? end_time[0] + ":" + end_time[1] + st_am_pm : (end_time[0]-12) + ":" + end_time[1] + et_am_pm);

          if(diff > 86400000) {
            if(attrs.cost_not_applicable > 0) {
              Element.getElementsBySelector(date_ele, ".cost")[0].innerHTML = "No registration required to attend";
            }
            else {
              Element.getElementsBySelector(date_ele, ".cost")[0].innerHTML = '<strong>' + ((attrs.is_free > 0) ? "Free" : "$" + attrs.cost_in_cents / 100) + '</strong> <a href=\"/events/signup?city=' + attrs.event_category_id + '&event=' + attrs.id + '\">Sign up</a>';
            }
          }
          else if(diff >= 0) {
            Element.getElementsBySelector(date_ele, ".cost")[0].innerHTML = '<strong>' + ((attrs.is_free > 0) ? "Free" : "$" + attrs.cost_in_cents / 100) + '</strong> Call 714&nbsp;612-4547';
          }
          else {
            Element.getElementsBySelector(date_ele, ".cost")[0].innerHTML = "Sign up has ended";
          }

          eventLocation = new Array();

          if(attrs.is_online != "1") {
            locationAttrs = new Array(attrs.location_name, attrs.address_line_1, attrs.address_line_2, attrs.city + ", " + attrs.state + " " + attrs.zip);

            for(j = 0; j <= locationAttrs.length-1; j++) {
              if(locationAttrs[j] != null && locationAttrs[j] != "") {
                eventLocation.push(locationAttrs[j]);
              }
            }
          }
          else {
            eventLocation.push("Takes place online");
          }

          Element.getElementsBySelector(date_ele, ".address")[0].innerHTML = eventLocation.join("<br />");
        }

        popup = this.parentNode.appendChild(popup);
        width = 249 + (DVAsite.events[id].size() - 1) * 131;
        popup.style.width = width + "px";
        popup.style.top = (-1 * popup.getHeight()) + Position.positionedOffset(popup.up(1).immediateDescendants()[0])[1] + "px";
        popup.style.display = "block";
        offsetLeft = Position.cumulativeOffset(popup.up().immediateDescendants()[0])[0] + width + 6;
        if(offsetLeft > 920) {
          popup.style.left = Position.cumulativeOffset(popup.up().immediateDescendants()[0])[0] + (920 - offsetLeft) + "px";
        }
        else {
          popup.style.left = Position.cumulativeOffset(popup.up().immediateDescendants()[0])[0] - Position.cumulativeOffset(popup.up(2))[0] + 1 + "px";
        }
      }
    });

    cal = $("calendar-wrap");
    normalDays = Element.getElementsBySelector(cal, "td.day");
    normalDays = normalDays.concat(Element.getElementsBySelector(cal, "td.otherMonth"));

    normalDays.each(function(normalDay) {
      normalDay.onmouseover = function() {
        Element.getElementsBySelector($("calendar-wrap"), "div.hasEvent").each(function(otherDay) {
          otherDay.parentNode.style.background = otherDay.parentNode.classNames().include("today") ? "#fffde3" : "transparent";
          $("popup").style.display = "none";
        });
      }
    });
  }

  var preview = $('news-item-preview-button');

  if(preview) {
    preview.onclick = function() {
      ApplicationClass.newsPreviewUpdate();
      return false;
    }
  }

  if($("news_type_highlight")) {
    $("news_type_highlight").onchange = function() {
      if($F(this) == true) {
        document.getElementsByClassName("dependent-highlight").each(function(ele) {
          ele.style.display = "block";
        });
      }
      else {
        document.getElementsByClassName("dependent-highlight").each(function(ele) {
          ele.style.display = "none";
        });
      }
    }
  }

  if($("event_is_free") && $("event_cost_not_applicable")) {
    $("event_is_free").onchange = function() {
      if($F(this) == true) {
        $("event_cost_not_applicable").disabled = true;
        $("event_cost").disabled = true;
        $("event_cost").value = null;
      }
      else {
        $("event_cost_not_applicable").disabled = false;
        $("event_cost").disabled = false;
      }
    }

    $("event_cost_not_applicable").onchange = function() {
      if($F(this) == true) {
        $("event_is_free").disabled = true;
        $("event_cost").disabled = true;
        $("event_cost").value = null;
      }
      else {
        $("event_is_free").disabled = false;
        $("event_cost").disabled = false;
      }
    }

    if($F("event_is_free") == true) {
      $("event_is_free").onchange();
    }
    else {
      $("event_cost_not_applicable").onchange();
    }
  }

  if($("event_cost")) {
    $("event_cost").onblur = function() {
      $("event_cost").value = toDollars($F("event_cost"));
    }
  }

  if($("event_is_online")) {
    $("event_is_online").onchange = function() {
      global = $("event_is_global");
      if(this.checked) {
        global.checked = true;
      }
    }
  }

  if($$("div.event")) {
    $$("div.event").each(function(ele) {
    	ele.getElementsByClassName("event-title")[0].onclick = function() {
    	  thisEvent = this.parentNode.parentNode;
    	  if(Element.hasClassName(thisEvent, "open")) {
    	    Element.removeClassName(thisEvent, "open");
    	    Element.removeClassName(thisEvent, "show-new");
    	  } else {
    	    Element.addClassName(thisEvent, "open");
    	  }
    	}

    	ele.getElementsByClassName("attendee-name").each(function(att) {
    	  att.onclick = function() {
    	    assignAttendeeOnclick(att);
    	  }
    	});

    	Element.getElementsBySelector(ele, ".new-attendee a.cancel").each(function(cancellink) {
    	  cancellink.onclick = function() {
    	    Element.removeClassName(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode, "show-new");
    	    this.parentNode.parentNode.parentNode.parentNode.reset();

          return false;
    	  }
    	});
    });

    $$(".new-attendee-link").each(function(newattlink) {
      newattlink.onclick = function() {
        Element.addClassName(this.parentNode.parentNode.parentNode, "show-new");
        return false;
      }
    });
    
    // Sample text in Call Me widget
    var phone = $('phone');
    if (phone != null) {
      phone.value = '555-555-5555';
      phone.addClassName('sample');
      phone.onfocus = function () {
        if (this.hasClassName('sample')) {
          this.removeClassName('sample');
          this.value = '';
        }
      };
    }

  }

}
