// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};

/* 	
//	When using JWPlayer (flash), Flash does not like your event listeners to be nested, in fact it doesn't work unless they are at the top level
//	So, here are the playerReady() and Google Analytics functions for the JWPlayer that we use for flash
*/

// Set up some global functions for JWPlayer (they need to be for some reason to do with flash)

// stateTracker is called by JWPlayer any time it's state changes
function stateTracker(object) {
	if (object.newstate == 'PAUSED'){ // if it's state is paused
		window.log('Video (Flash) paused at '+JWPlayerCurTime);
		// If GA is loaded, send the data to GA
		if(typeof _gaq == 'function'){
				_gaq.push(['_trackEvent', 'Video - Flash', 'Paused', 'Promo', JWPlayerCurTime]);
			}
	} else if (object.newstate == 'COMPLETED'){ // if it's state is completed
		window.log('Video (Flash) completed');
		// If GA is loaded, send the data to GA
		if(typeof _gaq == 'function'){
				_gaq.push(['_trackEvent', 'Video - Flash', 'Complete', 'Promo', 100]);
			}
	}
};

// JWPlayerCurTime will be updated on the fly by JWPlayer to allow us to grab the curTime when we log when the video is paused
var JWPlayerCurTime;
function timeTracker(object){
	JWPlayerCurTime = object.position;
	//window.log(curTime);
}

// This is the global var for the player and the playerReady function will be called by JWPlayer once it has initalized
var JWPlayer;
function playerReady(object) {
	window.log('flash player loaded');
	JWPlayer = document.getElementById(object.id);
	// add in the event listener for state changes, and fire stateTracker
	JWPlayer.addModelListener("state","stateTracker");
	// add in the event listener for is current time, and fire timeTracker
	JWPlayer.addModelListener("time","timeTracker");
};

/*	End of the JWPlayer top level variables */

/* Now get into the good stuff, jQuery yummyness */

jQuery(document).ready(function($) {
	
	// Track clicks to the LV demo cam
	$('a[href*="lobstervision.tv/demo"]').live('click', function(){
		window.log('Visit to Lobster Vision demo camera');
		// Check that the GA code is loaded
		if(typeof _gaq == 'function'){
			// If yes then track clicks through to LV demo cam
			_gaq.push(['_trackEvent', 'External Link', 'Lobster Vision', 'Demo Camera']);
		}
	})
	
	//Track downloads of the Lobster Pot brochures
	$('a[href*="Lobster_Pot_Light.pdf"]').live('click', function(){
		window.log('Lobster Pot Light brochure downloaded');
		if(typeof _gaq == 'function'){
			// If yes then track downloads of the brochure
			_gaq.push(['_trackEvent', 'Download', 'Lobster Pot Light', 'Brochure']);
		}
	})
	$('a[href*="Lobster_Pot.pdf"]').live('click', function(){
		window.log('Lobster Pot brochure downloaded');
		if(typeof _gaq == 'function'){
			// If yes then track downloads of the brochure
			_gaq.push(['_trackEvent', 'Download', 'Lobster Pot', 'Brochure']);
		}
	})
	$('a[href*="Lobster_Pot_Extreme.pdf"]').live('click', function(){
		window.log('Lobster Pot Extreme brochure downloaded');
		if(typeof _gaq == 'function'){
			// If yes then track downloads of the brochure
			_gaq.push(['_trackEvent', 'Download', 'Lobster Pot Extreme', 'Brochure']);
		}
	})
	
	// Check for the Services page to add in the gallery
	if($('body').hasClass('services')){
		$('#screenshots ul li a').live('click', function(event){
			event.preventDefault();
			var href = $(this).attr('href');
			window.log(href);
			$('#screenshots img').first().attr('src', href);
		});
	}
	
	// Redefine getScript so that it will now cache script calls
	$.getScript = function(url, callback, cache){
		$.ajax({
				type: "GET",
				url: url,
				success: callback,
				dataType: "script",
				cache: cache
		});
	};
	
	//check if a video tag exists on the page
	if($('video').length !== 0 || $('div[class*="video"]').length !== 0){
	    //window.log('video found')
	
		// Check that the browser supports HTML5 Video (using Modenizr)
		if(Modernizr.video){
			
			// If it does support HTML5 video, load in the VideoJS assets and activate it using the callback
			$.getScript('/assets/js/videojs.min.js', function(){
				window.log('VideoJS loaded');
				// Load the style sheet
				$('head').append('<link rel="stylesheet" type="text/css" href="/assets/css/video-js.css" />');
				// now replace the video controls of the player using VideoJS
				$('video').VideoJS();
			});
			
			/* Google Analytics video tracking */
			
			// Set the video variable to the video we are targeting
			var player = $('.promo');
			var video = $('.promo')[0];
			
			// Now bind the pause function for analytics and send the data to GA
			player.bind('pause', function(){
				window.log('Video (HTML5) paused at '+video.currentTime.toFixed(1));
				if(typeof _gaq == 'function'){
					_gaq.push(['_trackEvent', 'Video - HTML5', 'Pause', 'Promo', video[0].currentTime.toFixed(1)]);
				}
			});
			
			// Now bind the ended function for analytics and send the data to GA
			player.bind('ended', function(){
				window.log('Video (HTML5) completed');
				if(typeof _gaq == 'function'){
					_gaq.push(['_trackEvent', 'Video - HTML5', 'Complete', 'Promo', 100]);
				}
			});
	
		} else {
			
			// Load a flash video
			$.getScript('/assets/js/swfobject.js', function(){
				if(typeof SWFObject == 'function'){
					
					window.log('swfobject loaded');
					
					// set up the new SWFObject and add in the required parameters
					var so = new SWFObject('/assets/videos/player/jwplayer/player.swf','promo-flash','624','351','9');
					so.addParam('allowfullscreen','true');
					so.addParam('allowscriptaccess','always');
					so.addParam('flashvars', 'image=/assets/images/video_poster.jpg&file=http://lobsterpictures.s3.amazonaws.com/lobster-promo.mp4&stretching=fill&controlbar=over')
					so.write('promo');
					
				};
			});
		}
	}
	
	
});
