// This is the default Blueprint javascript lib for this site.
// This is the default Blueprint javascript lib for this site.
  // install everything after page is ready
  
document.observe("dom:loaded", function () {
  $('player').observe('click', function (evt) { evt.stop(); });
});  
  
window.onload = function() {
    var seekTo = getSeekToParam();
    
    var vidSizeCookie = new Cookie('vidsize', Cookie.days(90));

  
/*     $('controlbar').style.visibility = "visible";  */
    flowplayer("player", {src: "/static/scripts/flowplayer.commercial-3.2.1.swf", wmode: "transparent"}, {
    //flowplayer("player", {src: "/static/scripts/flowplayer-3.2.1.swf", wmode: "transparent"}, {

        key: '#$9db72e3ee9a0f6b89d8',
        
        scaling: 'fit',
          
        //log: { level: 'debug' },
       
        logo: {

            // default logo and its position
            url: '/static/images/player_watermark.png',
            top: 15,
            left: 15,
            opacity: 0.4,


            // if set to false, then the logo is also shown in non-fullscreen mode
            fullscreenOnly: false,

            // time to display logo (in seconds). 0 = show forever
            displayTime: 0,


            /*
                if displayTime > 0 then this specifies the time it will take for
                the logo to fade out. this happens internally by changing the opacity
                property from its initial value to full transparency.
                value is given in milliseconds.
            */
            
            fadeSpeed: 0,

            // for commercial versions you can specify where the user is redirected when the logo is clicked
            linkUrl: 'http://wheelercentre.com'
        },


        // default settings for the play button
      play: {

          /*
              relative path to the play button, this can be a JPG, PNG or SWF file.
              NOTE: the button can only be changed in the commercial versions
          */
          url: '/static/images/big_play_hover.png',

          // all display properties, except the z-index, can be modified in all versions
          opacity: 1,

          width: 94,
          height: 71,

          // label text; by default there is no text
          label: null,

          // label text at end of video clip
          replayLabel: null,

          /*
              progress indicator - this is shown while video is being loaded.
              it is always in the same place as the play button.
          */
          // how fast progress indicator fades out after video is loaded
          fadeSpeed: 500,

          // how fast buffering animation rotates
          rotateSpeed: 50
      },

      // don't start automatically
      clip: {
          autoBuffering: true,
          provider: 'rtmp',
          bufferLength: 4,
          // track start event for this clip
          onStart: function(clip) {
              pageTracker._trackEvent("Videos", "Play", CenvidPostTitle);
              $('player').style.background = "#000000";
              
              if (typeof(seekTo) != 'undefined') {
                $f().seek(seekTo);
              }
          },

          // track pause event for this clip. time (in seconds) is also tracked
          onPause: function(clip) {
              pageTracker._trackEvent("Videos", "Pause", CenvidPostTitle, parseInt(this.getTime()));
          },

          // track stop event for this clip. time is also tracked
          onStop: function(clip) {
              pageTracker._trackEvent("Videos", "Stop", CenvidPostTitle, parseInt(this.getTime()));
          },

          // track finish event for this clip
          onFinish: function(clip) {
              pageTracker._trackEvent("Videos", "Finish", CenvidPostTitle);
          },
                        // track finish event for this clip
          onBufferEmpty: function(clip) {
              pageTracker._trackEvent("Videos", "Buffer Empty", CenvidPostTitle);
          }

      },
      // disable default controls
     // plugins: {controls: null}
      plugins:  {
        
controls: {

      		// location of the controlbar plugin
      		url: '/static/scripts/flowplayer.controls-3.2.0.swf',
      
      		// display properties such as size, location and opacity

      		left: 0,
      		bottom: 0,
      		opacity: 1,
      		height: '40px',
      
      		// styling properties (will be applied to all plugins)
      	  backgroundColor: 'transparent', 
      		backgroundGradient: 'none',
      
      		// controlbar specific settings
      		timeColor: '#ffffff',
      		all: false,
      		play: true,
      		scrubber: true,
      		time: true,
      		duration: false,  
      		volume: true,
      		mute: true,
      		
      		buttonColor:'#ffffff',
      		buttonOverColor: '#e8600F',
      		sliderColor: '#ffffff',
      		bufferColor: '#e8600F',
      		progressColor: '#fecbad',
      		sliderBorder: 'none',
      		scrubberHeightRatio: 0.35,
      		scrubberBarHeightRatio: 0.2,
      		volumeSliderHeightRatio: 0.35,
      		volumeBarHeightRatio: 0.2,
      		timeBgHeightRatio: 0.5,
      		timeFontSize: 9,
      		tooltipColor: '#e8600F',

      		// tooltips (since 3.1)
      		tooltips: {
      			buttons: false
      		}
        },
        
    		// the RTMP streaming plugin
    		rtmp: {
    			url: '/static/scripts/flowplayer.rtmp-3.2.0.swf',
    			
    			// netConnectionUrl has our CloudFront domain name + 'cfx/st'
    			//netConnectionUrl: 'rtmp://' + CenvidStreamingURL + '/cfx/st'
    		}
	   }
        
    });
    
   
    choosePlayerFromSavedPref();
    
    //Only autoplay if there is no auto seek in the params.    
    if (typeof(seekTo) != 'undefined') {
      $f().play();
    }
  
      
    $$('#clips a').each(function(elem, i){
        var url = elem.readAttribute('clipurl');
        
        // Play the clip in the player on click.
        elem.observe('click', function(evt){
          var vidSize = this.hasClassName('lowres') ? 'small' : 'large';
          $f().stop();    
          cleanUpPlaylist();
          this.addClassName('on');
          setPlayerSize(vidSize);
          $f().play(url);
          evt.stop();
          vidSizeCookie.set(vidSize);
        });
      
    });
   
    function choosePlayerFromSavedPref() {
      var vidSize = vidSizeCookie.get();
  
      if (vidSize == 'large') {
        setPlayerSize(vidSize);
        cleanUpPlaylist();
        $$('#clips a.highres').each(function(elem){
          elem.addClassName('on');
        });    
      }
      
      vidSizeCookie.set(vidSize);    
    };
    
    function setPlayerSize(size) {
      var player = $('player');
      
      if (size == 'small') {
        player.style.width = "512px";
        player.style.height = "288px";
        player.style.padding = "72px 128px";
      }
      else {
        player.style.width = "768px";
        player.style.height = "432px";
        player.style.padding = "0";
      }
    };
   
    function cleanUpPlaylist() {
      $$('#clips a').each(function(elem){
        elem.removeClassName('on');
      });
    };
    
    
 
  /*
  $('playButton').observe('mouseover', function(){ $('playButton').setOpacity(1) });
    $('playButton').observe('mouseout', function(){ $('playButton').setOpacity(0.8) });
*/



    // dir: 1 to make opaque, -1 to make transparent
    // step: the amount to change the opacity per "frame" (a float)
    // rate: the number of milliseconds between each "frame"


    /*
function fadeControls(dir, step, rate) {

      var controls = $('controlbar');

      if (controls.fader) {
        clearTimeout(controls.fader);
      }

      if ( typeof(controls.opacity) == "undefined" ) {
        controls.opacity = 1;
        controls.setOpacity(1);
      }

      controls.opacity = controls.opacity + (step * dir);

      if (controls.opacity < 0) {
          controls.setOpacity(0);
          controls.opacity = 0;
      } else if (controls.opacity > 1) {
          controls.setOpacity(1);
          controls.opacity = 1;
      } else {
        controls.setOpacity(controls.opacity );
        controls.fader = setTimeout(function () {fadeControls(dir, step, rate)}, rate);
      }

      //controls.style.opacity = o;
    };
*/


  /*
  $('cinema').observe('mouseout', function () { fadeControls(-1, 0.1, 40) });
    $('cinema').observe('mouseover', function () { fadeControls(1, 0.1, 40) });
*/


};

function switchOn() {
  lightsOn();
  $('lightSwitchOn').hide();
  $('lightSwitchOff').show();
}

function switchOff() {
  lightsOff();
  $('lightSwitchOn').show();
  $('lightSwitchOff').hide();
}

function getSeekToParam() {
  var seekToPrm = getQueryStringParam('t');
  var seekTo;
  
  if (seekToPrm != "") {
    var regex = new RegExp('([^\d]*)m([^\d]*)s');
    var results = regex.exec(seekToPrm); 
    
    var mins = 0;
    var secs = 0;
    if (typeof(results[1]) != 'undefined' && typeof(results[2]) != 'undefined') {
      mins = parseFloat(results[1]);
      secs = parseFloat(results[2]);
    }
    seekTo = (mins*60)+(secs);
  }
  
  return seekTo;
}

function getQueryStringParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
