$(document).ready(function() {
  squareClick();
  smoothScroll();
  positionExtractLinks();
  $("#expansion").mouseleave(function() {
    setTimeout("unexpand();", 500); // give the CSS un-hover transition time to complete before hiding with jQuery
  })
});

function squareClick() {
  $(".square a").each(function(event) {
    var jqA = $(this);
    var href = jqA.attr("href");
    var jqSquare = jqA.closest(".square");
    jqSquare.click(function() {
      loadSquare(jqSquare, href);
    });
  });
  $(".square a").click(function(event) {
    event.preventDefault();
  })
}

function loadSquare(jqSquare, href) {
  var jqExpansion = $("#expansion");
  var squareTop = jqSquare.position().top;
  var squareLeft = jqSquare.position().left;
  // Start of with the expansion covering the square, then animate
  jqExpansion.css("top", squareTop);
  jqExpansion.css("left", squareLeft);
  jqExpansion.show();
  jqExpansion.addClass("expanded");
  if (jqSquare.hasClass("lightblue")) {
    jqExpansion.addClass("lightblue");
  } else if (jqSquare.hasClass("darkblue")) {
    jqExpansion.addClass("darkblue");
  } else if (jqSquare.hasClass("orange")) {
    jqExpansion.addClass("orange");
  }
  // Load content
  jqExpansion.load(href, function() {
    // Rewrite image srcs so the relative links work
    jqExpansion.find("img").each(function() {
      var src = $(this).attr("src");
      src = src.replace("../gtwp_images/", "gtwp_images/");
      $(this).attr("src", src);
    });
    if ($.browser.msie) {
      // IE only detects a hover when the mouse is moved, workaround
      $(this).addClass("hover");
    }
  });
  // Hide other 'we...' rows
  var rowNum = jqSquare.closest(".square_row").attr("rownum");
  $(".we_row").each(function() {
    var jqWeRow = $(this);
    if(jqWeRow.attr("rownum") != rowNum) {
      jqWeRow.addClass("transparent");
    }
  });
}

function unexpand() {
  var jqExpansion = $("#expansion");
  jqExpansion.text("");
  jqExpansion.hide();
  jqExpansion.removeClass("lightblue");
  jqExpansion.removeClass("darkblue");
  jqExpansion.removeClass("orange");
  $(".we_row").removeClass("transparent");
}

/* From http://beski.wordpress.com/2009/04/21/scroll-effect-with-local-anchors-jquery/ */
function smoothScroll() {
	$(".scroll").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
	});
}

function positionExtractLinks() {
  $("#extracts a.more").each(function() {
    var jqLink = $(this);
    var jqPreviousBox = jqLink.prev(".expanded_content");
    jqPreviousBox.append(jqLink);
  });
  $("#extracts a.more").click(function() {
    $(this).closest(".expanded_content").addClass("fullheight");
    $(this).fadeOut("normal");
    return false;
  });
}

/* Google RSS feed reader */

google.load("feeds", "1");

function initializeInABit() {
  setTimeout("initialize()", 2000);
}

function initialize() {
  var feed = new google.feeds.Feed("http://a3reports.wordpress.com/feed/");
  feed.setNumEntries(1); // Just the latest post
  feed.load(function(result) {
    if (!result.error) {
      var container = document.getElementById("feed");
      var entry = result.feed.entries[0];
      $("#a3reports_title").text(entry.title);
      $("#a3reports_post").html(entry.content);
      $("#a3reports_title").closest("a").attr("href",entry.link);
    }
  });
  feed = new google.feeds.Feed("http://blog.agilebase.co.uk/feed/");
  feed.setNumEntries(1); // Just the latest post
  feed.load(function(result) {
    if (!result.error) {
      var container = document.getElementById("feed");
      var entry = result.feed.entries[0];
      $("#agilebase_title").text(entry.title);
      $("#agilebase_post").html(entry.content);
      $("#agilebase_title").closest("a").attr("href",entry.link);
    }
  });
  feed = new google.feeds.Feed("http://leanitforsmes.wordpress.com/feed/");
  feed.setNumEntries(1); // Just the latest post
  feed.load(function(result) {
    if (!result.error) {
      var container = document.getElementById("feed");
      var entry = result.feed.entries[0];
      $("#leanit_title").text(entry.title);
      $("#leanit_post").html(entry.content);
      $("#leanit_title").closest("a").attr("href",entry.link);
    }
  });
}
google.setOnLoadCallback(initializeInABit);

