var videos = {};

$(document).ready(function(){
	$('#menu').find('li').mouseenter(function(){
		$(this).addClass('hover');
	}).mouseleave(function(){
		$(this).removeClass('hover');
	});
	
	$('a').removeAttr("title");
	
	$('.sidebar-container .sidebar-item:last-child').addClass('last');
	$('.wpcf7 small').wrap('<span>');
	$('input[type="checkbox"]').ezMark({
		checkboxCls: 'checkbox-unchecked' ,
		checkedCls: 'checkbox-checked'
	});
	
	
	// assigns ids to all the embed tags
  var i = 0;
  $('embed').each(function() {
	$(this).attr('id', "embed"+i);
	i++
  });
	
	
});

function onYouTubePlayerReady(playerId) {
  
  // loop through all embed tags assign a listener object only if not already assigned
  $('embed').each(function() {
	var embedid = $(this).attr('id');
	var ytplayer = document.getElementById(embedid);
	if (videos[embedid] == undefined) {
		window["dynamicYouTubeEventHandler" + embedid] = function(state) { onytplayerStateChange(state, embedid); }
		ytplayer.addEventListener("onStateChange", "dynamicYouTubeEventHandler"+embedid);
	}
	videos[embedid] = true;
  });
}

function onytplayerStateChange(newState, playerId) {
	// If one of the videos was played
	if (newState == 1) {
		// loop through each of the embed tags
		$('embed').each(function() {
			var embedid = $(this).attr('id');
			var ytplayer = document.getElementById(embedid);
			// Only pause video if not the current player
			if(embedid != playerId) {
				var current_state = ytplayer.getPlayerState();
				// Only pause if not already started
				if (current_state != '-1') {	ytplayer.pauseVideo();	}
			}
		});
	}
}
