$(document).ready(function() {

	$.validator.setDefaults({
		highlight: function(input) {
			$(input).addClass("error-highlight");
		},
		unhighlight: function(input) {
			$(input).removeClass("error-highlight");
		}
	});

	// Validation des champs à l'inscription.
	if ( $("#createAccount").exists() )
		$("#createAccount").validate();

	// Validation des champs address.
	if ( $("#contactAddress").exists() )
		$("#contactAddress").validate();


	// Validation des champs au login
	if ( $("#login").exists() )
		$("#login").validate();

	/***** ***** ***** ***** ***** *****
	 * 	Gestion des adresses de contact.
	 ***** ***** ***** ***** ***** *****/

	// E-commerce, sélection d'une adresse de livraison/facturation.
	$("#address_shipping, #address_billing").live('change', function() {

		if ( $(this).attr('id') == 'address_shipping' )
			var name		= 'shipping';
		else if ( $(this).attr('id') == 'address_billing' )
			var name		= 'billing';

		$.ajax({
			type: "GET",
			url: CMSUrl+'ajax=setOrderAdresses',
			data: "addressID="+ $(this).val() +"&addressType="+ name,
			success: function(data) {
				window.location.reload();
			}
		});
	});

	/***** ***** *****
	 * 	E-commerce
	 ***** ***** *****/

	// Suppression d'un produit du panier.
	$(".trash").live('click', function(e) {
		e.preventDefault();

		var thisProduct			= $(this).attr('id').split('-');
		var thisProductDetailID	= thisProduct[1];

		$.getJSON(CMSUrl+'ajax=deleteProduct', {
			'productDetailID': thisProductDetailID
		}, function(json) {
			if ( json.error ) {
				alert(json.message);
			}
			else {
				$('$[data-productID="'+ thisProductDetailID +'"]').fadeOut('slow');
				$('#basketPrice').text(json.basketPrice);
			}
		});
	});

	// Modification de la quantité d'un produit.
	$(".btQty .plus").live('click', function() {
		productID	= $(this).parents('[data-productID]').attr('data-productID');
		qty			= parseInt($("#qty-"+productID).val()) + 1;
		$("#qty-"+productID).val(qty);

		// Modification de la valeur en BDD.
		$.ajax({
			type: "GET",
			url: CMSUrl+'ajax=updateQuantity',
			data: "productID="+ productID +"&productQty="+ qty,
			success: function(data) {
				var json = eval('(' + data + ')');
				$('#totalPrice-'+productID).text(json.totalPrice);
				$('#basketPrice').text(json.basketPrice);
				//window.location.reload();
			}
		});
	});

	$(".btQty .moins").live('click', function() {
		productID	= $(this).parents('[data-productID]').attr('data-productID');
		qty			= parseInt($("#qty-"+productID).val()) - 1;

		if ( qty < 1 ) return;

		$("#qty-"+productID).val(qty);

		// Modification de la valeur en BDD.
		$.ajax({
			type: "GET",
			url: CMSUrl+'ajax=updateQuantity',
			data: "productID="+ productID +"&productQty="+ qty,
			success: function(data) {
				var json = eval('(' + data + ')');
				$('#totalPrice-'+productID).text(json.totalPrice);
				$('#basketPrice').text(json.basketPrice);
				//window.location.reload();
			}
		});
	});

	$(".qty").live('blur', function() {
		product		= $(this).attr('id').split('-');
		productID	= product[1];
		qty			= parseInt($(this).val());

		if ( qty < 1 ) {
			qty		= 1;
			$(this).val(1);
		}

		$.ajax({
			type: "GET",
			url: CMSUrl+'ajax=updateQuantity',
			data: "productID="+ productID +"&productQty="+ qty,
			success: function(data) {
				var json = eval('(' + data + ')');
				$('#totalPrice-'+productID).text(json.totalPrice);
				$('#basketPrice').text(json.basketPrice);
			}
		});
	});

	$("#validateCode").live('click', function() {

		code		= $('#code').val();

		if ( ! code ) {
			alert("Merci de saisir un code !");
			return;
		}

		$.ajax({
			type: "GET",
			url: CMSUrl+'ajax=validateCode',
			data: "code="+ code,
			success: function(data) {
				//window.location.reload();
			}
		});
	});

	/***** ***** ***** *****
	 * 	Module Calendrier
	 ***** ***** ***** *****/

	if ( $('#calendarMonth').is('select') )
		var calendarBaseMonth	= $('#calendarMonth').val();
	else
		var calendarBaseMonth	= $('#calendarMonth a.calendarSelectedMonth').attr('data-monthValue');

	$('#calendarMonth, #calendarDayBegin, #calendarDayEnd').live('change', function() {
		showCalendar();
	});

	$('#calendarMonth a').live('click', function(e) {
		e.preventDefault();
		$('#calendarMonth a').removeClass('calendarSelectedMonth');
		$(this).addClass('calendarSelectedMonth');

		showCalendar('month', $(this).attr('data-monthValue'));
	});

	$('#calendarDayBegin a').live('click', function(e) {
		e.preventDefault();
		$('#calendarDayBegin a').removeClass('calendarSelectedDay');
		$(this).addClass('calendarSelectedDay');

		showCalendar('dayBegin', $(this).attr('data-dayValue'));
	});

	$('#calendarDayEnd a').live('click', function(e) {
		e.preventDefault();
		$('#calendarDayEnd a').removeClass('calendarSelectedDay');
		$(this).addClass('calendarSelectedDay');

		showCalendar('dayEnd', $(this).attr('data-dayValue'));
	});

	var showTodayOnCalendarLoad = false;

	/* Permet le chargement du jour actuel au premier affichage du calendrier. */
	if ( showTodayOnCalendarLoad == false )
	{
		today		= new Date();

		$('#calendarDayBegin a[data-dayValue="'+ today.getDate() +'"]').click();
		$('#calendarMonth a[data-monthValue="'+ today.getMonth() + 1 +'"]').click();

		$('#calendarDayBegin').val(today.getDate());
		$('#calendarMonth').val(today.getMonth() + 1);
	}

	$('.blocCalendar tr').live('mouseover', function() {
		$(this).find('td.calendarNotSelected, .calendarTitle').addClass('calendarHover');
	});

	$('.blocCalendar tr').live('mouseout', function() {
		$(this).find('td.calendarNotSelected, .calendarTitle').removeClass('calendarHover');
	});

	$('td[data-calendarhorary]').live('mouseover', function() {
		var calendarHorary	= $(this).attr('data-calendarhorary');
		$('.calendarNotSelected[data-calendarhorary="'+ calendarHorary +'"]').addClass('calendarHover');
	});

	$('td[data-calendarhorary]').live('mouseout', function() {
		$('.calendarNotSelected[data-calendarhorary]').removeClass('calendarHover');
	});

});

