/*****************************************************************************
 *
 *  TITLE:		JWPlayer Customizations for Gray.com
 *  MODIFIED:	2011.09.14
 *  AUTHOR: 	Michael Poplin
 *	REQUIRES:	jQuery 1.5+
 *  SUMMARY:  Provides custom event tracking for Google Analytics using 
 *             JWPlayer 5.7 API
 *
 ****************************************************************************/
 
var initVid = {
  init: function() {
    this.player = jwplayer('container');
    this.eventCategory = "JW Player";
    this.pageURL = window.location.href;
    var vidURI = (vidData.fileURL).split('/').pop();
    var vidRef = this.pageURL + ' - ' + vidURI;
    this.player.setup({
      flashplayer: "/sites/all/libraries/jwplayer/player.swf",
      file: vidData.fileURL, // the template/node provides us a JSON object to get this value from
      image: vidData.imageURL, // this too
      "controlbar.position": "bottom",
      "stretching": "fill",
      width: 550,
      height: 364,
      events: {
        onReady: function(e) {
          AE.console.print("Video Loaded");
          _gaq.push(['_trackEvent', this.eventCategory, "Video Player Initialized", vidRef]);
        },
        onPlay: function(e) { 
          if (e.oldstate == 'PAUSED') {
            AE.console.print("Video Resumed");
            _gaq.push(['_trackEvent', this.eventCategory, "Video Resumed", vidRef]);
          } else {
            AE.console.print("Video Started");
            _gaq.push(['_trackEvent', this.eventCategory, "Video Started", vidRef]);
          }
        },
        onPause: function(e) {
          AE.console.print("Video Paused");
          _gaq.push(['_trackEvent', this.eventCategory, "Video Paused", vidRef]);
        },
        onSeek: function(e) {
          AE.console.print("Video Seek To " + e.offset);
          _gaq.push(['_trackEvent', this.eventCategory, "Video Seek To " + e.offset, vidRef]);
        },
        onBuffer: function(e) {
          if (e.oldstate == 'PLAYING') {
            AE.console.print("Video Buffering");
            _gaq.push(['_trackEvent', this.eventCategory, "Video Buffering", vidRef]);
          }
        },
        onComplete: function(e) {
          AE.console.print("Video Complete");
          _gaq.push(['_trackEvent', this.eventCategory, "Video Complete", vidRef]);
        }
      }
    });
  }  
}

var initNewVid = {
  init: function() {
  }
}

jQuery(function() {
  if ((vidData.videoID !== undefined) && (vidData.videoID.length > 0)) {
    initNewVid.init();
  }
  if (vidData.fileURL !== undefined) {
    initVid.init();
  }
});
