// DG ENTERPRISE GALLERY PLAYER
$(document).ready(function() {

	// Parse each link with the right attr title and 
	// attemps to create a gallery if the link points 
	// to an xml file 
	$('a[title=gallery-dg-enterprise]').each(function(){
		
		href = $(this).attr('href');
		hrefL = href.length;
		
		isXml = href.substr(hrefL-4);
		
		if (isXml == '.xml') {
			
			$(this).parent('p').after('<div id="dge-gallery-player"><div id="dge-player-wrapper"></div>' +
											'<span id="dge-gallery-player-prev" class="disabled">Previous</span>' +
											'<span id="dge-gallery-player-next">Next</span>' +
											'<ul id="dge-gallery-player-ul"></ul>' +
										'</div>');
			
			$(this).remove();
			
			var type, path, title, desc;
			
			$.ajax({
				type: 'GET',
				url: href,
				dataType: 'xml',
				success: function(xml) {
					
					if (xml) {
							
						var media = '', i = 1, length = $(xml).find('media').length;
						
						$(xml).find('media').each(function() {
														   
							type	= $(this).find('type').text();
							path 	= $(this).find('path').text();
							title 	= $(this).find('title').text();
							desc	= $(this).find('description').text();
							_class	= ' ';
							tooWide = '';
							
							_class += (i==1) ? ' active first' : '';
							_class += (i >= length) ? ' last' : '';
							
							if (type != 'flv' && type != 'mp4') {
								if (getImgSize(path) >= 590) {
									tooWide = ' class="too-wide"';
								}
								media  += '<li class="' + _class + '" data="' + i + '"><img src="'+ path +'"' + tooWide + ' alt="' + title + '" title="' + title +'" /><div class="desc">' + desc +'</div></li>';								   
							} else {
								media  += '<li class="' + _class + ' video" id="dge-video-' + i + '" data="' + i + '"><a href="'+ path +'">' + title + '</a><div class="desc">' + desc +'</div></li>';
							}
						
							i += 1;
							
						});
						
						$('#dge-gallery-player-ul').append(media);
						
						// Init the first item of the player
						(function() {
						
							$('#dge-gallery-player-prev').css('opacity', 0.5);
							
							// Create the counter
							$('#dge-gallery-player').append('<p id="dge-gallery-player-counter" data="' + length +'">1 / ' + length + '</p>');
/*
							// Insert a link to the original gallery
							curLang = $('html').attr('lang');
							linkToGal = href.substr(0, hrefL-14) + 'index_' + curLang + '.htm';
							
							switch(curLang) {
								case 'en':
									labelToGal = 'Go to the original gallery';
								break;
								
								case 'fr':
									labelToGal = 'Voir la galerie originale';	
								break;
								
								default:
									labelToGal = 'Go to the original gallery';
								break;
							}
							
							$('#dge-gallery-player').after('<p><a href="' + linkToGal + '" title="' + labelToGal + '">' + labelToGal + '</a></p>');
*/							
							if ($('#dge-gallery-player-ul li.active').hasClass('video')) {
								initPlayer($('#dge-gallery-player-ul li.active:eq(0) a'));
							}
						
						}());
					}
					
				}
			});
		
		}
		
	});
	
	$('#dge-gallery-player-prev:not(.disabled)').live('click', function() {
		changeMedia('prev');
	});
	
	$('#dge-gallery-player-next:not(.disabled)').live('click', function() {
		changeMedia('next');											 
	});
	
});

function getImgSize(imgSrc){
    var newImg = new Image();
    newImg.src = imgSrc;
    var height = newImg.height;
    var width = newImg.width;
    p = $(newImg).ready(function(){
        return {width: newImg.width, height: newImg.height};
    });
    return p[0]['width'];
}

// Insert stylesheet ASAP
(function(d, l) {
		  
	var	c 	= d.createElement(l),
		h 	= d.getElementsByTagName('head')[0];
		
	c.rel 	= 'stylesheet';
	c.href 	= '/enterprise/stylesheets/gallery-player.css';
	
	h.appendChild(c);
	
}(document, 'link'));


// Change the current media
function changeMedia (dir) {
	
	// Video le player video
	$('#dge-player-wrapper').html('');
	
	current = $('#dge-gallery-player-ul li.active:eq(0)');
	current.removeClass('active')
	
	if (dir == 'next') {
		
		current.next('li').addClass('active');
		$('#dge-gallery-player-prev').removeClass('disabled').css('opacity', 1);
		
		if ($('#dge-gallery-player-ul li.active:eq(0)').hasClass('last')) {
			$('#dge-gallery-player-next').addClass('disabled').css('opacity', 0.5);
		}
		
	} else {
	
		current.prev('li').addClass('active');
		$('#dge-gallery-player-next').removeClass('disabled').css('opacity', 1);
		
		if ($('#dge-gallery-player-ul li.active:eq(0)').hasClass('first')) {
			$('#dge-gallery-player-prev').addClass('disabled').css('opacity', 0.5);
		}
	
	}
	
	// Update counter
	updateCounter();
	
	// If current media is a video and hasn't been initialized yet
	if ($('#dge-gallery-player-ul li.active').hasClass('video') ) {
		initPlayer($('#dge-gallery-player-ul li.active:eq(0) a'));
	}
	
}

// Init SWF player
function initPlayer(target) {

	var 
		videoPath 	= target.attr('href'),
		videoPathL	= videoPath.length,
		imagePath 	= videoPath.substr(0, videoPathL-4),
		path		= '',
		video 		= new SWFObject('http://ec.europa.eu/wel/players/jwflvplayer/player.swf', 'dge-gallery-player-area', '80%', '280', '9'),
		_id			= target.parent('li').attr('id');
	
	// Create the image path to /images-videos/ folder
	imagePath	= imagePath + '.jpg'
	path		= imagePath.split('/');
	imagePathL	= path.length;
	imagePath	= '';
	
	for (i=0; i < imagePathL; i++) {
		if (i+1 == imagePathL) {
			imagePath += 'img-videos/' + path[i];
		} else {
			imagePath += path[i] + '/';
		}
	}
	
	video.addParam('flashvars', '&file=' + videoPath + '&image=' + imagePath);
	video.addParam('allowfullscreen', 'true');
	video.addParam('allowscriptaccess','always');
	video.addParam('allownetworking','all');
	video.addParam('wmode','opaque');
	
	video.write('dge-player-wrapper');

}

function updateCounter() {
	_cur = $('#dge-gallery-player-ul li.active:eq(0)').attr('data');
	_max = $('#dge-gallery-player-counter').attr('data');
	$('#dge-gallery-player-counter').html(_cur + ' / ' + _max);
}



