var urlFormDialog;

jQuery.fn.defuscate = function( settings ) {
		settings = jQuery.extend({
				link: true,
				find: /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi,
				replace: '$1@$2'
		}, settings);
		return this.each(function() {
				if ( $(this).is('a[@href]') ) {
						$(this).attr('href', $(this).attr('href').replace(settings.find, settings.replace));
						var is_link = true;
				}
				$(this).html($(this).html().replace(settings.find, (settings.link && !is_link ? '<a href="mailto:' + settings.replace + '">' + settings.replace + '</a>' : settings.replace)));
		});
};

function ajaxShowRequestFormDialog(formData, jqForm, options){
}

function ajaxShowResponseFormDialog(data, statusText){
	//$('#dialog').dialog('close');
}

$('#sendByEmail').live('click',function(){
	urlFormDialog = $(this).attr("href");
	$('#dialog').dialog('open');
	return false;
});

function showResponseAjaxForm(responseText, statusText)  {
	var dialogHeight;
	if(!responseText.erreurPourAjax){
		$('#dialog').dialog('close');
		dialogHeight = '50';
	}else{
		dialogHeight = '200';
	}
	$('body').prepend('<div id="dialogRetourAjaxForm"></div>');
	$('#dialogRetourAjaxForm').dialog({
		width:360,
		height:dialogHeight,
		modal: true,
		autoOpen: true,
		close: function(event, ui) {
		},
		open: function(event, ui){
			$(this).html(responseText.alert);
		}
	});
}

$(document).ready(function(){
	
	// Calendrier
	if ($('.simplecalendar').length) {
		$('.simplecalendar').datepicker({
			dateFormat: 'dd/mm/yy',
			showAnim: 'fadeIn',
			speed: 'fast',
			showOn: 'button',
			buttonText: 'Choose',
			buttonImage: '/images/calendar.png',
			buttonImageOnly: true
		});
	};

	var optionsAjaxForm = {
		success: showResponseAjaxForm,
		dataType:  'json'
	};

	// fadeIn
	// $('body').append('<div id="blackFadeInBox"></div>');
	// $('#blackFadeInBox').css({
	// 	"background":"black",
	// 	"position":"absolute",
	// 	"width":"100%",
	// 	"z-index":"2001",
	// 	"height":"100%"
	// }).hide('slow');

	// dialog : formulaire d'ajout/modification d’un élément
	$('body').prepend('<div id="dialog"></div>');
	$('#dialog').dialog({
		// title:'Envoyer à un ami',
		width:400,
		height:320,
		modal: true,
		autoOpen: false,
		close: function(event, ui) {
		},
		open: function(event, ui){
			$(this).load(urlFormDialog, function(){
				$('#ajaxCancelButton').click(function(){
					$('#dialog').dialog('close');
				});
				$(this).children('form').ajaxForm(optionsAjaxForm);
				$('.resteVide').hide();
				$(this).children('form').prepend('<input type="hidden" name="ajaxed" value="1" /><input type="hidden" name="ecards[page]" value="http://www.savilehouse.com'+window.location.pathname+'" /><input type="hidden" name="ecards[choix]" value="http://www.savilehouse.com'+window.location.pathname+'" />');
			});
		}
	});

	// animation du sous-menu
	$("#menu ul").hide();
	if(window.location.pathname.indexOf('/clothing/choice') != -1){
		$(".actif+ul").slideDown("slow");
	}
	if(window.location.pathname.indexOf('/clothing/') != -1){
		$(".actif+ul").show();
	}

	// boutons share this
	$('#addthis_button').hide();
	$('.addthis_button').mouseenter(function(){
		$('#addthis_button').slideDown();
	});
	$('.addthis_button').parent().mouseleave(function(){
		$('#addthis_button').slideUp("fast");
	});
	if ($('#addthis_button').length) {
		$('#addthis_button').bookmark({
			compact: false, 
				sites: ['linkedin','facebook', 'google', 'windows', 'twitthis', 'delicious', 'digg', 'technorati', 'wikio', 'myspace']
		});
	};
	
	// slider pour toutes les pages
	$('.philosophy #blogSlider, .choice #blogSlider, .details #blogSlider, .fabrics #blogSlider, .making-process #blogSlider, .heritage #blogSlider').codaSlider({
		easeTime: 600,
		easeFunc: "easeOut"
	});
	// slider pour page Services
	$('.services #blogSlider').codaSlider({
		easeTime: 600,
		easeFunc: "easeInOut"
	});
	// slider pour les fiches news
	$('#galeriePhotos #blogSlider').codaSlider({
		easeTime: 300,
		easeFunc: "easeInOut"
	});
	$('#stripNav0 li:last').addClass('boutonTexte');
	$('.panel:last').addClass('panelTexte');
	setTimeout(function () {
	}, 2000);

	
	// antispam formulaire
	$('.resteVide').hide();
	
	// Met forme les adresses électroniques machin(arobe)truc.com
	$(".adresse").defuscate();


});
