$('#homeBuckets li').click(function(){
	if($(this).find('img').attr('title')){
		$(this).html('<div class=\'\'>'+ $(this).find('img').attr('title') +'</div><a class="action-button" href="'+$(this).find('img').attr('rel')+'"><span>Learn More</span></a>');
		return false;
	}
});

/*
* this is the code for the offices page
*/
$('#michimap area').each(function(){
	
	$(this).qtip({
		content: {
		      text: 'Loading...', // The text to use whilst the AJAX request is loading
		      ajax: {
		         url: '/wp-admin/admin-ajax.php', // URL to the local file
		         type: 'POST', // POST or GET
		         data: "action=map_tip_ajax&id="+ $(this).attr('id')
		      }
		 },
	     position: {
	        at: 'center', // Position the tooltip above the link
	        my: 'center',
	        viewport: $(window), // Keep the tooltip on-screen at all times
	        effect: false // Disable positioning animation
	     },
	     show: {
	        event: 'mouseenter',
	        solo: true // Only show one tooltip at a time
	     },
	     hide: 'unfocus',
	     style: {
	        classes: 'office-tip'
	     }
	})// Make sure it doesn't follow the link when we click it
   .click(function(event) { event.preventDefault(); });
});

/*
* this is the ajax to get the post thats clicked on the stories page the change_success action is in functions.php
*/

$('ul#storiesPost li').click(function(){
	
	var id = $(this).find('img').attr('id');
	$('ul#storiesPost li').removeClass('active');
	$(this).addClass('active');
	$.ajax({
	  type: "POST",
	  url: "/wp-admin/admin-ajax.php",
	  data: "action=change_success&id="+id,
	  success: function(e){
	    $('.postArticle').hide();
	  	$('.postArticle').html(e).fadeIn();
	  	$('html,body').animate({scrollTop: $(".postArticle").offset().top},'normal');
	  }
	});
	
});

/*
* this gets the latest event for the grcc calendar and puts it in the sidebar
*/

$.getJSON('http://www.google.com/calendar/feeds/n3fphmre98blou6uitltj8h5dc%40group.calendar.google.com/public/full?alt=json-in-script&orderby=starttime&max-results=10&singleevents=true&sortorder=ascending&futureevents=true&callback=?', function(data) {
  
  var events = data['feed']['entry'];
  var eDate = formatGCalTime(events[0]['gd$when'][0]['startTime']);
  
  $('ul#events li').html('<a style="color:black;font-size:10pt;" href="/events/"><span>'+events[0]['title']['$t']+'</span></a><br>'+ eDate+' , '+ events[0]['gd$where'][0]['valueString']);
  
  //console.log(events);
  
});

/**
 * Converts an xs:date or xs:dateTime formatted string into the local timezone
 * and outputs a human-readable form of this date or date/time.
 *
 * @param {string} gCalTime is the xs:date or xs:dateTime formatted string
 * @return {string} is the human-readable date or date/time string
 */
function formatGCalTime(gCalTime) { 
  // text for regex matches
  var remtxt = gCalTime;

  function consume(retxt) {
    var match = remtxt.match(new RegExp('^' + retxt));
    if (match) {
      remtxt = remtxt.substring(match[0].length);
      return match[0];
    }
    return '';
  }

  // minutes of correction between gCalTime and GMT
  var totalCorrMins = 0;

  var year = consume('\\d{4}');
  consume('-?');
  var month = consume('\\d{2}');
  consume('-?');
  var dateMonth = consume('\\d{2}');
  var timeOrNot = consume('T');

  // if a DATE-TIME was matched in the regex 
  if (timeOrNot == 'T') {
    var hours = consume('\\d{2}');
    consume(':?');
    var mins = consume('\\d{2}');
    consume('(:\\d{2})?(\\.\\d{3})?');
    var zuluOrNot = consume('Z');

    // if time from server is not already in GMT, calculate offset
    if (zuluOrNot != 'Z') {
      var corrPlusMinus = consume('[\\+\\-]');
      if (corrPlusMinus != '') {
        var corrHours = consume('\\d{2}');
        consume(':?');
        var corrMins = consume('\\d{2}');
        totalCorrMins = (corrPlusMinus=='-' ? 1 : -1) * 
            (Number(corrHours) * 60 + 
	    (corrMins=='' ? 0 : Number(corrMins)));
      }
    } 

    // get time since epoch and apply correction, if necessary
    // relies upon Date object to convert the GMT time to the local
    // timezone
    var originalDateEpoch = Date.UTC(year, month - 1, dateMonth, hours, mins);
    var gmtDateEpoch = originalDateEpoch + totalCorrMins * 1000 * 60;
    var ld = new Date(gmtDateEpoch);

    // date is originally in YYYY-MM-DD format
    // time is originally in a 24-hour format
    // this converts it to MM/DD hh:mm (AM|PM) 
    dateString = (ld.getMonth() + 1) + '/' + ld.getDate() + ' ' + 
        ((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 {
    // if only a DATE was matched
    dateString =  parseInt(month, 10) + '/' + parseInt(dateMonth, 10);
  }
  return dateString;
}












