// Empty input field
function make_empty(t) {
  t.value = "";
}

// Character count
function character_counter() {
  var r = (500 - $("comment_comment").value.length);
  $("counter").innerHTML = r + " characters left.";
}

// Comments
function leave_comment() {
  $("leave-comment").hide();
  $("new-form").show();
  window.scrollBy(0,500);
}

function cancel_comment() {
  window.scrollBy(0,-450);
  $("new-form").hide();
  $("leave-comment").show();
}

// Make li same height
function homeColumns() {
  var columns = $$("#home li");
  var max_height = columns.invoke("getHeight").max();
  columns.invoke("setStyle", {height: (max_height - 20) + "px"});
}

function regularColumns() {
  var columns = $$("#regular li.notsized");
  var max_height = columns.invoke("getHeight").max();
  columns.invoke("setStyle", {height: (max_height - 20) + "px"});
  $$("#regular li.notsized").each(function(t) {
    t.removeClassName("notsized");
  });
}

function specialColumns() {
  var columns = $$("#special li.notsized");
  var max_height = columns.invoke("getHeight").max();
  columns.invoke("setStyle", {height: (max_height - 20) + "px"});
  $$("#special li.notsized").each(function(t) {
    t.removeClassName("notsized");
  });
}

function retiredColumns() {
  var columns = $$("#retired li.notsized");
  var max_height = columns.invoke("getHeight").max();
  columns.invoke("setStyle", {height: (max_height - 17) + "px"});
  $$("#retired li.notsized").each(function(t) {
    t.removeClassName("notsized");
  });
}

// Sidebar
function clear_actives() {
  $("sidebar-archive").childElements().each(function(t) {
    t.removeClassName("active").removeClassName("current");
  });
}

document.observe("dom:loaded", function() {
  // Adds _blank in non-deprecated way
  $$("a[rel='external']").each(function(t) {
    t.writeAttribute("target","_blank");
    t.writeAttribute("title","Opens in new window");
  });
  $$("a[rel='archive']").each(function(t){
    t.writeAttribute("target","_blank");
    t.writeAttribute("title","Opens in new window");
  });
  // Tabs and sidebar links
  $$(".regular_link a").invoke("observe", "click", function() {
    clear_actives();
    $$(".regular_link a").each(function(t) {
      t.up(1).addClassName("active").addClassName("current");
    });
  });
  $$(".special_link a").invoke("observe", "click", function() {
    clear_actives();
    $$(".special_link a").each(function(t) {
      t.up(1).addClassName("active").addClassName("current");
    });
  });
  $$(".retired_link a").invoke("observe", "click", function() {
    clear_actives();
    $$(".retired_link a").each(function(t) {
      t.up(1).addClassName("active").addClassName("current");
    });
  });
  // Search categories
  $$("#search_schedule").invoke("observe", "click", function() {
    $("search_form").writeAttribute("action","/schedule/search/");
  });
  $$("#search_djs").invoke("observe", "click", function() {
    $("search_form").writeAttribute("action","/djs/search/");
  });
  $$("#search_programs").invoke("observe", "click", function() {
    $("search_form").writeAttribute("action","/programs/search/");
  });
  $$("#search_events").invoke("observe", "click", function() {
    $("search_form").writeAttribute("action","/events/search/");
  });
  $$("#search_news").invoke("observe", "click", function() {
    $("search_form").writeAttribute("action","/news/search/");
  });
  $$("#search_all").invoke("observe", "click", function() {
    $("search_form").writeAttribute("action","/search/");
  });
  // Hide error notices
  $$("#error-notice").invoke("observe", "click", function() {
    this.hide();
  });
});

Event.addBehavior({
  // Sidebar
	"li.parent h4:click": function() {
		this.up(0).toggleClassName("active");
		return false;
	},
	"li.current h5:click": function() {
		this.up(0).removeClassName("current");
		return false;
	},
	"li.child h5:click": function() {
		this.up(0).toggleClassName("active-child");
		return false;
	}
})
