$( document ).ready( function(){
	
	checkContentCaddie();
	
	$('#modeLivraison').change( updatePrixLivraison );
	
	checkCodePromo();
	$('#codePromo').keyup( checkCodePromo );
	
	$('#passerCommande').click( checkModeLivraisonChoisi );
	
	checkCookies();
});


function checkCookies()
{
	if (document.cookie)
	{
		//alert("Les cookies sont bien paramétrés !");
	}
	else
	{
		document.cookie = "essai=cookie";
		if( document.cookie ) 
		{
			//alert("Cookies correctement paramétrés !");
		}
		else
		{
			ajaxContext.showAll();
			ajaxContext.animAjaxDiv( ajaxContext.messageBox.curry( ajaxContext, '<p class="error">Pour que le caddie fonctionne, il faut que votre navigateur accepte les cookies, si vous rencontrez des problèmes, vous pouvez contacter notre holine: 09.53.25.55.20</p>' ) );
		}
	}
	
	return false;
}


function supprimerProduit( id )
{	
	$.ajax( { async : true, data:  'id='+id, url: SITE_URL+'/ecommerce/Caddie/supprimerProduit', beforeSend:showAjaxLoader2,  success : successAjax, error: showCaddieError  } );
}

function augmenterQuantite( id )
{	
	// en async false, pas de beforeSend sur safari et ie!!
 	$.ajax( { async : true, data:  'id='+id, url: SITE_URL+'/ecommerce/Caddie/augmenterQuantite',beforeSend:showAjaxLoader2, success : successAjax, error: showCaddieError  } );
}

function diminuerQuantite( id )
{	
	$.ajax( { async : true, data:  'id='+id, url: SITE_URL+'/ecommerce/Caddie/diminuerQuantite', beforeSend:showAjaxLoader2,  success : successAjax, error: showCaddieError  } );
}

function showAjaxLoader2()
{
	$('#ajaxLoader2').css( 'display', 'inline' );
}

function hideAjaxLoader2()
{
	$('#ajaxLoader2').hide();
}

function successAjax( response )
{
	if( response.status == 'ok' ) 
	{ 
		switch( response.action )
		{
			case 'supprimerProduit':
				successSupprimer( response );
				break;	
			
			case 'augmenterQuantite':
				successAugmenter( response );
				break;				
			
			case 'diminuerQuantite':
				successDiminuer( response );
				break;			
			
			default:
				alert( 'action inconnue' );
		}
	}
	else 
	{
		ajaxContext.showAll();
		ajaxContext.animAjaxDiv( ajaxContext.messageBox.curry( ajaxContext, '<p class="error">'+response.messageError+'</p>' ) );
	}

	hideAjaxLoader2();
}

function showCaddieError( XMLHTTPREQUEST )
{
	hideAjaxLoader2();
	ajaxContext.showAll();
	ajaxContext.animAjaxDiv( ajaxContext.messageBox.curry( ajaxContext, '<p class="error">'+XMLHTTPREQUEST.responseText+'</p>' ) );
}

function successSupprimer( response )
{
	var id = response.id;

	$('#ligneCaddie_'+id ).remove();
	
	// SsTtotal
	$('#ssTotal').text( $.sprintf( '%.2f', response.ssTotal ) );
	
	// Livraison
	$('#modeLivraison option').next().val( response.fraisPort );
	
	// Reduction
	if( !$('#codePromoForm').length )
	{
		var reduction = ( response.reduction ) ?  $.sprintf( ' %.2f', response.reduction ) : '0.00';
		$('#tdReduction').html( '- <span id="reduction">'+reduction+'</span> &euro;' );
	}
	
	checkContentCaddie();
}

function successAugmenter( response )
{
	successChangerQuantite( response, 1 );
}

function successDiminuer( response )
{
	successChangerQuantite( response, -1 );
}

function successChangerQuantite( response, nombre )
{
	var id = response.id;
	
	var newQte = parseInt( response.qte );
	$('#qte_'+id).text( newQte );
	
	var prixUnit = parseFloat( $('#prixUnit_'+id ).text() );
	
	// prix total ligne
	var prixTotalLigne = prixUnit * newQte;
	$('#prixTotal_'+id ).text( $.sprintf('%.2f', prixTotalLigne ) );

	// sous total
	$('#ssTotal').text( $.sprintf('%.2f', response.ssTotal ) );
	
	// frais de port
	$('#modeLivraison option').next().val( response.fraisPort );
	
	if( !$('#codePromoForm').length )
	{
		var reduction = ( response.reduction )?  $.sprintf( ' %.2f', response.reduction ) : '0.00';
		$('#tdReduction').html( '- <span id="reduction">'+reduction+'</span> &euro;' );
	}
	
	if( newQte == 0 )
	{
		$('#ligneCaddie_'+response.id ).remove();
		
		checkContentCaddie();
	}
	else
	{
		updatePrixLivraison();
	}
	
	return newQte;
}

function checkContentCaddie()
{
	if( $('tr.ligneCaddie').length == 0 )
	{
		$('#ssTotal').text( '0.00' );
		$('#total').text( '0.00' );
		$('#passerCommande').css( 'display', 'none' );
		$('#passerCommande').parent().css( 'line-height', '0');
		$('#passerCommande').parent().css( 'padding', '0');
		$('#modeLivraison option:first').attr( 'selected', 'selected' );
		$('#modeLivraison').attr( 'disabled', 'disabled' );
		$('#codePromo').attr( 'disabled', 'disabled' );
		$('#prixLivraison').html( '&nbsp;' );
		//$('#reduction').html( '&nbsp;' );
		$('#tdReduction').html( '&nbsp;' );
		if( $('#panierVide').length == 0 ) $('#caddie tr:first').after( '<tr><td colspan="7">Votre panier est vide</td></tr>' );
	}
	else
	{
		updatePrixLivraison();
	}
}

function updatePrixLivraison()
{
	var prixLivraison = 0;
	var valLivraison = $('#modeLivraison').val();
	
	if(  valLivraison == 0 )
	{
		$('#prixLivraison').html( 'offert' );
	}
	else if( valLivraison == -1 )
	{
		$('#prixLivraison').html( '&nbsp;' );
	}
	else
	{
		prixLivraison = parseFloat( $('#modeLivraison').val() )
		$('#prixLivraison').html( $.sprintf( '%.2f', prixLivraison ) + " &euro;" );
	}
	
	var reduction;
	if( $('#reduction').length )
	{
		reduction = parseFloat( $('#reduction').text() );
	}
	reduction = reduction ? reduction : 0;
	
	// mis à jour du total
	var total = parseFloat( $('#ssTotal').text() ) + prixLivraison - reduction;
	$('#total').text( $.sprintf( '%.2f', total ) );
}

function checkModeLivraisonChoisi()
{
	if( $('#modeLivraison').val() != -1 )
	{
		return true;
	}
	else
	{
		ajaxContext.showAll();
		ajaxContext.animAjaxDiv( ajaxContext.messageBox.curry( ajaxContext, '<p class="error">Vous devez choisir un mode de livraison<br />avant de passer la commande</p>' ) );
		return false;
	}
}

function checkCodePromo()
{
	if( $('#codePromo').val() != '' ) $('#submitCodePromo').show();
	else $('#submitCodePromo').hide();
}