window.addEvent('domready', function(){
    var adcount = Number(Cookie.read('media_page_ad_count')) || 0;
    Cookie.write('media_page_ad_count', adcount + 1);
    var msg = (MediaJSON.clips[0].contentType == 'video') ? 'Watch Now ' : 'Listen In ';
    var inlinePlayerLink = new Element('button', {
      id: 'video_opener',
      html: msg + '<img src="http://static.oprah.com/images/global/global_arrows_calltoaction_5x8.gif" />',
      events: {
        'click': function(event){
          event.stop();
          if (this.cached) {
           this.cached.show();
          } else {
            this.cached = new inlinePlayer(adcount);
          }
        }
      }
    });
    $('article_body').getElement('div.article_image').grab(inlinePlayerLink);
});

var inlinePlayer = new Class({
  initialize: function(adcount){ 
    this.overlay = new Modal.Overlay;
    this.closeButton = new Element('button', {
      html: '&times;',
      styles: {padding:'0 1px', fontSize: 10},
      title: 'Close player',
      events: {
        'click': function(event){
          event.stop();
          this.hide();
        }.bind(this)
      }
    });

    this.container = new Element('div', {
      id: 'inline_player',
      html: '<div id="embed_video" style="width:470px;height:340px;"></div><div id="companioninline" style="width:234px;height:60px;"></div>'
    }).grab(this.closeButton, 'top');

    this.container.inject(document.body);
    this.player = this.makePlayer(adcount);

    window.addEvent('unload', function(event){
      if(this.player.getFABridge().shouldClipBeTracked()){
        this.player.getFABridge().closeMedia();
      }
    }.bind(this));

    this.show();
    
  },
  show: function(){
    this.container.setStyles({
      'display':'block',
      'visibility': 'visible',
      'top':window.getScroll().y + 100
    });
    this.overlay.show();
    if (this.player.controls) {
      this.player.controls.play(); 
    }
  },
  hide: function(){
    this.container.setStyle('visibility', 'hidden');
    this.overlay.hide();
    if (this.player.controls) {
      this.player.controls.pause();
    }
    
  },
  makePlayer: function(adcount){
    Schematic.VideoManager.setAppSource('http://static.oprah.com/flash/player/harpoPlayer.swf');
    
    var queue = function() {
      this.loadPlaylist(MediaJSON);
      this.playlist.playAll();
      this.controls.play();
    };
    
    return Schematic.VideoManager.embedPlayer({
      id : 'vp3',
      target : 'embed_video',
      expressInstallUrl : "http://static.oprah.com/flash/expressinstall/expressinstall.swf",
      configUrl : 'http://static.oprah.com/flash/player/xml/config_inline.xml',
      page : 'Article',
      additionalEmbedVars : {
        "sAccount" : "harpocom",
        "sMovieID" : "articlePlayer_"+MediaJSON.clips[0].id,
        "sPageName" : "article.jsp", 
        "sPageURL" : window.location.href
      },
      onReady : function() {
        this.addEventListener('ad_complete', function(evt) {
          queue.call(this);  
        }.bind(this));

        if(MediaJSON.clips[0].playingTime > 59) {
          this.getFABridge().playAd(MediaJSON.clips[0]);
        } else {
          queue.call(this);
        }                      
      },
      onComplete: function(){
        //alert('done and done.');
      }   
    });
  }
});


//Companion Ad Requests
function syncRoadBlock(adTag) {
a = adTag.split(';');
if (a.length>0) {
   for (x=0; x<=a.length-1; x++) {
     if (a[x].indexOf('sz=') == 0) {
       size = a[x].substring(3);
       dims = size.split('x');
       width = dims[0];
       height = dims[1];
       if(width==234) {
         //alert("234: "+adTag); 
        loadRBs("companioninline",width,height,adTag);    
       }
     }
   }
 }      
}

function loadRBs(s,w,h,adTag) {  
  //alert('loading '+document.getElementById(s));
  $(s).set('html', '<iframe src="' + adTag + '" id="ifr_companion" width="'+w+'" height="'+h+'" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no>' + '</iframe>');
  document.getElementById(s).style.backgroundImage="url(http://static.oprah.com/images/global/global_body_verticaladlabel_11x60.gif)";
  document.getElementById(s).style.backgroundRepeat="no-repeat";
  document.getElementById(s).style.paddingLeft="12px";
}

// talk to marlon, I think we can do this.
// function syncRoadBlock(adTag) {
//   var tag = advert.adTag;
//   if(tag.x == 234) {
//    $('companioninline').setStyles({
//      'background-image': 'url(http://static.oprah.com/images/global/global_body_verticaladlabel_11x60.gif)',
//      'background-repeat': 'no-repeat',
//      'padding-left': 12
//    }).set('html', '<iframe src="' + adtag + '" id="ifr_companion" width="'+w+'" height="'+h+'" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no>' + '</iframe>');
//   }
// }