/* Affiche le calendrier avec les valeurs sélectionnées de jours et mois.
 * type permet d'indiquer directement un type de valeur (mois, jour...)
 * value indique la valeur de la sélection. */
function showCalendar( type, value)
{
	/* Sélection du jour de début. */
	if ( type == 'dayBegin' )
		var dayBegin	= value;
	else if ( $('#calendarDayBegin').is('select') )
		var dayBegin	= $('#calendarDayBegin').val();
	else
		var dayBegin	= $('#calendarDayBegin a.calendarSelectedDay').attr('data-dayValue');

	/* Sélection du jour de fin. */
	var dayEnd		= $('#calendarDayEnd').length ? $('#calendarDayEnd').val() : dayBegin;

	/* Sélection du mois. */
	if ( type == 'month' )
		var month	= value;
	else if ( $('#calendarMonth').is('select') )
		var month	= $('#calendarMonth').val();
	else
		var month	= $('#calendarMonth a.calendarSelectedMonth').attr('data-monthValue');


	var bID			= $('input[name=calendar_bID]').val();
	var moduleOptID	= $('input[name=calendar_optID]').val();

	$.ajax({
		type: "GET",
		url: CMSUrl+'ajax=getBlocListCalendar',
		data: "moduleOptID="+ moduleOptID +"&calendarMonth="+ month +"&calendarDayBegin="+ dayBegin +"&calendarDayEnd="+ dayEnd +"&bID="+ bID +"&returnAJAX=1",
		success: function(data) {
			// Affichage du calendrier aux dates sélectionnées.
			$('#showModuleCalendar').html(data);
			addContextMenuToBlocs();
		}
	});

	// Récupération du menu des jours.
	if ( month != calendarBaseMonth )
	{
		calendarBaseMonth	= month;

		$.ajax({
			type: "GET",
			url: CMSUrl+'ajax=getModuleCalendarDays',
			data: "moduleOptID="+ moduleOptID +"&calendarMonth="+ month +"&calendarDayBegin="+ dayBegin +"&calendarDayEnd="+ dayEnd +"&bID="+ bID +"&returnAJAX=1",
			success: function(data) {
				// Affichage du calendrier aux dates sélectionnées.
				$('#calendarDayBegin').replaceWith(data);
			}
		});
	}

} // showCalendar()

jQuery.fn.exists = function(){
	return jQuery(this).length > 0;
}


