$(document).ready(function(){

	/******** insertion swf ********/
	
var params = {allowscriptaccess:"sameDomain", bgcolor:"#000000", wmode: "transparent"};
swfobject.embedSWF(root+"/medias/swf/banniere-"+secteur+".swf", "flash", "652", "233", "8.0.0", false, false, params);
if (page == 'agences')
swfobject.embedSWF(root+"/medias/swf/carte.swf", "carte", "650", "400", "8.0.0", false, false, params);

/*********** fancybox **********/

$("a.thumb").fancybox({
	'overlayShow': true,
	'overlayOpacity': 0.6,
	'centerOnScroll':true
});

/*********** cache l'effet active **********/

$("li a, a.thumb, a.supprimer, a.blur").click(function(){
	this.blur();
});

/*********** ajout icones + popup **********/

$("a.external")
	.append('<img src="'+root+'/assets/images/picto-external.gif" title="nouvelle fenêtre" />')
	.click(function(){
		var attr='scrollbars=yes,menubar=yes,titlebar=yes,toolbar=yes,location=yes,directories=yes,statut=yes,resizable=yes';
		window-open(this.href, '', attr);
		return false;
	});

/*********** expend news **********/

$('#news div.article').hide();
$('#news a.expend').show();

$('#news a.expend').click(function(){	
		$("#news div.article").slideUp("normal");
		$('#news a.expend').show();
		$(this).hide().next("div.article").slideDown("normal");
		return false;
});	

/*********** ajout :focus sur IE **********/

$("a").addClass("focus");

$(".focus").removeClass("focused").focus(function(){
	$(this).addClass("focused");
}).blur(function(){
	$(this).removeClass("focused");
})

/*********** manipulation formulaires **********/

if (page == 'contact' || page == 'telecharger-la-demo') validForm();
	
if (page == 'contact') showProducts();


});


/****** FUNCTIONS FORMULAIRES ******/

function resetForm(){
	$("#error-msg").css('display', 'none').empty();
	$(".requiered").parent("div").removeClass("error");
}

function validForm(){
	
	if ( !$("#error-msg").length ){
		$("form").prepend('<div id="error-msg"></div>');
		$("div#error-msg").css('display','none');
	}

	$("#submit").click(function(){
			this.blur();
			resetForm();
			errormsg = "";
			valid = true;	
			$(".requiered").each(function(){
				if( $(this).val() == "" ){
						$(this).parent("div").addClass("error");
						errormsg = "Les champs marqués d'un * sont obligatoires<br />";
						valid = false;
				}
			});		
			if ( valid ){
				if( !$("#tel").val().match(/^[-+.\(\)\s0-9]{6,}$/) ){
						$("#tel").parent("div").addClass("error");
						errormsg += "Le format du numéro de téléphone n'est pas valide<br />";
						valid = false;			
				}			
				if( !$("#email").val().match(/^[0-9a-zA-Z][-_.0-9a-zA-Z]*@[0-9a-zA-Z]([-.]?[0-9a-zA-Z]+)*\.[a-zA-Z]{2,9}$/) ){
						$("#email").parent("div").addClass("error");
						errormsg += "Le format de l'adresse email n'est pas valide<br />";
						valid = false;	
				}
			}		
			if ( !valid ) {
				if ( page == 'contact')
					$('html, body').animate({scrollTop: '410'}, 300, function(){
						$("div#error-msg").html(errormsg).fadeIn('2000'); });
				else
					$("div#error-msg").html(errormsg).fadeIn('2000');
			}
			
			return valid;
		});
	
	$("#reset").click(function(){
			this.blur();
			if ( page == 'contact')
				$('html, body').animate({scrollTop: '410'}, 300, function(){ resetForm(); });
			else resetForm();
	});

}

function showProducts(){
	
	$("#secteur").change(function() {			
			secteur = $("#secteur").val(); 
			if ( secteur == 'chr' || secteur == 'vae' ) 
						$("#products").slideDown();
			else 	$("#products").slideUp();	
	});

}
