var array_player = new Array();
var array_thumb = new Array();
var array_thumb_bar = new Array();
var current;

/**
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
/**
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};
function select_video(video_number,opened){
	$("#youtube_bg_feed").css({marginLeft: 0})
	if(opened==false){
		$("#youtubeplayer").fadeIn();
		/*$("#youtubeplayeroverlay").fadeIn("fast");*/
		/*var arrPageSizes = ___getPageSize();
		$('#youtubeplayeroverlay').css({
			width:		arrPageSizes[0],
			height:		arrPageSizes[1]
		});*/
		$("#youtube_bg_feed").fadeIn();
		var arrPageScroll = ___getPageScroll();
		$("#youtube_bg_feed").css({
			top:	(arrPageScroll[1] + 450)
		});
	}
	current=video_number;
	// Set the player div empty
	$("#youtubeplayer").html("");
	// Put the right player
	$("#youtubeplayer").append(array_player[video_number]);
	$(".youtubeplayercontrol").click( function(){
		close_overlay();
	});
	// Slide the feed div so that the selected thumb is on the middle of the page	
	//$("#youtubevidfeed").css({marginLeft: (document.body.clientWidth/2 - 60) - 120*video_number});
	$("#youtubevidfeed").animate({marginLeft: (document.body.clientWidth/2 - 350)});
	// Reset the border on all thumbs
	$(".youtubethumb").css("border", "none");
	// Put a solid border on the selected thumb
	//$("#thumb_"+(video_number+1)).css("border", "1px solid white");
	var arrPageScroll = ___getPageScroll();
	$("#youtubeplayer").css({
		top:	(arrPageScroll[1] + 40),
		marginLeft: (document.body.clientWidth/2 - 315)
	});
}
function close_overlay(){
	$("#youtubeplayer").fadeOut("fast");
	$("#youtubeplayer").empty();
	/*$("#youtubeplayeroverlay").fadeOut("fast");*/
	$("#youtube_bg_feed").fadeOut("fast");
}
function initialize_vid_lightbox(){
	/*$("#youtubeplayeroverlay").click( function(){
		close_overlay();
	});*/
	
	var feed = new google.feeds.Feed("http://gdata.youtube.com/feeds/api/users/"+channel+"/uploads?orderby=updated&format=5");
	
	feed.setNumEntries(10)
	feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
	
	feed.load(function(result){
		if (!result.error){
			
			//for (var i = 0; i < result.feed.entries.length; i++){
			for (var i = 0; i < 5; i++){
			
				// RECUPERATION DES DONNEES
				
				var entry = result.feed.entries[i];
				
				var url_video = entry.link;
				var title_video = entry.title;
				var code_video = url_video.substr(31,11);
				
				var url_thumb  ="http://img.youtube.com/vi/" + code_video + "/3.jpg";
				var tag_thumb = "<span class=\"video_thumb\"><span class=\"thumb\"><img onclick=\"select_video("+i+",true);\" src='"+ url_thumb + "' id='thumb_"+ (i+1) + "'/></span><span class=\"video_thumb_title\">" + title_video + "</span></span>";
				
				// - dynamically generated video list is disabled
				//var tag_thumb_bar = "<img onclick=\"select_video("+ i +",false);\" src='"+ url_thumb +"' class=\"thumb_bar\"/>";
				
				var tag_player = '<div class="youtubeplayerbox"><div class="youtubeplayercontrol"></div><object width="630" height="380"><param name="movie" value="http://www.youtube.com/v/' +  code_video +'&hl=fr&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + code_video + '&hl=fr&fs=1&color1=0x990000&color2=0xFFFFFF" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="630" height="380"></embed></object></div>';
				
				
				// STOCKAGE TABLEAUX
				
				// Create an array with the object tag for the players
				array_player[i] = tag_player;
				// Create an array with the img tags of the thumbs
				array_thumb[i] = tag_thumb;
				
				// Create an array with the 5 thumbs displayed on the page - dynamically generated video list is disabled
				/*if(i<5){
					array_thumb_bar[i] = tag_thumb_bar;
				}*/
			}
			// Affichage des 5 miniatures sur la page - dynamically generated video list is disabled
			/*for (var j=0; j<=array_thumb_bar.length-1;j++)
			{
				$("#video_bar").append(array_thumb_bar[j]);
			}	*/	

			// Create a div on the container, to contain all the thumbs
			$("#youtubevidfeed").append("<div></div>");
			// Set feed container width
			//$("#youtubevidfeed").width(120*array_player.length+2);
			$("#youtubevidfeed").width(700);

			// Put all the thumbs in the div "feeds"
			for (var j=0; j<=array_thumb.length-1;j++){
				$("#youtubevidfeed div").append(array_thumb[j]);
			}
		}
	});
}	
// If window was resized, calculate the new overlay dimensions
$(window).resize(function() {
	var arrPageScroll = ___getPageScroll();
	$("#youtubevidfeed").animate({marginLeft: (document.body.clientWidth/2 - 350)});
	$("#youtube_bg_feed").css({
		top:	(arrPageScroll[1] + 450)
	});
	$("#youtubeplayer").animate({
		top:	(arrPageScroll[1] + 40),
		marginLeft: (document.body.clientWidth/2 - 315)
	});
	// Get page sizes
	var arrPageSizes = ___getPageSize();
	// Style overlay and show it
	/*$('#youtubeplayeroverlay').css({
		width:		arrPageSizes[0],
		height:		arrPageSizes[1]
	});*/
});
