var K = function () {
    var a = navigator.userAgent;
    return {
        ie: a.match(/MSIE\s([^;]*)/)
    }
}();
 
var H = function (a) {
    var b = new Date();
    var c = new Date(a);
    if (K.ie) {
        c = Date.parse(a.replace(/( \+)/, ' UTC$1'))
    }
    var d = b - c;
    var e = 1000,
        minute = e * 60,
        hour = minute * 60,
        day = hour * 24,
        week = day * 7;
    if (isNaN(d) || d < 0) {
        return ""
    }
    if (d < e * 7) {
        return "right now"
    }
    if (d < minute) {
        return Math.floor(d / e) + " seconds ago"
    }
    if (d < minute * 2) {
        return "about 1 minute ago"
    }
    if (d < hour) {
        return Math.floor(d / minute) + " minutes ago"
    }
    if (d < hour * 2) {
        return "about 1 hour ago"
    }
    if (d < day) {
        return Math.floor(d / hour) + " hours ago"
    }
    if (d > day && d < day * 2) {
        return "yesterday"
    }
    if (d < day * 365) {
        return Math.floor(d / day) + " days ago"
    } else {
        return "over a year ago"
    }
};

var latest_tweet = "";
var latest_tweet_time = 0;

function got_tweet(obj){
	latest_tweet = obj[0].text;
	latest_tweet_time = obj[0].created_at;
	set_tweet();
	set_tweet_time();
}

function set_tweet_time(){
	if (latest_tweet_time)
		$(".latest_tweet_ago").text(H(latest_tweet_time));
}

function set_tweet(){
	if (latest_tweet.length)
		$(".latest_tweet").text(latest_tweet);
}



jQuery.prototype.scrollTo = function(offs){
	$("html, body").stop().animate({scrollTop: this.offset().top + (offs ? offs : 0)}, 333);
	return this;
};

jQuery.prototype.scrollToEnd = function(offs){
	this.scrollTo((offs ? offs : 0) + this.innerHeight());
	return this;
}

function do_nav(elts){
	for (var i = 0; i < elts.length; ++i){
		var elt = elts.eq(i);
		if (!elt.attr("href")) continue;
		if (location.href.indexOf(elt.attr("href").split("/").slice(-2, -1)[0]) != -1){
			elt.addClass("active");
			break;
		}
	}
}

$(function(){
	do_nav($($("#header .nav a").get().reverse()));
	do_nav($($("#footer .nav a").get().reverse()));
	
	set_tweet();
	set_tweet_time();
	$('#gch_nav .arrow').click(function(){
		$('#gch_nav').scrollToEnd();
	});
	
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
$("a,div").click(function(){  // Update class to point at the head of the list
});

}

	var inps = $("input, textarea");
	$.each(inps, function(idx, inp){
	if ($.trim($(inp).val()) == ""){
		$(inp).val($(inp).attr("def"));
	}
	});
	inps.focus(function(){
		if ($(this).val() == $(this).attr("def")){
			$(this).val("");
		}
	});
	inps.blur(function(){
		if ($.trim($(this).val()) == ""){
			$(this).val($(this).attr("def"));
		}
	});
});

// Constructor function
function Tooltip(opts, marker) {

  // Initialization
  this.setValues(opts);
  this.map_ = opts.map;
  this.marker_ = marker;
  var div = this.div_ = document.createElement("div");
  // Class name of div element to style it via CSS
  div.className = "tooltip";
  this.markerDragging = false;
}


 Tooltip.prototype = {

  // Define draw method to keep OverlayView happy
  draw: function() {},

  visible_changed: function() {
  var vis = this.get("visible");
  this.div_.style.visibility  = vis ? "visible" : "hidden";
 },

 getPos: function(e) {
  var projection = this.getProjection();
  // Position of mouse cursor
  var pixel = projection.fromLatLngToDivPixel(e.latLng);
  var div = this.div_;

  // Adjust the tooltip's position
  var gap = 15;
  var posX = pixel.x + gap;
  var posY = pixel.y + gap;

  var menuwidth = div.offsetWidth;
  // Right boundary of the map
  var boundsNE = this.map_.getBounds().getNorthEast();
  boundsNE.pixel = projection.fromLatLngToDivPixel(boundsNE);

  if (menuwidth + posX > boundsNE.pixel.x) {
    posX -= menuwidth + gap;
  }
  div.style.left = posX + "px";
  div.style.top = posY + "px";

  if (!this.markerDragging) {
   this.set("visible", true);
  }
 },

 addTip: function() {
  var me = this;
  var g = google.maps.event;
  var div = me.div_;
  div.innerHTML = me.get("text").toString();
  // Tooltip is initially hidden
  me.set("visible", false);
  // Append the tooltip's div to the floatPane
  me.getPanes().floatPane.appendChild(this.div_);

  // In IE this listener gets randomly lost after it's been cleared once.
  // So keep it out of the listeners array.
   g.addListener(me.marker_, "dragend", function() {
     me.markerDragging = false; });

  // Register listeners
  me.listeners = [
//   g.addListener(me.marker_, "dragend", function() {
//    me.markerDragging = false; }),
   g.addListener(me.marker_, "position_changed", function() {
    me.markerDragging = true;
    me.set("visible", false); }),
   g.addListener(me.map_, "mousemove", function(e) {
     me.getPos(e); })
  ];
 },

 removeTip: function() {
  // Clear the listeners to stop events when not needed.
  if (this.listeners) {
   for (var i = 0, listener; listener = this.listeners[i]; i++) {
     google.maps.event.removeListener(listener);
   }
   delete this.listeners;
  }
  // Remove the tooltip from the map pane.
  var parent = this.div_.parentNode;
  if (parent) parent.removeChild(this.div_);
 }
};


 function inherit(addTo, getFrom) {

  var from = getFrom.prototype;  // prototype object to get methods from
  var to = addTo.prototype;      // prototype object to add methods to
  for (var prop in from) {
   if (typeof to[prop] == "undefined") to[prop] = from[prop];
  }
 }
