var players = function()
{
  // Private variables
  var tracker = null, trackingid;
  var swf = 'template/lessonframe/flash/flowplayer-3.2.4.swf';
  var controls = 'flowplayer.controls-3.2.2.swf';
  
  // Private functions 
  // This must only be called if google is not null
  function setGoogleAnalytics(clip) {
    clip.onStart = function(clip) { 
   	  tracker._trackEvent('Video', 'Start', trackingid);
    }; 
     
    clip.onPause = function(clip) {             
      tracker._trackEvent('Video', 'Pause', trackingid, parseInt(this.getTime())); 
    };                 
     
    clip.onStop = function(clip) {             
      tracker._trackEvent('Video', 'Stop', trackingid, parseInt(this.getTime())); 
    };
      
    clip.onFinish = function(clip) {             
      tracker._trackEvent('Video', 'Finish', trackingid);  
    };   
  }

  function createConfig(url, type, uniq) {
    var config = { 
      type: type,
      key: '#$b7a77fb62923901ae34',
      plugins: { 
        synchronised: {
          url: 'flowplayer.synchronised.swf',
          synch: (type !== 'single'),
          master: (type == 'master'),
          suffix: uniq,
          autoStart: false
         },
         /*
         screen: {
             height: "100pct",
             top: 0
         },
         */
	     controls: {
        	 url: controls,
        	 all:(type != 'slave'),
	         borderRadius: '0px',
	         timeColor: '#ffffff',
	         slowForward: true,
	         bufferGradient: 'none',
	         backgroundColor: 'rgba(0, 0, 0, 0.7)',
	         volumeSliderGradient: 'none',
	         slowBackward: false,
	         timeBorderRadius: 20,
	         progressGradient: 'none',
	         time: (type != 'slave'),
	         height: 26,
	         volumeColor: '#4599ff',
	         tooltips: {
	             marginBottom: 5,
	             volume: true,
	             scrubber: true,
	             buttons: false
	         },
	         opacity: 1,
	         fastBackward: false,
	         timeFontSize: 12,
	         border: '0px',
	         bufferColor: '#a3a3a3',
	         volumeSliderColor: '#ffffff',
	         buttonColor: '#ffffff',
	         mute: (type != 'master'),
	         autoHide: {
	             enabled: true,
	             hideDelay: 500,
	             mouseOutDelay: 500,
	             hideStyle: 'fade',
	             hideDuration: 400,
	             fullscreenOnly: false
	         },
	         backgroundGradient: [0.5,0,0.3],
	         width: '100pct',
	         display: 'block',
	         sliderBorder: '1px solid rgba(128, 128, 128, 0.7)',
	         buttonOverColor: '#ffffff',
	         fullscreen: true,
	         timeBgColor: 'rgb(0, 0, 0, 0)',
	         scrubberBarHeightRatio: 0.2,
	         bottom: 0,
	         stop: false,
	         sliderColor: '#000000',
	         zIndex: 1,
	         scrubberHeightRatio: 0.6,
	         tooltipTextColor: '#ffffff',
	         sliderGradient: 'none',
	         timeBgHeightRatio: 0.8,
	         volumeSliderHeightRatio: 0.6,
	         name: 'controls',
	         timeSeparator: ' ',
	         volumeBarHeightRatio: 0.2,
	         left: '50pct',
	         tooltipColor: 'rgba(0, 0, 0, 0)',
	         playlist: false,
	         durationColor: '#b8d9ff',
	         play: (type != 'slave'),
	         fastForward: (type != 'slave'),
	         timeBorder: '0px solid rgba(0, 0, 0, 0.3)',
	         progressColor: '#4599ff',
	         volume: (type != 'master'),
	         scrubber: (type != 'slave'),
	         builtIn: false,
	         volumeBorder: '1px solid rgba(128, 128, 128, 0.7)'
	     }
      },
      autoHide: {
    	  // always enable
    	  fullsccreenOnly: false,
    	    
    	  // make it hide faster
    	  hideDelay: 2000
      },
      clip: {
        scaling: 'fit',
        bufferLength: 10,
        provider: 'synchronised', 
        autoPlay: false,
        autoBuffering: true
      },
      playlist: [
        { url: url }
      ],
      play: {
        opacity: 0
      },
      log: {
    	  level : 'info',
          filter: 'org.flowplayer.synchronised.*'
      }     
    };
    
    /*
    if(type === 'slave') {
      config.plugins.controls.all = false;
      config.plugins.controls.volume = true;
      config.plugins.controls.mute = true;
      config.plugins.controls.playlist = false
    } else if(type === 'master') {
      config.plugins.controls.all = true;
      config.plugins.controls.stop = false;
      config.plugins.controls.volume = false;
      config.plugins.controls.mute = false;
      config.plugins.controls.playlist = false
    } else { 
      config.plugins.controls.all = true;
      config.plugins.controls.stop = false;
      config.plugins.controls.playlist = false
    }
	*/
    
    if(tracker && type !== 'slave') {
    	setGoogleAnalytics(config.clip); 
    }

    return config;
  }

  function createPlayer(url, id, type, uniq) {
    return $f(id, swf, createConfig(url, type, uniq));
  }
	  
  // Public stuff
  return {

    setGoogleAnalyticsTracker: function(_tracker, _articleid, _lesson) {
	  if(typeof(_tracker) === "object"){
        tracker = _tracker;
        trackingid = _articleid + ": " + _lesson;
	  }
    },
	
    load: function (vid1, vid2) {
      // generate a unique suffix for the interplayer control pipes
      var uniq = Math.round(Math.random() * 32767) + '';
      if(vid2) {
        createPlayer(vid1, "player1", "master", uniq);
        createPlayer(vid2, "player2", "slave", uniq); 
      } else {
        createPlayer(vid1, "player1", "single", '');
      }
    },
    
    fullscreenPause: function() {
      $f("*").each(function(){
        this.getPlugin('synchronised').fullscreenPause(); 
      });
    }
  };
} ();
