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

				this.label = this.header.getElement("a[id=mySelectionTxt]");
				this.label.addEvent("click", function() { this.toggle() }.bind(this));

				this.imgPin = this.header.getElement("td[id=mySelectionPin]");
				this.imgTws = this.header.getElement("div[id=mySelectionTws]");

				this.imgArw = this.header.getElement("a[id=mySelectionArw]");
				this.imgArw.addEvent("click", function() { this.toggle() }.bind(this));

				this.layer = $(layer);

				this.container = $(container).getElement("tbody");
				this.count = this.container.getChildren().length - 1;

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

				this.preload();
				this.load();
				this.close();

				// preopens selection on czech homepage if items are under 10
				if (_site == "cz" && _page == "index.php")
				{
					if (this.count > 0 && this.count < 10)
					{
						this.open();
					}
					
				}
				if (_site=="fr" && _page == "index.php")
				{
					if (this.count > 0)
					{
						this.open();
					}
					else
					{
						$('maselection').setStyle("display", "none");
					}
					
				}				
			},

		preload:
			function()
			{
				this.classes = new Array();

				this.classes[0] = 'openPin';
				this.classes[1] = 'closePin';

				this.classes[2] = 'openTws';
				this.classes[3] = 'closeTws';

			},

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

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

				this.imgPin.setProperty("class", this.classes[1]);
				this.imgTws.setProperty("class", this.classes[3]);

				this.layer.setStyle("display", "block");

				this.isOpen = true;
			},

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

				this.imgPin.setProperty("class", this.classes[0]);
				this.imgTws.setProperty("class", this.classes[2]);

				this.layer.setStyle("display", "none");

				this.isOpen = false;
			},

		setLabel:
			function()
			{
				text = LABEL["CART_HEADER"].replace(/#NB_ITEMS#/, this.count) /*+ (this.count > 0 ? " - " : "")*/;

				this.label.setHTML(text);

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

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

		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

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

					// cleans previous photo parameter
					parameters[7] = parameters[7].replace(/http\:\/\/images.logicimmo.com\/getimage2.php\?v5\=1\&path\=thumb\&wm=440\&hm=330\&type=prop\&id=/, "");
					parameters[7] = parameters[7].replace(/\/images\/common\/default_small.jpg/, "");

					image_0 = _static_img + "/images/common/default_small.jpg";
					image_1 = "http://images.logicimmo.com/getimage2.php?v5=1&path=thumb&wm=69&hm=53&type=crop&id=";

					if ($("selectedAd" + parameters[0]))
					{
						myMsgBox.show(LABEL["CART_ALREADY_SELECTED"], 1);

						return;
					}

					line = new Element("tr", { "id" : "selectedAd" + parameters[0]}).injectInside(this.container);
					arrw = new Element("td", { "class" : "fleche" }).adopt(new Element("img", { "src" : _static_img + "/images/pictos/fleche_titre.gif" })).injectInside(line);

					// content
					body = new Element("td", { "class" : "bien" }).injectInside(line);

					// link
					price = (parameters[1].toInt() >= 2000000000 ? LABEL["COMMON_NC"] : n_f(parseInt(parameters[1])) + " " + LABEL["COMMON_PRICE_UNIT"]);

					link = new Element("a", { "href" : "detail.php?id=" + parameters[0] + "&site=" + _site + "&lang=" + _lang + "&univers=" + parameters[8] + "&ext=1" });

					link.adopt(new Element("img", { "align" : "left", "src" : (parameters[7].length == 0 ? image_0 : image_1 + parameters[7]), "height" : 33, "width" : 44 }));
					link.adopt(new Element("span", { "class" : "desc" }).setHTML(LABEL["COMMON_PROPERTY_" + parameters[4]] + (parameters[5] > 0 ? " &#149; " + parameters[5] + " " + LABEL["COMMON_AREA_UNIT"] : "") + (parameters[6] > 0 ? " &#149; " + parameters[6] + " " + LABEL["COMMON_BEDROOMS_UNIT_BE"] : "")));
					link.adopt(new Element("br"));
					link.adopt(new Element("span", { "class" : "lieu" }).setHTML(parameters[2]));
					link.adopt(new Element("br"));
					link.adopt(new Element("span", { "class" : "prix" }).setHTML(LABEL["COMMON_UNIVERSE_" + parameters[8]] + " - " + price));
					

					link.injectInside(body);

					// delete button
					bttn = new Element("td", { "class" : "supp" }).adopt(new Element("a", { "href" : "javascript:mySelection.confirm(\"" + parameters[0] + "\", \"del\")" }).adopt(new Element("img", { "src" : _static_img + "/images/pictos/close.gif", "height" : 9, "width" : 9 }))).injectInside(line);

					this.count = this.count + 1;

					this.setLabel();

					if (save == true)
					{
						this.save(parameters);	
					}

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

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

						myMsgBox.show(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)
			{
				$("selectedAd" + id).remove();

				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.remove() });
				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.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
mySelection = "";

// loader
window.addEvent("domready",
	function()
	{
		if ($("mySelectHeader"))
		{
			mySelection = new agSelection("mySelectHeader", "maselectioncontent", "mySelectContent");
		}
	}
);