var urlPanier = 'panier_ajax.php';
var urlDetail = 'detail.php';
var urlResult = 'panier.php';
var iconSupprimerPanier = jsCst_PATH_ICON + 'icone_supprimer_panier.gif';
var miniImage = jsCst_PATH_ICON + 'photo_annonces_indispo_pf.gif';

function add_item(id, photo, desc){
	cart('add', id, photo, desc);
}
function del_item(id, clearBien){
	cart('del', id, null, null);
	if(clearBien){
		document.getElementById('bien_' + id).style.display = 'none';
	}
}
function cart(mode, id, photo, desc){
    if(mode == 'add'){
        data = "mode=add&id=" + id + "&photo=" + photo + "&desc=" + desc;
    } else if (mode == 'del'){
    	data = "mode=del&id=" + id;
    } else if (mode == 'clear'){
        data = "mode=clear";
    } else {
    	data = "mode=load";
    }
    if (document.getElementById("panier")) {
    	var panierPresent = true
    } else {
    	var panierPresent = false
    }
    
	jQuery.getJSON(urlPanier, data,
        function(data){
			if(panierPresent){
			    var panier = document.getElementById('panier');
			    jQuery("#panier div").remove();
	
			    // On cree la div Titre qui va dans la div panier
	            var itemTitle = document.createElement("DIV");
	            itemTitle.id = "title";
	            itemTitle.className = "foncia_panier_title";
		            var titleDesc = document.createElement("LABEL");
		            titleDesc.innerHTML = data.title;
		            itemTitle.appendChild(titleDesc);
	            panier.appendChild(itemTitle);
	            
	            jQuery.each(data.items,
	                function(i,item){
	                    // On cree la div panier
	                    var itemPanier = document.createElement("DIV");
	                    itemPanier.id = "item_" + item.id;
	                    itemPanier.className = "foncia_panier_item";
	
	                        // On cree la div Photo qui va dans la div panier
	                        var itemPhoto = document.createElement("DIV");
	                        itemPhoto.id = "itemPhoto_" + item.id;
	                        itemPhoto.className = "foncia_panier_photo";
		                        var lienPhoto = document.createElement("A");
		                        lienPhoto.setAttribute("href", urlDetail + "?bien_id=" + item.id);
		                            var photoUrl =  document.createElement("IMG");
		                            if(item.urlPhoto != ''){
		                            	photoUrl.setAttribute("src", item.urlPhoto);
		                            } else {
		                            	photoUrl.setAttribute("src", miniImage);
		                            }
		                            photoUrl.style.border = "0px";
		                            photoUrl.style.width = "36px";
		                            photoUrl.style.height = "27px";
		                            photoUrl.setAttribute("alt","");
		                            lienPhoto.appendChild(photoUrl);
		                         itemPhoto.appendChild(lienPhoto);
	                        itemPanier.appendChild(itemPhoto);
	
	                        // On cree la div descr. qui va dans la div panier
	                        var itemDescription = document.createElement("DIV");
	                        itemDescription.id = "itemDesc_" + item.id;
	                        itemDescription.className = "foncia_panier_description";
		                        var lienDescription = document.createElement("A");
		                        lienDescription.setAttribute("href", urlDetail + "?bien_id=" + item.id);
		                        var description = item.description.replace(";EURO;", '&euro;');
		                        description = description.replace("|", '<br />');
		                        lienDescription.innerHTML = description.replace(";SUP2;", '&sup2;');
		                        itemDescription.appendChild(lienDescription);
	                        itemPanier.appendChild(itemDescription);
	
	                        // On cree la div boutton qui va dans la div panier
	                        var itemBouton = document.createElement("DIV");
	                        itemBouton.id = "itemDelete_" + item.id;
	                        itemBouton.className = "foncia_panier_action";
	                        	var lienBouton = document.createElement("A");
	                        	lienBouton.setAttribute("href", "javascript:void(0);");
	                        	lienBouton.onclick = function(){del_item(item.id, false)};
		                        	var BoutonImg =  document.createElement("IMG");
		                            BoutonImg.src = iconSupprimerPanier;
		                            BoutonImg.setAttribute("border","0");
		                            BoutonImg.setAttribute("alt","");
		                            lienBouton.appendChild(BoutonImg);
	                            itemBouton.appendChild(lienBouton);
	                        itemPanier.appendChild(itemBouton);
	                        
	                        var breaker = document.createElement("DIV");
	                        breaker.className = "breaker";
	                        itemPanier.appendChild(breaker);
	
	                    panier.appendChild(itemPanier);
	                    if ( i == 4 ) {
	                        return false;
	                    }
	                }
	            );
	            
	            if(data.nbItems > 0){
	            	var breaker = document.createElement("DIV");
	            	breaker.className = "breaker";
	                panier.appendChild(breaker);
	                
		            // On cree la div Lien qui va dans la div panier
		            var itemLink = document.createElement("DIV");
		            itemLink.id = "link";
		            itemLink.className = "foncia_panier_link";
			            var LinkDesc = document.createElement("A");
			            LinkDesc.setAttribute("href", urlResult);
				        LinkDesc.innerHTML = data.detail;
				        itemLink.appendChild(LinkDesc);
		            panier.appendChild(itemLink);
	            }
	        }
		}
    );
}
//jQuery(document).ready(function(){
//	cart();
//});