$(document).ready(function() {
   prepJQueryMedia();
   prepVideos();
   prepImages();
   prepAudio();
});

function prepJQueryMedia() {
   $.blockUI.defaults['overlayCSS'] = { backgroundColor: '#000000', opacity: '0.5' };
   $.fn.media.defaults['flvPlayer'] = '/elements/swf/mediaplayer.swf'
   $.fn.media.defaults['mp3Player'] = '/elements/swf/mediaplayer.swf'
   $.fn.media.defaults['flashVersion'] = '8',
   
   $.extend($.fn.media.defaults['flashvars'], {
      autostart: true,
      usefullscreen: false,
      width: 640,
      height: 500
   });
}

function prepVideos() {
   var videos = $('a.multimedia-video');
   for (var i = 0; i < videos.length; i++) {
      var video = $(videos[i]);
      // get the caption
      var caption = video.siblings('div.caption');
      if (caption.length)
         caption = caption[0].innerHTML;
      else
         caption = '';
      // initialize the media box
      video.mediabox({
         width: 640,
         height: 500,
         boxTitle: '<div class="title">Press Esc or click outside this box to close the video.</div>',
         caption: '<div class="caption">' + caption + "</div>"
      });
   }
}

function prepImages() {
   var images = $('a.multimedia-image');
   for (var i = 0; i < images.length; i++) {
      var image = $(images[i]);
      // get the caption (blockUI content)
      var caption = image.siblings('div.caption');
      if (caption.length)
         caption = caption[0].innerHTML;
      else
         caption = '';
      // this closure returns a function to serve as an onclick handler for the image link
      function showImageGen(image, caption) {
         function f() {
            var text = '<div class="multimedia-image">';
            text += '<div class="title">Press Esc or click outside this box to close the image.</div>';
            text += '<div class="caption">' + caption + '</div>';
            text += '<img src="' + $(this).attr('href') + '" />';
            $(text).appendTo('body').displayBox();
            return false;
         }
         return f;
      }
      image.click(showImageGen(image, caption));
   }
}

function prepAudio() {
      /*
   var audio = $('a.multimedia-audio');
   // For each MP3 link:
   for (var i = 0; i < audio.length; i++) {
      var mp3 = $(audio[i]);
      // get the caption
      var caption = mp3.siblings('div.caption');
      if (caption.length)
         caption = caption[0].innerHTML;
      else
         caption = '';
      // initialize the media box
      mp3.mediabox({
         width: 640,
         height: 20,
         boxTitle: '<div class="title">Press Esc or click outside this box to close the audio player.</div>',
         caption: '<div class="caption">' + caption + "</div>",
         flashvars: {
            height: 20
         }
      });
   }
   */
}

