var getQTEmbed = function(url, videoWidth, videoHeight) {
	var displayWidth = videoWidth;
	var displayHeight = videoHeight + 16;
	var embedHTML = '';

	embedHTML += '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" ';
	embedHTML += 'width="' + displayWidth + '" height="' + displayHeight + '" ';
	embedHTML += 'codebase="http://www.apple.com/qtactivex/qtplugin.cab">';
	embedHTML += '<param name="src" value="' + url + '" />';
	embedHTML += '<param name="autoplay" value="true" />';
	embedHTML += '<param name="controller" value="true" />';
	embedHTML += '<param name="loop" value="false" />';
	embedHTML += '<embed src="' + url + '" ';
	embedHTML += 'width="' + displayWidth + '" height="' + displayHeight + '" ';
	embedHTML += 'autoplay="true" controller="true" loop="false" pluginspage="http://www.apple.com/quicktime/" />';
	embedHTML += '</object>';
	
	return embedHTML;
}

var setupQT = function() {
	setTimeout(function() {
		$$('div.video-ecran-qt').each(function(videoEcranQT) {
			var videoEcranQTHeight = videoEcranQT.getHeight();
			var link = videoEcranQT.down('a');
			var linkHeight = link.getHeight();
			var linkPadding = ((videoEcranQTHeight - linkHeight) / 2) - 1;
			
			link.setStyle({
				paddingTop: linkPadding + 'px',
				paddingBottom: linkPadding + 'px'
			});
			
			Event.observe(link, 'click', function(event) {
				var link = event.element();
				if(link.tagName != 'A') link = link.up('a');
				link.blur();
	
				var url = link.down('input[name="url"]').value;
				var width = link.down('input[name="width"]').value * 1;
				var height = link.down('input[name="height"]').value * 1;
				
				videoEcranQT.update(getQTEmbed(url, width, height));
				event.stop();
			});
		});
	}, 200);
}

var updateVideo = function(videoId, videoTitre, videoLink) {
	var requestURL = baseURL + '/smcq/' + baseLngDir + '/video/select.php?video=' + videoId;
	var videoZone = $('video-zone');
	var videoPoste = videoZone.down('div.video-poste');

	new Ajax.Updater({ success: videoPoste }, requestURL, { 
		evalScripts: true,
		
		onSuccess: function() {
			if(videoLink) {
				var videoList = videoLink.up('div.video-list');
				videoList.select('li').each(function(e) { e.removeClassName('sel'); });
				videoLink.up('li').addClassName('sel');
			}
			
			setupQT();
		}
	});
}

var initPhotoVideoTab = function() {
	$('video-zone').hide();
	$('photo-zone').insert({ before: '<ul id="nav-intab"></ul>' });
	var photoTitre = $('photo-zone').down('.photo-list-titre').innerHTML;
	var videoTitre = $('video-zone').down('.video-list-titre').innerHTML;
	$('nav-intab').insert({ bottom: '<li class="sel"><a class="to-photo" href="#">' + photoTitre + '</a></li>' });
	$('nav-intab').insert({ bottom: '<li><a class="to-video" href="#">' + videoTitre + '</a></li>' });
	$('nav-intab').insert({ bottom: '<div class="clear"></div>' });
	$('nav-intab').select('li').each(initNavTopPtr);

	$('nav-intab').select('a').each(function(link) {
		Event.observe(link, 'click', function(event) {
			var link = event.element();
			if(link.tagName != 'A') link = link.up('a.video');
			link.blur();

			if(!link.up('li').hasClassName('sel')) {
				$('nav-intab').select('li.sel').each(function(e) { e.removeClassName('sel'); });
				link.up('li').addClassName('sel');
				
				if(link.hasClassName('to-photo')) {
					Effect.BlindUp($('video-zone'), { duration: 0.15, queue: 'end' });
					Effect.BlindDown($('photo-zone'), { duration: 0.15, queue: 'end' });
				} else {
					Effect.BlindUp($('photo-zone'), { duration: 0.15, queue: 'end' });
					Effect.BlindDown($('video-zone'), { duration: 0.15, queue: 'end' });
				}
			}
			
			event.stop();
		});
	});
}

var initVideoLinks = function() {
	$$('a.video').each(function(link) {
		var linkTitre = link.down('span.titre');
		linkTitre.hide();

		if($('tip') == undefined) {
			document.body.insert({
				bottom: '<div id="tip" style="display: none;"><div id="tip-contenu"></div><div class="nav-ptr"></div></div>'
			});

			$('tip').isActif = false;
		}

		Event.observe(link, 'mouseover', function(event) {
			$('tip-contenu').update(linkTitre.innerHTML);
			if(tipTimer) clearTimeout(tipTimer);
			$('tip').show();
			$('tip').isActif = true;
		});

		Event.observe(link, 'mouseout', function(event) {
			tipTimer = setTimeout(function() {
				$('tip').hide();
				$('tip').isActif = false;
			}, 100);
		});

		Event.observe(link, 'click', function(event) {
			var link = event.element();
			if(link.tagName != 'A') link = link.up('a.video');
			link.blur();
			var linkRel = link.rel;
			var linkHref = link.href;

			if(linkRel.match(/^video\:.+$/)) {
				var videoId = linkRel.match(/^video\:(.+)$/)[1];
				updateVideo(videoId, link.down('span.titre').innerHTML, link);
				event.stop();

			} else if(linkHref.match(/\?video=.+$/)) {
				var videoId = linkHref.match(/\?video=(.+)$/)[1];
				updateVideo(videoId, link.down('span.titre').innerHTML, link);
				event.stop();
			}
		});
	});
}

var initVideo = function() {
	$$('.video-list-titre').each(function(e) { e.hide(); });
	
	if($('photo-zone') && $('video-zone')) {
		initPhotoVideoTab();
		//$('photo-zone').hide();
	}

	if($$('a.video')) {
		initVideoLinks();
	}
	
	setupQT();
}
