/**
 * @author Brian Sage
 * @projectDescription Requires Prototype js library
 */


// SpokesPerson JS Object avoids re-using any global variables
var SP = {
  videoUrl : 0,
  
  browserSucks: function(){
    return (typeof(document.body.style.filter) != 'undefined' ) ? true : false;
  },

  init: function(videoUrl){
    if(videoUrl) SP.videoUrl = videoUrl;
    
    $('spokesperson').innerHTML = '<div id="spokesperson_stopped"><div id="spokesperson_open" onclick="SP.show();" title="Play video guide"></div><div id="spokesperson_poster" onclick="SP.show();" title="Play video guide"></div></div><div id="spokesperson_playing"><div id="spokesperson_close" onclick="SP.hide();" title="Hide video guide"></div><div id="spokesperson_video"></div></div>';

    $('spokesperson_playing').style.cssText = 'display: none; position:fixed; bottom:0; right:0; width:320px; height:320px; padding:0; margin:0; line-height:0;';
    $('spokesperson_stopped').style.cssText = 'position:fixed; bottom:0; right:0; width:186px; height:166px; padding:0; margin:0; line-height:0;';
    $('spokesperson_video').style.cssText = 'width:320px; height:320px;';
    $('spokesperson_poster').style.cssText = 'width:186px; height:166px; background: transparent url(/images/video_poster.png) no-repeat 0 0;';
    $('spokesperson_open').style.cssText = 'position:fixed; bottom: 36px; right: 39px; z-index:10000; width:20px; height:20px; background: transparent url(/images/video_play.png) no-repeat 0 -20px;';
    $('spokesperson_close').style.cssText = 'position:fixed; bottom: 36px; right: 39px; z-index:10000; width:20px; height:20px; background: transparent url(/images/video_close.png) no-repeat 0 0;';
    
    SP.dontShow = SP.readCookie('spokesperson');
    if (!SP.dontShow) {
      SP.show(videoUrl);
    }
  },
    
  hide: function(){
    $("spokesperson_video").flvplayerPause();
    if (SP.browserSucks()) {
      $('spokesperson_playing').hide();
      $('spokesperson_stopped').style.display = 'block';
    } else {
      $('spokesperson_playing').fade({ from:1, to:0 });
      $('spokesperson_stopped').appear();
    }
  },
  
  show: function(videoUrl){
    if(!videoUrl) videoUrl = SP.videoUrl;;
    
    if (SP.browserSucks()) {
      $('spokesperson_stopped').hide();
      $('spokesperson_playing').style.display='block';
    } else {
      $('spokesperson_stopped').fade({ from:1, to:0 });
      $('spokesperson_playing').appear();
    }
    
    // Write flash <object> to teh DOM
    swfobject.embedSWF('/flash/spokesperson_320_walpha.swf', "spokesperson_video", "320", "320", "9.0.0", "/flash/expressInstall.swf", {
      vidurl: videoUrl
    }, {
      wmode: "transparent",
      allowScriptAccess: "always"
    });
    
    // Create or refresh the cookie, so video remains hidden for frequent visitors
    SP.createCookie('spokesperson','dontShow',7)
  },
  
  // Great thanks to http://www.quirksmode.org/js/cookies.html
  createCookie: function(name, value, days){
    if (days) {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      var expires = "; expires=" + date.toGMTString();
    }
    else 
      var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
  },
  
  readCookie: function(name){
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ') 
        c = c.substring(1, c.length);
      if (c.indexOf(nameEQ) == 0) 
        return c.substring(nameEQ.length, c.length);
    }
    return false;
  },
  
  eraseCookie: function(name){
    createCookie(name, "", -1);
  }
};

