﻿$(function () {
  var thePlayerDefaultHtml = "<p>Flash content: video player.<br \/><br \/>You need to upgrade your Flash Player to view this. <a href=\"http:\/\/get.adobe.com\/flashplayer\/\">Get Adobe Flash Player<\/a><\/p>";

  function loadPlayer(theFile, theContainer, thePlayer) {
    // Initialize the player content
    $("#" + theContainer).html('<div id="' + thePlayer + '">' + thePlayerDefaultHtml + '</div>');

    // Start the player.  
    swfobject.embedSWF(theFile, thePlayer, "320", "162", "9.0.0", "",
                              { autostart: "true", screencolor: "#000000" },
                              { allowfullscreen: "true", allowscriptaccess: "always", wmode: "transparent", bgcolor: "#000000" },
                              {});

    // Set value for selected file (for email).
    $('#selectedMediaPath input').val(theFile);
    $('#emailThis').attr('href', theFile);
  };

  // Set click event for playlist
  $("#medialist a.thumb").click(function () {
    // Set active item
    $("#medialist a.thumb").removeClass("active");
    $(this).removeClass("active").addClass("active");

    // Load the video
    //swfobject.removeSWF(thePlayerID);
    loadPlayer($(this).attr("href"), "mediaplayerContainer", "mediaplayer");

    return false;
  });

  // Load a video into the player
  if (typeof (qPageUrlParams) != 'undefined' && typeof (qPageUrlParams.media) != 'undefined' && qPageUrlParams.media != '') {
    // If the video URL was passed in through a querystring, then use that
    // (need to include getUrlParams.js on the page that calls this control).
    $("#medialist a.thumb[href$='" + qPageUrlParams.media + "']").click();
  } else {
    // Otherwise load the first video in the list
    $("#medialist tr:first-child a.thumb").click();
  };

  // Set click events for email link
  $("#emailThis").click(function () {
    $("#emailThisVideoBlock").slideDown();
    return false;
  });
  $("#emailThisBlock a.emailThisVideoHeader").click(function () {
    $("#emailThisVideoBlock").slideUp();
    return false;
  });
});

