var MainSplashController = Class.create({
  
  init:function()
  {
    $('feature-video-link').observe('click', this.featureVideoLinkClicked.bind(this));
  },
  
  featureVideoLinkClicked:function(theEvent)
  {
    theEvent.stop();
    
    this.insertPageBlackoutElement();
    this.insertSWFContainerElement();
    this.insertSWFObject();
  },
  
  hideVideo:function(theEvent)
  {
    // was this click inside the video?
    var clickedDiv = theEvent.element().up('div');
    if (clickedDiv && clickedDiv.id == 'main_splash_page_video')
      return;
    
    
    // cancel event and hide video
    theEvent.stop();
    $('main_splash_page_blackout').remove();
    $('main_splash_page_video_wrapper').remove();
  },
  
  insertPageBlackoutElement:function()
  {
    var el = new Element('div', {
      'id': 'main_splash_page_blackout',
      'style': 'position: fixed; width: 100%; height: 100%; top: 0; left: 0; background-color: #000; opacity: 0.5; filter: alpha(opacity=50); z-index: 1000;'
    });
    document.body.appendChild(el);
    
    el.observe('click', this.hideVideo.bind(this));
    
    // handle IE 6's lack of position: fixed support
    if (this.isIE6()) {
      el.style.position='absolute';
    }
  },
  
  insertSWFContainerElement:function()
  {
    // create wrapper el (full page element)
    var wrapperEl = new Element('div', {
      'id': 'main_splash_page_video_wrapper',
      'style': 'position: fixed; width: 100%; height: 100%; top: 0; left: 0; z-index: 1010;'
    });
    
    var html = '<table cellpadding="0" cellspacing="0" style="height: 100%; width: 320px; margin: 0 auto;"><tr><td style="width: 320px; height: 181px;">';
    html += '<div id="main_splash_page_video" style="width: 320px; height: 181px; background-color: #fff;">';
    html += '<p>You must install the latest <a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">Flash Player 9</a> to watch this video.</p>';
    html += '</div>';
    html += '</td></tr></table>';
    wrapperEl.innerHTML = html;
    
    // add to page
    document.body.appendChild(wrapperEl);
    
    // observe clicks to close
    wrapperEl.observe('click', this.hideVideo.bind(this));
    
    // handle IE 6's lack of position: fixed support
    if (this.isIE6()) {
      document.body.style.height = '100%';
      wrapperEl.style.position='absolute';
      this.ie6Scroll();
      window.onscroll = this.ie6Scroll;
    }
  },
  
  insertSWFObject:function()
  {
    var fo = new SWFObject("http://andrewgriffiths.com.au/flash/movieplayer.swf", "Video Playback", "480", "272", "7", "#FFFFFF", true);
    fo.addParam("allowScriptAccess", "always");
    fo.addParam("allowFullScreen","true");
    fo.addVariable("MainColor","0xff0000");
    fo.addVariable("flvFile","http://andrewgriffiths.com.au/media/video/agshort-lo.flv");
    fo.addVariable("imgFile","images/p-keynote-splash-480.jpg");
    fo.addVariable("autoplay","false");
    fo.write("main_splash_page_video");
  },
  
  isIE6:function()
  {
    return (navigator.userAgent.match(/MSIE 6\./));
  },
  
  ie6Scroll:function()
  {
    var wrapperEl = document.getElementById('main_splash_page_video_wrapper');
    if(wrapperEl) {
      wrapperEl.style.top = document.documentElement.scrollTop;
      wrapperEl.style.left = document.documentElement.scrollLeft;
    }
    
    var blackoutEl = document.getElementById('main_splash_page_blackout');
    if(blackoutEl) {
      blackoutEl.style.top = document.documentElement.scrollTop;
      blackoutEl.style.left = document.documentElement.scrollLeft;
    }
    
    
    document.recalc();
  }
  
});
 
document.observe('dom:loaded', function() {
  new MainSplashController().init();
});