$(document).ready( function () {
	correctPNG();
	Cufon.replace('h2, h3, .colonneTiers h1');
	
	$('.scroll-pane').jScrollPane({
		showArrows: true
	});
	
	//--> ColorBox
	$(".youtube").colorbox({iframe:true, width:480, height:385, opacity:0.5});
	
	$('#listeVehicules.vehiculesOccasion a').hover(
	
		function () {
			$(this).children(".bordureVignette").css({
				borderColor: '#ebb72c'
			});
			
			$(this).children(".bordureVignette").children(".masqueVignette").children("img").stop().animate({
				width: 120,
				left: -9,
				top:-9
			}, 750, "easeOutElastic");
		},
		
		function () {
			
			$(this).children(".bordureVignette").css({
				borderColor: '#808080'
			});
			
			$(this).children(".bordureVignette").children(".masqueVignette").children("img").stop().animate({
				width: 98,
				left: 0,
				top: 0
			}, 250);
		}
	
	);
	$('#listeVehicules.vehiculesNeufs a img').css({
		width: 190,
		left: -10,
		top:-10
	})
	$('#listeVehicules.vehiculesNeufs a').hover(
	
		function () {
			$(this).children(".bordureVignette").css({
				borderColor: '#ebb72c'
			});
			
			$(this).children(".bordureVignette").children(".masqueVignette").children("img").stop().animate({
				width: 206,
				left: -20,
				top:-20
			}, 750, "easeOutElastic");
		},
		
		function () {
			
			$(this).children(".bordureVignette").css({
				borderColor: '#808080'
			});
			
			$(this).children(".bordureVignette").children(".masqueVignette").children("img").stop().animate({
				width: 190,
				left: -10,
				top:-10
			}, 250);
		}
	
	);
	
	//--> Animation des elments de l'entete
	$("#infosEntetes").css({
			opacity: 0,
			top: '150px'
		});

	if ( $("#infosEntetes.texte3Cols").length > 0 ) {
		$("#infosEntetes.texte3Cols").animate({
				opacity: 1,
				top: '55px'
		}, 750);
	} else {
		$("#infosEntetes").animate({
				opacity: 1,
				top: '85px'
		}, 750);
	}
	
});

function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (img.className == "pngfix")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}

function niceClose(element) {
	$("#" + element).animate({
		opacity: 0,
		top: '35px'
	}, 500);
}

function valideFormSAV() {
	if ( $("#nom").val() == "" ) {
		alert("Veuillez renseigner votre nom.");
		$("#nom")[0].focus();
		return false;
	}
	
	if ( $("#prenom").val() == "" ) {
		alert("Veuillez renseigner votre prénom.");
		$("#prenom")[0].focus();
		return false;
	}
	
	if ( $("#code_postal").val() == "" ) {
		alert("Veuillez renseigner votre code postal.");
		$("#code_postal")[0].focus();
		return false;
	}
	
	if ( $("#rendez_vous_0")[0].checked == true && $("#telephone_fixe").val() == "" && $("#telephone_mobile").val() == "") {
		alert("Veuillez renseigner votre numéro de téléphone fixe ou mobile pour prendre rendez-vous.");
		$("#telephone_fixe")[0].focus();
		return false;
	}
	
	var emailValide = checkMail( $("#email").val() );
	if ( emailValide == true ) {
		// Successful match
	} else {
		alert("Veuillez renseigner une adresse e-mail valide.");
		$("#email")[0].focus();
		return false;
	}
	
	$("#formContact")[0].submit();
}

function valideFormInfos() {
	if ( $("#nom").val() == "" ) {
		alert("Veuillez renseigner votre nom.");
		$("#nom")[0].focus();
		return false;
	}
	
	if ( $("#prenom").val() == "" ) {
		alert("Veuillez renseigner votre prénom.");
		$("#prenom")[0].focus();
		return false;
	}
	
	if ( $("#code_postal").val() == "" ) {
		alert("Veuillez renseigner votre code postal.");
		$("#code_postal")[0].focus();
		return false;
	}
	
	if ( $("#telephone_fixe").val() == "" && $("#telephone_mobile").val() == "") {
		alert("Veuillez renseigner un numéro de téléphone.");
		$("#telephone_fixe")[0].focus();
		return false;
	}
	
	var emailValide = checkMail( $("#email").val() );
	if ( emailValide == true ) {
		// Successful match
	} else {
		alert("Veuillez renseigner une adresse e-mail valide.");
		$("#email")[0].focus();
		return false;
	}
	
	$("#formContact")[0].submit();
}

function checkMail(email)
{
	if (/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i.test(email)) {
		return true;
	} else {
		return false;
	}
}