//met à jour les infos du panier, dans le menu de droite
function updateInfosPanier(article, euro)
{
	$('home_panier_article').update(article);
	$('home_panier_euro').update(euro);
}

// redirige vers la page du panier
function goToPanier()
{
	window.location.href = '/panier/';
}

/*
 * Detail Article
 */
// appelé par le bouton "ajouter au panier"
function addPanier(article)
{
	var option = $('size_f').getValue();
	var url = '/script.php?file=panier&action=add&article='+article+'&option='+option;
	show_loading();
	
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(response)
		{
			hide_loading();
			
			// success
			if(response.responseJSON.error == 0)
			{
				updateInfosPanier(response.responseJSON.article, response.responseJSON.euro);
				openAddPanier();
			}
			else
			{
				var new_text = '';
				if(response.responseJSON.error == 1) new_text = 'L\'article demandé est introuvable';
				else if(response.responseJSON.error == 2) new_text = 'L\'option demandée n\'existe pas pour cet article';
				else if(response.responseJSON.error == 3) new_text = 'Désolé, mais cet article est en rupture de stock';
				else return;
				
				openAddPanierFail(new_text);
			}
		}
	});
}

/*
 * Bloc ajouté au panier
 */
// affiche le bloc "article ajouté"
function openAddPanier()
{
	var dim_bloc	= $('cadre_fake_popup').getDimensions();
	var dim_window	= document.viewport.getDimensions();
	var scroll		= document.viewport.getScrollOffsets();
	
	var posx = (dim_window.width / 2) - (dim_bloc.width / 2) + scroll.left;
	var posy = (dim_window.height / 2) - (dim_bloc.height / 2) + scroll.top;
	
	if (Prototype.Browser.IE) hide_select();
	$('cadre_fake_popup').setStyle({top:posy+'px',left:posx+'px'});
	$('cadre_fake_popup').appear({duration:0.5});
}

// masque le bloc "article ajouté"
function closeAddPanier()
{
	$('cadre_fake_popup').fade({duration:0.5});
	if (Prototype.Browser.IE) show_select();
}

// affiche le bloc "article ajouté" (echec d'ajout)
function openAddPanierFail(text)
{
	var dim_bloc	= $('cadre_fake_popup_failure').getDimensions();
	var dim_window	= document.viewport.getDimensions();
	var scroll		= document.viewport.getScrollOffsets();
	
	var posx = (dim_window.width / 2) - (dim_bloc.width / 2);
	var posy = (dim_window.height / 2) - (dim_bloc.height / 2);
	
	$('cadre_fake_popup_failure_texte').update(text);
	
	if (Prototype.Browser.IE) hide_select();
	$('cadre_fake_popup_failure').setStyle({top:posy+'px',left:posx+'px'});
	$('cadre_fake_popup_failure').appear({duration:0.5});
}

// masque le bloc "article ajouté" (echec d'ajout)
function closeAddPanierFail()
{
	$('cadre_fake_popup_failure').fade({duration:0.5});
	if (Prototype.Browser.IE) show_select();
}

/*
 * PANIER
 */
//actualise l'affichage du panier
function updatePanier(vars)
{
	new Ajax.Request('/script.php?file=panier&'+vars, {
		method: 'get',
		onSuccess: function(response)
		{
			hide_loading();
			$('panier_body').update(response.responseJSON.panier);
			updateInfosPanier(response.responseJSON.article, response.responseJSON.euro);
			
			//pas assez de stock
			if(response.responseJSON.error == 1)
			{
				var new_text = '';
				if(response.responseJSON.error == 1) new_text = 'Désolé, mais nos stocks sont insuffisants';
				openAddPanierFail(new_text);
			}
		}
	});
}

//ordonne la suppression d'un element du panier
function deletePanier(id)
{
	show_loading();
	updatePanier('action=delete&elem_id='+id);
}

/*
 * Modif des quantité
 */
//affiche le bouton "ok" des quantité
function showSubmitQuantite(id)
{
	if($('quantite_b'+id)) $('quantite_b'+id).show();
}

//masque le bouton "ok" des quantité
function hideSubmitQuantite(id)
{
	if($('quantite_b'+id)) $('quantite_b'+id).hide();
}

//
function submitChangeQuantite(id)
{
	show_loading();
	hideSubmitQuantite(id);
	var quantite = $('quantite_f'+id).value;
	updatePanier('action=quantite&elem_id='+id+'&new_q='+quantite);
}
