//Get the current url anchor
var video = jQuery.url.param('v');

if(!video){
  video = "ZglgXeH1H8k"; //If there is no url anchor
}

//Setup the youtube video player
swfobject.embedSWF("http://www.youtube.com/v/"+video+"?enablejsapi=1&playerapiid=videoplayer&rel=0",
  "videoplayer", "642", "388", "8", null, null, {
    allowScriptAccess: "always",
    wmode:"transparent"
  }, {
    id: "videoplayer"
  });

function setVideoShareLinks(shareTitle, shareDesc, shareUrl){

  emailShareLink = "mailto:?subject=The Blue Sweater by Jacqueline Novogratz&body=Hi,%0A%0AHave you read The Blue Sweater? It's Jacqueline Novogratz's personal memoir about her journey from international banker to founder of Acumen Fund, an organization that builds businesses providing critical goods and services to the world's poor.%0A%0AWatch this video to learn more:%0A%0A"+shareUrl+"%0A%0AJacqueline will donate her paperback royalties to Acumen Fund and other organizations tackling social change."; 
  fbShareLink = "http://www.facebook.com/sharer.php?u="+shareUrl+"&t="+shareTitle;
  suShareLink = "http://www.stumbleupon.com/submit?url="+shareUrl+"&title="+shareTitle;
  diggShareLink = "http://digg.com/submit?url="+shareUrl+"&title="+shareTitle+"&bodytext="+shareDesc;
  twitterShareLink = "http://twitter.com/home?status=Read The Blue Sweater - Bridging the gap between the rich and poor in an interconnected world: "+shareUrl;

  $('#mailVideoShare').attr('href', emailShareLink);
  $('#fbVideoShare').attr('href', fbShareLink);
  $('#twitterVideoShare').attr('href', twitterShareLink);
  $('#suVideoShare').attr('href', suShareLink);
  $('#diggVideoShare').attr('href', diggShareLink);
  $('#permVideoShare').attr('href', shareUrl);
}  

//DOM is ready
google.setOnLoadCallback(function() {
																	
	var shareTitle = $('meta[name=title]').attr('content');
  var shareDesc = $('meta[name=description]').attr('content');
  var shareUrl = window.location.href;

  setVideoShareLinks(shareTitle, shareDesc, shareUrl);

  //Sets up the correct video
  $("#videoList a").each(function(){
    jQuery.url.setUrl($(this).attr('href'));

  });
																	
//make entire div on Our Work page clickable
	$(".videoBox").click(function(){
	  var videoUrl = $(this).find("a").attr("href");
		$("#videoList .selected").removeClass("selected");
    $(this).addClass("selected");

    var ytplayer = $("#videoplayer").get(0);

    ytplayer.loadVideoByUrl(videoUrl);
    ytplayer.playVideo();
		
		jQuery.url.setUrl(window.location.href);
		url = jQuery.url.attr("protocol")+"://"+jQuery.url.attr("host")+jQuery.url.attr("path");
		
		param  = jQuery.url.setUrl($(this).children('a').attr('href')).segment(1);
		
		setVideoShareLinks(shareTitle, shareDesc, url+'?'+'v='+param);

    return false;

	});


  $('.videoBox').hoverIntent(
    function(){
      var sumHeight = 0;
      
      childDiv = $(this).children('div').get(0);

      $(childDiv).children().each(function(){
        sumHeight += $(this).height();
      });

      $(childDiv).animate({height:sumHeight}, 300);
    },
    function(){
      $(childDiv).animate({height:15}, 300);
    }
  );
	
	$("#aboutAuthor").colorbox({
		speed:500,
		transition:"none",
		inline:true,
		href:"#authorPopup"
	});

	$("#aboutBook").colorbox({
		speed:500,
		transition:"none",
		inline:true,
		href:"#bookPopup"
	});

	$("#preview").colorbox({
		speed:500,
		transition:"none",
		inline:true,
		href:"#previewPopup"
	});

	$("#order").colorbox({
		speed:500,
		transition:"none",
		inline:true,
		href:"#buyPopup"
	});

	$("#included").colorbox({
		speed:500,
		transition:"none",
		inline:true,
		href:"#includedPopup"
	});
	
});