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

if(!video){
  video = "xCcgHcoxvnQ"; //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=An even better approach to tackling poverty&body=Hi,%0AI just learned about this interesting, new approach to tackling poverty that actually makes a lasting difference.%0AIf you are considering supporting a charity this season, Acumen Fund seems like a worthwhile cause.%0AWatch this video "+shareUrl+" - and let me know what you think."; 
  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=Want to do good this season? Why not do even better? Here’s how: "+shareUrl+". Please RT";

  $('#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);

  //Carousel
  $("#quoteCarousel").jCarouselLite({
    btnNext: ".next",
    btnPrev: ".prev",
    vertical: true,
    visible: 1,
    auto: 15000,
    speed: 1000,
    easing: "easeOutQuart"
  });

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

    if(video == jQuery.url.segment(1)){
      $('#videoList .selected').removeClass('selected');
      $(this).parent().addClass('selected');
    }
  });

  $("#videoList a").click(function(){

    $("#videoList .selected").removeClass("selected");
    $(this).parent().addClass("selected");

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

    ytplayer.loadVideoByUrl($(this).attr('href'));
    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).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);
    }
    );

});
