$(
  	function(){
		manageWishList();
		deleteFromWishList();
		emptyWishList();
		getWishListItems();
	});

	function addToWishList(type,name,url,id){
		
		$.ajax({
			type	: "POST",
			url		: dirName+"includes/proc-wish-list.php",
			cache	: false,
			data	: "action=add&lang="+lang+"&productType="+type+"&productName="+name+"&productUrl="+url+"&productId="+id+"&path="+dirName,
			success	: function(html){
				
				splits = html.split('|');
				
				if(splits[0] == 'ok'){
					switch(lang){
						case 'en': 
							alert('The '+name+' '+type+' has been added successfuly to your wish list');
						break;
						case 'es':
							alert('El '+type+' '+name+' ha sido agregado exitosamente');
						break;
					}
				}
				deleteFromWishList();
			}
		});
	}
	
	function manageWishList() {
		$('.addWishList').click(function(){
			//productType = $('input#product-type').val();
			productType = $(this).parent().parent().find('input.product-type').val();
			productName = $(this).parent().parent().find('input.product-name').val();
			productUrl = $(this).parent().parent().find('input.product-url').val();
			productId = $(this).parent().parent().find('input.product-id').val();
			
			//productUrl = productUrl
			//console.log(productUrl);
			
			switch(productType){
				case 'hotel': 
					fkpkPais = $('#fk_pk_pais').val();
					fkpkCiudad = $('#fk_pk_prv').val();
					clasification = $('#int_clasificacion').val();
					productUrl += "?fk_pk_pais="+fkpkPais+"&fk_pk_ciudad="+fkpkCiudad+"&int_cls="+clasification;
				break;
				case 'restaurant': 
					fkpkPais = $('#fk_pk_pais').val();
					fkpkCiudad = $('#fk_pk_prv').val();
					productUrl += "?fk_pk_pais="+fkpkPais+"&fk_pk_ciudad="+fkpkCiudad;
				break;
				
			}
			productUrlClean = encodeURIComponent(productUrl);
			
			addToWishList(productType,productName,productUrlClean,productId);
		});
	}

	function getWishListItems() {
		$.ajax({
			type	: "POST",
			url		: dirName+"includes/proc-wish-list.php",
			cache	: false,
			data	: "action=getItems&lang="+lang+"&path="+dirName,
			success	: function(html){
				splits = html.split('|');
				
				if(splits[0] == 'ok'){
					$('#wish-list-content').html(splits[1]);
				} else {
					$('#wish-list-content').addClass('noTours');
					var div = "<div class='wish-list-item notice'>";
					div += splits[1]+"</div>";
					$('#wish-list-content').html(div);
				}
				deleteFromWishList();
			}
		});		
	}
	
	function deleteFromWishList(){
		$('img.delete').click(function(){					   
			var ele = $(this);
			var productType = $(this).prev().prev().prev().val();
			var productId = $(this).prev().prev().val();
		
			size = $('#wish-list-content').find('.wish-list-item').size();
			
			$.ajax({
				type	: "POST",
				url		: dirName+"includes/proc-wish-list.php",
				cache	: false,
				data	: "action=delete&lang="+lang+"&productType="+productType+"&productId="+productId,
				success	: function(html){
					splits = html.split('|');
					
					if(splits[0] == 'ok'){
						if(size == 1){
							$('#wish-list-content').addClass('noTours');
						}
						
						//$(ele).parent().parent().parent().fadeOut('slow').remove();
						//console.log(productId);
						$('#wish-list-content #'+productType+productId).fadeOut('slow').remove();
						//console.log(size);
					}
				}
			});								
		});
	}
	
	function emptyWishList() {
		$('.clear-list').click(function(){
			$.ajax({
			type	: "POST",
			url		: dirName+"includes/proc-wish-list.php",
			cache	: false,
			data	: "action=emptyBasket&lang="+lang+"&path="+dirName,
			success	: function(html){
				splits = html.split('|');
				
				if(splits[0] == 'ok'){
					$('#wish-list-content').addClass('noTours');
					var div = "<div class='wish-list-item notice'><p>";
					div += splits[1]+"</p></div>";
					$('#wish-list-content').empty().append(div);
					
					//$('#casas-container').find('input[type=checkbox]').attr('checked',false);
					//console.log(checkboxs)/*.attr('checked',' '); */
				}
			}
			});	
			deleteFromWishList();
		});
	}
