
$(document).ready(function() {

	// Accès client
	$('#identification div.open').click(function() {
		$('#identification div.content').slideToggle('slow', function() {
			$this = $('#identification div.open a');
			if($this.hasClass('open')) {
				$this.removeClass('open');
			} else {
				$this.addClass('open');
			}
		});

		return false;
	});

	// Lien - Projet
	$('#bouton-projets').hover(function() { 
	
	/*$(this).animate({left:"+=5px"},40).animate({top:"+=5px"},40)
    .animate({top:"-=10px"},40).animate({left:"-=10px"},40)
    .animate({top:"+=5px"},40).animate({left:"+=5px"},40)
    .animate({left:"+=5px"},40).animate({top:"+=5px"},40)
    .animate({top:"-=10px"},40).animate({left:"-=10px"},40)
    .animate({top:"+=5px"},40).animate({left:"+=5px"},40); */
	}, function() {  });

	// Survol travaux
	$('img.hover').insetBorder({speed: 750, opacityMax: 1});

	// Flèches news
	$('img.news-up').click(function() {
		currentNews = parseInt($('div.actualite.current').attr('id').replace('news-', ''));

		if(currentNews > 0 && $('#news-'+(currentNews-1)).length > 0)
		{
			// Changement classe
			$('div.actualite.current').removeClass('current');
			$('#news-'+(currentNews-1)).addClass('current');

			$('div.news').scrollTo($('#news-'+(currentNews-1)), {duration: 1000});
		}
	});
	$('img.news-down').click(function() {
		currentNews = parseInt($('div.actualite.current').attr('id').replace('news-', ''));

		if($('#news-'+(currentNews+1)).length > 0)
		{
			// Changement classe
			$('div.actualite.current').removeClass('current');
			$('#news-'+(currentNews+1)).addClass('current');
			
			$('div.news').scrollTo($('#news-'+(currentNews+1)), {duration: 1000});
		}
	});

	// Formulaire contact - Switch Pro / Part
	$('#contact-type').live('change', function() {
		theValue = $('#contact-type :selected').val();

		if(theValue == '2') {
			$('p.form-societe').fadeIn('slow');
		} else {
			$('p.form-societe').fadeOut('slow');
		}
	});

	// Notre savoir faire
	$('div.opener a.open').click(function() {

		if($(this).parent().hasClass('open'))
			return false;

		$('div.opener.open div.content').slideUp('slow');
		$('div.opener.open').removeClass('open');

		$(this).next('div.content').slideToggle('slow');

		$(this).parent().addClass('open');

		return false;
	});
/*
	// Clients
	$('.sponsorFlip').click(function() {

		var elem = $(this);

		if(elem.data('flipped'))
		{
			elem.revertFlip();

			elem.data('flipped', false)
		} else {
			elem.flip({
				direction: 'lr',
				speed: 350,
				color: '#f00',
				onBefore: function() {
					elem.html(elem.siblings('.sponsorData').html());
				}
			});

			elem.data('flipped', true);
		}
	});
*/

	// Références
	$('.references .reference div.ibe_outer').hover(function() {
		$(this).css({border: '1px solid #EF8900'});
	}, function() {
		$(this).css({border: '1px solid #000'});
	});

	// Commercial
	$('div.content div.exemples a').click(function() {
		currentView = $(this).parent().find('img.current');
		currentType = $(this).attr('id').replace('exemples-', '').replace('-previous', '').replace('-next', '')
		currentViewId = parseInt(currentView.attr('id').replace(currentType+'-preview-', ''));

		nbView = $(this).parent().find('img.preview').length;

		if($(this).hasClass('gauche'))
		{
			if((currentViewId-1) > 0)
			{
				$('#'+currentType+'-preview-'+(currentViewId-1)).addClass('current').removeClass('hide');
				currentView.removeClass('current').addClass('hide');
			}
		}
		else if($(this).hasClass('droite'))
		{
			if((currentViewId+1) <= nbView)
			{
				$('#'+currentType+'-preview-'+(currentViewId+1)).addClass('current').removeClass('hide');
				currentView.removeClass('current').addClass('hide');
			}
		}

		return false;
	});

	// Titres
	if($('div#bloc-titre').length == 1)
	{
		var pxToTop = 275;
		var pxLargeur = 360;
		var positionPage = $('#page').offset();

		$('div#bloc-titre').css('padding-left', positionPage.left);
		$('div#bloc-titre').css('left', 0);
		$('div#bloc-titre').css('top', pxToTop);
		$('div#bloc-titre').css('width', pxLargeur);
		$('div#bloc-titre').css('z-index', 5);
		$('div#bloc-titre').appendTo('body');
	}
	if($('div.bloc-titre').length > 0)
	{
		var pxToTop = 285;
		var pxLargeur = 800;
		var positionPage = $('#page').offset();

		$('div.bloc-titre').each(function() {
// alert('ok');
			$(this).css('padding-left', positionPage.left);
			$(this).css('left', 0);
			$(this).css('top', pxToTop);
			$(this).css('width', pxLargeur);
			$(this).css('z-index', 5);
			$(this).appendTo('body');

			pxToTop += 85;
		});
	}

	// Diaporama accueil
	$('#slides').slides({
		preload: true,
		preloadImage: 'img/loading.gif',
		play: 4000,
		pause: 2000,
		hoverPause: true,
		container: 'slides_container'
	});

	// Clients
	$('div.client').each(function() {
		var desc = $(this).find('span.description');
		var image = $(this).find('span.image');

		// Css
		$(this).css({'position': 'relative', 'width': '180px', 'height': '180px', 'margin': '30px', 'float': 'left'});
		desc.css({'position': 'absolute', 'top': 10, 'left': 10, 'background': 'transparent url(images/interface/filtre-gris.png) repeat top left', 'display': 'block', 'width': '140px', 'height': '140px', 'color': '#fff', 'font-size': '0.7em', 'padding': '10px'});
		desc.find('a').css({'color': '#FEC10F', 'text-decoration': 'none', 'margin-top': '4px'});
		image.css({'position': 'absolute', 'top': 0, 'left': 0});
		
		desc.hide();

		$(this).hover(function() {
			desc.slideDown();
		}, function() {
			desc.slideUp();
		});
	});

	// PortFolio
	if($('#portfolio').length == 1)
	{
		function hidePhotosForRef(ref) {
			$('#portfolio div#ref'+ref+' div.content div.images img').each(function(key) {
				if(key >= 1)
					$(this).hide();
				else
					$(this).show();
			});
		}

		function gestionLiens(currentRef, currentImg, totalImg)
		{
			// On cache le lien si plus d'images
			if(currentImg == totalImg) {
				$('#portfolio div#ref'+currentRef+' a.next').remove();
			}
			if(currentImg == 1) {
				$('#portfolio div#ref'+currentRef+' a.previous').remove();
			}
			if(currentImg > 1) {
				$('<a href="#" class="previous"><img src="images/portfolio/fleche-gauche.png" alt="Gauche" style="top:120px; left: 10px;" /></a>').appendTo($('#portfolio div.reference.current div.images'));
			}
			if(currentImg < totalImg) {
				$('<a href="#" class="next"><img src="images/portfolio/fleche-droite.png" alt="Droite" style="top:120px; right: 10px;" /></a>').appendTo($('#portfolio div.reference.current div.images'));
			}
		}

		function updateNbImages(currentRef)
		{
			return $('#ref'+currentRef+' div.content div.images img.img').length;
		}

		var portfolio = $('#portfolio');

		var currentRef = 1;
		var currentImg = 1;

		var nbReferences = $('#portfolio div.reference').length;
		var nbImages = updateNbImages(currentRef);

		// On cache toutes les références
		portfolio.find('div.reference div.content').hide();
		// Sauf la première
		portfolio.find('div.reference div.content').first().show();
		// On cache les contenus
		hidePhotosForRef(currentRef);

		gestionLiens(currentRef, currentImg, nbImages);

		/* Lien suivant (images) */
		$('#portfolio a.next, #portfolio a.previous').live('click', function() {

			var next = false;
			if($(this).hasClass('next'))
				next = true;

			portfolioChangeImg(next);

			return false;
		});

		function portfolioChangeImg(isNext)
		{
			// Cacher
			$('#portfolio div.reference.current div.images img#ref'+currentRef+'-img'+currentImg).fadeOut().removeClass('current');

			// Nouvelle image
			if(isNext) {
				currentImg += 1;
			} else {
				currentImg -= 1;
			}

			// Afficher
			$('#portfolio div.reference.current div.images img#ref'+currentRef+'-img'+currentImg).fadeIn().addClass('current');

			// Liens images Suivant / Précédent
			gestionLiens(currentRef, currentImg, nbImages);
		}

		/* Changement de référence */
		$('#portfolio div.titre a').live('click', function() {
			var clickRef = $(this).parent().parent().attr('id').replace('ref', '');

			portfolioChangeRef(clickRef);

			return false;
		});

		function portfolioChangeRef(newRef)
		{
			if(currentRef == newRef)
				return false;

			// Cacher
			$('#portfolio div#ref'+currentRef).removeClass('current').find('div.content').animate({width: 'toggle'}).find('a.previous, a.next').remove();
			// Nouvelle ref
			currentRef = newRef;
			currentImg = 1;
			// Afficher
			$('#portfolio div#ref'+currentRef).addClass('current').find('div.content').animate({width: 'toggle'});

			nbImages = updateNbImages(currentRef);
			hidePhotosForRef(currentRef);
			gestionLiens(currentRef, currentImg, nbImages);
		}

		/**
			Navigation
		**/
		function portfolioCheckKey(e)
		{
            switch (e.keyCode)
			{
	            case 39:
					if($('#ref'+(currentRef+1)).length == 1) {
						portfolioChangeRef((currentRef+1));
					}

	                break;
	            case 37:
					if($('#ref'+(currentRef-1)).length == 1) {
						portfolioChangeRef((currentRef-1));
					}

	                break;
				default:
					break;
            }
        }

        if ($.browser.mozilla) {
            $(document).keypress(portfolioCheckKey);
        } else {
            $(document).keydown(portfolioCheckKey);
        }
	}

	// Page contact
	$('input[title], textarea[title]').example(function() {
		return $(this).attr('title');
	}, {
		className: 'example'
	});

	// Changement des fonds
	$('#fonds a').click(function() {
		var choixFond = $(this).attr('fond'); //$(this).attr('href').replace('choix-fond-', '').replace('.html', '');

		$.ajax({
			url: 'includes/ajax.php',
			type: 'POST',
			data: 'action=fond&f='+choixFond,
			success: function(data) {
				$('body').css('background-image', 'url(images/interface/fonds/'+choixFond+')');
			}
		});

		return false;
	});

	// Puces qui sommes-nous
	$('div.quisommesnous div.puces a img').hover(function() {

		var cur = $(this);

		// Textes
		if(cur.hasClass('puce-mel')) {
			var titre = 'Mise en ligne';
			var texte = 'Accompagnement, disponibilité, formation, évolution';
		} else if (cur.hasClass('puce-prod')) {
			var titre = 'Production';
			var texte = 'Développement Web, multimedia, smartphones, site, multilingue, réalisation vidéo';
		} else if (cur.hasClass('puce-crea')) {
			var titre = 'Création';
			var texte = 'Graphisme, édition, design, photographie, vidéo';
		}  else if (cur.hasClass('puce-proj')) {
			var titre = 'Gestion de projet';
			var texte = 'Stratégie, concept, plan d\'action, budget, planning de production, conseils';
		}

		// Html
		var htmlContent = '<div class="bulle"><div class="bulle-titre">'+titre+'</div><div class="bulle-content">'+texte+'</div></div>';

		// Positions de l'image
		var posImg = $(this).offset();

		// On ajoute
		$('body').append(htmlContent);

		// Css
		$('div.bulle').css({left: (posImg.left-$('div.bulle').width()-5), top: (posImg.top-($('div.bulle').height()/2)+7)});
	}, function() {
		// On supprime la bulle
		// $('div.bulle').fadeOut(function() {
			$('div.bulle').remove();
		// });
	});

	// Gallerie
	if($('.ad-gallery').length > 0)
	{
		var galleries = $('.ad-gallery').adGallery({
			 width: 800,
			 height: 600
		});
	}
	$('img.fancybox').click(function() {
		$.fancybox({
			href: '#ad-gallery',
			autoDimensions: false,
			width: '840',
			height: '740',
			onStart: function() {
				$('#ad-gallery').show();
				$('#ad-gallery').parent().show();
			},
			onClosed: function() {
				$('#ad-gallery').hide();
				$('#ad-gallery').parent().hide();
			}
		});
	});

	$('#ad-gallery-video').hide();
	$('img.fancyboxvideo').click(function() {

		var vid=$(this).attr("rel");
		$.fancybox({
			href: '#ad-gallery-video',
			autoDimensions: false,
			width: '640',
			height: '540',
			onStart: function() {

				$('#ad-gallery-video').show();
				$('#ad-gallery-video').parent().show();
				play_video(vid);
			},
			onClosed: function() {
				$('#ad-gallery-video').hide();
				$('#ad-gallery-video').parent().hide();
			}
		});
	});
	// Checkbox
	$('p.check-newsletter img').click(function() {
		// alert($(this).attr('src'));

		if(!$(this).hasClass('client')) {
			$('#newsletter-client').val('0');
			$('p.check-newsletter img.client').attr('src', 'images/interface/check-client.png');
			$('p.check-newsletter img').not('.client').attr('src', 'images/interface/check-pas-client.png');
			
		} else {
			$('#newsletter-client').val('1');
			$('p.check-newsletter img.client').attr('src', 'images/interface/check-pas-client.png');
			$('p.check-newsletter img').not('.client').attr('src', 'images/interface/check-client.png');
		}
	});

	/**
		Webmail
	**/
	$('a.webmail').click(function() {
		$.fancybox({
			href: '#fancybox-email',
			onStart: function() {
				$('#fancybox-email').show();
				$('#dmn-email').val('http://');
				$('#fancybox-email form p.erreur').remove();
			},
			onClosed: function() {
				$('#fancybox-email').hide();
			}
		});

		return false;
	});
	// Validation
	$('#dmn-submit').click(function() {
		// alert($('#dmn-email').val());
		$.ajax({
			url: 'includes/ajax.php',
			type: 'POST',
			dataType: 'json',
			data: 'action=getWebmail&d='+$('#dmn-email').val(),
			success: function(data) {
				if(data)
				{
					if(data.result == 'erreur')
					{
						if($('#fancybox-email form p.erreur').length == 0) {
							$('#fancybox-email form').prepend('<p class="erreur"></p>');
						}

						$('#fancybox-email form p.erreur').html(data.erreur);
					}
					else
					{
						if(!data.webmail)
						{
							if($('#fancybox-email form p.erreur').length == 0) {
								$('#fancybox-email form').prepend('<p class="erreur"></p>');
							}

							$('#fancybox-email form p.erreur').html('Impossible de récupérer les informations du domaine.');
						}
						else
						{
							$.fancybox('<p style="padding: 15px;">Pour accèder à votre webmail <a href="'+data.webmail+'" title="Consulter mon webmail" onclick="window.open(this.href); return false;" style="color: #FFA200;" class="open-webmail">cliquez ici</a>.</p>');
						}
					}
				}
			}
		});
	});
	$('a.open-webmail').live('click', function() { $.fancybox.close(); });

	/* Fesse de bouc */
	$('div.fb-like').attr({
		'data-href': 'https://www.facebook.com/siteenligne',
		'data-send': 'false',
		'data-layout': 'button_count',
		'data-width': '120',
		'data-show-faces': 'false',
		'data-action': 'like',
		'data-font': 'verdana'
	});
});

function play_video(vid) {

	$("#player").attr("href", vid);

	flowplayer("player", "swf/flowplayer.swf",  {
		clip: {
			autoPlay: true, 
			autoBuffering: true 
		}
	});
}
