/*
 * Require jQuery JavaScript Library v1.3.2+
 * http://jquery.com/
 *
 * Copyright © 2011 MBA Multimédia (www.mba-multimedia.com)
 * 
 *
 */
 
var ie,ie7,ie8,ff,ff3;


$(function(){
	ie = $.browser.msie;
	ie7 = ie && parseInt($.browser.version.slice(0, 1)) <= 7;
	ie8 = ie && parseInt($.browser.version.slice(0, 1)) <= 8;
	ff = $.browser.mozilla;
	ff3 = ff && parseInt($.browser.version.slice(0, 1)) <= 3;
	/*if (ie) {
		ph();
	}**/
	
	// PARAGRAPHES
	if ($('.paragraphes').length) {
		initParagraphes ();
	}
	
	// RETOUR HAUT DE PAGE
	$('#btn-hdp a').attr('href','').click(function(){ $('html').stop().scrollTo(0,500); return false; });

	// MENU
	$('#header .menuprim-ctn').css({ height:0, display:"block"});
	$('#header .menuprim').hover(openMenu, closeMenu);
	
	// LISTINGS
	if($('.element').length){
		$('.element').hover(cadreOn, cadreOff);
		$('.element-ctn').click(cadreClick);
	}	
	
	// OVERLAY
	if ($('a[rel]').length){
		//console.log("a[rel] > overlay");
		$('a[rel]').overlay({
			mask: {
				color: '#fff',
				loadSpeed: 200,
				opacity:0.8
			},
			//top:'2%',
			closeOnClick:true
		});
	}
	
	// FILTRE LISTING
	if($('#filtre').length){
		$('#filtre select').change(function(){ this.form.submit(); });
	}
	
	// REFLECTION
	//$("img.reflect").reflect({});
	
}); 

function openMenu (){
	$('#header .menuprim-ctn').stop().animate({height:123}, 200);
}
function closeMenu (){
	$('#header .menuprim-ctn').stop().animate({height:0}, 400);
}
function cadreOn (){
	$(this).addClass ('on');
}
function cadreOff (){
	$(this).removeClass ('on');
}

function cadreClick (){
	window.open($(this).find('a').attr('href'), '_top');
}

function testNavigateur () {
	// Désactiver le javascript pour Iinternet Explorer 6 et plus vieux
	return !(jQuery.browser.msie && jQuery.browser.version.substr(0,1)<8);
}

function initParagraphes () {
	if(testNavigateur ()) {
		
			var style = '<style  type="text/css"><!--\n';
			style += '.paragraphes .paragraphe-ttr { cursor:pointer; }\n';
			style += '.paragraphes .o .paragraphe-ctn { display:block; }\n';
			style += '.paragraphes .f .paragraphe-ctn { display:none;}\n';
			style += '.paragraphes .o .btn-moins { display:block; }\n';
			style += '.paragraphes .f .btn-plus { display:block; }\n';
			style += '--></style>\n';
			
			$('head').append (style);
			
			$('.col-d .paragraphe-ttr').hover(cadreOn, cadreOff);
			
			$('.paragraphe').each (function(){
				var id = $(this).attr("id");
				//console.log("paragraphe id :"+id);
			
				// État initial
				if ($(this).is(".o")) {
					openParagraphe (id);
				} else if ($(this).is (".f")) {
					$(this).find('.paragraphe-ctn').hide ();
					closeParagraphe (id, 0);
				}
			
				// Clic sur titre paragraphe
				$(this).find('.paragraphe-ttr').click(function (){ openCloseParagraphe(id); } );
				
				// Clic sur bouton ouvrir
				$(this).find('.btn-plus').click(function (){ openParagraphe(id); return false; } );

				// Clic sur bouton fermer
				$(this).find('.btn-moins').click(function (){ closeParagraphe(id); return false; } );
				
			});
	} else {
		// on laisse tout ouvert...
	}
}

function openCloseParagraphe (id) {
	// Si ni la classe CSS "f" ni la classe CSS "o" n'est spécifiée, le paragraphe doit rester ouvert.
	if ($("#"+id).is(".o") || $("#"+id).is(".f")) {
		if ($("#"+id).find('.paragraphe-ctn').is(':hidden')) {
			openParagraphe (id);
		} else {
			closeParagraphe (id);
		}
	}		
}

function openParagraphe (id) {
	// masquer le bouton ouvrir
	$("#"+id+' .btn-plus').hide('slow');

	// afficher le bouton fermer
	$("#"+id+' .btn-moins').show('slow');

	// Ouvrir le calque
	$("#"+id+' .paragraphe-ctn').stop().slideDown ();
	/*var h = $("#"+id+' .paragraphe-ctn')[0].scrollHeight;
	console.log (h);
	$("#"+id+' .paragraphe-ctn').animate({height:h}, 200); */
}

function closeParagraphe (id) {
	// afficher le bouton ouvrir
	$("#"+id).find('.btn-plus').show('slow');

	// masquer le bouton fermer
	$("#"+id).find('.btn-moins').hide('slow');

	// Fermer le calque
	$("#"+id).find('.paragraphe-ctn').stop().slideUp (); 
	//$("#"+id).find('.paragraphe-ctn').animate({height:0}, 400); 
}

function ph() {
	$('[placeholder]').each(function () {
		var ph = $(this).attr("placeholder");
		if (ph != "") {
			$(this).focus(function () {
				if ($(this).val() == ph) {
					$(this).val('');
				}
			}).blur(function () {
				if ($(this).val() == '') {
					$(this).val(ph);
				}
			}).blur();
		}
	});

	// Validation du formulaire : vider les champs dont le contenu vaut la valeur du placeholder
	$('[placeholder]').parents('form').submit(function () {
		$(this).find('[placeholder]').each(function () {
			if ($(this).val() == $(this).attr('placeholder')) {
				$(this).val('');
			}
		})
	});
}
