var comment = {
    
    init : function()
    {
        if($('#commentform').length == 0) return;
        
        if($('#commentform #author').val() == '' || $('#commentform #author').val() == "Votre nom") {
            $('#commentform #author').val('Votre nom');
            
            $('#commentform #author').focus(function(){
                $(this).val('');
                $(this).unbind('focus');
            });

        }
        if($('#commentform #email').val() == '' || $('#commentform #email').val() == "Votre email (Ne sera pas publié)") {
            $('#commentform #email').val('Votre email (Ne sera pas publié)');
            
            $('#commentform #email').focus(function(){
                $(this).val('');
                $(this).unbind('focus');
            });
        }
        if($('#commentform #url').val() == '' || $('#commentform #url').val() == "Votre site web") {
            $('#commentform #url').val('Votre site web');
            
            $('#commentform #url').focus(function(){
                $(this).val('');
                $(this).unbind('focus');
            });
        }
        if($('#commentform #comment').val() == '' || $('#commentform #comment').val() == "Votre commentaire") {
            $('#commentform #comment').val('Votre commentaire');
            
            $('#commentform #comment').focus(function(){
                $(this).val('');
                $(this).unbind('focus');
            });
        }
    }
};

var fade = {
	nbslide:0,
	innerfade:null,
	
	start : function ()
	{
	    
        this.innerfade = $('#homeslideshow').innerfade({
			speed: 'slow',
			timeout: 6000,
			type: 'sequence'
		});
        
	}
};

var b2gForm = {
	init : function () {
		$(".button-140:button, .button-140:submit").each(function (i) {
			var label = ($(this).text() ? $(this).text() : $(this).attr('value') );

			$(this).replaceWith('<a href="javascript:;" onclick="b2gForm.submit(this);" class="' + $(this).attr('class') + '">' + label + '</a>');
			
		});
	},
	
	submit : function(e) {
		var frm =  $(e).parents('form')[0];
		frm.submit();
	}
};


var newsletter = {
    
    init : function () {
        
        // show a simple loading indicator
		var loader = jQuery('<div id="loader"><img src="/wp-content/themes/bdg/images/loading.gif" alt="loading..." /></div>')
			.css({position: "fixed", top: "50%", left: "50%"})
			.appendTo("body")
			.hide();
		jQuery().ajaxStart(function() {
			loader.show();
		}).ajaxStop(function() {
			loader.hide();
			// + Scroll
			$target = $("#result");
		    $('html,body').animate({ scrollTop: $target.offset().top }, 1000);
		    
		}).ajaxError(function(a, b, e) {
			throw e;
		});
		
		$("form.newsletter").validate({
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					target: "#result"
				});
			},
			rules: {
    			"firstname": "required",
    			"lastname": "required",
    			"email": {
    				"required": true,
    				"email": true
    			},
    			"localite": "required",
    			"codepostal": "required"
    		},
    		messages: {
    			"firstname": "Merci d'indiquer votre prenom",
    			"lastname": "Merci d'indiquer votre nom",
    			"email": {
    				"required": "Merci d'indiquer une adresse mail",
    				"email": "Votre adresse n'est pas valide"
    			},
    			"localite": "Merci d'indiquer votre localité",
    			"codepostal": "Merci d'indiquer un code postal"
    		}
		});

	}
};


// Utils
function Crypt(s) {
	var s, n;
	r='';
	for(i=0;i<s.length;i++){
		n=s.charCodeAt(i);
		if (n>=8364) {
			n = 128;
		}
		r += String.fromCharCode( n - 3 );
	}
	return r;
}


var domainB2G = "besoindegauche" + "." + "fr";

function writemail(nom, domaine) {
    var a ="pdlowr=";
    
    var m = '&#64;';
    var d = unescape(m);
    
    var aro = nom + d + domaine;    
    
	document.write('<a href='+ Crypt(a) + aro + '>');
	document.write( aro + '</a>');
}

function webmastermail() {

    writemail('webmaster', domainB2G);
}

function contactmail() {
    writemail('contact', domainB2G);
}