Ext.onReady(function() {

  
});

function showFullBio() {
  var content = Ext.get("content");
  var overlay = Ext.get("overlay") || content.createChild({tag:"div", id: "overlay"});
  overlay.show().fadeIn({endOpacity: .8});
  
  var body = Ext.getBody();
  
  var bio = Ext.get("fullBio");
  bio.setTop(Math.max(135,body.getScroll().top));
  bio.slideIn("t",{
    easing: 'easeOut',
    duration: .5,
    callback: function() {
      body.on("click", hideBio);
    }
  });
  
  removeOverflow();
}

function hideBio() {
  Ext.getBody().un("click", hideBio);
  Ext.fly("overlay").fadeOut({callback: restoreOverflow});
  Ext.fly("fullBio").fadeOut();



}

// Firefox has a rendering issue where overflow:auto elements burn through while 
// the overlay is animating, so change overflow to hidden while the overlay is up.
function removeOverflow() {
  if (Ext.isGecko) {
    var overflowItems = Ext.get("body").select("tr td div.rounded");
    overflowItems.setStyle("overflow","hidden");
  }
}

function restoreOverflow() {
  if (Ext.isGecko) {
    var overflowItems = Ext.get("body").select("tr td div.rounded");
    overflowItems.setStyle("overflow","auto");
  }
}

function renderNews(items) {
  items.sort(function(a,b) { return a.start - b.start; });
  
  var html = [];
  for (var i=0, item; item = items[i]; i++) {
    html.push("<h3>" + markup(item.title) + "</h3>");
    
    var start = new Date(item.start * 1000);
    html.push("<h4>" + getDate(start));
    
    if (!item.allDay) {
      html.push(" " + getTime(start));
    }
    
    html.push(" " + markup(item.where) + "</h4>");
    html.push("<p>" + markup(item.content) + "</p>");
  }
  
  if (!html.length) {
    html[0] = "<h3>No items found.</h3>";
  }
  
  document.getElementById("news").innerHTML = html.join('');
  
}

function markup(string) {
  return string
    .replace(/\[a href=([^\]]+)\]([^\[]*)\[\/a\]/ig,'<a href="$1">$2</a>')
    .replace(/\n/g,"<br />");
}

function getDate(date) {
  return (date.getMonth()) + 1 + "." + date.getDate() + "." + date.getFullYear();
}

function getTime(date) {
  var hours = date.getHours();
  var suffix = " PM";
  if  (hours > 12) {
    hours -= 12;
  } else if (hours < 12) {
    suffix = " AM";
  }
  
  if (hours < 10) {
    hours = "0" + hours;
  }
  
  var minutes = date.getMinutes();
  if (minutes < 10) {
    minutes = "0" + minutes;
  }
  return hours + ":" + minutes + suffix;
}
