(function() {
  
  var base_path = "http://creative.myspacecdn.com/design/myspace/circleOf8/";
  
  var Femtoverlay=(function(d){var c={resize:function(){var e=(window.innerHeight&&window.scrollMaxY)?(window.innerHeight+window.scrollMaxY):((document.body.scrollHeight>document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight);var f=d(document).height();d("#lightbox_overlay").height(e>f?e:f)},reposition:function(e){d("#femtocontent").css({width:e.x+"px",height:e.y+"px",marginLeft:(-e.x/2)+"px",marginTop:(d(document).scrollTop()+(-e.y/2))+"px"})},keydown:function(e){if(e.keyCode===27){Femtoverlay.close()}},show:function(e){d("#lightbox_overlay").fadeTo(500,0.67,e)},hide:function(e){d("#lightbox_overlay, #femtocontent").fadeTo(500,0,function(){d(this).remove();e()})}};var a=false;var b={};return{open:function(e){e=e||{};b=e;if(!d("#lightbox_overlay").length){d("body").append('<div id="lightbox_overlay"></div><div id="femtocontent"></div>');d("#lightbox_overlay").css({position:"absolute",left:0,top:0,width:"100%",backgroundColor:"#000",opacity:0,zIndex:60000,display:"block"}).click(Femtoverlay.close);d("#femtocontent").css({position:"absolute",top:"50%",left:"50%",overflow:"hidden",zIndex:60006,width:64,height:64,marginTop:-32,marginLeft:-32});if(!a){d(window).resize(c.resize);d(document).keydown(c.keydown);a=true}c.resize()}if(e.onCreate){e.onCreate()}c.show(e.onOpen||function(){})},close:function(){c.hide(b.onClose||function(){})},reposition:function(e){c.reposition(e)}}})(jQuery);
  
  window.ehdvpapi = (function(global){    
    global.is_video_player_loaded = false;
    global.is_video_selector_loaded = false;
    
    global.refreshLeaderboard = function() { swfobject.embedSWF(base_path + "swf/leaderboard.swf", "trivia_leaderboard_flash", "940", "138", "9.0.115", base_path + "swf/expressInstall.swf", {},  {base: base_path + 'swf/', bgcolor: '#000000', wmode: 'opaque', allowFullScreen: 'true', allowScriptAccess: 'always', id: 'trivia_leaderboard_flash'}, {}); };
    global.video_player_loaded = function(){ global.is_video_player_loaded = true; };
    global.video_selector_loaded = function(){ global.is_video_selector_loaded = true; };
    
    return {
      refreshLeaderboard: global.refreshLeaderboard,
      video_player_loaded: global.video_player_loaded,
      video_selector_loaded: global.video_selector_loaded
    };
  })(window);
  
  (function($) {
    var fn = {
      fixIE6quirks: function() {
        $('#top_tabs li a:not(a[target])').each(function() {
          $(this).addClass($(this).attr('href').replace('#', ''));
        });
      },
      
      switchContent: function(section) {
        $('#top_tabs li').filter('.selected').removeClass('selected').end().filter(':has(a[href=' + section + '])').addClass('selected');
        
        switch(section) {
          case '#game':
            $('#dyn_content_area').html('<div id="video_player"><div id="trivia_game_cont"></div></div><div id="trivia_leaderboard"><div id="trivia_leaderboard_flash"></div></div>');
            swfobject.embedSWF(base_path + "swf/trivia-loader.swf", "trivia_game_cont", "940", "529", "9.0.115", base_path + "swf/expressInstall.swf", {},  {base: base_path + 'swf/', bgcolor: '#000000', wmode: 'opaque', allowFullScreen: 'true', allowScriptAccess: 'always'}, {});
            $('#video_player').css({borderStyle: 'solid none none none'});
            window.refreshLeaderboard();
            break;
          case '#extras':
            $('#dyn_content_area').html('<div id="video_player"><div id="video_player_player"></div></div><div id="episode_selector"><div id="episode_selector_selector"></div></div>');
            swfobject.embedSWF(base_path + "swf/EHDVP.swf", "video_player_player", "940", "529", "9.0.115", base_path +"swf/expressInstall.swf", {},  {base: base_path + 'swf/', bgcolor: '#000000', wmode: 'opaque', allowFullScreen: 'true', allowScriptAccess: 'always'}, {});
            swfobject.embedSWF(base_path + "swf/extras-selector.swf", "episode_selector_selector", "940", "138", "9.0.115", base_path +"swf/expressInstall.swf", {},  {base: base_path + 'xml/', bgcolor: '#000000', wmode: 'opaque', allowScriptAccess: 'always'}, {});
            break;
          case '#index':
          default:
            $('#dyn_content_area').html('<div id="video_player"><div id="video_player_player"></div></div><div id="episode_selector"><div id="episode_selector_selector"></div></div>');
            $('#episode_selector').css({height: '235'});
            swfobject.embedSWF(base_path + "swf/EHDVP.swf", "video_player_player", "940", "529", "9.0.115", base_path +"swf/expressInstall.swf", {},  {base: base_path + 'swf/', bgcolor: '#000000', wmode: 'opaque', allowFullScreen: 'true', allowScriptAccess: 'always'}, {});
            swfobject.embedSWF(base_path + "swf/video-selector.swf", "episode_selector_selector", "940", "230", "9.0.115", base_path +"swf/expressInstall.swf", {},  {base: base_path + 'xml/', bgcolor: '#000000', wmode: 'window', allowScriptAccess: 'always'}, {});
        }
      }
    };
    
    $(document).ready(function() {
      fn.switchContent(document.location.hash || '#index');
      fn.fixIE6quirks();
      
      $('#grab_widget').click(function(e) {
        Femtoverlay.open({
          onCreate: function() {
            $('#femtocontent').html('<div id="flash_content"></div><span id="overlay_close"></span>')
                              .css({border: '1px solid #222'});
            Femtoverlay.reposition({x: 340, y: 450});
            $('#overlay_close').css({
              position: 'absolute',
              background: 'url(' + base_path + 'images/buttons/overlay_close.png)',
              top: ($('#femtocontent').offset().top - 18) + 'px',
              left: ($('#femtocontent').offset().left + 340 - 65) + 'px',
              cursor: 'pointer',
              width: '67px',
              height: '19px',
              zIndex: '70507',
              overflow: 'hidden'
            }).click(Femtoverlay.close)
              .mouseover(function(){ $(this).css({backgroundPosition: 'left bottom'}); })
              .mouseout(function(){ $(this).css({backgroundPosition: 'left top'}); });
            document.body.appendChild(document.getElementById('overlay_close'));
            try { swfobject.getObjectById('video_player_player').pause(); } catch(err) {}
            swfobject.embedSWF(base_path + "swf/widget.swf", "flash_content", "340", "450", "9.0.115", base_path + "swf/expressInstall.swf", {}, {base: base_path + 'xml/', bgcolor: '#000000'}, {});
            $('#episode_selector_selector').css({marginLeft: '-9999em'});
          },
          onClose: function() {
            try { swfobject.getObjectById('video_player_player').play(); } catch(err2) {}
            try { (function(el){ el.parentNode.removeChild(el);})(document.getElementById('overlay_close')); } catch(err) {}
            $('#episode_selector_selector').css({marginLeft: '0'});
          }
        });
        return false;
      });
      var el = document.createElement('div');
      el.setAttribute('id', 'air_badge');
      $('#grab_widget').hide().parent()[0].appendChild(el);
      swfobject.embedSWF(base_path + "swf/badge.swf", "air_badge", "300", "250", "9.0.115", base_path + "swf/expressInstall.swf", {appname: 'Circle of Eight', appurl: base_path + 'swf/mobile_air.air', airversion: '1.5'},  {base: base_path + 'swf/', bgcolor: '#000000', wmode: 'opaque', allowScriptAccess: 'always', id: 'air_badge_flash'}, {});
      
      $('#top_tabs li a:not(a[target])').click(function() {
        fn.switchContent($(this).attr('href'));
        //return false;
      });
      
      window.showWidget = function() { $('#grab_widget').click(); };
      window.showIndex = function() { fn.switchContent('#index'); };
      
    });
  })(jQuery);
  
})();
