///
/// functions related to videos
///

////
//// configuration
////

// prefix to all file parameters (if they all come from the same stream or server,
// put it here rather than in the array below)
var video_fileprefix = "";
// prefix to image parameters
var video_imageprefix = "";

// location of popup HTML file
var video_ourpopurl = "/ki/news/videopop.html";
// when using our own popup HTML file, add these to the video size to get
// the window size (to account for video player controls, borders, etc.)
var video_chromeheight = 20;
var video_chromewidth = 0;

////
//// Array used to associate video "index" with parameters.
////
// Example: from videos_2003.html we link to video.html?vid=2003_lecture01;
// then in video.html, we look up and find that vid 2003_lecture01 corresponds
// to file chemistrylecture.flv.
//
// Important: for RTMP streams, the file is the stream, and the id is the
// filename without the .flv - example:
// videodata["2006_austin"] =    { file:"rtmp://18.39.0.27/streams/",  id:"sept-1-austin-26jun2006_SEPT_VP6_384K_480x360_Strm"};
// For ordinary non-stream videos, the file contains the .flv file URL, and id is blank.
//
//
// Each element in the video_data array has the following properties:
//    file:   the URL of the .flv file, or the stream (will be prefixed by video_fileprefix)
//    image:  the URL of the preview image (will be prefixed by video_imageprefix)(optional)
//    id:     for streaming videos, the stream id; for files, blank
//    width, height: size of our video
//    popurl: a URL to open in the popup window, rather than our own video.html (can be used with TechTV, for example)
//    popwidth, popheight: window size for popurl popup window
//
var video_data = new Array();

// groundbreaking
video_data["groundbreaking_mead"] =        { file:"http://www.viddler.com/explore/mittechtv/videos/723.flv", id:"", width:400, height: 225 };
video_data["groundbreaking_hockfield"] =   { file:"http://www.viddler.com/explore/mittechtv/videos/827.flv", id:"", width:400, height: 225 };
video_data["groundbreaking_jacks"] =       { file:"http://www.viddler.com/explore/mittechtv/videos/807.flv", id:"", width:400, height: 225 };
video_data["groundbreaking_niederhuber"] = { file:"http://www.viddler.com/explore/mittechtv/videos/787.flv", id:"", width:400, height: 225 };
video_data["groundbreaking_koch"] =        { file:"http://www.viddler.com/explore/mittechtv/videos/770.flv", id:"", width:400, height: 225 };
video_data["groundbreaking_ceremony"] =    { file:"http://www.viddler.com/explore/mittechtv/videos/563.flv", id:"", width:400, height: 225 };

// media day 2008
video_data["mediaday2008_ki"] =         { file:"http://www.viddler.com/explore/mittechtv/videos/879.flv", id:"", width:320, height:200 };
video_data["mediaday2008_drugs"] =      { file:"http://www.viddler.com/explore/mittechtv/videos/901.flv", id:"", width:320, height:200 };
video_data["mediaday2008_wiring"] =     { file:"http://www.viddler.com/explore/mittechtv/videos/917.flv", id:"", width:320, height:200 };
video_data["mediaday2008_bigimpact"] =  { file:"http://www.viddler.com/explore/mittechtv/videos/944.flv", id:"", width:320, height:200 };
video_data["mediaday2008_immune"] =     { file:"http://www.viddler.com/explore/mittechtv/videos/887.flv", id:"", width:320, height:200 };
video_data["mediaday2008_qa"] =         { file:"http://www.viddler.com/explore/mittechtv/videos/877.flv", id:"", width:320, height:200 };

// symposium 2008
video_data["symposium2008_hockfield"]      = { file:"http://www.viddler.com/explore/mittechtv/videos/792.flv", id:"", width:320, height:240 };
video_data["symposium2008_jacks1"]         = { file:"http://www.viddler.com/explore/mittechtv/videos/738.flv", id:"", width:320, height:240 };
video_data["symposium2008_grodzinski"]     = { file:"http://www.viddler.com/explore/mittechtv/videos/832.flv", id:"", width:320, height:240 };
video_data["symposium2008_weissleder"]     = { file:"http://www.viddler.com/explore/mittechtv/videos/891.flv", id:"", width:320, height:240 };
video_data["symposium2008_hammond"]        = { file:"http://www.viddler.com/explore/mittechtv/videos/873.flv", id:"", width:320, height:240 };
video_data["symposium2008_richardskortum"] = { file:"http://www.viddler.com/explore/mittechtv/videos/834.flv", id:"", width:320, height:240 };
video_data["symposium2008_heath"]          = { file:"http://www.viddler.com/explore/mittechtv/videos/905.flv", id:"", width:320, height:240 };
video_data["symposium2008_jacks2"]         = { file:"http://www.viddler.com/explore/mittechtv/videos/376.flv", id:"", width:320, height:240 };

