// JavaScript Document
var zindex = 100;
var regexmail = /^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,3}$/;
var regexphone = /^0[1-68]([\.|\-|\s]*[0-9]{2}){4}$/;

jQuery.fn.regex = function( pattern ) { 
  return ( $(this).val().match( pattern ) ) ? true : false; 
}

jQuery.fn.showInfoBulle = function( msg ) {
	zindex++;
	var identifier = $( this ).attr( 'id' );
	
	if( $( '#'+identifier+'-info-bulle' ).size() == 0 ) {
		$( this ).after( '<div class="info-bulle" id="'+identifier+'-info-bulle" style="z-index:'+zindex+';"><div class="info-bulle-head"></div><div class="info-bulle-body"><p>'+msg+'</p></div><div class="info-bulle-foot"></div></div>' );
		if ( $.browser.msie  ) {
			$( '#'+identifier+'-info-bulle' ).css('display', 'block');
		}else{
			$( '#'+identifier+'-info-bulle' ).fadeIn(500);
		}
	}else{
		$( '#'+identifier+'-info-bulle' ).css( 'z-index', zindex );
	}
}
jQuery.fn.updateInfoBulle = function( msg ) {
	
	var identifier = $( this ).attr( 'id' );
	
	$( '#'+identifier+'-info-bulle p' ).html( msg );
}
jQuery.fn.hideInfoBulle = function() {
	
	var identifier = $( this ).attr( 'id' );
	if( $( '#'+identifier+'-info-bulle' ).size() != 0 ) {
		if ( $.browser.msie  ) {
			$( '#'+identifier+'-info-bulle' ).remove();
		}else{
			$( '#'+identifier+'-info-bulle' ).fadeOut(500, function () { $( '#'+identifier+'-info-bulle' ).remove(); });
		}
	}
}

jQuery.fn.attacheInfoBulle = function( msg, testtype, errormsg ) {	
	
	$( this ).focus( function() {
		
		var curentValue = $( this ).val();
		
		// Selon le test à éffectuer sur la vealeur du champ
		switch( testtype ) {
			
			case 'notempty' :	if( curentValue != '' ) {
									$( this ).showInfoBulle( msg );
								}else{
									$( this ).showInfoBulle( msg + '<br/><span style="color:red">' + errormsg + '</span>' );
								}
								break;
						
			case 'ismail' : 	if( curentValue == '' ) {
									$( this ).showInfoBulle( msg + '<br/><span style="color:red">Ce champ doit &ecirc;tre renseign&eacute;.</span>' );
								}else if( !$(this).regex( regexmail ) ) {
									$( this ).showInfoBulle( msg + '<br/><span style="color:red">' + errormsg + '</span>' );
								}else{
									$( this ).showInfoBulle( msg );
								}
								break;
								
			case 'isphone' : 	if( curentValue!='' && !$(this).regex( regexphone ) ) {
									$( this ).showInfoBulle( msg + '<br/><span style="color:red">' + errormsg + '</span>' );
								}else{
									$( this ).showInfoBulle( msg );
								}
								break;
								
			default : 			$( this ).showInfoBulle( msg );
		}
	});
	
	$( this ).bind( "keyup click select change", function() {
		
		var curentValue = $( this ).val();
		
		switch( testtype ) {
			
			case 'notempty' :	if( curentValue == '' ) {
									$( this ).updateInfoBulle( msg + '<br/><span style="color:red">' + errormsg + '</span>' );
								}else{
									$( this ).updateInfoBulle( msg );
								}
								break;
						
			case 'ismail' : 	if( curentValue == '' ) {
									$( this ).updateInfoBulle( msg + '<br/><span style="color:red">Ce champ doit &ecirc;tre renseign&eacute;.</span>' );
								}else if( !$(this).regex( regexmail ) ) {
									$( this ).updateInfoBulle( msg + '<br/><span style="color:red">' + errormsg + '</span>' );
								}else{
									$( this ).updateInfoBulle( msg );
								}
								break;
								
			case 'isphone' : 	if( curentValue!='' && !$(this).regex( regexphone ) ) {
									$( this ).updateInfoBulle( msg + '<br/><span style="color:red">' + errormsg + '</span>' );
								}else{
									$( this ).updateInfoBulle( msg );
								}
								break;
								
			default : 			$( this ).updateInfoBulle( msg );
		}
	});
	
	$( this ).blur( function() {
		$( this ).hideInfoBulle();
	});
}

jQuery.fn.setImageButtonStates = function( default_img, mouseover_img, mousedown_img ) {
	$( this ).mouseover( function() {
		$(this).attr( "src", mouseover_img );
	});
	$( this ).mouseout( function() {
		$(this).attr( "src", default_img );
	});
	$( this ).mousedown( function() {
		$(this).attr( "src", mousedown_img );
	});
	$( this ).mouseup( function() {
		$(this).attr( "src", default_img );
	});
}


$(document).ready(function() {

	//if ( !($.browser.msie ) ) {
		
		$( '#submit' ).setImageButtonStates( "images/submit-bg.jpg", "images/submit-bg_hover.jpg", "images/submit-bg_active.jpg" );
		
		$("#name").attacheInfoBulle( 'Saisissez votre nom complet ou la raison sociale de votre entreprise.', 'notempty', 'Ce champ doit &ecirc;tre renseign&eacute;.' );
		$("#email").attacheInfoBulle( 'Saisissez votre adresse email.', 'ismail', 'Cette adresse n\'est pas une adresse email valide.' );
		$("#phone").attacheInfoBulle( 'Saisissez votre num&eacute;ro de t&eacute;l&eacute;phone.', 'isphone', 'Ce num&eacute;ro n\'est pas un num&eacute;ro de t&eacute;l&eacute;phone fran&ccedil;ais.' );
		$("#message").attacheInfoBulle( 'Saisissez votre message.', 'notempty', 'Ce champ doit &ecirc;tre renseign&eacute;.' );
		
		$("#contact").submit( function() {

			$("#name").hideInfoBulle();
			$("#email").hideInfoBulle();
			$("#phone").hideInfoBulle();
			$("#message").hideInfoBulle();
			
			if( $("#name").val() == '' ) {
				$("#name").focus();
				
			}else if( !$("#email").regex( regexmail ) ) {
				$("#email").focus();
				
			}else if( $("#phone").val() != '' && !$( "#phone" ).regex( regexphone ) ) {
				$("#phone").focus();
				
			}else if( $("#message").val() == '' ) {
				$("#message").focus();
				
			}else{ 
				return true;
			}
			return false;
		});
	//}
	$('a[href^=#]').click( function () { 
		cible = $(this).attr('href');
		if( $(cible).length > 0 ) {
			hauteur = $(cible).offset().top;
		}else{
			hauteur = $('a[name=' + cible.substr(1, cible.length-1 )+']').offset().top;
		}
		$('html,body').animate( {scrollTop:hauteur}, 1000, 'easeOutSine', function () { if( cible == '#contact' ) $("#name").focus(); } );
		return false; 
	});
	
});

