//----------------------------------
//  playVideo
//----------------------------------

function playVideo(parentID, videoID, videoWidth, videoHeight) {
	$("#" + parentID).html("<iframe title='YouTube video player' class='youtube-player' type='text/html' width='"+ videoWidth +"' height='"+ videoHeight +"' src='http://www.youtube.com/embed/"+ videoID +"?rel=0&hd=1&autoplay=1' frameborder='0'></iframe>");
	return false;
}

//----------------------------------
//  playVideo
//----------------------------------

var _divID = null;
var _videoID = null;
var _videoWidth = null;
var _videoHeight = null;

var _videoDiv = null;
var _videoPlayer = null;
var _videoPlayerId = null;

var _nextVideo = null;

//----------------------------------
//  displayVideoInContainer
//----------------------------------

function displayVideoInContainer(divID, videoID, videoWidth, videoHeight) {
	if (_videoPlayer) {
		_nextVideo = {
			divID:divID,
			videoID:videoID,
			videoWidth:videoWidth,
			videoHeight:videoHeight
		};
		closeCurrentVideoPlayer();
	} else {
		_divID = divID;
		_videoID = videoID;
		_videoWidth = videoWidth;
		_videoHeight = videoHeight;
		_videoDiv = $("#" + _divID);
		_videoDiv.fadeOut(400, _videoDiv_fadeOutComplete);
		_videoPlayerId = _divID + _videoID;
		_videoDiv.after(
			"<div class='section videoplayer' id='"+ _videoPlayerId +"' style='display:none;width:"+ videoWidth +"px'>" +
				"<a class='close' href='#' onclick='closeCurrentVideoPlayer();return false;'>fermer la vid&eacute;o</a>" +
				"<div id='"+ _videoPlayerId+"swf" +"'></div>" +
			"</div>");
	}
}

//----------------------------------
//  _videoDiv_fadeOutComplete
//----------------------------------

function _videoDiv_fadeOutComplete() {
	_videoPlayer = $("#" + _videoPlayerId);
	_videoPlayer.css("display","block");
	playVideo(_videoPlayerId+"swf", _videoID, _videoWidth, _videoHeight);
}

//----------------------------------
//  closeCurrentVideoPlayer
//----------------------------------

function closeCurrentVideoPlayer() {
	if (_videoPlayer) {
		_videoPlayer.remove();
		_videoDiv.fadeIn(300, _videoDiv_fadeInComplete);
	}
}

//----------------------------------
//  _videoDiv_fadeInComplete
//----------------------------------

function _videoDiv_fadeInComplete() {
	_videoPlayer = null;
	_videoDiv = null;
	if (_nextVideo) {
		displayVideoInContainer(_nextVideo.divID, _nextVideo.videoID, _nextVideo.videoWidth, _nextVideo.videoHeight);
		_nextVideo = null;
	}
}


