$(document).ready(function(){
	
	
	/****************************************/
	
	// Initialisation de la fenêtre de dialog
	$( "#dialog-wish" ).dialog({
		height		: 340,
		width		: 340,
		autoOpen	: false,
		resizable	: false,
	 	modal		: true
	});
	
	// Initialisation de la fenêtre de dialog
	$( "#create-contact" ).dialog({
		height		: 240,
		width		: 370,
		autoOpen	: false,
		resizable	: false,
	 	modal		: true,
	 	buttons: {
			"Annuler": function() {
				$( this ).dialog( "close" );
			},
			"Valider": function() {
				//window.open( 'spe_print_liste.php' );
				recContact();
				$( this ).dialog( "close" );
			}
		}
	});
	
	// Initialisation de la fenêtre de dialog
	$( "#see-wish-list" ).dialog({
		height		: 600,
		width		: 980,
		autoOpen	: false,
		resizable	: false,
	 	modal		: true,
		buttons: {
			"Fermer": function() {
				$( this ).dialog( "close" );
			},
			"Imprimer": function() {
				isPrintable();					
				$( this ).dialog( "close" );
			},
			"Nouvelle liste": function() {
				unlinkWishList();
				$( this ).dialog( "close" );
			}
		}

	});


	/****************************************/
	
	// Evènement sur le click du bouton ajouter a ma liste
	$( '#add-wish' ).click(function(){
		openDialWish( 'dialog-wish' );
	});
	
	// Evènement sur le click du bouton ajouter
	$( '.btn-wish #add' ).click(function(){
		addToWishingList();
	});
	
	// Evènement sur le click pour voir la liste
	$( '#voir-wish-list' ).click(function(){
		seeWishList();
	});
	
	
	
	
	
	
	/****************************************/
	init();
	
	
	
	/****************************************/
	
	// Crée un contact pour une liste et lance l'impression
	function recContact(){
		
		var email	=	$( '#wish-mail' ).val();
		var csp		=	$( '#csp' ).val();
		$.getJSON(
			'../../../../ajax/spe_wish_list.php',
			{
				type :	'contact',
				email:	email,
				csp	 :	csp
			},
			function( data ){
				
				if( data.retour ){
					startPrint();				
				}
			}
		);
	}
	
	// Redirige pour l'impression
	function startPrint(){
		window.open( 'spe_print_liste.php' );		
	}
	
	// Test si il est possible d'imprimer
	// ==> oui, on imprime
	// ==> non, on affiche le formulaire de contact
	function isPrintable(){
		
		$.getJSON(
			'../../../../ajax/spe_wish_list.php',
			{
				type:	'printable'
			},
			function( data ){
				
				if( data.retour ){
					startPrint();	
				}
				else{
					openDialWish( 'create-contact' );		
				}
			}
		);
	}

	// Supprime le cookie permettant de faire la liaison avec la liste
	function unlinkWishList(){
		
		$.getJSON(
			'../../../../ajax/spe_wish_list.php',
			{
				type:	'unlink'
			},
			function( data ){
				
				if( data.retour ){

					$( '#qte-wish-list' ).html( data.qte );
					$( '#montant-wish-list' ).html( data.montant );
					$( '#content-wish-list' ).fadeOut();					
				}
			}
		);
		
	}
	
	// Initialise les events sur la boite de dialog de ma sélection
	function initEventAfterLoad(){
		
		// Evènement pour supprimer un article de la liste
		$( '.remove-this' ).unbind().click(function(){
			removeArticle( $(this) );
		});
		
		// Force l'utilisateur à n'écrire que des valeurs numériques
		$('.spe-qte-list').numeric();
		
		// Lorsqu'on change une quantité, on recalcule
		$( '.spe-qte-list' ).change(function(){
			changeQteArticle( $(this) );
		});
		
		
	}
	
	// Modifie une quantité d'article dans la wish list
	function changeQteArticle( pObject ){
		var idLine		=	pObject.parent().parent().attr( 'id' ).replace( 'p-ligne-' , '');
		var newQte		=	Math.abs( pObject.val() );
		
		$.getJSON(
			'../../../../ajax/spe_wish_list.php',
			{
				il	:	idLine,
				qte	:	newQte,
				type:	'edit'
			},
			function( data ){
				
				if( data.retour ){

					$( '#montant-total-see' ).html( data.montant+' &euro;' ).effect( 'highlight' );
					$( '#qte-total-see' ).html( data.qte ).effect( 'highlight' );
					
					if( data.type=='del' ){
						$( '#p-ligne-'+idLine ).hide( 'blind', function(){
							$(this).remove();
						} );
					}
					else{
						$( '#p-ligne-'+idLine+' .prix-total' ).html( data.newMontant +' &euro;');
					}
					
					
					
					refreshWishBox( data );
				}
			}
		);
		
	}
	
	
	// Retire un article de ma wishList
	function removeArticle( pObject ){		
		var idLine		=	pObject.parent().attr( 'id' ).replace( 'p-ligne-' , '');
		
		$.getJSON(
			'../../../../ajax/spe_wish_list.php',
			{
				il	:	idLine,
				type:	'del'
			},
			function( data ){
				
				if( data.retour ){

					$( '#montant-total-see' ).html( data.montant+' &euro;' ).effect( 'highlight' );
					$( '#qte-total-see' ).html( data.qte ).effect( 'highlight' );
					$( '#p-ligne-'+idLine ).hide( 'blind', function(){
						$(this).remove();
					} );
					
					refreshWishBox( data );
				}
			}
		);
	}
	
	// Fonction ouverture de la fenêtre de dialog
	function openDialWish( pId ){
		$( '#dialog-qte-wish' ).html($( '#quantite' ).val());
		$( '#'+pId ).dialog('open');
	}
	
	// Fonction de fermeture de la fenêtre de dialog
	function closeDialWish( pId ){
		$( '#'+pId ).dialog('close');
	}
	
	
	// Met à jour le contenu de la boite de wish list
	function refreshWishBox( pJson ){
		$( '#qte-wish-list' ).html( pJson.qte );
		$( '#montant-wish-list' ).html( pJson.montant );
		showWishBox();		
	}
	
	
	// Affiche la wish box
	function showWishBox(){
		if( $( '#content-wish-list' ).css('display')=='none' ){
			$( '#content-wish-list' ).show('bounce');
		}
		else{
			$( '#content-wish-list' ).effect('shake',250)
		}
	}
	
	// Initialise la wish box
	function init(){
		
		$.getJSON(
			'../../../../ajax/spe_wish_list.php',
			{
				type:	'init'
			},
			function( data ){
				
				if( data.retour )
					refreshWishBox( data );
			}
		);
	}
	
	
	// Ajoute un article à la wish list
	function addToWishingList(){
		
		$.getJSON(
			'../../../../ajax/spe_wish_list.php',
			{
				qte	:	$( '#dialog-qte-wish' ).html(),
				av	:	$( '#av' ).val(),
				type:	'add'
			},
			function( data ){
				
				if( data.retour ){
					
					// Fermeture de la fenêtre de dialog
					closeDialWish( 'dialog-wish' );
					refreshWishBox( data );
					
				}
			}
		);
	}
	
	
	// Charge le contenu de la fenêtre de dialog
	function seeWishList(){
		
		$('#content-wish-see').load(
			'../../../../ajax/spe_wish_list.php',
			{
				type:	'load'
			},
			function(){
				openDialWish( 'see-wish-list' );
				initEventAfterLoad();
			}
		);
	}
	
	
});

