var apiEndpoint = 'http://www.vimeo.com/api/v2/';
var oEmbedEndpoint = 'http://www.vimeo.com/api/oembed.json';
var oEmbedCallback = 'switchVideo';
var videosCallback = 'setupGallery';
var vimeoUsername = 'ncnewschools';
var videos = [];
var remaining = 3;
// Get the user's videos
$(document).ready(function() {
  remaining = 3;
  $.getScript(apiEndpoint + vimeoUsername + '/videos.json?page=1&callback=loadVideos');
  $.getScript(apiEndpoint + vimeoUsername + '/videos.json?page=2&callback=loadVideos');
  $.getScript(apiEndpoint + vimeoUsername + '/videos.json?page=3&callback=loadVideos');
});
function getVideo(url) {
  $.getScript(oEmbedEndpoint + '?url=' + url + '&width=504&callback=' + oEmbedCallback);
}
function loadVideos(v) {
  for (var i = 0; i < v.length; i++)
    videos.push(v[i]);
  remaining -= 1;
  if (remaining == 0)
    setupGallery(videos);
}
function setupGallery(videos) {
  // Add the videos to the gallery
  for (var i = 0; i < videos.length; i++) {
    var title = videos[i].title.split(' - ');
    var person = title[0];
    var school = title[1];
    var type = title[2];
    if(type == "Student Voices" || type == "Teacher Voices" || type == "Making a Difference") {
      if($('body').attr("class") == "students" && type == "Student Voices") {
        var html = '<li><div class="image"><img alt="' + type + ': ' + person + ' - ' + school + '" src="' + videos[i].thumbnail_medium + '"><a href="' + videos[i].url + '" class="play-btn-overlay">Play Button</span></a></div><div class="video-content"><h6><a href="' + videos[i].url + '" title="' + person + '">' + person + '</a><br>' + school + '</h6><p>' + videos[i].description + '</p><a href="' + videos[i].url + '" class="play-btn">Play This Video</a></div></li>';
        $('.video-list').append(html);
      }
      if($('body').attr("class") == "teachers" && type == "Teacher Voices") {
        var html = '<li><div class="image"><img alt="' + type + ': ' + person + ' - ' + school + '" src="' + videos[i].thumbnail_medium + '"><a href="' + videos[i].url + '" class="play-btn-overlay">Play Button</span></a></div><div class="video-content"><h6><a href="' + videos[i].url + '" title="' + person + '">' + person + '</a><br>' + school + '</h6><p>' + videos[i].description + '</p><a href="' + videos[i].url + '" class="play-btn">Play This Video</a></div></li>';
        $('.video-list').append(html);
      }
      if($('body').attr("class") == "making-a-difference" && type == "Making a Difference") {
        var html = '<li><div class="image"><img alt="' + type + ': ' + person + ' - ' + school + '" src="' + videos[i].thumbnail_medium + '"><a href="' + videos[i].url + '" class="play-btn-overlay">Play Button</span></a></div><div class="video-content"><h6><a href="' + videos[i].url + '" title="' + person + '">' + person + '</a><br>' + school + '</h6><p>' + videos[i].description + '</p><a href="' + videos[i].url + '" class="play-btn">Play This Video</a></div></li>';
        $('.video-list').append(html);
      }
      var ssize = $('#student-voices-module .body div').length;
      var tsize = $('#teacher-voices-module .body div').length;
      if(type == "Student Voices" && ssize != 2){
        var newhtml = '<div class="image"><img alt="' + type + ': ' + person + ' - ' + school + '" src="' + videos[i].thumbnail_large + '"><a href="' + videos[i].url + '" class="play-btn-overlay">Play Button</a><p><span>' + person + '</span>, ' + school + '</p></div>';
        $('#student-voices-module .body').append(newhtml);
      }

      if(type == "Teacher Voices" && tsize != 2){
        var newhtml = '<div class="image"><img alt="' + type + ': ' + person + ' - ' + school + '" src="' + videos[i].thumbnail_large + '"><a href="' + videos[i].url + '" class="play-btn-overlay">Play Button</a><p><span>' + person + '</span>, ' + school + '</p></div>';
        $('#teacher-voices-module .body').append(newhtml);
      }
    }
  }
  $('.video-list a, #teacher-voices-module .body .image a, #student-voices-module .body .image a').click(function(event) {
    var topParent = $(this).parent().parent().parent().attr("class");
    if(topParent == "secondary-content") {
      var parent = $(this).parents(".body").parent().attr("id").replace("-voices-module","").replace(/^\w/, function($0) { return $0.toUpperCase(); });
      $("#lb-container").html("<span class=\"close\"></span><span id=\"embed\"></span><a href=\"/our-schools/"+parent.toLowerCase()+"s\" class=\"more-videos\">View more "+parent+" Voices videos</a>");
      $('#lb-container .close').click(function () {
        $('#lightbox').hide();
      });
    } else {
    }
    event.preventDefault();
    getVideo(this.href);
    return false;
  });
}
function switchVideo(video) {
  $("#lightbox").show();
  $("#lightbox").height($(document).height());
  $('#lb-container').css("position","absolute");
  $('#lb-container').css("top", ( $(window).height() - $('#lb-container').height() ) / 4+$(window).scrollTop() + "px");
  $('#lb-container').css("left", ( $(window).width() - $('#lb-container').width() ) / 2+$(window).scrollLeft() + "px");
  $('#embed').html(unescape(video.html.replace(/height="(\d+)"/, 'height="280"')));
  $("#embed param").each(function(){ $(this).attr("value",$(this).attr("value").replace("show_byline=1","show_byline=0").replace("show_title=1","show_title=0")); });
}
