//
// Gallerie d'images
//
function nextButtonStateHandler(carousel, button, enabling)	{
    if (enabling) {
        jQuery(button).attr('disabled', false)
                      .attr('src', '/images/next-enabled.gif')
                      .css('cursor', 'pointer');
    } else {
        jQuery(button).attr('disabled', true)
                      .attr('src', '/images/next-disabled.gif')
                      .css('cursor', 'default');
    }
}

function prevButtonStateHandler(carousel, button, enabling)	{
    if (enabling) {
        jQuery(button).attr('disabled', false)
                      .attr('src', '/images/prev-enabled.gif')
                      .css('cursor', 'pointer');
    } else {
        jQuery(button).attr('disabled', true)
                      .attr('src', '/images/prev-disabled.gif')
                      .css('cursor', 'default');
    }
}

// engin
jQuery(document).ready(function() {
    jQuery('.jcarousel').jcarousel({
        itemWidth : 500,
        itemHeight : 375,
        itemVisible: 1,
        itemScroll: 1,
        scrollAnimation: "slow",
        nextButtonStateHandler: nextButtonStateHandler,
        prevButtonStateHandler: prevButtonStateHandler
    });
});

//
// Formulaire de contact
//

function enginContact() {
		$('div.jcarousel-clip').hide('slow')
		$('div.none').addClass('form').show('slow');
		$('img.btContact').remove();
		$('div#venteNav-2').append('<img src="/images/images-enabled.gif" border="0" class="btImages" onclick="enginImages()" />');
		return false;
	}
	function enginImages() {
		$('div.form').addClass('none').hide('slow');
		$('div.jcarousel-clip').show('slow')			
		$('img.btImages').remove();
		$('div#venteNav-2').append('<img src="/images/contact-enabled.gif" border="0" class="btContact" onclick="enginContact()" />');
		return false;
	}
	// document ready
	$(document).ready(function() {
		 $(".contact").ajaxForm('.res');
	});	
	
//
// Envoi un mail 
//
function envoiMail() {
			
	var nomPost = document.getElementById('sendNom').value
	var emailPost = document.getElementById('sendEmail').value
	var msgPost = document.getElementById('sendMsg').value
	var modelPost = document.getElementById('sendModel').value
	
	$('img#load').addClass('loading').show('slow');
	$('div.form').addClass('form-none').hide('slow');
	
	// teste des champs
	isMail('sendEmail');
	isEmpty('sendNom');
	isEmpty('sendMsg');
	
	$.post("/vehicules/mail", {nom:nomPost, email:emailPost, msg:msgPost, model:modelPost}, function(xml) {

		// resultat
		var res = [$("bool", xml).text()];
		var msg = [$("message", xml).text()];

		// affichage
		$("div.result").html(msg.join('')).show('slow');
	
		if (res == 'true')
			setTimeout("ok()",4000);
		else
			setTimeout("error()",3000);
	} );
}


function ok() {
	$('div.result').addClass('div.none').hide('slow');
	$('div.jcarousel-clip').addClass('jcarousel-clip').show('slow');
	$('img.btImages').remove();
	$('div#venteNav-2').append('<img src="/images/contact-enabled.gif" border="0" class="btContact" onclick="enginContact()" />');
	$('img#load').addClass('none').hide('slow');
}

function error() {
	$('div.none').addClass('form').show('slow');
	$('img#load').addClass('none').hide('slow');
	$('div.result').addClass('div.none').hide('slow');
}

function isMail(id) {
	var o = document.getElementById(id);
	var a = o.value;

	testm = false ;
	
	for (var j=1 ; j<(a.length) ; j++) {
	
		if (a.charAt(j)=='@') {
		
			if (j<(a.length-4)){
			
				for (var k=j ; k<(a.length-2) ; k++) {
			
					if (a.charAt(k)=='.') testm = true;
			
				}
			}
		}
	}

	if (testm == false)
		o.style.border = "1px solid #EA000D";
	else
		o.style.border = "1px solid #2f5fBB";

	return testm;
}

function isEmpty(id) {
	var o = document.getElementById(id);
	var a = o.value;
	
	if ( a == '' )
		o.style.border = "1px solid #EA000D";
	else
		o.style.border = "1px solid #2f5fBB";
}
