// selection
var agSelection =
	new Class(
	{
		initialize:
			function(container)
			{
				// properties
				this.container = $(container);

				this.button_img = $(container + "-button");

				this.button_lnk = $(container + "-toggle");

				this.button_lnk.addEvent("click", function() { this.toggle() }.bind(this));

				this.label = $(container + "-title");
				this.layer = $(container + "-ads");

				this.count = 0;

				// cookie
				this.cookie = new Hash.Cookie("LICartCookie", { duration: 10, path: "/" });

				// processing
				this.preload();
				this.load();
				this.close();
			},

		preload:
			function()
			{
				path = _urlmmf + "static/pictos/";

				this.images = new Array();

				this.images[0] = new Image();
				this.images[0].src = path + "bt-open.gif";

				this.images[1] = new Image();
				this.images[1].src = path + "bt-close.gif";
			},

		toggle:
			function()
			{
				if (this.count > 0)
				{
					this.isOpen ? this.close() : this.open();
				}
			},

		open:
			function()
			{
				if (this.isOpen == true)
				{
					return;
				}

				this.button_img.setProperty("src", this.images[1].src);
				this.layer.setStyle("display", "block");

				this.isOpen = true;
			},

		close:
			function()
			{
				if (this.isOpen == false)
				{
					return;
				}

				this.button_img.setProperty("src", this.images[0].src);
				this.layer.setStyle("display", "none");

				this.isOpen = false;
			},

		setLabel:
			function()
			{
				if (this.count > 0)
				{
					if(this.count == 1)
					{
						text = "Ma sélection contient " + this.count + " bien";
					}
					else
					{
						text = "Ma sélection contient " + this.count + " biens";
					}

				}
				else
				{
					text = "Ma sélection ne contient aucun bien";
				}

				this.label.set('html',text);

				/*
				clear = this.header.getElement("a[id=mySelectionClear]");

				if (this.count > 0)
				{
					if (!clear)
					{
						clear = new Element("a", { "id" : "mySelectionClear", "href" : "javascript:mySelection.confirm(\"\", \"clearAll\")" }).injectAfter(this.label);
						clear.set("text",LABEL["CART_EMPTY"]);
					}
				}
				else
				{
					if (clear)
					{
						clear.dispose();
					}
				}
				*/
			},

		add:
			function(parameters, save)
			{
				// 0 - id
				// 1 - price
				// 2 - default_town
				// 3 - post_code
				// 4 - property_type
				// 5 - area
				// 6 - nb_rooms
				// 7 - photo
				// 8 - univers
				// 9 - sourcepage

				if ($("li-selection-ad-" + parameters[0]))
				{
					openInterstitiel('interstitiel_ajout_selection.php?sourcepage='+parameters[9],'Information',600,100); return false;
					return;
				}

				if (this.count < 25)
				{
					// default town
					if (parameters[2].length > 20)
					{
						parameters[2] = parameters[2].substring(0, 16) + "...";
					}

					// features
					features = new Array();

					features.push(LABEL["COMMON_PROPERTY_" + parameters[4]]);

					if (parameters[5] > 0)
					{
						features.push(parameters[5] + LABEL["COMMON_AREA_UNIT"]);
					}

					if (parameters[6] > 0)
					{
						features.push(parameters[6] + LABEL["COMMON_ROOMS_UNIT"])
					}

					// image
					//image = (parameters[7] == "" ? _urlmmf + "static/selection/default.gif" : "http://images.logicimmo.com/getimage2.php?v5=1&path=thumb&wm=69&hm=53&type=crop&id="+parameters[7]);
					// A ACTIVER LORS DU PASSAGE SUR YACAST !!!
					image = (parameters[7] == "" ? _urlmmf + "static/selection/default.gif" : ("http://mmf.logic-immo.com/mmf/ads/photo-crop-69x53/"+parameters[0].substr(0,3)+"/"+parameters[7]+".jpg").toLowerCase());

					// link => Rewrited if possible
					if(parameters[8]==1) link = "detail-vente-" + parameters[0] + ".htm?ext=1";
					else if(parameters[8]==2) link = "detail-location-" + parameters[0] + ".htm?ext=1";
					else if(parameters[8]==8) link = "detail-colocation-" + parameters[0] + ".htm?ext=1";
					else link = "detail.php?id=" + parameters[0] + "&site=" + _site + "&lang=" + _lang + "&univers=" + parameters[8] + "&ext=1";
					link = _http + link;	// Utiliser l'url

					// separator
					this.layer.adopt(new Element("div", { "class" : "li-selection-line", "id" : "li-selection-line-" + parameters[0] }));

					// container
					var container = new Element("div", { "class" : "li-selection-ad", "id" : "li-selection-ad-" + parameters[0] }).injectInside(this.layer);

					// content
					var content = new Element("div", { "class" : "li-selection-content" , "id" : "li-selection-content-" + parameters[0] });
					content.injectInside(container);

					// photo
					var photo = new Element("div", { "class" : "li-selection-photo" }).injectInside(content);
					var photo_lnk = new Element("a", { "href" : link, "title" : "" }).injectInside(photo);

					photo_lnk.adopt(new Element("img", { "src" : image, "width" : 81, "height" : 61 }));

					// description
					var desc = new Element("div", { "class" : "li-selection-desc" }).injectInside(content);

					/*
					AVANT modif inès 02/02/2010
					var desc_lnk = new Element("a", { "href" : link, "title" : "", "class" : "price" }).injectInside(desc);

					desc_lnk.adopt(new Element("p", { "class" : "price" }).set('html',LABEL["COMMON_UNIVERSE_" + parameters[8]] + " &#149; " + (parameters[1].toInt() >= 2000000000 ? LABEL["COMMON_NC"] : n_f(parameters[1]) + " " + LABEL["COMMON_PRICE_UNIT"])));
					desc_lnk.adopt(new Element("p", { "class" : "loc" }).set('html',parameters[2] + " (" + parameters[3] + ")"));
					desc_lnk.adopt(new Element("p").set('html',features.join(" &#149; ")));

					*/

					/* Après modif Inès 02/02/2010 */

					var desc_price = new Element("a", { "href" : link, "title" : "", "class" : "price" }).injectInside(desc);
					desc_price.set('html',LABEL["COMMON_UNIVERSE_" + parameters[8]] + " &#149; " + (parameters[1].toInt() >= 2000000000 ? LABEL["COMMON_NC"] : n_f(parameters[1]) + " " + LABEL["COMMON_PRICE_UNIT"]));

					var desc_loc = new Element("a", { "href" : link, "title" : "", "class" : "loc" }).injectInside(desc);
					desc_loc.set('html',parameters[2] + " (" + parameters[3] + ")");

					var desc_features = new Element("a", { "href" : link, "title" : "" }).injectInside(desc);
					desc_features.set('html',features.join(" &#149; "));

					/* fin modif ines*/


					// option
					var option = new Element("div", { "class" : "li-selection-option" }).injectInside(container);
					var option_lnk = new Element("a", { "href" : "javascript:mySelection.del('" + parameters[0] + "')" }).injectInside(option);

					option_lnk.adopt(new Element("img", { "src" : _urlmmf + "static/pictos/delete-selection.gif", "width" : 23, "height" : 17, "title" : "Supprimer de ma sélection"  }));

					this.count = this.count + 1;
					this.setLabel();

					// updating cookie
					if (save == true)
					{
						this.save(parameters);

						//ines
						var myLabel = "Information";

						if (_minisiteagence == 1)
						{
							var url_doc = _http+"form_relation_agence_powersite.php?id="+parameters[0]+"&pagef="+_self+"&univers="+_univ+"&pageref="+_page;

    						if (_gallery == 1)
    						{
    						    url_doc += "&gallery=1";
    						}
						}
						else
						{
							var url_doc = "/interstitiel_relation_agence.php?id="+parameters[0]+"&pagef="+_self+"&univers="+_univ+"&pageref="+_page+"&sourcepage="+_page;

    						if (_gallery == 1)
    						{
    						    url_doc += "&gallery=1";
    						}
						}

						var paramtrans = parameters;
						var mode = 1;

						// alert(_page);
						if(_page == 'resultats.php'){
							xt_med('F', '19', 'Partager_une_annonce::Ajouter_a_ma_selection_list_result');
						}else {
							xt_med('F', '19', 'Partager_une_annonce::Ajouter_a_ma_selection_fiche_annonce');
						}
                        openInterstitiel(url_doc,myLabel,700,350);

						/////////////////////////////////////////////////
						//Appel de la sonde ambiguous
						/*
						params = {

							'locname'  	: parameters[2],
							'zip'    	: parameters[3],
							'type'    	: parameters[4],
							'univers'	: _univ

						};

						new Request.JSON({
							url: "/ajax/sonde_ajoutselection.php",
							onComplete: function(data) {

								if (data["state"] == "0")
								{
									//alert("Error !!!");
								}
								else
								{
									//alert("ok");

								}

							}
						}).send(params);
						//fin ines
						*/
					}

					//ines
					this.open();
					//fin ines
				}
				else
				{
					// myMsgBox.show(LABEL["CART_LIMIT_REACHED"].replace(/#NB_ITEMS#/, 25), 1);
					return;
				}
			},

		confirm:
			function(id, method)
			{
				switch (method)
				{
					case "del" :
						this.id_to_remove = id;

						(LABEL["CART_DEL_CONFIRMATION"], 2, this, "del");

						break;

					case "clearAll" :
						myMsgBox.show(LABEL["CART_CLEAR_CONFIRMATION"], 2, this, "clearAll");

						break;
				}
			},

		proceed:
			function(method)
			{
				switch (method)
				{
					case "del" :
						this.del(this.id_to_remove);
						this.id_to_remove = "";

						break;

					case "clearAll" :
						this.clearAll();

						break;
				}
			},

		del:
			function(id)
			{
				$("li-selection-line-" + id).dispose();
				$("li-selection-ad-" + id).dispose();

				this.count = this.count - 1;

				this.setLabel();

				if (this.count == 0)
				{
					this.close();
				}

				data = this.cookie.get("selection");

				if (data != null)
				{
					for (var i = 0; i < data.length; i ++)
					{
						if (data[i][0] == id)
						{
							data.splice(i, 1);
						}
					}

					this.cookie.set("selection", data);
				}
			},

		clearAll:
			function()
			{
				this.container.getChildren().each( function(item) { item.dispose() });
				this.cookie.set("selection", []);

				this.count = 0;

				this.setLabel();
				this.close();
			},

		load:
			function()
			{
				data = this.cookie.get("selection");

				if (data != null)
				{
					for (var i = 0; i < data.length; i ++)
					{
						this.add(data[i], false);
					}

					this.countAds = i;
				}

				this.setLabel();
			},

		save:
			function(data)
			{
				temp = this.cookie.get("selection");

				if (temp == null)
				{
					this.cookie.set("selection", [data]);
				}
				else
				{
					temp.push(data);

					this.cookie.set("selection", temp);
				}
			}
	});

// global
var mySelection = "";

// loader
window.addEvent("domready",
	function() {

		if ($("li-selection"))
		{
			mySelection = new agSelection("li-selection");
		}
	}
);
