$.fn.resizenow = function () {
	return this.each(function () {
		// Gather browser and current image size
		var imagewidth = $(this).width();
		var imageheight = $(this).height();
		var browserwidth = $(window).width();
		var browserheight = $(window).height();

		// Define image ratio
		// var ratio = 700/1200;
		// var ratio = imageheight / imagewidth;
		var ratio = 854 / 1280;

		// Resize image to proper ratio
		if ((browserheight/browserwidth) > ratio){
			$(this).height(browserheight);
			$(this).width(browserheight / ratio);
			$(this).children().height(browserheight);
			$(this).children().width(browserheight / ratio);
		} else {
			$(this).width(browserwidth);
			$(this).height(browserwidth * ratio);
			$(this).children().width(browserwidth);
			$(this).children().height(browserwidth * ratio);
		}
		return false;
	});
};

$(document).ready(function () {

	// opérations de layout
	$('#background img').resizenow(); 

	$('ul#shops li:last-child').addClass('last');

	// gérer le hash
	var page = 'contact';
	var hash = location.hash.substring(1);
	if (hash == 'revendeurs') {page = hash;}
	//console.log('h: ', hash, ' p: ', page);
	$('div.collapse[rel=' + page + ']').addClass('open');

	// collapsable blocks
	var animate_show = {'height': 'show', 'opacity': 'show'};
	var animate_hide = {'height': 'hide', 'opacity': 'hide'};
	$('div.collapse').hide();
	$('div.collapse.open').show();
	$('h2.collapse').click(function () {
		var block = $(this).next();
		if (block.is('.open')) {
			block.removeClass('open').animate(animate_hide, 350);
			//location.hash = '';
		} else {
			page = block.attr('rel');
			location.hash = page;
			if ($('div.collapse.open').length) {
				$('div.collapse.open').removeClass('open').animate(animate_hide, 350, function () {
					block.addClass('open').animate(animate_show, 500);
				});
			} else {
				block.addClass('open').animate(animate_show, 500);
			}
		}
	});

	// "crypter" les adresses e-mail
	// Pour tous les liens commençant par "mailto" ou les span ayant une class "wmail"
	// remplace "[arobase]" par "@" et "[point]" par "."
	$('a[href^="mailto"], span.wmail').each(function (i) {
			//Remplacment du texte dans l'élément
			var temp = $(this).html();
			temp = temp.replace("[arobase]","@");
			temp = temp.replace("[point]",".");
			$(this).html(temp);
			//Si il y a un attribut "href", on remplace le texte dans l'attribut
			if($(this).attr("href")){
					var temphref = $(this).attr("href");
					temphref = temphref.replace("[arobase]","@");
					temphref = temphref.replace("[point]",".");
					$(this).attr("href",temphref);
			}
	})

}); // end document.ready

$(window).bind('resize', function(){$('#background').resizenow();});
$(window).load(function() {$('#background').resizenow();});


