function openPDFWin(url) {
  window.open(url, '', 'resizable');
}

function countdown() {
  var target      = new Date("april 6, 2010 13:00:00"); /* UTC */
  var now         = new Date();
  var diff_secs   = Math.floor((target - now) / 1000);
  var days_remain = Math.floor(diff_secs / 86400); 
  var hrs_remain  = Math.floor((diff_secs - days_remain * 86400) / 3600); 
  var mins_remain = Math.floor((diff_secs - days_remain * 86400 - hrs_remain * 3600) / 60); 
  var secs_remain = diff_secs - days_remain * 86400 - hrs_remain * 3600 - mins_remain * 60; 

  $("days").innerHTML = leading_zero(days_remain);
  $("hrs").innerHTML   = leading_zero(hrs_remain);
  $("mins").innerHTML = leading_zero(mins_remain);
  $("secs").innerHTML  = leading_zero(secs_remain);
}

function leading_zero(val) {
  return (val >= 10) ? val : ("0" + val);
}

document.observe("dom:loaded", function() {
  
  // displays countdown, if startpage
  if ($$("h1.home").size() > 0) {
    countdown();
    window.setInterval(countdown, 60);
  }
    
  // links to pdf open in a new pdfwindow
  $$("a[href$=.pdf]").each(function(el) {
    el.onclick = function() { openPDFWin(el.href); return false };
  });
  
  // external links should be opened in new window
  $$("a[href^=http]:not(a[href^=http://www.sparkling])").each(function(elem) { elem.writeAttribute("target", "_blank"); });
  
  // blog image links gets an lightbox rel for nicer image openers
  $$("a[href*=wp-content]").invoke("writeAttribute", "rel", "lightbox[blog]");

  // toggle events
  if ($$(".events").size() > 0) {
    $$(".events div.eventdetails p:not(:first-child), .events div.eventdetails ul").invoke("hide");
    $$(".events div.event h3").each(function(elem) {
      var currentParent = elem.ancestors().first();
      if (currentParent.select("div.eventdetails p").size() > 1) {
        Element.insert(currentParent.select("div.eventdetails").first(), "<div class=\"more_link\" style=\"color: #3E3641; cursor: pointer; margin-bottom: 10px;\">(" + more + ")</div>");
      }
    });
    $$(".more_link").invoke("observe", "click", function() {
      this.ancestors()[1].select("div.eventdetails p:not(:first-child)").invoke("toggle");
      this.ancestors()[1].select("div.eventdetails ul").invoke("toggle");
      this.innerHTML = (this.innerHTML.match(more)) ? "(" + less + ")" : "(" + more + ")";
    });
  }

  // toggle registration notice
  if ($("registration_link")) { $("registration_link").observe("click", function() { $("notice").toggle(); }); }
  
  if ($$("a.startlink").size() > 0) {
    $("more").hide();
    reloadLogo();
    $$("a.startlink").first().setAttribute("href", "#");
    $$("a.startlink").first().observe("click", function() { Effect.toggle("more", "blind", { afterFinish: reloadLogo }); });
  }
});

 // IE6 fix
function reloadLogo() {
  $("logo").hide();
  $("logo").show();
}