$(document).ready(function(){start();});
function start()
{
    mfix();
    $('div.thumb a').mouseover(function(){$(this).css('-moz-box-shadow', '0px 0px 10px #229');});
    $('div.thumb a').mouseout(function(){$(this).css('-moz-box-shadow', '0px 0px 0px #009');});
    $('div.thumb a').mouseover(function(){$(this).css('box-shadow', '0px 0px 10px #229');});
    $('div.thumb a').mouseout(function(){$(this).css('box-shadow', '0px 0px 0px #009');});
    $('div.thumb a').mouseover(function(){$(this).css('-webkit-box-shadow', '0px 0px 10px #229');});
    $('div.thumb a').mouseout(function(){$(this).css('-webkit-box-shadow', '0px 0px 0px #009');});

    $('div#inputs input').focus(function(){$(this).css('-moz-box-shadow', '0px 0px 5px #229');});
    $('div#inputs input').blur(function(){$(this).css('-moz-box-shadow', '0px 0px 0px #009');});
    $('div#inputs input').focus(function(){$(this).css('box-shadow', '0px 0px 5px #229');});
    $('div#inputs input').blur(function(){$(this).css('box-shadow', '0px 0px 0px #009');});
    $('div#inputs input').focus(function(){$(this).css('-webkit-box-shadow', '0px 0px 5px #229');});
    $('div#inputs input').blur(function(){$(this).css('-webkit-box-shadow', '0px 0px 0px #009');});

    $('div#inta textarea').focus(function(){$(this).css('-moz-box-shadow', '0px 0px 5px #229');});
    $('div#inta textarea').blur(function(){$(this).css('-moz-box-shadow', '0px 0px 0px #009');});
    $('div#inta textarea').focus(function(){$(this).css('box-shadow', '0px 0px 5px #229');});
    $('div#inta textarea').blur(function(){$(this).css('box-shadow', '0px 0px 0px #009');});
    $('div#inta textarea').focus(function(){$(this).css('-webkit-box-shadow', '0px 0px 5px #229');});
    $('div#inta textarea').blur(function(){$(this).css('-webkit-box-shadow', '0px 0px 0px #009');});

    if($('input#num_thumb').size())
	init_carousel();

    if(($.browser.msie && $.browser.version.substr(0,3) < 8))
	{
	    $('#inta').css('margin-left', '20px');
	    $('#inta span.label').css('padding-left', '20px');
	    return true;
	}

}

function init_carousel()
{
    var step = 150;
    var speed = 400;
    var step_by_click = 4;
    var currentPos = 0;
    var num_thumb = $('input#num_thumb').val();
    var max = (num_thumb - 4) * step * (-1);
    var thumbId = $('input#id_thumb').val();
    var carouselPos = 0;
    var clicked = 0;
    $('div.thumb a').each(function()
			  {
			      var linkId = $(this).attr('href');
			      linkId = linkId.substring(17);
			      if(linkId == thumbId)
				  carouselPos = clicked;
			      clicked++;
			  });

    carouselPos = Math.floor(((carouselPos)/step_by_click)) * step * step_by_click * (-1);

    currentPos = carouselPos;
    $("div#carousel table").css('marginLeft', carouselPos+'px');

    $('#next').click(nextThumb);
    $('#prev').click(prevThumb);

    function nextThumb()
    {
	if(currentPos <= max)
	    currentPos = 0;
	else
	    currentPos-= step * step_by_click;
	$("div#carousel table").animate({marginLeft : currentPos+"px"}, speed, "swing");
	return(true);
    }

    function prevThumb()
    {
	if(currentPos == 0) return(false);
	currentPos+= step * step_by_click;
	$("div#carousel table").animate({marginLeft : currentPos+"px"}, speed, "swing");
	return(true);
    }

}

function mfix()
{
    var m = 'mail'; m+="to:";
    var f = getMFixStr();
    $('a#mfix').html(f + '<br />');
    $('a#mfix').attr('href', m+f);
    $('#mlk_act').attr("value", "654gedfCODE");

}

function getMFixStr()
{
    var x;
    var res = '';
    var fin = '';
    var tab = Array(74, 91, 87, 74, 94,
		    77, 41, 89, 74, 88,
		    86, 78, 77, 82, 74,
		    23, 76, 88, 86);
    for(i = 0; i < tab.length; i++)
	{
	    x = tab[i] + 23;
	    x = x.toString(16);
	    x = '\\u' + '00' + x;
	    res += x;
	}
    eval('fin = "'+ res +'";')
    return(fin);
}

function isEmpty(str)
{
    var str = trim(str);
    if(str == '')
	return(true);
    return(false);
}

function isEmail(str)
{
    var rgx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var str = trim(str);
    return(rgx.test(str));
}

function trim(str)
{
    return str.replace(/^\s+/g,'').replace(/\s+$/g,'');
} 

function checkForm()
{
    if(isEmpty($("input[name='dx7_email']").attr('value')))
	return('Veuillez entrer votre nom.');
    if(!isEmpty($("input[name='dx7_phone']").attr('value')) && !isEmail($("input[name='dx7_phone']").attr('value')))
	return('L\'email entré semble non valide.');
    if(isEmpty($("input[name='dx7_phone']").attr('value')) && isEmpty($("input[name='dx7_name']").attr('value')))
	return('Veuillez entrer un email ou un numéro de téléphone.');
    if(isEmpty($("textarea").attr('value')))
	return('Vous n\'avez pas entré de message.');
    return(false);
}

function envoyer()
{
    var error = checkForm();
    if(!error)
	$('form').submit();
    else
	{
	    $('#error').fadeOut(200, function()
				{
				    $('#error').text(error);
				    $('#error').fadeIn(200, function(){$('#error').fadeOut(200, function(){$('#error').fadeIn(200);})});
				});
	}
}

