document.vidGalleryXML = null;
document.vidGalleryThumbsPath = "images/"; // Configuré dans le XML. Sous-dossier des thumbnails. Ex: "images/humanima1/";
document.vidGalleryVidPath = ""; // Configuré dans le XML. Sous-dossier des vidéos (sous flash/ressources/...). Ex: "humanima1/";
document.websiteUrl = "";
var vidGalleryItem = null;
var autoPlay = true;
var myPlayer = null;

// Initialisation du portfolio
function vidGalleryInit(xmlFile) {
	// Emplacement du vidéo
	document.websiteUrl = "http://www.productionsnovamedia.com/";
	try { // Adresse absolue pour le player flash
		var loc = String(document.location);
		document.websiteUrl = loc.substring(0, loc.lastIndexOf("/") + 1);
	} catch (e) { }
	
	$.ajax({
		type: "GET",
		encoding:"UTF-8",
		url: xmlFile,
		dataType: "xml",
		success: vidGalleryOnXMLLoaded,
		error: vidGalleryOnError
	});
}

// Affichage d'une vidéo
function vidGalleryShowItem(idx) {
	var itemNode = $(document.vidGalleryXML).find('item').eq(idx);
	var file = $(itemNode).find('vid').text();
	vidGalleryItem = idx;
	
	var vidPlayer = getSwf("flashVideoPlayer");
	
	if(!vidPlayer) {
		// Premier affichage, création du player
		var newVideoContent = '<object id="flashVideoPlayer" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="592" height="364" title="Player">';
				newVideoContent += '<param name="movie" value="flash/lecteurVideo.swf?mediaSource=' + file + '&imagesPath=' + document.websiteUrl + 'flash/ressources/' + document.vidGalleryVidPath + '&videoPath=' + document.websiteUrl + 'flash/ressources/' + document.vidGalleryVidPath + '" />';
				newVideoContent += '<param name="quality" value="high" />';
				newVideoContent += '<param name="allowFullScreen" value="true" />';
				newVideoContent += '<param name="allowScriptAccess" value="always" />';
				newVideoContent += '<embed name="flashVideoPlayer" allowFullScreen="true" src="flash/lecteurVideo.swf?mediaSource=' + file + '&imagesPath=' + document.websiteUrl + 'flash/ressources/' + document.vidGalleryVidPath + '&videoPath=' + document.websiteUrl + 'flash/ressources/' + document.vidGalleryVidPath + '" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" allowScriptAccess="always" width="592" height="364"></embed>';
			newVideoContent += '</object>';
		$('#player').html(newVideoContent);
	} else {
		vidPlayer.loadVideo(file);
	}
	
	// Mise a jour selection
	$('#galleryThumbs .active').removeClass("active");
	$('#vidThumb_' + idx).addClass("active");
}

function getSwf(movieName) {
   if (navigator.appName.indexOf("Microsoft") != -1 && navigator.appVersion.indexOf("MSIE 9.") == -1) {
        return window[movieName];
    } else {
        return document[movieName];
   }
}

function vidGalleryOnError(xhrInstance, message, optional) {
	alert("Error: " + message);
}

// XML chargé, création des thumbnails
function vidGalleryOnXMLLoaded(xml) {
	// Paramètres globaux
	document.vidGalleryXML = xml;
	document.vidGalleryThumbsPath = $(xml).find("gallery").attr("thumbPath");
	document.vidGalleryVidPath = $(xml).find("gallery").attr("vidPath");
	
	// Construction des thumbnails
	$('#galleryThumbs').html("");
	var items = $(xml).find('item');
	items.each(function (idx) {
		var thumb = $(this).find('thumb').text();
		var title = $(this).find('title').text();
		$('<a href="#" id="vidThumb_' + idx + '" title="' + title + '"><img src="' + document.websiteUrl + document.vidGalleryThumbsPath + thumb + '.jpg" alt="' + title + '" /></a>').appendTo('#galleryThumbs');
		$('#vidThumb_' + idx).click(function () { vidGalleryShowItem(idx); return false; });
	});
	vidGalleryShowItem(0);
}
