if( Drupal.jsEnabled ) {

Drupal.vidCount = {};

$(document).ready(function() {

 var $yt = $('div.embed-youtube');
 var $vim = $('div.embed-vimeo');
 var $ust = $('div.embed-ustream');
 var $gv = $('div.embed-googlevideo');
 var $fl = $('div.embed-flickr');
 var $jw = $('div.embed-jwplayer');

 if( $yt.length || $vim.length || $ust.length || $gv.length || $fl.length || $jw.length ) {
	$.getScript('http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js', function(data, textStatus) {
	var idCounter = 1;

	$yt.each(function() {
		 var width = $(this).css('width').replace(/\D/g, '');;
		 var height = $(this).css('height').replace(/\D/g, '');;
		 var id = $(this).attr('id').replace(/^youtube_(.+)/, '$1');

		  if( id.length ) {
			swfobject.embedSWF('http://www.youtube.com/v/' + id + '&enablejsapi=1&playerapiid=' + id + '&rel=0&fs=1',
			'youtube_' + id, width, height, '9.0.0', null, null,
			{
			  'allowScriptAccess': 'always',
			  'allowFullScreen': 'true',
			  'wmode': 'transparent'
			}, null);
		  }
		});

   $vim.each(function() {
		 var width = $(this).css('width').replace(/\D/g, '');;
		 var height = $(this).css('height').replace(/\D/g, '');;
		 var id = $(this).attr('id').replace(/^vimeo_(.+)/, '$1');

		  if( id.length ) {
			swfobject.embedSWF('http://vimeo.com/moogaloop.swf',
			'vimeo_' + id, width, height, '9.0.0', null,
			{
			  'clip_id': id,
			  'show_portrait': 1,
			  'show_byline': 1,
			  'show_title': 1,
			  'js_api': 1,
			  'js_onLoad': 'vimeo_player_loaded',
			  'js_swf_id': id
			},
			{
			  'allowScriptAccess': 'always',
			  'allowFullScreen': 'true',
			  'wmode': 'transparent'
			}, null);
		  }
	    });


    $ust.each(function() {
		 var width = $(this).css('width').replace(/\D/g, '');
		 var height = $(this).css('height').replace(/\D/g, '');
		 var id = $(this).attr('id').replace(/^ustream_(.+)/, '$1');

		  if( id.length ) {
			swfobject.embedSWF('http://www.ustream.tv/flash/live/1/' + id,
			'ustream_' + id, width, height, '9.0.0', null, null,
			{
			  'allowScriptAccess': 'always',
			  'allowFullScreen': 'true',
			  'wmode': 'transparent'
			}, null);
		  }
        });

    $gv.each(function() {
		 var width = $(this).css('width').replace(/\D/g, '');
		 var height = $(this).css('height').replace(/\D/g, '');
		 var id = $(this).attr('id').replace(/^googlevideo_(.+)/, '$1');

		  if( id.length ) {
			swfobject.embedSWF('http://video.google.com/googleplayer.swf?docid=' + id,
			'googlevideo_' + id, width, height, '9.0.0', null, null,
			{
			  'allowScriptAccess': 'always',
			  'allowFullScreen': 'true',
			  'wmode': 'transparent'
			}, null);
		  }
        });

    $fl.each(function() {
		 var width = $(this).css('width').replace(/\D/g, '');
		 var height = $(this).css('height').replace(/\D/g, '');
		 var id = $(this).attr('id').replace(/^flickr_(.+)/, '$1');
		 var user = $(this).attr('user');

		  if( id.length && user.length ) {
			swfobject.embedSWF('http://www.flickr.com/apps/slideshow/show.swf?v=71649',
			'flickr_' + id, width, height, '9.0.0', null,
			{
			  'offsite': 'true',
			  'lang': 'en-us',
			  'page_show_url': '%2Fphotos%2F' + user + '%2Fsets%2F' + id + '%2Fshow%2F',
			  'page_show_back_url': '%2Fphotos%2F' + user + '%2Fsets%2F' + id,
			  'set_id': id,
			  'jump_to': ''
			},
			{
			  'allowScriptAccess': 'always',
			  'allowFullScreen': 'true',
			  'wmode': 'transparent'
			}, null);
		  }
        });

    $jw.each(function() {
		 var width = $(this).css('width').replace(/\D/g, '');
		 var height = $(this).css('height').replace(/\D/g, '');

		 var id = $(this).attr('id');

		 if( ! id ) {
		   id = 'jwplayer_' + idCounter++;
		   $(this).attr('id', id);
		 }

		 var vars = $.trim($(this).attr('url')); // this is the preferred way
		 if( ! vars ) vars = $.trim($(this).text());

		 if( vars.match(/^\{.+\}$/) ) {
		   // Note that we are not using jquery 1.4, so invoking parseJSON will fail
		   try {
			 vars = $.parseJSON(vars);
		   }
		   catch(e) {
			 return;
		   }

		   if( ! vars.screencolor ) vars.screencolor = '#000';
		 }
		 else {
		   vars = { file: vars, screencolor: '#000' };
		   if( t = vars.file.match(/\?image=(.+)/) ) {
		     vars.file = vars.file.replace(/\?image=.+/, '');
			 vars.image = t[1];
			 if( ! vars.image.match(/^(https?:\/)?\//) && (t = vars.file.match(/(.+\/).+/)) )
			   vars.image = t[1] + vars.image;
		   }
		 }

		  if( vars.file ) {
			if( vars.file.match(/\.mp3/i) ) height = 24;

			swfobject.embedSWF('http://www.wri.org/tools/js/jwplayer.swf',
			id, width, height, '9.0.0', null, vars,
			{
			  'allowScriptAccess': 'always',
			  'allowFullScreen': 'true',
			  'wmode': 'transparent'
			}, null);
		  }
        });
   });
 }
});

/* glue functions to track stop/start events on Youtube and Vimeo embedded videos on our site */

function onYouTubePlayerReady(id) {

  if( window.pageTracker ) {
    var player = document.getElementById('youtube_' + id);

	// Youtube's event listener does not pass an identifier for the object, so we must create a separate listener for each video
	var key = 'ytEvent_' + id;
	key = key.replace(/\W/i, '_');
    player.addEventListener('onStateChange', key);
    window[key] = function(state) { ytEvent(state, 'youtube_' + id); };
  }
}

function ytEvent(state, id) {

  if( state == 1 ) {
    // starting
    if( ! Drupal.vidCount[id] ) Drupal.vidCount[id] = 0;

    Drupal.vidCount[id]++;
    pageTracker._trackEvent('Video', 'Play', id);

    if( Drupal.vidCount[id] == 1 ) pageTracker._trackEvent('Video', 'Start', id);
  }
  
  if( state == 0 ) {
    pageTracker._trackEvent('Video', 'Complete', id);
  }
}

function vimeo_player_loaded(id) {

  if( window.pageTracker ) {
    var player = document.getElementById('vimeo_' + id);

	player.api_addEventListener('onPlay', 'vimEventPlay');
	player.api_addEventListener('onFinish', 'vimEventFinish');
  }
}

function vimEventPlay(id) { ytEvent(1, 'vimeo_' + id) }
function vimEventFinish(id) { ytEvent(0, 'vimeo_' + id) }

}

