// USAGE: Set the rel attribute of any <a> equal to "colorbox" to load the colorbox for that link. 
// Add [your group] to group links into multiple colorbox groups. ie: <a rel="colorbox[group1]" ...
// Add the following attributes to be used by the colorbox: title, href, image(used for the splash image on a video), videowidth, videoheight
// If the Image attribute does not exist, this script will check for an IMG child and use the src attribute from that element. ie: <a><img src="This will be used as the splash image for video files"></a>

$(document).ready(function(){ 
  $("a[rel^=colorbox]").each(function(i,o){
    var FileExt = ($(this).attr("href").split(".")); FileExt = FileExt[FileExt.length-1].toLowerCase();
    if(FileExt=="mp4"||FileExt=="mp3"||FileExt=="mpg"||FileExt=="avi"||FileExt=="mov"||FileExt=="wmv"||FileExt=="flv"||FileExt=="m4v"||FileExt=="f4v"){
      var VideoURL = ($(this).attr("href"));
      if(VideoURL.indexOf("&")>=0) VideoURL = VideoURL.split("&")[0]; // &amp; in the video url will break the entire video player.
      var rel = ($(this).attr("rel"));
      var ImageURL = "";
      if(ImageURL=="" && $(this).attr("image")!=undefined) ImageURL = $(this).attr("image");
      if(ImageURL=="" && $(this).children("img").attr("src")!=undefined) ImageURL = $(this).children("img").attr("src");
      if(ImageURL.indexOf("&")>=0) ImageURL = ImageURL.split("&")[0]; // &amp; in the image url will break the entire video player.
      if(ImageURL.indexOf(".asp")>=0) ImageURL = ImageURL+="#.jpg"; // the video player detects the image file format from the file name
      var Width= ($(this).attr("videowidth")); if(Width==undefined) Width = 640;
      var Height = ($(this).attr("videoheight")); if(Height==undefined) Height = 480;
      $(document.body).append("<div style=\"display:none;\"><object id=\"colorbox-video-"+i+"\" style=\"width: "+Width+"px; height: "+Height+"px; \" type=\"application/x-shockwave-flash\" data=\"/app/media/swf/flowplayer-3.2.1.swf\"><param name=\"movie\" value=\"/app/media/swf/flowplayer-3.2.1.swf\"><param name=\"allowfullscreen\" value=\"true\"><param name=\"flashvars\" value=\"config={'playlist':["+((ImageURL!="")?"'"+ImageURL+"', ":"")+"{'url': '"+VideoURL+"','autoPlay':false,'autoBuffering':true}]}\"><img src=\""+ImageURL+"\" style=\"width: "+Width+"px; height: "+Height+"px; \" title=\"No video playback capabilities.\"></object></div>");
      $(this).colorbox({inline:true, href:"#colorbox-video-"+i});
    }else if($(this).is('[href*="youtube"]')){
      $(this).colorbox({iframe:true, innerWidth:425, innerHeight:344});
    }else{
      $(this).colorbox();
    }
  });
});

