var Showcase = {

  ajaxifyPagination: function() {
    showcase = new Object;
    window.showcase = showcase;
    document.observe("dom:loaded", function() {
      var container = $(document.body)
    
      if (container) {

        container.observe('click', function(e) {
          var el = e.element()
          if (el.match('.pagination a')) {
            e.stop();
            window.showcase.partialLoaded = false;
            window.showcase.playerHeight = parseInt($$("object")[0].height);
            window.showcase.contentWrapperHeight = $$(".vBoxContent").first().getHeight() - 20;
            $$(".vBoxContent").first().style.height = window.showcase.contentWrapperHeight + "px";
            $("videosContainer").style.height = $("videosContainer").getHeight() + "px";
            var loadAjax = false
            if (window.ie){
              Effect.multiple($$(".showcaseUnit"), Effect.Fade,{afterFinish: function() {
                $("videosContainer").hide();
                $$(".paginationLoading").first().show();
                loadAjax = true
              }});
              var ajaxInterval = setInterval(function() {
                if (loadAjax){
                clearInterval(ajaxInterval);
                new Ajax.Request(el.href, { method: 'get', onSuccess: function() {
                  Showcase.paginationShowVideo();       
                }});
                }}, 100);
            }
            else{
              $("videosContainer").fade({afterFinish: function() {
                $$(".paginationLoading").first().show();
                new Ajax.Request(el.href, { method: 'get', onSuccess: function() {
                  Showcase.paginationShowVideo();       
                }});
              }})
             }
          }
        })

      }
    })
  },

  paginationShowVideo: function() {

    var interval = setInterval(function() {
        if (window.showcase.partialLoaded) {
          clearInterval(interval);
          var contentHeight = (window.showcase.contentWrapperHeight + parseInt($$("object")[0].height) - window.showcase.playerHeight) + "px";
          $$(".vBoxContent").first().morph("height: " + contentHeight, {afterFinish: function() {
            $$(".paginationLoading").first().hide();
            if (window.ie){
              $$("#videosContainer").first().immediateDescendants().each(function(s) {s.hide();});
              $("videosContainer").show();
              Effect.multiple($$(".showcaseUnit"), Effect.Appear,{afterFinish: function() {
               $$("#videosContainer").first().immediateDescendants().each(function(s) {s.show();});
                }});
            }
            else
              $("videosContainer").appear();}});
          }
      }, 200);
  },

  inPageShowVideo: function(current) {
    window.showcase.partialLoaded = false
    window.showcase.loading = true
    window.showcase.playerHeight = parseInt($$("object")[0].height);
    window.showcase.contentWrapperHeight = $$(".vBoxContent").first().getHeight() - 20;
    window.showcase.playerWrapperHeight = $("showcasePlayerBox").getHeight() - 32;
    Showcase.highlightUnit(current);
    $("showcasePlayerBox").style.height = window.showcase.playerWrapperHeight + "px";
    $('videoPlayer').fade();
    var interval = setInterval(function() {
        if(window.showcase.partialLoaded){ 
          clearInterval(interval);
          $$('object').first().hide();
          $$('.encodedAt').first().hide();
          $$('.inPageLoading').first().show();
          var interval2 = setInterval(function() {
            if (Showcase.videoLoaded()){
              clearInterval(interval2);
              var contentHeight = (window.showcase.contentWrapperHeight + parseInt($$("object")[0].height) - window.showcase.playerHeight) + "px";
              var playerHeightNew = (window.showcase.playerWrapperHeight + parseInt($$("object")[0].height) - window.showcase.playerHeight) + "px";
              $("showcasePlayerBox").morph("height: " + playerHeightNew);
              $$(".vBoxContent").first().morph("height: " + contentHeight, {afterFinish: function() {
                $$('.inPageLoading').first().hide();
                $$('object').first().appear();
                $$('.encodedAt').first().appear();
                window.showcase.loading = false
              }});
            }
          }, 100);
        }}, 100)
  },

  videoLoaded: function() {
    var video = $$("object").first();
    return video.height == video.getHeight();
  },

  highlightUnit: function(current) {
    $$(".showcaseUnit").each(function(shUnit) {
      unit = shUnit.childNodes[1];
      currentItem = current.parentNode;
      if (unit == currentItem){
	    
        unit.className = "selected";}

      else{
        unit.className = "content";}

    });
  }

}
