function gct(t){var rt=t;function c(r){var m=rt.match(new RegExp('^'+r));if (m) {rt=rt.substring(m[0].length);return m[0];}return '';}var tcm=0;var yr=c('\\d{4}');c('-?');var mo=c('\\d{2}');c('-?');var dm=c('\\d{2}');var tn=c('T');if(tn=='T'){var hr=c('\\d{2}');c(':?');var min=c('\\d{2}');c('(:\\d{2})?(\\.\\d{3})?');var z=c('Z');if(z!='Z'){var cpm=c('[\\+\\-]');if(cpm!=''){var ch=c('\\d{2}');c(':?');var cm=c('\\d{2}');tcm=(cpm=='-'?1:-1)*(Number(ch)*60+(cm==''?0:Number(cm)));}}var e=Date.UTC(yr,mo-1,dm,hr,min);var ge=e+tcm*1000*60;var ld=new Date(ge);ds=(ld.getMonth()+1)+'/'+ld.getDate()+'/'+ld.getFullYear()+' - '+((ld.getHours()>12)?(ld.getHours()-12):(ld.getHours()===0?12:ld.getHours()))+':'+((ld.getMinutes()<10)?('0'+ld.getMinutes()):(ld.getMinutes()))+((ld.getHours()>=12)?'pm':'am');}else{ds=parseInt(mo,10)+'/'+parseInt(dm,10);}return ds;}

$(function(){
  $('#contact').click(function(){
    $('<div>').dialog({
      autoOpen: false,
      width: 600,
      modal: true,
      resizable: false,
      title: 'Contact BNMI',
      buttons: {
        "Ok": function() {
          $(this).dialog("close");
          $(this).dialog("destroy");
        },
      }
    }).html('For now, please send an e-mail to <a href="mailto:info@bostonnewmusic.org">info@bostonnewmusic.org</a>.').dialog('open');
    return false;
  });
  $(".todo").click(function(){
    $('<div>').html("This page is undergoing maintenance. Please check back another time.").dialog({
      autoOpen: false,
      width: 600,
      modal: true,
      resizable: false,
      buttons: {
        "Ok": function() {
          $(this).dialog("close");
        },
      }
    }).dialog('open');
    return false;
  });
});

function listEvents(root, divId) {
  $(divId).text('').addClass('event-box');
  if (root.feed.entry) {
    $.each( root.feed.entry, function(i, value) {
      var content = value.content["$t"];
      if ( content != "" && content.length >= 70 ) {
        content = content.substring(0,144) + '...';// [read more]';
      }
      var uid = value["gCal$uid"].value;
      if(i%2){n='odd'}else{n='even'};
      $(divId).append(
        $("<a>").append(
          $("<div>").addClass("event-icon"),
          $("<div>").addClass("event-info").append(
            $("<h1>").append(
              value.title["$t"]
            ),
            $("<div>").append( value["gd$where"][0].valueString ).addClass("event-location"),
            $("<div>").append( content ).addClass("event-content"),
            $("<div>").append( gct( value["gd$when"][0].startTime ) ).addClass("event-time")
          )
        ).attr("id", uid).addClass("event-item").addClass(n).attr("title", value.title["$t"] + ' @ ' + value["gd$where"][0].valueString).fadeIn(1000).attr('href',value['link'][0]['href'])
      )
    });
  }
}

function upcomingEvents(root) {
  listEvents(root, '#upcoming-events');
}

function pastEvents(root) {
  listEvents(root, '#past-events');
}

function listNews(root, divId) {
  $(divId).text('').addClass('news-box');
  if (root.feed.entry) {
    $.each( root.feed.entry, function(i, value) {
      if(i%2){n='odd'}else{n='even'};
      $(divId).append(
        $("<div>").append(
          $("<div>").addClass("news-icon"),
          $("<div>").addClass("news-info").append(
            $("<h1>").append(
              value.title["$t"]
            ),
            $("<div>").append( gct( value["updated"]["$t"] ) ).addClass("news-time")
          )
        ).attr("id", value["sites$pageName"]["$t"]).addClass("news-item").addClass(n)
      )
    });
  }
}

function newNews(root) {
  listNews(root, '#new-news');
}