// symposium 2009
video_data["symposium2009_jackshynes"] = { file:"http://www.viddler.com/explore/mittechtv/videos/6684.flv", id:"", width:320, height:240 };
video_data["symposium2009_gertler"]    = { file:"http://www.viddler.com/explore/mittechtv/videos/6683.flv", id:"", width:320, height:240 };
video_data["symposium2009_morrison"]   = { file:"http://www.viddler.com/explore/mittechtv/videos/6682.flv", id:"", width:320, height:240 };
video_data["symposium2009_chambers"]   = { file:"http://www.viddler.com/explore/mittechtv/videos/6681.flv", id:"", width:320, height:240 };
video_data["symposium2009_weinberg"]   = { file:"http://www.viddler.com/explore/mittechtv/videos/6680.flv", id:"", width:320, height:240 };
video_data["symposium2009_polyak"]     = { file:"http://www.viddler.com/explore/mittechtv/videos/6679.flv", id:"", width:320, height:240 };
video_data["symposium2009_wang"]       = { file:"http://www.viddler.com/explore/mittechtv/videos/6678.flv", id:"", width:320, height:240 };
video_data["symposium2009_massague"]   = { file:"http://www.viddler.com/explore/mittechtv/videos/6677.flv", id:"", width:320, height:240 };
video_data["symposium2009_jacks"]      = { file:"http://www.viddler.com/explore/mittechtv/videos/6676.flv", id:"", width:320, height:240 };


//
// generalized code to parse get parameters
// results placed in array getparameters[]
// (indices are paremeters, values are parameter values)
//
var getparameters = new Array();
function parse_searchstring() {
  // get search string
  var ss = location.search;
  // remove initial "?" if any
  ss = ss.substring(ss.lastIndexOf("?")+1,ss.length);
  var ssa = ss.split('&');
  for (var i=0; i < ssa.length; i++) {
    var ssa2 = ssa[i].split('=');
    if (ssa2.length == 2) {
      getparameters[decodeURIComponent(ssa2[0])] = decodeURIComponent(ssa2[1]);
      //alert("parameter "+ssa2[0]+", value "+ssa2[1]); // debugging
    }
  }
}
parse_searchstring();

//
// return "file" parameter for video player
// (for rtmp, this is the stream; for progressive downloads, this is the path to the flv)
// called within video.html
//
function getvideofile() {
  var videofile = "";
  var vid = getparameters["vid"];
  if (typeof video_data[vid] == "object") {
    videofile = video_fileprefix + video_data[vid].file;
  }
  //alert(videofile); // debugging
  return videofile;
}
//
// return "image" parameter for video player
// this is a preview image of the video
// called within video.html
//
function getvideoimage() {
  var videoimage = "";
  var vid = getparameters["vid"];
  if (typeof video_data[vid] == "object") {
    videoimage = video_imageprefix+video_data[vid].image;
  }
  //alert(videoimage); // debugging
  return videoimage;
}
//
// return "id" parameter for video player
// (for rtmp, this is the file, minus the .flv extension; for progressive downloads, this is usually blank)
// called within video.html
//
function getvideoid() {
  var videoid = "";
  var vid = getparameters["vid"];
  if (typeof video_data[vid] == "object") {
    videoid = video_data[vid].id;
  }
  //alert(videoid); // debugging
  return videoid;
}

//
// return "type" parameter for video player
// (makes a reasonable guess; if the id is not blank, returns "rtmp", otherwise returns "flv")
// called within video.html
//
function getvideotype() {
  var videotype = "";
  var vid = getparameters["vid"];
  if (typeof video_data[vid] == "object") {
    videotype = (video_data[vid].id == "" ? "flv" : "rtmp");
  }
  //alert(videotype); // debugging
  return videotype;
}
//
// return video width and height
// called within video.html
//
function getvideowidth() {
  var width = 640;
  var vid = getparameters["vid"];
  if (typeof video_data[vid] == "object") {
    width = video_data[vid].width+video_chromewidth;
  }
  return width;
}
function getvideoheight() {
  var height = 500;
  var vid = getparameters["vid"];
  if (typeof video_data[vid] == "object") {
    height = video_data[vid].height+video_chromeheight;
  }
  return height;
}
//
// open video page in popup - called from page listing videos
//
function openvideopopup(vid) {
  if (video_data[vid].popurl) {
    if (window.open(video_data[vid].popurl,"","width="+(video_data[vid].popwidth)+",height="+(video_data[vid].popheight)+",toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes"))
      return false;
    else
      return true;
  }
  else {
    if (window.open(video_ourpopurl+"?vid="+vid,"","width="+(video_data[vid].width+video_chromewidth)+",height="+(video_data[vid].height+video_chromeheight)+",toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes"))
      return false;
    else
      return true;
  }
}
