$(document).ready(function()
{
  var origTitle = document.title;
  var resizeTimer = null;
  var operaTimer = null;

  $("a[rel*=capture]").each(

    function() { $(this).overlay(
    {
      oneInstance: true,
      api: true,
      left: "center",

      expose: {
        api: true,
        color: '#000',
        loadSpeed: 200,
        opacity: 0.3,
        maskId: 'exposediv'+($(this).attr('rel')).replace('#','')
      },

      onBeforeLoad: function() {
      },

      onLoad: function(content) {
        document.title = origTitle;
      },

      onClose: function(content) {
        $f().unload();
        document.title = origTitle;
      }
    });

  });
	
  // 

  $("a[rel*=overlay]").each(

    function() { $(this).overlay(
    {
      oneInstance: true,
      api: true,
      left: "center",
	  top: "-2000",
      

      expose: {
        api: true,
        color: '#000',
        loadSpeed: 200,
        opacity: 0.3,
        maskId: 'exposediv'+($(this).attr('rel')).replace('#','')
      },

      onBeforeLoad: function() {
        adjustOverlay($(window).width(), $(window).height());
        this.getOverlay().find(".player").flowplayer(0).load();
      },

      onLoad: function(content) {
        document.title = origTitle;
      },

      onClose: function(content) {
        $f().unload();
        document.title = origTitle;
      }
    });

  });
  
  $(".player").flowplayer("linked/flash/flowplayer-3.1.5.swf", {
    clip:{
      scaling: 'orig',
      autoBuffering: true,
      onMetaData:function(clip)
      {
        this.setVolume(100);

        $("[id*=exposediv]").height($(document).height());
        $("[id*=exposediv]").width($(window).width());

        adjustOverlay(clip.metaData.width, clip.metaData.height);

        $(window).bind('resize', function() {
            if (resizeTimer) clearTimeout(resizeTimer);
            resizeTimer = setTimeout(function() { adjustOverlay(clip.metaData.width, clip.metaData.height); }, 100);
        });
      }
    },
    plugins: {
      controls: {
        url: 'linked/flash/flowplayer.controls-3.1.5.swf',

        autoHide: 'always',

        play:true,
        volume:true,
        mute:true,
        time:false,
        stop:true,
        playlist:false,
        fullscreen:true,

        scrubber: true,

        height: 22,
        bottom: 0
      }
    }
  }); // initialize flowplayer

  function adjustOverlay(currentClipWidth, currentClipHeight)
  {
    var clipwidth  = currentClipWidth;
    var clipheight = currentClipHeight;

    var overlayPadding = parseInt($(".overlay").css('padding-left'))*2;
    var overlayBorder  = parseInt($(".overlay").css('borderLeftWidth'))*2;
    if(isNaN(overlayBorder)) {
      overlayBorder  = parseInt($(".overlay").css('border-width-left'))*2;
    }

    var fullSizePlayerWidth  = clipwidth+overlayPadding+overlayBorder;
    var fullSizePlayerHeight = clipheight+overlayPadding+overlayBorder;

    var posLeft = 0;
    var posTop  = 0;

    var newClipwidth  = 0;
    var newClipheight = 0;
    var oldToNewRatio = 1;

    if(fullSizePlayerWidth > $(window).width())
    {
      newClipwidth  = $(window).width() - (overlayPadding+overlayBorder);
      oldToNewRatio = newClipwidth / clipwidth;

      clipwidth  = newClipwidth;
      clipheight = Math.floor(clipheight * oldToNewRatio);

      fullSizePlayerWidth  = clipwidth+overlayPadding+overlayBorder;
      fullSizePlayerHeight = clipheight+overlayPadding+overlayBorder;
    }

    if(fullSizePlayerHeight > $(window).height())
    {
      newClipheight = $(window).height() - (overlayPadding+overlayBorder);
      oldToNewRatio = newClipheight / clipheight;

      clipheight = newClipheight;
      clipwidth  = Math.floor(clipwidth * oldToNewRatio);

      fullSizePlayerWidth  = clipwidth+overlayPadding+overlayBorder;
      fullSizePlayerHeight = clipheight+overlayPadding+overlayBorder;
    }

    posLeft = parseInt(($(window).width()-fullSizePlayerWidth)/2);
    posTop  = parseInt(($(window).height()-fullSizePlayerHeight)/2);
	
    $(".overlay").css('left', (posLeft >= 0) ? $(window).scrollLeft()+posLeft : $(window).scrollLeft());
    $(".overlay").css('top', (posTop >= 0) ? posTop : 25);

    $(".overlay").width(clipwidth);
    $(".overlay").height(clipheight);

    if(!window.opera)
    {
      $("object[id*=fp_]").attr('height', clipheight);
      $("object[id*=fp_]").attr('width', clipwidth);
    }
    else
    {
      if (operaTimer)
        clearTimeout(operaTimer);

      operaTimer = setTimeout(function() { $(".overlay").width(clipwidth+2); $(".overlay").height(clipheight+2); }, 100);
    }
  } // function adjustOverlay
});

