// JavaScript Document
jQuery.noConflict();
(function($){
$(function(){

$(document).ready(function(){
	//Récupération des textes traduits
	var text = $.ajax({
		type: "POST",
		url: "/fr/ajax/getText.php",
		dataType: "json",
		async: false,
		cache: false,
		data: "file=wishlist"
	});
	text = eval('(' + text.responseText + ')');
	
	$('div#wishList a').live('click', function(){
		id = $(this).attr('id').substr(12, $(this).attr('id').length);
		//Ajout du produit dans la wishlist	
		if($(this).hasClass('add')){
			var json = $.ajax({
				type: "POST",
				url: "http://"+window.location.host+"/"+text.info.languageCode+"/ajax/wishlist.php",
				dataType: "json",
				async: false,
				data: "action=add&idProduct="+id
			});
			json = eval('(' + json.responseText + ')');
			if(json.message.type != 'error'){
				//Ajout du produit dans la wishlist Visuel
				nameProduct = $(this).parent().children('span').html();
				urlProduct 	= window.location.href;
				//Si la liste est vide, je supprime le texte et ajoute le bouton partager
				if(!$('div#listeCadeaux a').position()){
					$('div#listeCadeaux').html('<div id="grpButtonModuleWishList"><input type="button" id="shareWishList" value="'+text.PARTAGER+'" /><input type="button" id="saveWishList" value="'+text.SAUVER+'" /></div>');
				}
				$('div#listeCadeaux').prepend('<a id="wishProdId'+id+'" href="'+urlProduct+'">'+nameProduct+'</a>');
				//Effet sur l'ajout de produit dans la wishlist
				$('body').append('<div id="effectWish"></div>');
				var position = $(this).position();
				$('div#effectWish').css('position','absolute')
								   .css('border','1px solid #000')
								   .css('width','115px')
								   .css('height','30px')
								   .css('top',position.top-2)
								   .css('left',position.left-8);
				var position2 = $('div#listeCadeaux').position();
				$('div#effectWish').animate({left:position2.left, top:position2.top, width:0, height:0},
											1000,
											'swing',
											function(){
												$('div#effectWish').css('border','0px');	
												$('div#effectWish').remove();
											});
				//On d�sactive le click pour ne pas rajouter plusieurs fois le produit � la wishlist
				//$('div#wishList a').die( "click" )
				//Affichage du bouton ajout
				$('a#prodWishLink'+id).removeClass('add');
				$('a#prodWishLink'+id).addClass('delete');
				$('a#prodWishLink'+id).html(text.SUPPRIMER_DE_MA_LISTE_DE_CADEAUX);
			}
		}
		//Suppression du produit dans la wishlist	
		else if($(this).hasClass('delete')){
			//Suppression visuelle du produit
			$('a#wishProdId'+id+'').remove();
			//Suppression en BDD
			var json = $.ajax({
				type: "POST",
				url: "http://"+window.location.host+"/"+text.info.languageCode+"/ajax/wishlist.php",
				dataType: "json",
				async: false,
				data: "action=delete&idProduct="+id
			});
			//Affichage du bouton ajout
			$('a#prodWishLink'+id).removeClass('delete');
			$('a#prodWishLink'+id).addClass('add');
			$('a#prodWishLink'+id).html(text.AJOUTER_A_MA_LISTE_DE_CADEAUX);
			//Si la liste est vide je supprime les btn partager et sauver
			if(!$('div#listeCadeaux a').position()){
				$('div#listeCadeaux').html(text.VIDE);
				$('div#grpButtonModuleWishList').remove();
			}
		}
	});
	//Partage de la liste en cours
	$('input#shareWishList').live('click', function(){
		window.location.href = "http://"+window.location.host+"/"+text.info.languageCode+"/share_wishlist.php";
	});
	//Sauvegarde de la liste en cours
	$('input#saveWishList').live('click', function(){
		var json = $.ajax({
			type: "POST",
			url: "http://"+window.location.host+"/"+text.info.languageCode+"/ajax/isLog.php",
			dataType: "json",
			async: false
		});
		json = eval('(' + json.responseText + ')');
		//Si je suis identifi� je demande sous quel nom sauvegarder la liste
		if(json.message.type != 'error'){
			window.location.href = "http://"+window.location.host+"/"+text.info.languageCode+"/listing_wishlist.php";
		//Si je ne suis pas identifi� j'informe qu'il faut s'identifier et je redirige vers l'identification
		}else{
			window.location.href = "http://"+window.location.host+"/"+text.info.languageCode+"/login.php?origin=listing_wishlist.php";
		}
	});
	//Nouvelle liste en cours
	$('input#newWishList').live('click', function(){
		var json = $.ajax({
			type: "POST",
			url: "http://"+window.location.host+"/"+text.info.languageCode+"/ajax/wishlist.php",
			dataType: "json",
			async: false,
			data: "action=new"
		});
		json = eval('(' + json.responseText + ')');
		if(json.message.type != 'error'){
			window.location.href = "http://"+window.location.host+"/"+text.info.languageCode+"/listing_wishlist.php";
		}
	});
	//Nouvelle liste en cours
	$('input#saveNameCurrentListe').live('click', function(){
		var json = $.ajax({
			type: "POST",
			url: "http://"+window.location.host+"/"+text.info.languageCode+"/ajax/wishlist.php",
			dataType: "json",
			async: false,
			data: "action=name&name="+$('input#nameListeCurrent').val()
		});
		json = eval('(' + json.responseText + ')');
		//Pas d'erreur je change le texte du label
		if(json.message.type != 'error'){
			//window.location.href = "http://"+window.location.host+"/"+text.info.languageCode+"/listing_wishlist.php";
			if(!$('div#retour').position()){
				$('input#saveNameCurrentListe').after('<div id="retour">'+json.message.value+'</div>');
				var idCurrentListe = $('h3.bleu').attr('id').substr(7,$('h3.bleu').attr('id').length);
				$('a#liste'+idCurrentListe).html($('input#nameListeCurrent').val());																		 
			}else{
				$('div#retour').html(json.message.value);
				var idCurrentListe = $('h3.bleu').attr('id').substr(7,$('h3.bleu').attr('id').length);
				$('a#liste'+idCurrentListe).html($('input#nameListeCurrent').val());
			}
		}												   
	});
});

});
})(jQuery);