/*
 *
 *	@titre: projet-carousel.
 *	@description: javascript carousel projet - Moniteur Archizz.
 *	@auteur: neov - http://www.neov.net.
 *	@creation: 20090825.
 *	@modification: -.
 *	@required: jquery 1.3.2
 *
*/


// createElement.
var dc = function(el) { return document.createElement(el); }

var p =  function() { return $(dc('p')); }

// document ready.
$(
	function()
	{
		$('#prj-carousel').find('a').each(
			function()
			{
				// evenement au click.				
				$(this).click(
					function()
					{
						return false;
					}
				);
				
				// evenement hover.
				$(this).hover(
					function()
					{
						$('#prj-carousel a').removeClass('selected');
						$(this).addClass('selected');
						// check video.
						var video = $(this).hasClass('videoThumb')? true : false;
						if (video) {
							
							var idVideo = $(this).find('img').eq(1).attr('id').replace('video_','').split('_');
							var id = idVideo[0];
							var type = idVideo[1];
							var iProjetId = idVideo[2];
							var iImageTrasferer = idVideo[3];

							var zUrl = $(this).find('img').attr('url');
							$('.img-desc').find('a').hide();

							$.ajax({
								type: "POST",
								url: zUrl,
								data: {
									'id': id,
									'type': type,
									'iProjetId': iProjetId,
									'iImageTrasferer':iImageTrasferer
								},
								dataType: 'json',
								success: function(video){
									// test la presence d'un object flash.
									if ($('.img-prj').find('object').length != 0) {
										var current = $('.img-prj').find('object').attr('id').replace('video_', '');
									} else {
										var current = 0;
									}
									
									// test si la video en correspond au rollover.
									if (current != video.video_mediaId) {
										// affichage legende et copyright.
										$('.img-desc .legendeImg').text(video.video_imageLegende);
										$('.img-desc .copyrightImg').text('© ' + video.video_copyright);
										
										// creation conteneur pour le flash.
										var $flashContent = $(dc('p'));
										$flashContent.attr({id: 'videoPlayer'});
										
										// affichage du conteneur false.
										$('.img-prj').find('p')
											.empty()
											.append($flashContent);
										
										// recuperation des parametres flash.
										var getVars = $(video.video_appercu).attr('flashvars');
										var vars = getVars.replace('image=','').split('&file=');
										var src = $(video.video_appercu).attr('src');
										
										// attribution des parametres flash.
										var parametre = {};
										parametre.allowScriptAccess = "sameDomain";
										parametre.quality = "high";
										parametre.allowFullScreen = true;
										parametre.wmode = "transparent";
										
										var flashvars = {};
										flashvars.image = vars[0];
										flashvars.file = vars[1];
										
										var attr = {};
										attr.id = 'video_' + video.video_mediaId;
										if(vars[0]) {
										var nimage = new Image();
											nimage.onload = function () {
												nimage.onload = null;
												
												// embeding flash.
												swfobject.embedSWF(src, "videoPlayer", "540", "360", "9.0.28",false, flashvars, parametre, attr);
												
											};
											
											nimage.src = vars[0];
										} else {
											swfobject.embedSWF(src, "videoPlayer", "540", "360", "9.0.28",false, flashvars, parametre, attr);
										}
									}
									
									
								}
							 });
							
						} else { // Image
							var idImg = $(this).find('img').attr('id').replace('image_','').split('_');
							var id = idImg[0];
							var type = idImg[1];
							var iProjetId = idImg[2];
							var iImageTrasferer = idImg[3];

							var zUrl = $(this).find('img').attr('url');
							$('.img-desc').find('a').show();

							$.ajax({
								type: "POST",
								url: zUrl,
								data: {
									'id': id,
									'type': type,
									'iProjetId': iProjetId,
									'iImageTrasferer':iImageTrasferer
								},
								dataType: 'json',
								success: function(image){
									// test la presence d'une image.
									if ($('.img-prj').find('img').length != 0) {
										var current = $('.img-prj').find('img').attr('src');
									} else {
										var current = 0;
									}
									
									// test si l'image en correspond au rollover.
									if (current != image.url) {

										// affichage legende et copyright.
										$('.img-desc .legendeImg').text(image.image_legende);
										$('.img-desc .copyrightImg').text('© ' + image.image_copyright);

										// preload de la nouvelle image
										var nimage = new Image();
					
										nimage.onload = function () {
											nimage.onload = null;
											
											// affichage de l'image après chargement.
											$(nimage)
												.attr({id: 'bimage_' + id + '_' + type + '_' + iProjetId, url:zUrl})
												.hide();
											$('.img-prj').find('p')
														.empty()
														.append(
															$(nimage),
															$(nimage).removeAttr('style')
														);
											$(nimage).fadeIn();
										};
										nimage.src = image.url;
									}
								}
							 });
						}
					},
					function()
					{
						return false;
					}
				);
			}
		);
		
		$('.img-desc a').click(
			function()
			{
				var idImg = $('.img-prj').find('img').attr('id').replace('bimage_','').split('_');
				var id = idImg[0];
				var type = idImg[1];
				var iProjetId = idImg[2];
				var $fermerbtn = $(dc('a'));
				var contImgW = 1200;

				$fermerbtn
					.addClass('fermer')
					.attr({href: '#', title: 'Fermer'})
					.text('Fermer');
				
				var $para = $(dc('div'));
				$para.addClass('contentImg');

				var $parainner = $(dc('p'));
				$parainner.appendTo($para);

				var $btnPrev = $(dc('a'));
				$btnPrev
					.addClass('zoom-car-prev')
					.attr({href: '#', title: ''})
					.click(
						function ()
						{
							if ($parainner.find('img.visible').prev('img').length != 0)
							{
								$parainner.find('img.visible')
									.removeClass('visible')
									.fadeOut(250)
									.queue(
										function ()
										{
											var posLink = ((contImgW - $(this).prev().width())/2) - 17;
											if (posLink < 17) posLink = 10;
											
											$btnNext.animate({right: posLink}, {duration: 500, queue: false, easing: 'swing'});
											$btnPrev.animate({left: posLink}, {duration: 500, queue: false, easing: 'swing'});

											$(this).prev()
												.fadeIn(500)
												.addClass('visible');

											$(this).dequeue();
										}
									);
							}
							return false;
						}
					);

				var $btnNext = $(dc('a'));
				$btnNext
					.addClass('zoom-car-next')
					.attr({href: '#', title: ''})
					.click(
						function()
						{
							if ($parainner.find('img.visible').next().length != 0)
							{
								$parainner.find('img.visible')
									.removeClass('visible')
									.fadeOut(250)
									.queue(
										function ()
										{
											var posLink = ((contImgW - $(this).next().width())/2) - 17;
											if (posLink < 17) posLink = 10;
											
											$btnNext.animate({right: posLink}, {duration: 500, queue: false, easing: 'swing'});
											$btnPrev.animate({left: posLink}, {duration: 500, queue: false, easing: 'swing'});

											$(this).next()
												.fadeIn(500)
												.addClass('visible');

											$(this).dequeue();
										}
									);
							}
							return false;
						}
					);

				$para.append($btnPrev, $btnNext);
				
				var $zoomImage = $(dc('div'));
				$zoomImage
					.addClass('zoomImage')
					.attr({id: 'zoomImage'})
					.append($fermerbtn, $para)
					.appendTo('body')
					.hide();

				var cnt = 0;
				var current = 0;
				var $image = $('#prj-carousel a').not('.videoThumb').find('img');
				var nbrItem = $image.length;

				// preloader.
				$image.each(
					function()
					{
						var img = new Image();
	
						img.onload = function () {
							img.onload = null;
							cnt++;

							// action a la fin du preload.
							if(cnt == nbrItem) {

								//affichage du zoom de l'image en cours
								if ($('#prj-carousel a.selected').length)
									current = $('#prj-carousel a').index($('#prj-carousel a.selected'));

								$parainner.find('img').eq(current)
									.addClass('visible')
									.fadeIn(500);

								var posLink = ((contImgW - $parainner.find('img').eq(current).width())/2) - 17;
								if (posLink < 17) posLink = 10;

									$btnNext.animate({right: posLink}, {duration: 500, queue: false, easing: 'swing'});
									$btnPrev.animate({left: posLink}, {duration: 500, queue: false, easing: 'swing'});
							}
							
						};

						$(img)
							.attr({src: $(this).attr('src').replace('_80000_54', '_1200_600').replace('format6', 'format1')})
							.css({display: 'none'});
						$parainner.append($(img));
					}
				);

				$(document).keydown(
					function(e)
					{
						var code = (e.keyCode ? e.keyCode : e.which);
						
						if(code == 37) {
							$btnPrev.click();
						} else if(code == 39) {
							$btnNext.click();
						}
					}
				);

				$fermerbtn.click (
					function ()
					{
						$parainner.find('img').removeClass('visible');
					}
				);

				$zoomImage.showElem();
				$fermerbtn.hidePop();
				
				return false;
			}
		);
		
		$('.depliComment').click(
			function()
			{
				$('#depliComment').hide();
				$('#repliComment').show();
				return false;
			}
		);
		
		$('.repliComment').click(
			function()
			{
				$('#depliComment').show();
				$('#repliComment').hide();
				return false;
			}
		);
		
		$('.depli').click(
			function()
			{
				$('#depli').hide();
				$('#repli').show();
				return false;
			}
		);
		
		$('.repli').click(
			function()
			{
				$('#depli').show();
				$('#repli').hide();
				return false;
			}
		);

		$('.repliCollaborateur').click(
			function()
			{
				$('#depliCollaborateur').show();
				$('#depliListeCollaborateur').show();

				$('#repliCollaborateur').hide();
				$('#repliListeCollaborateur').hide();
				return false;
			}
		);

		$('.depliCollaborateur').click(
			function()
			{
				$('#repliCollaborateur').show();
				$('#repliListeCollaborateur').show();

				$('#depliCollaborateur').hide();
				$('#depliListeCollaborateur').hide();
				return false;
			}
		);

/************************NON RETENU****************************/
		$('.repliIntervenant').click(
			function()
			{
				$('#depliIntervenant').show();
				$('#depliListeIntervenant').show();

				$('#repliIntervenant').hide();
				$('#repliListeIntervenant').hide();
				return false;
			}
		);

		$('.depliIntervenant').click(
			function()
			{
				$('#repliIntervenant').show();
				$('#repliListeIntervenant').show();

				$('#depliIntervenant').hide();
				$('#depliListeIntervenant').hide();
				return false;
			}
		);
/************************NON RETENU****************************/

/************************ LAUREAT *****************************/

		$('.depliCommentLaureat').click(
			function()
			{
				$('#depliCommentLaureat').hide();
				$('#repliCommentLaureat').show();
				return false;
			}
		);
		
		$('.repliCommentLaureat').click(
			function()
			{
				$('#depliCommentLaureat').show();
				$('#repliCommentLaureat').hide();
				return false;
			}
		);	


		$('.depliArchiLaureat').click(
			function()
			{
				$('#depliArchiLaureat').hide();
				$('#repliArchiLaureat').show();
				return false;
			}
		);
		
		$('.repliArchiLaureat').click(
			function()
			{
				$('#depliArchiLaureat').show();
				$('#repliArchiLaureat').hide();
				return false;
			}
		);

		$('.repliAutreIntervenantLaureat').click(
			function()
			{
				$('#depliAutreIntervenantLaureat').show();
				$('#depliListeAutreIntervenantLaureat').show();

				$('#repliAutreIntervenantLaureat').hide();
				$('#repliListeAutreIntervenantLaureat').hide();
				return false;
			}
		);

		$('.depliAutreIntervenantLaureat').click(
			function()
			{
				$('#repliAutreIntervenantLaureat').show();
				$('#repliListeAutreIntervenantLaureat').show();

				$('#depliAutreIntervenantLaureat').hide();
				$('#depliListeAutreIntervenantLaureat').hide();
				return false;
			}
		);


		$('.repliCollaborateurLaureat').click(
			function()
			{
				$('#depliCollaborateurLaureat').show();
				$('#depliListeCollaborateurLaureat').show();

				$('#repliCollaborateurLaureat').hide();
				$('#repliListeCollaborateurLaureat').hide();
				return false;
			}
		);

		$('.depliCollaborateurLaureat').click(
			function()
			{
				$('#repliCollaborateurLaureat').show();
				$('#repliListeCollaborateurLaureat').show();

				$('#depliCollaborateurLaureat').hide();
				$('#depliListeCollaborateurLaureat').hide();
				return false;
			}
		);

		$('.repliCotraitantLaureat').click(
			function()
			{
				$('#depliCotraitantLaureat').show();
				$('#depliListeCotraitantLaureat').show();

				$('#repliCotraitantLaureat').hide();
				$('#repliListeCotraitantLaureat').hide();
				return false;
			}
		);

		$('.depliCotraitantLaureat').click(
			function()
			{
				$('#repliCotraitantLaureat').show();
				$('#repliListeCotraitantLaureat').show();

				$('#depliCotraitantLaureat').hide();
				$('#depliListeCotraitantLaureat').hide();
				return false;
			}
		);

		

/************************ LAUREAT  ****************************/

/************************ PROJET EN COURS**********************/

		$('.depliCommentEnCours').click(
			function()
			{
				$('#depliCommentEnCours').hide();
				$('#repliCommentEnCours').show();
				return false;
			}
		);
		
		$('.repliCommentEnCours').click(
			function()
			{
				$('#depliCommentEnCours').show();
				$('#repliCommentEnCours').hide();
				return false;
			}
		);		

		$('.depliArchiEnCours').click(
			function()
			{
				$('#depliArchiEnCours').hide();
				$('#repliArchiEnCours').show();
				return false;
			}
		);
		
		$('.repliArchiEnCours').click(
			function()
			{
				$('#depliArchiEnCours').show();
				$('#repliArchiEnCours').hide();
				return false;
			}
		);

		$('.repliAutreIntervenantEnCours').click(
			function()
			{
				$('#depliAutreIntervenantEnCours').show();
				$('#depliListeAutreIntervenantEnCours').show();

				$('#repliAutreIntervenantEnCours').hide();
				$('#repliListeAutreIntervenantEnCours').hide();
				return false;
			}
		);

		$('.depliAutreIntervenantEnCours').click(
			function()
			{
				$('#repliAutreIntervenantEnCours').show();
				$('#repliListeAutreIntervenantEnCours').show();

				$('#depliAutreIntervenantEnCours').hide();
				$('#depliListeAutreIntervenantEnCours').hide();
				return false;
			}
		);

		$('.repliCollaborateurEnCours').click(
			function()
			{
				$('#depliCollaborateurEnCours').show();
				$('#depliListeCollaborateurEnCours').show();

				$('#repliCollaborateurEnCours').hide();
				$('#repliListeCollaborateurEnCours').hide();
				return false;
			}
		);

		$('.depliCollaborateurEnCours').click(
			function()
			{
				$('#repliCollaborateurEnCours').show();
				$('#repliListeCollaborateurEnCours').show();

				$('#depliCollaborateurEnCours').hide();
				$('#depliListeCollaborateurEnCours').hide();
				return false;
			}
		);

		$('.repliCotraitantEnCours').click(
			function()
			{
				$('#depliCotraitantEnCours').show();
				$('#depliListeCotraitantEnCours').show();

				$('#repliCotraitantEnCours').hide();
				$('#repliListeCotraitantEnCours').hide();
				return false;
			}
		);

		$('.depliCotraitantEnCours').click(
			function()
			{
				$('#repliCotraitantEnCours').show();
				$('#repliListeCotraitantEnCours').show();

				$('#depliCotraitantEnCours').hide();
				$('#depliListeCotraitantEnCours').hide();
				return false;
			}
		);

/************************ PROJET EN COURS**********************/
/************************ REALISATION**********************/

		$('.depliCommentRealisation').click(
			function()
			{
				$('#depliCommentRealisation').hide();
				$('#repliCommentRealisation').show();
				return false;
			}
		);
		
		$('.repliCommentRealisation').click(
			function()
			{
				$('#depliCommentRealisation').show();
				$('#repliCommentRealisation').hide();
				return false;
			}
		);		

		$('.depliArchiRealisation').click(
			function()
			{
				$('#depliArchiRealisation').hide();
				$('#repliArchiRealisation').show();
				return false;
			}
		);
		
		$('.repliArchiRealisation').click(
			function()
			{
				$('#depliArchiRealisation').show();
				$('#repliArchiRealisation').hide();
				return false;
			}
		);

		$('.depliCommentaireCertificationEnvironnement').click(
			function()
			{
				$('#depliCommentaireCertificationEnvironnement').hide();
				$('#repliCommentaireCertificationEnvironnement').show();
				return false;
			}
		);
		
		$('.repliCommentaireCertificationEnvironnement').click(
			function()
			{
				$('#depliCommentaireCertificationEnvironnement').show();
				$('#repliCommentaireCertificationEnvironnement').hide();
				return false;
			}
		);	

		$('.depliCommentaireCertificationEnergetique').click(
			function()
			{
				$('#depliCommentaireCertificationEnergetique').hide();
				$('#repliCommentaireCertificationEnergetique').show();
				return false;
			}
		);
		
		$('.repliCommentaireCertificationEnergetique').click(
			function()
			{
				$('#depliCommentaireCertificationEnergetique').show();
				$('#repliCommentaireCertificationEnergetique').hide();
				return false;
			}
		);	

		$('.repliAutreIntervenantRealisation').click(
			function()
			{
				$('#depliAutreIntervenantRealisation').show();
				$('#depliListeAutreIntervenantRealisation').show();

				$('#repliAutreIntervenantRealisation').hide();
				$('#repliListeAutreIntervenantRealisation').hide();
				return false;
			}
		);

		$('.depliAutreIntervenantRealisation').click(
			function()
			{
				$('#repliAutreIntervenantRealisation').show();
				$('#repliListeAutreIntervenantRealisation').show();

				$('#depliAutreIntervenantRealisation').hide();
				$('#depliListeAutreIntervenantRealisation').hide();
				return false;
			}
		);


		$('.repliCollaborateurRealisation').click(
			function()
			{
				$('#depliCollaborateurRealisation').show();
				$('#depliListeCollaborateurRealisation').show();

				$('#repliCollaborateurRealisation').hide();
				$('#repliListeCollaborateurRealisation').hide();
				return false;
			}
		);
		

		$('.depliCollaborateurRealisation').click(
			function()
			{
				$('#repliCollaborateurRealisation').show();
				$('#repliListeCollaborateurRealisation').show();

				$('#depliCollaborateurRealisation').hide();
				$('#depliListeCollaborateurRealisation').hide();
				return false;
			}
		);
		/*MAJ 12/11/2009*/
		$('.repliAgenceCollaborateurRealisation').click(
			function()
			{
				var indice = $(this).attr('iIndice');
				
				$('#depli_'+indice).show();
				$('#depliAgence_'+indice).show();
				$('#repli_'+indice).hide();
				$('#repliAgence_'+indice).hide();
				
				return false;
			}
		);
		

		$('.depliAgenceCollaborateurRealisation').click(
			function()
			{
				var indice = $(this).attr('iIndice');
				
				$('#repli_'+indice).show();
				$('#repliAgence_'+indice).show();
				$('#depli_'+indice).hide();
				$('#depliAgence_'+indice).hide();
				
				return false;
			}
		);
		/*FIN MAj 12/11/2009*/

		$('.repliCotraitantRealisation').click(
			function()
			{
				$('#depliCotraitantRealisation').show();
				$('#depliListeCotraitantRealisation').show();

				$('#repliCotraitantRealisation').hide();
				$('#repliListeCotraitantRealisation').hide();
				return false;
			}
		);

		$('.depliCotraitantRealisation').click(
			function()
			{
				$('#repliCotraitantRealisation').show();
				$('#repliListeCotraitantRealisation').show();

				$('#depliCotraitantRealisation').hide();
				$('#depliListeCotraitantRealisation').hide();
				return false;
			}
		);

		$('.depliEntreprise').click(
			function()
			{
				$('#depliEntreprise').hide();
				$('#repliEntreprise').show();
				return false;
			}
		);
		
		$('.repliEntreprise').click(
			function()
			{
				$('#depliEntreprise').show();
				$('#repliEntreprise').hide();
				return false;
			}
		);

		$('.depliMateriau').click(
			function()
			{
				$('#depliMateriau').hide();
				$('#repliMateriau').show();
				return false;
			}
		);
		
		$('.repliMateriau').click(
			function()
			{
				$('#depliMateriau').show();
				$('#repliMateriau').hide();
				return false;
			}
		);

		$('.depliProduit').click(
			function()
			{
				$('#depliProduit').hide();
				$('#repliProduit').show();
				return false;
			}
		);
		
		$('.repliProduit').click(
			function()
			{
				$('#depliProduit').show();
				$('#repliProduit').hide();
				return false;
			}
		);	

/************************ REALISATION**********************/
		}
);
/*teste si des champs sont vides lors de l'envoi du projet mail*/
/*function testeSiVide(value,id){
	if(value==undefined || value==''){
		switch (id) {
			case 'exp-nom':
				$('#'+id).val('Vos nom et prénom');
				break;
			case 'exp-mail':
				$('#'+id).val('Votre e-mail');
			break;
			case 'dest-nom':
				$('#'+id).val('Vos nom et prénom *');
			break;
			case 'dest-mail':
				$('#'+id).val('Votre e-mail *');
			break;
			case 'message':
				$('#'+id).val('commentaire');
			break;
			
		}
	}
return false;
}*/
