var TIMEOUT = 5000;

var Tools = {
	tr: 0,
	suggests: 0,
	suggesthandle: -1,
	curpage: "",
	hintto: 0,
	deselect: function() {
		if (document.selection)
			document.selection.empty();
		else if (window.getSelection)
			window.getSelection().removeAllRanges();
	},
	alignColumns: function() {
		if (!$("bodyhelp") || !$("right-column") || !$("left-column"))
			return;
		var hg = Math.max($("bodyhelp").clientHeight, $("right-column").clientHeight, $("left-column").clientHeight);
		if (hg<650)
			hg = 650;
		$("body").style.height = hg+"px";
		$("left").style.height = ($("root").clientHeight-80)+"px";
		$("right").style.height = ($("root").clientHeight-80)+"px";
	},
	loading: function(el, width, height, black) {
		if (width) {
			el.style.width = width+"px";
		}
		el.style.height = height ? height+"px" : "100px";
		el.innerHTML = "";
		if (black)
			el.style.background = "url(/img/loading-black.gif) no-repeat center black";
		else
			el.style.background = "url(/img/loading.gif) no-repeat center";
	},
	stopLoading: function(el) {
		el.style.background = "";
		el.style.height = "";
		el.style.width = "";
	},
	showImage: function(url) {
		if (!$("tools-image")) {
			var a = document.createElement("a");
			a.id = "tools-image";
			a.href = "javascript:void(0)";
			a.onclick = function() {
				this.style.display = "none";
				return false;
			}
			$("root").appendChild(a);
		}
		Tools.loading($("tools-image"), 500, 500, true);
		showandcenter($("tools-image"));
		$("tools-image").style.backgroundImage = "url("+url+")";
	},
	dropDown: function(obj, align, corrected, closecallback) {
		obj.style.display = "block";
		obj.style.position = "absolute";
		obj.style.left = (align.offsetLeft+(corrected?0:10))+"px";
		obj.style.top = (align.offsetTop+18)+"px";
		obj.onmousedown = function(e) {
			if (window.event)
				window.event.cancelBubble = true;
			if (e && e.stopPropagation)
				e.stopPropagation();
		}
		document.onmousedown = function() {
			obj.style.display = "none";
			document.onmousedown = function() { }
			if (closecallback)
				closecallback();
		}
	},
	tabBind: function(tab, content) {
		tab.onmouseover = function(e) {
			var width = 538; //this.parentNode.parentNode.parentNode.clientWidth;
			this.parentNode.className = "active";
			e = e ? e : window.event;
			content.style.display = "block";
			var left = tab.parentNode.offsetLeft;
			var cleft = 100;
			if (cleft + left + content.clientWidth > width) {
				left = width - content.clientWidth - cleft;
			}
			content.style.left = left+"px";
			return false;
		}
		tab.onmouseout = function(e) {
			tab.timeout = setTimeout(function() {
				tab.parentNode.className = "";
				content.style.display = "none";
			}, 100);
		}
		content.onmouseover = function(e) {
			clearTimeout(tab.timeout);
		}
		content.onmouseout = function(e) {
			tab.onmouseout();
		}
	},
	pageBind: function(tab, content, dflt) {
		tab.onclick = function(e) {
			if (Tools.curpage)
				Tools.curpage.style.display = "none";
			Tools.curpage = content;
			content.style.display = "block";
		}
		if (dflt)
			this.curpage = content;
	},
	showHint: function(txt, event, element, reverse) {
		if (!hints)
			return;
		txt = txt.rplace(" ","&nbsp;");
		if (!$("hint")) {
			var div = document.createElement("div");
			div.id = "hint";
			var divhelp = document.createElement("div");
			divhelp.id = "hint-help";
			div.appendChild(divhelp);
			$("root").appendChild(div);
		}
		$("hint-help").innerHTML = txt;

//		element.onmouseover = Tooltip.on;
		element.onmouseout = Tooltip.off;
		element.onmousemove = Tooltip.move;
		Tooltip.on();
		
//		element.onmouseover(event);
	},
	hintShow: function() {
		if (this.hintto)
			clearTimeout(this.hintto);
	},
	hintHide: function() {
		this.hintto = setTimeout(function() { $("hint").style.display = "none"; }, 100);
	},
	suggestable: function(el, url, checkurl) {
		if (!el.id)
			el.id = "tools-suggest-"+(++this.suggests);
		el.showing = false;
		el.idx = -1;
		el.parentNode.style.position = "relative";

		if (el.parentNode.tagName.toLowerCase()!="div") {
			var cn = document.createElement("div");
			cn.style.position = "relative";
			el.parentNode.insertBefore(cn, el);
			el.parentNode.removeChild(el);
			cn.appendChild(el);
		}
		var iframe = document.createElement("iframe");
		iframe.id = "suggest-container-cover-"+el.id;
		iframe.src = "about:blank";
		iframe.frameBorder = "0";
		iframe.border = "0";
		iframe.setAttribute("frameborder", "0");
		iframe.setAttribute("noscroll", "1");
		iframe.className = "suggest-container-cover";
		iframe.style.display = "none";
		el.parentNode.appendChild(iframe);

		var div = document.createElement("div");
		div.id = "suggest-container-"+el.id;
		div.className = "suggest-container";
		div.style.display = "none";
		el.parentNode.appendChild(div);
		
		el.setvalue = function(str) {
			var parts = el.value.explode(",");
			parts[parts.length-1] = " "+str;
			this.value = parts.join(",").trim();
		}
		if (checkurl)
			el.onblur = function(e) {
				request.json(checkurl.rplace("%s", this.value), function(rs) {
					var legal = rs.legal.join(", ");
					el.value = legal;
	
					if (rs.illegal.length) {
						if (rs.illegal.length==1)
							$("suggest-container-"+el.id).innerHTML = "<ul><li>Word <b>"+rs.illegal.join(", ")+"</b> is rejected</li></ul>";
						else
							$("suggest-container-"+el.id).innerHTML = "<ul><li>Words <b>"+rs.illegal.join(", ")+"</b> are rejected</li></ul>";
						$("suggest-container-"+el.id).style.display = "block";
						$("suggest-container-"+el.id).style.left = "0px";
						$("suggest-container-"+el.id).style.top = "22px";

						$("suggest-container-cover-"+el.id).style.display = "block";
						$("suggest-container-cover-"+el.id).style.left = "-1px";
						$("suggest-container-cover-"+el.id).style.top = "21px";
						$("suggest-container-cover-"+el.id).style.width = ($("suggest-container-"+el.id).clientWidth+4)+"px";
						$("suggest-container-cover-"+el.id).style.height = ($("suggest-container-"+el.id).clientHeight+4)+"px";

						document.onmousedown = function() {
							$("suggest-container-"+el.id).style.display = "none";
							$("suggest-container-cover-"+el.id).style.display = "none";
							document.onmousedown = function() { }
						}
					}
				});
			}
		el.onkeydown = function(e) {
			if (!e)
				e = window.event;
			if (el.showing && $("suggest-container-"+el.id)) {
				var captured = false;
				var code = e.keyCode ? e.keyCode : e.charCode;
				if (code==40) {
					if (el.idx<$("suggest-container-"+el.id).childNodes[0].childNodes.length-1) {
						el.idx ++;
						if (el.idx>0)
							$("suggest-container-"+el.id).childNodes[0].childNodes[el.idx-1].className = "";
						$("suggest-container-"+el.id).childNodes[0].childNodes[el.idx].className = "selected";
					}
					captured = true;
				} else if (code==38) {
					if (el.idx>0) {
						el.idx --;
						if (el.idx<$("suggest-container-"+el.id).childNodes[0].childNodes.length-1)
							$("suggest-container-"+el.id).childNodes[0].childNodes[el.idx+1].className = "";
						$("suggest-container-"+el.id).childNodes[0].childNodes[el.idx].className = "selected";
					}
					captured = true;
				} else if (code==27) {
					$("suggest-container-"+el.id).style.display = "none";
					$("suggest-container-cover-"+el.id).style.display = "none";
					el.showing = false;
					captured = true;
				} else if (code==13) {
					if (el.idx>=0) {
						if ($("suggest-container-"+el.id).childNodes[0].childNodes[el.idx].childNodes[1])
							$("suggest-container-"+el.id).childNodes[0].childNodes[el.idx].childNodes[1].onclick();
						else
							$("suggest-container-"+el.id).childNodes[0].childNodes[el.idx].childNodes[0].onclick();
					}
					captured = true;
				}
				if (captured) {
					if (window.event)
						window.event.cancelBubble = true;
					else if (e.stopPropagation)
						e.stopPropagation();
					el.focus();
					return false;
				}
			}
			
			var oldvalue = el.value;
			setTimeout(function() {
				if (oldvalue==el.value)
					return;
				var part = el.value.explode(",");
				part = part[part.length-1];
				if (!part)
					return;
				part = part.trim();
				if (!part.length)
					return;
				if (Tools.suggesthandle>=0)
					request.release(Tools.suggesthandle);
				Tools.suggesthandle = request.json(url.rplace("%s", part), function(rs) {
					Tools.suggesthandle = -1;
					el.showing = false;
					dropAllElements($("suggest-container-"+el.id));
					if (!rs || !rs.length) {
						$("suggest-container-cover-"+el.id).style.display = "none";
						$("suggest-container-"+el.id).style.display = "none";
//						el.focus();
						return;
					}
					var html = "<ul>";
					for (var i=0; i<rs.length; i++)
						html += "<li>"+
								(parseInt(rs[i].cnt)>0 ? "<div class=\"stats\">x"+rs[i].cnt+"</div>" : "")+
								"<a href=\"javascript:void(0)\" onclick=\"$('"+el.id+"').setvalue(this.innerHTML);$('suggest-container-"+el.id+"').style.display='none';$('suggest-container-cover-"+el.id+"').style.display='none';$('"+el.id+"').focus();\">"+rs[i].word+"</a></li>";
					html += "</ul>";
					$("suggest-container-"+el.id).innerHTML = html;
					$("suggest-container-"+el.id).style.display = "block";
					$("suggest-container-"+el.id).style.left = "0px";
					$("suggest-container-"+el.id).style.top = "22px";
					$("suggest-container-"+el.id).style.zIndex = "300";
					$("suggest-container-cover-"+el.id).style.display = "block";
					$("suggest-container-cover-"+el.id).style.left = "-1px";
					$("suggest-container-cover-"+el.id).style.top = "21px";
					$("suggest-container-cover-"+el.id).style.width = ($("suggest-container-"+el.id).clientWidth+4)+"px";
					$("suggest-container-cover-"+el.id).style.height = ($("suggest-container-"+el.id).clientHeight+4)+"px";
					$("suggest-container-"+el.id).style.zIndex = "290";
					document.onmousedown = function() {
						$("suggest-container-cover-"+el.id).style.display = "none";
						$("suggest-container-"+el.id).style.display = "none";
						document.onmousedown = function() { }
					}
					$("suggest-container-"+el.id).onmousedown = function(e) {
						try { 
							if (window.event)
								window.event.cancelBubble = true;
							if (e && e.stopPropagation)
								e.stopPropagation();
						} catch (e) { }
					}
					el.showing = true;
					el.idx = -1;
//					el.focus();
				});
			}, 10);
		}
	},
	dropdownBind: function(obj, align) {
		obj.style.display = "block";
		obj.style.position = "absolute";
		obj.style.left = (align.offsetLeft+10)+"px";
		obj.style.top = (align.offsetTop)+"px";
		obj.onmousedown = function(e) {
			if (window.event)
				window.event.cancelBubble = true;
			if (e && e.stopPropagation)
				e.stopPropagation();
		}
		document.onmousedown = function() {
			obj.style.display = "none";
			document.onmousedown = function() { }
		}
	},
	checkAll: function(form, value) {
		for (var i=0; i<form.elements.length; i++)
			if (form.elements[i].type=="checkbox" && form.elements[i].name)
				form.elements[i].checked = value;
	},
	dialog: function(url, width, height) {
		if (!$("tools-dialog")) {
			var d = document.createElement("div");
			d.id = "tools-dialog";
			d.className = "dialog scrollable-dialog";
			$("root").appendChild(d);
		}
		request.html(url, function(h) {
			dropAllElements($("tools-dialog"));
			$("tools-dialog").innerHTML = h;
			if (width)
				$("tools-dialog").style.width = width+"px";
			if (height)
				$("tools-dialog").style.height = height+"px";
			showandcenter($("tools-dialog"));
		});
	},
	hideDialog: function() {
		if ($("tools-dialog")) {
			$("tools-dialog").style.display = "none";
			dropAllElements($("tools-dialog"));
		}
	},
	inline: function(url) {
		if (!window.finishedloading) {
			window.queueload.push(function() { Tools.inline(url); });
			return;
		}
		if (url.indexOf("?")>=0)
			url += "&inline=1";
		else
			url += "?inline=1";
		request.html(url, function(h) {
			if ($("inlinecontainer")) {
				dropAllElements($("inlinecontainer"));
				$("inlinecontainer").innerHTML = h;
			} else {
				for (var i=0; i<$("bodyhelp").childNodes.length; i++) {
					if ($("bodyhelp").childNodes[i].id!='facilities') {
						$("bodyhelp").removeChild($("bodyhelp").childNodes[i]);
						i --;
					}
				}
				var ic = document.createElement("div");
				ic.id = "inlinecontainer";
				ic.innerHTML = h;
				$("bodyhelp").appendChild(ic);
			}
			Tools.alignColumns();
		});
	},
	slidein: function(el, spd, callback) {
		var div = document.createElement("div");
		var hg = el.clientHeight;
		div.style.height = hg+"px";
		div.style.overflow = "hidden";
		div.style.clear = "both";
		
		el.parentNode.insertBefore(div, el);
		el.parentNode.removeChild(el);
		div.appendChild(el);
		
		var speed = typeof spd != 'undefined' ? spd : 3;
		var step = 4;
		
		var fheight = function() {
			if (hg>=0) {
				hg -= step;
				div.style.height = hg+"px";
				setTimeout(fheight, speed);
			} else {
				dropElement(div);
				if (callback)
					callback();
			}
		}
		
		fheight();
	}
};

var Marquee = {
	body: null,
	text: null,
	to: null,
	width: 0,
	position: 0,
	started: false,
	start: function(obj) {
		if (this.started)
			return;
		this.started = true;
		this.body = obj;
		var content = this.body.innerHTML;
		dropAllElements(this.body);
		this.text = document.createElement("span");
		this.text.innerHTML = content;
		
		this.text.style.position = "absolute";
		this.text.style.top = "5px";
		this.width = 596; //this.body.clientWidth;
		this.position = this.width;
		this.text.style.left = this.position+"px";
		this.body.appendChild(this.text);

		this.body.onmouseover = function() { Marquee.suspend(); }
		this.body.onmouseout = function() { Marquee.resume(); }
		
		setInterval(function() { Marquee.step(); }, 20);
	},
	resume: function() {
		this.to = setInterval(function() { Marquee.step(); }, 20);
	},
	suspend: function() {
		clearTimeout(this.to);
	},
	step: function() {
		this.position --;
		this.text.style.left = this.position+"px";
		if (this.position<=-1*this.text.clientWidth) {
			this.position = this.width;
		}
	}
};

var Items = {
	reason: 0,
	edit: function(id) {
		dropAllElements($("vcard-actions-"+id));
		$("vcard-actions-"+id).style.display = "block";
		Tools.loading($("vcard-actions-"+id));
		Tools.alignColumns();
		request.html(prefix+"items/pedit/"+id+"/", function(h) {
			Tools.stopLoading($("vcard-actions-"+id));
			dropAllElements($("vcard-actions-"+id));
			$("vcard-actions-"+id).innerHTML = h;
			Tools.alignColumns();
		});
	},
	cancelEdit: function(id) {
		dropAllElements($("vcard-actions-"+id));
		$("vcard-actions-"+id).style.display = "none";
		Tools.alignColumns();
	},
	premium: function(id) {
		dropAllElements($("vcard-actions-"+id));
		$("vcard-actions-"+id).style.display = "block";
		Tools.loading($("vcard-actions-"+id));
		Tools.alignColumns();
		request.html(prefix+"items/premium/"+id+"/", function(h) {
			Tools.stopLoading($("vcard-actions-"+id));
			dropAllElements($("vcard-actions-"+id));
			$("vcard-actions-"+id).innerHTML = h;
			Tools.alignColumns();
		});
	},
	cancelPremium: function(id) {
		dropAllElements($("vcard-actions-"+id));
		$("vcard-actions-"+id).style.display = "none";
		Tools.alignColumns();
	},
	drop: function(id, confirm, reason) {
		if (reason)
			this.reason = reason;
		if (confirm) {
			Tools.loading($("vcard-help-"+id), x, y);
			request.post(prefix+"items/delete/", { id: id, confirmed: 1, reason_id: this.reason }, function(rs) {
//				dropElement($("vcard-item-"+id));
				Tools.slidein($("vcard-item-"+id), 3, function() { Tools.alignColumns(); });
				this.reason = 0;
				Profile.updateIndicators(function(data) {
					if (data.items<1 && $("no-items"))
						$("no-items").style.display = "block";
					else if (data.items_own<1 && $("no-items-own"))
						$("no-items-own").style.display = "block";
					else if (data.items_favorites<1 && $("no-items-favorites"))
						$("no-items-favorites").style.display = "block";
					return true;
				});
			});
		} else {
			var x = $("vcard-info-"+id).clientWidth;
			var y = $("vcard-info-"+id).clientHeight;
			$("vcard-info-"+id).style.display = "none";
			$("vcard-controls-"+id).style.display = "none";
			$("vcard-help-"+id).style.display = "block";
			Tools.loading($("vcard-help-"+id), x, y);
			request.html(prefix+"items/delete/"+id+"/", function(h) {
				Tools.stopLoading($("vcard-help-"+id));
				$("vcard-help-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	cancelDelete: function(id) {
		$("vcard-help-"+id).style.display = "none";
		$("vcard-info-"+id).style.display = "block";
		$("vcard-controls-"+id).style.display = "block";
		dropAllElements($("vcard-help-"+id));
	},
	favorite: function(id, confirm, pblc) {
		if (confirm) {
			Tools.loading($("item-help-"+id), x, y);
			request.posthtml(prefix+"item/favorite/", { id: id, confirmed: 1, "public": pblc?1:0 }, function(h) {
				Tools.stopLoading($("item-help-"+id));
				$("item-help-"+id).innerHTML = h;
				request.html(prefix+"item/icons/"+id+"/", function(h) {
					$("icons-"+id).innerHTML = h;
					request.json(prefix+"item/indicators/"+id+"/", function(rs) {
						if ($("item-count-followers"))
							$("item-count-followers").innerHTML = rs.followers;
						if ($("item-count-groups"))
							$("item-count-groups").innerHTML = rs.groups;
						if ($("item-count-comments"))
							$("item-count-comments").innerHTML = rs.comments;
					});
				});
				setTimeout(function() {
					$("item-info-"+id).style.display = "block";
					dropAllElements($("item-help-"+id));
					$("item-help-"+id).style.display = "none";
					$("favorite-button-"+id).className = "faint";
				}, TIMEOUT);
				Profile.updateIndicators();
			});
		} else {
			var x = $("item-info-"+id).clientWidth;
			var y = $("item-info-"+id).clientHeight;
			$("item-info-"+id).style.display = "none";
			$("item-help-"+id).style.display = "block";
			Tools.loading($("item-help-"+id), x, y);
			request.html(prefix+"item/favorite/"+id+"/", function(h) {
				Tools.stopLoading($("item-help-"+id));
				$("item-help-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	cancelFavorite: function(id) {
		$("item-help-"+id).style.display = "none";
		$("item-info-"+id).style.display = "block";
		dropAllElements($("item-help-"+id));
	},
	sendToFriend: function(id,friendid,confirmed) {
		if (friendid && confirmed) {
			Tools.loading($("vcard-help-"+id), x, y);
			request.posthtml(prefix+"items/sendtofriend/", { id: id, friendid: friendid, confirmed: 1 }, function(h) {
				dropAllElements($("shortvcard-help-"+friendid));
				$("shortvcard-help-"+friendid).innerHTML = h;
//				setTimeout(function() {
//					Items.cancelSendToFriend(id);
//				}, TIMEOUT);
			});
		} else if (friendid) {
			if ($("shortuser-link-image-"+friendid))
				$("shortuser-link-image-"+friendid).src = "/img/icon-add-active.gif";
			var x = $("shortvcard-info-"+friendid).clientWidth;
			var y = $("shortvcard-info-"+friendid).clientHeight;
			$("shortvcard-info-"+friendid).style.display = "none";
			$("shortvcard-help-"+friendid).style.display = "block";
			Tools.loading($("shortvcard-help-"+friendid), x, y);
			request.html(prefix+"items/sendtofriend/"+id+"/"+friendid+"/", function(h) {
				Tools.stopLoading($("shortvcard-help-"+friendid));
				dropAllElements($("shortvcard-help-"+friendid));
				$("shortvcard-help-"+friendid).innerHTML = h;
				Tools.alignColumns();
			});
		} else {
			if ($("vcard-actions-"+id).style.display=="block")
				this.cancelSendToFriend(id);
			else {
				if ($("sending-to-friend")) {
					$("sending-to-friend").parentNode.style.display = "none";
					dropAllElements($("sending-to-friend").parentNode);
				}
				request.html(prefix+"items/sendtofriend/"+id+"/", function(h) {
					dropAllElements($("vcard-actions-"+id));
					$("vcard-actions-"+id).innerHTML = h;
					$("vcard-actions-"+id).style.display = "block";
					Tools.alignColumns();
				});
			}
		}
	},
	cancelSendToFriend: function(id,friendid) {
		if (friendid) {
			if ($("shortuser-link-image-"+friendid))
				$("shortuser-link-image-"+friendid).src = "/img/icon-add.gif";
			$("shortvcard-help-"+friendid).style.display = "none";
			$("shortvcard-info-"+friendid).style.display = "block";
			dropAllElements($("shortvcard-help-"+friendid));
		} else {
			dropAllElements($("vcard-actions-"+id));
			$("vcard-actions-"+id).style.display = "none";
		}
	},
	sendToGroup: function(id,groupid,confirmed) {
		if (groupid && confirmed) {
			Tools.loading($("vcard-help-"+id), x, y);
			request.posthtml(prefix+"items/sendtogroup/", { id: id, groupid: groupid, confirmed: 1 }, function(h) {
				Tools.stopLoading($("vcard-help-"+id), x, y);
				dropAllElements($("shortvcard-help-"+groupid));
				$("shortvcard-help-"+groupid).innerHTML = h;
//				setTimeout(function() {
//					Items.cancelSendToGroup(id);
//				}, TIMEOUT);
			});
		} else if (groupid) {
			if ($("shortgroup-link-image-"+groupid))
				$("shortgroup-link-image-"+groupid).src = "/img/icon-add-active.gif";
			var x = $("shortvcard-info-"+groupid).clientWidth;
			var y = $("shortvcard-info-"+groupid).clientHeight;
			$("shortvcard-info-"+groupid).style.display = "none";
			$("shortvcard-help-"+groupid).style.display = "block";
			Tools.loading($("shortvcard-help-"+groupid), x, y);
			request.html(prefix+"items/sendtogroup/"+id+"/"+groupid+"/", function(h) {
				Tools.stopLoading($("shortvcard-help-"+groupid));
				dropAllElements($("shortvcard-help-"+groupid));
				$("shortvcard-help-"+groupid).innerHTML = h;
				Tools.alignColumns();
			});
		} else {
			if ($("vcard-actions-"+id).style.display=="block")
				this.cancelSendToGroup(id);
			else {
				if ($("sending-to-group")) {
					$("sending-to-group").parentNode.style.display = "none";
					dropAllElements($("sending-to-group").parentNode);
				}
				request.html(prefix+"items/sendtogroup/"+id+"/", function(h) {
					dropAllElements($("vcard-actions-"+id));
					$("vcard-actions-"+id).innerHTML = h;
					$("vcard-actions-"+id).style.display = "block";
					Tools.alignColumns();
				});
			}
		}
	},
	cancelSendToGroup: function(id,groupid) {
		if (groupid) {
			if ($("shortgroup-link-image-"+groupid))
				$("shortgroup-link-image-"+groupid).src = "/img/icon-add.gif";
			$("shortvcard-help-"+groupid).style.display = "none";
			$("shortvcard-info-"+groupid).style.display = "block";
			dropAllElements($("shortvcard-help-"+groupid));
		} else {
			dropAllElements($("vcard-actions-"+id));
			$("vcard-actions-"+id).style.display = "none";
		}
	},
	proposeGroup: function(id,groupid,confirmed) {
		if (groupid && confirmed) {
			Tools.loading($("shortvcard-help-"+groupid), $("shortvcard-help-"+groupid).clientWidth, $("shortvcard-help-"+groupid).clientHeight);
			request.posthtml(prefix+"items/proposegroup/", { id: id, groupid: groupid, confirmed: 1 }, function(h) {
				Tools.stopLoading($("shortvcard-help-"+groupid));
				dropAllElements($("shortvcard-help-"+groupid));
				$("shortvcard-help-"+groupid).innerHTML = h;
//				setTimeout(function() {
//					Items.cancelSendToFriend(id);
//				}, TIMEOUT);
			});
		} else if (groupid) {
			if ($("shortgroup-link-image-"+groupid))
				$("shortgroup-link-image-"+groupid).src = "/img/icon-add-active.gif";
			$("shortvcard-info-"+groupid).style.display = "none";
			$("shortvcard-help-"+groupid).style.display = "block";
			Tools.loading($("shortvcard-help-"+groupid));
			request.html(prefix+"items/proposegroup/"+id+"/"+groupid+"/", function(h) {
				Tools.stopLoading($("shortvcard-help-"+groupid));
				dropAllElements($("shortvcard-help-"+groupid));
				$("shortvcard-help-"+groupid).innerHTML = h;
				Tools.alignColumns();
			});
		} else {
			if ($("vcard-actions-x-"+id).style.display=="block")
				this.cancelProposeGroup(id);
			else
				request.html(prefix+"items/proposegroup/"+id+"/", function(h) {
					dropAllElements($("vcard-actions-x-"+id));
					$("vcard-actions-x-"+id).innerHTML = h;
					$("vcard-actions-x-"+id).style.display = "block";
					Tools.alignColumns();
				});
		}
	},
	cancelProposeGroup: function(id,groupid) {
		if (groupid) {
			if ($("shortgroup-link-image-"+groupid))
				$("shortgroup-link-image-"+groupid).src = "/img/icon-add.gif";
			$("shortvcard-help-"+groupid).style.display = "none";
			$("shortvcard-info-"+groupid).style.display = "block";
			dropAllElements($("shortvcard-help-"+groupid));
		} else {
			dropAllElements($("vcard-actions-x-"+id));
			$("vcard-actions-x-"+id).style.display = "none";
		}
	},
	preview: function() {
		if ($("preview").childNodes.length==0) {
			request.html(prefix+"items/preview/", function(h) {
				dropAllElements($("preview"));
				$("preview").innerHTML = h;
				Tools.alignColumns();
			});
		} else
			dropAllElements($("preview"));
	},
	findKeywordChildren: function(id) {
		var old = $("keywords-dropdown-help").childNodes[0];
		if (old) {
			$("keywords-dropdown-help").removeChild(old);
			$("container-cat-2").appendChild(old);
		}
		
		$("kids-"+id).parentNode.removeChild($("kids-"+id));
		$("keywords-dropdown-help").appendChild($("kids-"+id));
	},
	report: function(id, el, reason) {
		if (!reason) {
			if ($("vcard-actions-"+id).innerHTML) {
				dropAllElements($("vcard-actions-"+id));
				$("vcard-actions-"+id).style.display = "none";
			} else {
				Reason.callback('user-report-illegal', function(rid) {
					Items.report(id, $('report-illegal-'+id), rid);
				}, $('vcard-actions-'+id));
			}
		} else {
			request.html(prefix+"item/report/"+id+"/?reason_id="+reason, function(h) {
				$("vcard-actions-"+id).innerHTML = h;
				$("vcard-actions-"+id).style.display = "block";
			});
		}
	},
	cancelReport: function(id) {
		dropAllElements($("vcard-actions-"+id));
		$("vcard-actions-"+id).style.display = "none";
	},
	requestInfo: function(id, el, reason) {
		if (!reason)
			reason = "";
		request.json(prefix+"item/requestinfo/"+id+"/?reason_id="+reason, function(rs) {
			el.parentNode.innerHTML = rs.result;
		});
	},
	additionalInfo: function(queueid, itemid, content) {
		if (content) {
			request.post(prefix+"items/additional-info/"+itemid+"/"+queueid+"/", { confirmed: 1, content: content }, function(rs) {
				Items.cancelAdditionalInfo();
				Queue.remove(queueid);
				Profile.updateIndicators();
			});
		} else {
			if (!$("additional-info")) {
				var d = document.createElement("div");
				d.id = "additional-info";
				d.className = "dialog";
				$("root").appendChild(d);
			}
			request.html(prefix+"items/additional-info/"+itemid+"/"+queueid+"/", function(h) {
				dropAllElements($("additional-info"));
				$("additional-info").innerHTML = h;
				showandcenter($("additional-info"));
			});
		}
	},
	cancelAdditionalInfo: function() {
		$("additional-info").style.display = "none";
		dropAllElements($("additional-info"));
	},
	addMoreKeywords: function(el) {
		var d = document.createElement("div");
		d.className = "fancy-input keywords-input";
		var inp = document.createElement("input");
		inp.type = "text";
		inp.name = "keywords[]";
		d.appendChild(inp);
		$("container-keywords").insertBefore(d, $("add-more-keywords"));
		Tools.suggestable(inp, prefix+"keywords/lookup/?pattern=%s");
	},
	adminPreview: function(form) {
		request.posthtml(prefix+"admin/items/preview/", dumpform(form), function(h) {
			dropAllElements($("preview-container"));
			$("preview-container").innerHTML = h;
		});
	},
	addComment: function(id, message, mode) {
		request.posthtml(prefix+"item/postcomment/", { item_id: id, message: message, mode: mode }, function(h) {
			request.json(prefix+"item/indicators/"+id+"/", function(rs) {
				if ($("item-count-followers"))
					$("item-count-followers").innerHTML = rs.followers;
				if ($("item-count-groups"))
					$("item-count-groups").innerHTML = rs.groups;
				if ($("item-count-comments"))
					$("item-count-comments").innerHTML = rs.comments;
			});
			
			if ($("no-comments"))
				dropElement($("no-comments"));
			
			var d = document.createElement("div");
			d.innerHTML = h;
			$("item-comments").insertBefore(d, $("item-comments").childNodes[0]?$("item-comments").childNodes[0]:null);
			Tools.alignColumns();
		});
	},
	deleteComment: function(id, itemid) {
		request.post(prefix+"item/deletecomment/", { id: id }, function(rs) {
			request.json(prefix+"item/indicators/"+rs.result+"/", function(rs) {
				if ($("item-count-followers"))
					$("item-count-followers").innerHTML = rs.followers;
				if ($("item-count-groups"))
					$("item-count-groups").innerHTML = rs.groups;
				if ($("item-count-comments"))
					$("item-count-comments").innerHTML = rs.comments;
			});
			if (rs.result)
				dropElement($("comment-"+id));
		});
	},
	restoreComment: function(id, itemid) {
		request.post(prefix+"item/restorecomment/", { id: id }, function(rs) {
			request.json(prefix+"item/indicators/"+rs.result+"/", function(rs) {
				if ($("item-count-followers"))
					$("item-count-followers").innerHTML = rs.followers;
				if ($("item-count-groups"))
					$("item-count-groups").innerHTML = rs.groups;
				if ($("item-count-comments"))
					$("item-count-comments").innerHTML = rs.comments;
			});
			if (rs.result) {
				dropElement($("comment-deleted-"+id));
				dropElement($("comment-restore-"+id));
			}
		});
	},
	commentSwitchMode: function(value) {
		var oldvalue = $("item-comment-mode").value;
		if (oldvalue!=value) {
			$("item-switch-mode-"+oldvalue).src = "/img/icon-add.gif";
			$("item-switch-mode-"+value).src = "/img/icon-add-active.gif";
		}
		$("item-comment-mode").value = value;
	},
	loadSubKeywords: function(id, kid) {
		request.html(prefix+"items/subkeywords/?id="+id+"&kid="+kid, function(h) {
			if (h.trim()) {
				$("subcat-tr-1").style.display = "";
				$("subcat-tr-2").style.display = "";
				dropAllElements($("subcat-td"));
				$("subcat-td").innerHTML = h;
			} else {
				$("subcat-tr-1").style.display = "none";
				$("subcat-tr-2").style.display = "none";
				dropAllElements($("subcat-td"));
			}
			Tools.alignColumns();
			setTimeout(function() { Tools.alignColumns(); }, 300);
		});
	},
	more: function() {
		$("shorter-descr").style.display = "none";
		$("normal-descr").style.display = "block";
	},
	acceptUPC: function(title, descr, image) {
		document.forms.step1.name.value = $('upc-title').innerHTML;
		document.forms.step1.descr.value = $('upc-description').innerHTML;
		document.forms.step1.defcat.value = 1;
		$("upload").style.display = "none";
		$("image-upload-link-more").style.display = "none";
		
		var url = $('upc-image').getAttribute("largesrc");
		if (!url)
			url = $("upc-image").src;

		for (var i=0; i<$("saved-images").childNodes.length; i++) {
			if (!$("saved-images").childNodes[i].id)
				continue;
			var cid = $("saved-images").childNodes[i].id;
			if (cid.substr(0,9)=="oldimage-") {
				var xid = cid.substr(9);
				dropElement($('oldimage-input-'+xid));
				dropElement($('oldimage-'+xid));
				dropElement($('iu-crop_x_image[]'));
				dropElement($('iu-crop_y_image[]'));
				dropElement($('iu-image[]'));
			}
		}
		$("saved-images-hint").style.display = "none";

		Tools.loading($("upload-loading"));
		var id = ImageUpload.maxid;
		request.json("/imageupload/download/?url="+escape(url)+"&type=item&name=image[]&form=step1&id="+id, function(rs) {
			Tools.stopLoading($("upload-loading"));
			$("upload").style.display = "block";
			$("image-upload-link-more").style.display = "block";
			if (rs.result==1) {
				ImageUpload.edit(id, rs.bgimage, rs.image, rs.prewidth, rs.preheight, rs.width, rs.height);
			}
		});
	},
	showPremiumInfo: function(id) {
		if ($("premium-hint-"+id)) {
			this.hidePremiumInfo(id);
			return;
		}

		var div = document.createElement("div");
		div.className = "vcard-premium-hint";
		div.id = "premium-hint-"+id;
		div.innerHTML = "<div>"+$("premium-link-"+id).getAttribute("rel")+"</div>";
		div.style.height = ($("vcard-item-"+id).clientHeight-13)+"px";

		var width = $("vcard-info-"+id).clientWidth+$("premium-link-"+id).clientWidth;
		var rest = 0;
		var speed = 3;
		if ((width%2)>0)
			width ++;

		$("vcard-image-div-"+id).appendChild(div);
		var func = function() {
			if (width>0) {
				width -= 7;
				rest += 7;
				$("premium-hint-"+id).style.width = rest+"px";
				$("premium-link-"+id).style.right = "-"+rest +"px";
				setTimeout(func, speed);
			}
		}
		func();
	},
	hidePremiumInfo: function(id) {
		var width = $("premium-hint-"+id).clientWidth;
		var speed = 3;
		var func = function() {
			if (width>0) {
				width -= 7;
				$("premium-hint-"+id).style.width = width+"px";
				$("premium-link-"+id).style.right = "-"+width+"px";
				setTimeout(func, speed);
			} else {
				dropElement($("premium-hint-"+id));
				$("premium-link-"+id).style.right = "0px";
			}
		}
		func();
	}
};

var Users = {
	friendPropose: function(id, confirm) {
		if (confirm) {
			Tools.loading($("user-help-"+id), x, y);
			request.posthtml(prefix+"user/friendpropose/", { id: id, confirmed: 1 }, function(h) {
				Tools.stopLoading($("user-help-"+id));
				dropAllElements($("user-help-"+id));
				$("user-help-"+id).innerHTML = h;
//				addClassName($("propose-button-"+id), "faint");
				if ($("propose-button-image-"+id)) {
					addClassName($("propose-button-image-"+id), "faint");
					$("propose-button-"+id).removeAttribute("href");
					$("propose-button-"+id).onclick = function() { }
				}
				request.html(prefix+"user/icons/"+id+"/", function(h) {
					$("icons-"+id).innerHTML = h;
				});
				setTimeout(function() {
					if ($("user-info-"+id))
						$("user-info-"+id).style.display = "block";
					if ($("user-indicators-"+id))
						$("user-indicators-"+id).style.display = "block";
					dropAllElements($("user-help-"+id));
					$("user-help-"+id).style.display = "none";
				}, TIMEOUT);
				Profile.updateIndicators();
			});
		} else {
			var x = 0, y = 0;
			if ($("user-info-"+id)) {
				var x = $("user-info-"+id).clientWidth;
				var y = $("user-info-"+id).clientHeight;
				$("user-info-"+id).style.display = "none";
			}
			if ($("user-indicators-"+id)) {
				$("user-indicators-"+id).style.display = "none";
			}
			$("user-help-"+id).style.display = "block";
			Tools.loading($("user-help-"+id), x, y);
			request.html(prefix+"user/friendpropose/"+id+"/", function(h) {
				Tools.stopLoading($("user-help-"+id));
				$("user-help-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	cancelFriendPropose: function(id) {
		$("user-help-"+id).style.display = "none";
		$("user-info-"+id).style.display = "block";
		$("user-indicators-"+id).style.display = "block";
		dropAllElements($("user-help-"+id));
	},
	adminPreview: function(form) {
		request.posthtml(prefix+"admin/users/preview/", dumpform(form), function(h) {
			dropAllElements($("preview-container"));
			$("preview-container").innerHTML = h;
		});
	},
	showSummaryDetails: function(id,userid,altdesign) {
		var el = $("details-"+id) ? $("details-"+id) : $("exchange-container-"+id);
		if (el.style.display=="none") {
			addClassName($("nu-comments-"+id), "comments-clicked");
			request.html(prefix+"user/exchangedetails/"+id+"/"+userid+"/"+(altdesign?"1/":""), function(h) {
				dropAllElements(el);
				el.innerHTML = h;
				el.style.display = "";
				Tools.alignColumns();
			});
		} else {
			removeClassName($("nu-comments-"+id), "comments-clicked");
			el.style.display = "none";
		}
	},
	more: function() {
		$("shorter-keywords").style.display = "none";
		$("normal-keywords").style.display = "block";
	},
	addZip: function(country, zip, city) {
		if (country && zip && city) {
			request.post(prefix+"admin/users/addzip", { country_id: country, zip: zip, city: city }, function() {
				Tools.hideDialog();
			});
		} else
			Tools.dialog(prefix+"admin/users/addzip", 270, 130);
	},
	checkZip: function(country, zip) {
		request.json(prefix+"admin/users/checkzip?country_id="+country+"&zip="+zip, function(rs) {
			if (rs.length)
				alert("Cities found: "+rs.join(", "));
			else
				alert("No entries found");
		});
	},
	cancelAddZip: function() {
		Tools.hideDialog();
	}
};

var Groups = {
	reason: 0,
	drop: function(id, confirm, reason) {
		if (reason)
			this.reason = reason;
		if (confirm) {
			Tools.loading($("vcard-help-"+id), x, y);
			request.post(prefix+"groups/delete/", { id: id, confirmed: 1, reason_id: this.reason }, function(rs) {
//				dropElement($("vcard-group-"+id));
				Tools.slidein($("vcard-group-"+id), 3, function() { Tools.alignColumns(); });
				this.reason = 0;
				Profile.updateIndicators(function(data) {
					if (data.groups<1 && $("no-groups"))
						$("no-groups").style.display = "block";
					else if (data.groups_own<1 && $("no-groups-admin"))
						$("no-groups-admin").style.display = "block";
					else if (data.groups_memberships<1 && $("no-groups-member"))
						$("no-groups-member").style.display = "block";
					return true;
				});
			});
		} else {
			var x = $("vcard-info-"+id).clientWidth;
			var y = $("vcard-info-"+id).clientHeight;
			$("vcard-info-"+id).style.display = "none";
			$("vcard-controls-"+id).style.display = "none";
			$("vcard-help-"+id).style.display = "block";
			Tools.loading($("vcard-help-"+id), x, y);
			request.html(prefix+"groups/delete/"+id+"/", function(h) {
				Tools.stopLoading($("vcard-help-"+id));
				$("vcard-help-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	cancelDelete: function(id) {
		$("vcard-help-"+id).style.display = "none";
		$("vcard-info-"+id).style.display = "block";
		$("vcard-controls-"+id).style.display = "block";
		dropAllElements($("vcard-help-"+id));
	},
	inviteFriend: function(id,friendid,confirmed) {
		if (friendid && confirmed) {
			Tools.loading($("vcard-help-"+id), x, y);
			request.posthtml(prefix+"groups/invitefriend/", { id: id, friendid: friendid, confirmed: 1 }, function(h) {
				Tools.stopLoading($("vcard-help-"+id));
				dropAllElements($("shortvcard-help-"+friendid));
				$("shortvcard-help-"+friendid).innerHTML = h;
				addClassName($("shortvcard-button-"+friendid).parentNode.parentNode, "faint");
//				setTimeout(function() {
//					Groups.cancelInviteFriend(id);
//				}, TIMEOUT);
			});
		} else if (friendid) {
			if ($("shortuser-link-image-"+friendid))
				$("shortuser-link-image-"+friendid).src = "/img/icon-add-active.gif";
			var x = $("shortvcard-info-"+friendid).clientWidth;
			var y = $("shortvcard-info-"+friendid).clientHeight;
			$("shortvcard-info-"+friendid).style.display = "none";
			$("shortvcard-help-"+friendid).style.display = "block";
			Tools.loading($("shortvcard-help-"+friendid), x, y);
			request.html(prefix+"groups/invitefriend/"+id+"/"+friendid+"/", function(h) {
				Tools.stopLoading($("shortvcard-help-"+friendid));
				dropAllElements($("shortvcard-help-"+friendid));
				$("shortvcard-help-"+friendid).innerHTML = h;
				Tools.alignColumns();
			});
		} else {
			if ($("vcard-actions-"+id).style.display=="block")
				this.cancelInviteFriend(id);
			else
				request.html(prefix+"groups/invitefriend/"+id+"/", function(h) {
					dropAllElements($("vcard-actions-"+id));
					$("vcard-actions-"+id).innerHTML = h;
					$("vcard-actions-"+id).style.display = "block";
					Tools.alignColumns();
				});
		}
	},
	cancelInviteFriend: function(id, friendid) {
		if (friendid) {
			if ($("shortuser-link-image-"+friendid))
				$("shortuser-link-image-"+friendid).src = "/img/icon-add.gif";
			$("shortvcard-help-"+friendid).style.display = "none";
			$("shortvcard-info-"+friendid).style.display = "block";
			dropAllElements($("shortvcard-help-"+friendid));
		} else {
			dropAllElements($("vcard-actions-"+id));
			$("vcard-actions-"+id).style.display = "none";
		}
		Tools.alignColumns();
	},
	addItem: function(id,itemid,confirmed) {
		if (itemid && confirmed) {
			Tools.loading($("shortvcard-help-"+itemid));
			request.posthtml(prefix+"groups/additem/", { id: id, itemid: itemid, confirmed: 1 }, function(h) {
				request.json(prefix+"group/indicators/"+id+"/", function(rs) {
					if ($("group-count-items"))
						$("group-count-items").innerHTML = rs.items;
					if ($("group-count-members"))
						$("group-count-members").innerHTML = rs.members;
					if ($("group-count-comments"))
						$("group-count-comments").innerHTML = rs.comments;
				});
				Tools.stopLoading($("shortvcard-help-"+itemid));
				dropAllElements($("shortvcard-help-"+itemid));
				$("shortvcard-help-"+itemid).innerHTML = h;
//				setTimeout(function() {
//					Groups.cancelAddItem(id);
//				}, TIMEOUT);
			});
		} else if (itemid) {
			if ($("shortitem-link-image-"+itemid))
				$("shortitem-link-image-"+itemid).src = "/img/icon-add-active.gif";
			var x = $("shortvcard-info-"+itemid).clientWidth;
			var y = $("shortvcard-info-"+itemid).clientHeight;
			$("shortvcard-info-"+itemid).style.display = "none";
			$("shortvcard-help-"+itemid).style.display = "block";
			Tools.loading($("shortvcard-help-"+itemid), x, y);
			request.html(prefix+"groups/additem/"+id+"/"+itemid+"/", function(h) {
				Tools.stopLoading($("shortvcard-help-"+itemid));
				dropAllElements($("shortvcard-help-"+itemid));
				$("shortvcard-help-"+itemid).innerHTML = h;
				Tools.alignColumns();
			});
		} else {
			if ($("vcard-actions-"+id).style.display=="block") {
				this.cancelAddItem(id);
			} else
				request.html(prefix+"groups/additem/"+id+"/", function(h) {
					dropAllElements($("vcard-actions-"+id));
					$("vcard-actions-"+id).innerHTML = h;
					$("vcard-actions-"+id).style.display = "block";
					Tools.alignColumns();
				});
		}
	},
	cancelAddItem: function(id, itemid) {
		if (itemid) {
			if ($("shortitem-link-image-"+itemid))
				$("shortitem-link-image-"+itemid).src = "/img/icon-add.gif";
			$("shortvcard-help-"+itemid).style.display = "none";
			$("shortvcard-info-"+itemid).style.display = "block";
			dropAllElements($("shortvcard-help-"+itemid));
		} else {
			dropAllElements($("vcard-actions-"+id));
			$("vcard-actions-"+id).style.display = "none";
		}
	},
	join: function(id, confirm) {
		if (confirm) {
			Tools.loading($("group-help-"+id), x, y);
			request.posthtml(prefix+"group/join/", { id: id, confirmed: 1 }, function(h) {
				Tools.stopLoading($("group-help-"+id));
				dropAllElements($("group-help-"+id));
				$("group-help-"+id).innerHTML = h;
				$("join-button-"+id).className = "faint";
				setTimeout(function() {
					request.html(prefix+"group/"+id+"/members/inline/", function(h) {
						dropAllElements($("bodyhelp"));
						$("bodyhelp").innerHTML = h;
//						request.json(prefix+"group/indicators/"+id+"/", function(rs) {
//							$("group-count-items").innerHTML = rs.items;
//							$("group-count-members").innerHTML = rs.members;
//						});
					});
				}, TIMEOUT);
//				setTimeout(function() {
//					$("group-info-"+id).style.display = "block";
//					dropAllElements($("group-help-"+id));
//				}, TIMEOUT);
				Profile.updateIndicators();
			});
		} else {
			var x = $("group-info-"+id).clientWidth;
			var y = $("group-info-"+id).clientHeight;
			$("group-info-"+id).style.display = "none";
			$("group-help-"+id).style.display = "block";
			Tools.loading($("group-help-"+id), x, y);
			request.html(prefix+"group/join/"+id+"/", function(h) {
				Tools.stopLoading($("group-help-"+id));
				$("group-help-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	cancelJoin: function(id) {
		$("group-help-"+id).style.display = "none";
		$("group-info-"+id).style.display = "block";
		dropAllElements($("group-help-"+id));
	},
	leave: function(id, confirm) {
		if (confirm) {
			Tools.loading($("vcard-help-"+id), x, y);
			request.posthtml(prefix+"groups/leave/", { id: id, confirmed: 1 }, function(h) {
				Tools.stopLoading($("vcard-help-"+id));
				$("vcard-help-"+id).innerHTML = h;
				setTimeout(function() {
//					dropElement($("vcard-group-"+id));
					Tools.slidein($("vcard-group-"+id), 3, function() { Tools.alignColumns(); });
					Profile.updateIndicators(function(data) {
						if (data.groups<1 && $("no-groups"))
							$("no-groups").style.display = "block";
						else if (data.groups_own<1 && $("no-groups-admin"))
							$("no-groups-admin").style.display = "block";
						else if (data.groups_memberships<1 && $("no-groups-member"))
							$("no-groups-member").style.display = "block";
						return true;
					});
				}, TIMEOUT);
			});
		} else {
			var x = $("vcard-info-"+id).clientWidth;
			var y = $("vcard-info-"+id).clientHeight;
			$("vcard-info-"+id).style.display = "none";
			$("vcard-controls-"+id).style.display = "none";
			$("vcard-help-"+id).style.display = "block";
			Tools.loading($("vcard-help-"+id), x, y);
			request.html(prefix+"groups/leave/"+id+"/", function(h) {
				Tools.stopLoading($("vcard-help-"+id));
				$("vcard-help-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	cancelLeave: function(id) {
		$("vcard-help-"+id).style.display = "none";
		$("vcard-info-"+id).style.display = "block";
		$("vcard-controls-"+id).style.display = "block";
		dropAllElements($("vcard-help-"+id));
	},
	removeItem: function(groupid, itemid, view, confirm) {
		var id = view=="item" ? itemid : groupid;
		if (confirm) {
			Tools.loading($("vcard-help-"+id), x, y);
			request.post(prefix+"groups/removeitem/", { groupid: groupid, itemid: itemid, view: view, confirmed: 1 }, function(rs) {
				if (view=="item") {
					request.json(prefix+"group/indicators/"+groupid+"/", function(rs) {
						if ($("group-count-items"))
							$("group-count-items").innerHTML = rs.items;
						if ($("group-count-members"))
							$("group-count-members").innerHTML = rs.members;
						if ($("group-count-comments"))
							$("group-count-comments").innerHTML = rs.comments;
					});
					Tools.slidein($("vcard-item-"+id), 3, function() { Tools.alignColumns(); });
//					dropElement($("vcard-item-"+id));
				} else {
					request.json(prefix+"item/indicators/"+id+"/", function(rs) {
						if ($("item-count-followers"))
							$("item-count-followers").innerHTML = rs.followers;
						if ($("item-count-groups"))
							$("item-count-groups").innerHTML = rs.groups;
						if ($("item-count-comments"))
							$("item-count-comments").innerHTML = rs.comments;
					});
//					dropElement($("vcard-group-"+id));
					Tools.slidein($("vcard-group-"+id), 3, function() { Tools.alignColumns(); });
				}
			});
		} else {
			var x = $("vcard-info-"+id).clientWidth;
			var y = $("vcard-info-"+id).clientHeight;
			$("vcard-info-"+id).style.display = "none";
			$("vcard-help-"+id).style.display = "block";
			Tools.loading($("vcard-help-"+id), x, y);
			request.html(prefix+"groups/removeitem/"+groupid+"/"+itemid+"/"+view+"/", function(h) {
				Tools.stopLoading($("vcard-help-"+id));
				$("vcard-help-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	cancelRemoveItem: function(groupid, itemid, view) {
		var id = view=="item" ? itemid : groupid;
		$("vcard-help-"+id).style.display = "none";
		$("vcard-info-"+id).style.display = "block";
		dropAllElements($("vcard-help-"+id));
	},
	removeUser: function(groupid, userid, confirm) {
		if (confirm) {
			Tools.loading($("vcard-help-"+userid), x, y);
			request.post(prefix+"groups/removeuser/", { groupid: groupid, userid: userid, confirmed: 1 }, function(rs) {
				request.json(prefix+"group/indicators/"+groupid+"/", function(rs) {
					if ($("group-count-items"))
						$("group-count-items").innerHTML = rs.items;
					if ($("group-count-members"))
						$("group-count-members").innerHTML = rs.members;
					if ($("group-count-comments"))
						$("group-count-comments").innerHTML = rs.comments;
				});
				Tools.slidein($("vcard-user-"+id), 3, function() { Tools.alignColumns(); });
//				dropElement($("vcard-user-"+userid));
			});
		} else {
			var x = $("vcard-info-"+userid).clientWidth;
			var y = $("vcard-info-"+userid).clientHeight;
			$("vcard-info-"+userid).style.display = "none";
			$("vcard-help-"+userid).style.display = "block";
			Tools.loading($("vcard-help-"+userid), x, y);
			request.html(prefix+"groups/removeuser/"+groupid+"/"+userid+"/", function(h) {
				Tools.stopLoading($("vcard-help-"+userid));
				$("vcard-help-"+userid).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	cancelRemoveUser: function(groupid, userid) {
		$("vcard-help-"+userid).style.display = "none";
		$("vcard-info-"+userid).style.display = "block";
		dropAllElements($("vcard-help-"+userid));
	},
	preview: function() {
		if ($("preview").childNodes.length==0) {
			request.html(prefix+"groups/preview/", function(h) {
				dropAllElements($("preview"));
				$("preview").innerHTML = h;
				Tools.alignColumns();
			});
		} else
			dropAllElements($("preview"));
	},
	findKeywordChildren: function(id) {
		var old = $("keywords-dropdown-help").childNodes[0];
		if (old) {
			$("keywords-dropdown-help").removeChild(old);
			$("container-cat-2").appendChild(old);
		}
		
		$("kids-"+id).parentNode.removeChild($("kids-"+id));
		$("keywords-dropdown-help").appendChild($("kids-"+id));
	},
	findKeywordChildren2: function(id) {
		var old = $("keywords-dropdown-help-2").childNodes[0];
		if (old) {
			$("keywords-dropdown-help-2").removeChild(old);
			$("container-cat-2").appendChild(old);
		}
		
		$("kids-"+id).parentNode.removeChild($("kids-"+id));
		$("keywords-dropdown-help-2").appendChild($("kids-"+id));
	},
	report: function(id, el, reason) {
		if (!reason) {
			if ($("vcard-actions-"+id).innerHTML) {
				dropAllElements($("vcard-actions-"+id));
				$("vcard-actions-"+id).style.display = "none";
			} else {
				Reason.callback('user-report-illegal', function(rid) {
					Groups.report(id, $('report-illegal-'+id), rid);
				}, $('vcard-actions-'+id));
			}
		} else {
			request.html(prefix+"group/report/"+id+"/?reason_id="+reason, function(h) {
				$("vcard-actions-"+id).innerHTML = h;
				$("vcard-actions-"+id).style.display = "block";
			});
		}
	},
	cancelReport: function(id) {
		dropAllElements($("vcard-actions-"+id));
		$("vcard-actions-"+id).style.display = "none";
	},
	transferLeave: function(id, memberid, confirmed) {
		this.transfer(id,true,memberid,confirmed);
	},
	transferStay: function(id, memberid, confirmed) {
		this.transfer(id,false,memberid,confirmed);
	},
	transfer: function(id,leave,memberid,confirmed) {
		if (memberid && confirmed) {
			Tools.loading($("shortvcard-help-"+memberid), x, y);
			request.posthtml(prefix+"groups/transfer/", { id: id, memberid: memberid, confirmed: 1, leave: leave?1:0 }, function(h) {
				Tools.stopLoading($("shortvcard-help-"+memberid));
				dropAllElements($("shortvcard-help-"+memberid));
				$("shortvcard-help-"+memberid).innerHTML = h;
				if ($("transfer-link-"+id))
					$("transfer-link-"+id).style.display = "none";
				setTimeout(function() {
					Groups.cancelTransfer(id, memberid);
					Groups.cancelTransfer(id);
					Groups.cancelDelete(id);
				}, TIMEOUT);
				Profile.updateIndicators();
			});
		} else if (memberid) {
			if ($("shortuser-link-image-"+memberid))
				$("shortuser-link-image-"+memberid).src = "/img/icon-add-active.gif";
			var x = $("shortvcard-info-"+memberid).clientWidth;
			var y = $("shortvcard-info-"+memberid).clientHeight;
			$("shortvcard-info-"+memberid).style.display = "none";
			$("shortvcard-help-"+memberid).style.display = "block";
			Tools.loading($("shortvcard-help-"+memberid), x, y);
			request.html(prefix+"groups/transfer/"+id+"/"+memberid+"/"+(leave?"1":"0")+"/", function(h) {
				Tools.stopLoading($("shortvcard-help-"+memberid));
				dropAllElements($("shortvcard-help-"+memberid));
				$("shortvcard-help-"+memberid).innerHTML = h;
				Tools.alignColumns();
			});
		} else {
			if ($("vcard-actions-"+id).style.display=="block")
				this.cancelTransfer(id);
			else
				request.html(prefix+"groups/transfer/"+id+"/0/"+(leave?"1":"0")+"/", function(h) {
					dropAllElements($("vcard-actions-"+id));
					$("vcard-actions-"+id).innerHTML = h;
					$("vcard-actions-"+id).style.display = "block";
					Tools.alignColumns();
				});
		}
	},
	cancelTransfer: function(id, memberid) {
		if (memberid) {
			if ($("shortuser-link-image-"+memberid))
				$("shortuser-link-image-"+memberid).src = "/img/icon-add.gif";
			$("shortvcard-help-"+memberid).style.display = "none";
			$("shortvcard-info-"+memberid).style.display = "block";
			dropAllElements($("shortvcard-help-"+memberid));
		} else {
			dropAllElements($("vcard-actions-"+id));
			$("vcard-actions-"+id).style.display = "none";
		}
	},
	adminPreview: function(form) {
		request.posthtml(prefix+"admin/groups/preview/", dumpform(form), function(h) {
			dropAllElements($("preview-container"));
			$("preview-container").innerHTML = h;
		});
	},
	more: function() {
		$("shorter-descr").style.display = "none";
		$("normal-descr").style.display = "block";
	}
};

var Queue = {
	processIndicators: function(stats) {
		if (parseInt(stats.requests_all)==0) {
			window.location = prefix+"journal/";
			return false;
		}
		return true;
	},
	drop: function(id, reason) {
		request.json(prefix+"queue/delete/"+id+"/"+(reason?"?reason_id="+reason:""), function(rs) {
			Queue.remove(id);
			Profile.updateIndicators(Queue.processIndicators);
		});
	},
	remove: function(id) {
//		dropElement($("queue-"+id));
		Tools.slidein($("queue-"+id), 3, function() { Tools.alignColumns(); });
	},
	acceptFriend: function(id, uid, autohide) {
		request.html(prefix+"queue/acceptfriend/"+id+"/", function(h) {
			dropAllElements($("queue-info-"+id));
			$("queue-info-"+id).innerHTML = h;
			if ($("queue-buttons-"+id))
				$("queue-buttons-"+id).style.display = "none";
			if (autohide)
				setTimeout(function() { dropElement($("queue-"+id)); }, TIMEOUT);
			request.json(prefix+"user/indicators/"+uid+"/", function(rs) {
				if ($("user-count-items"))
					$("user-count-items").innerHTML = rs.items;
				if ($("user-count-friends"))
					$("user-count-friends").innerHTML = rs.friends;
				if ($("user-count-exchanges"))
					$("user-count-exchanges").innerHTML = rs.exchanges;
				if ($("user-count-groups"))
					$("user-count-groups").innerHTML = rs.groups;
				if ($("user-count-comments"))
					$("user-count-comments").innerHTML = rs.comments;
				request.html(prefix+"user/icons/"+uid+"/", function(h) {
					if ($("icons-"+uid))
						$("icons-"+uid).innerHTML = h;
					Profile.updateIndicators(Queue.processIndicators);
				});
			});
		});
	},
	rejectFriend: function(id, uid) {
		this.drop(id);
		if (uid) {
			request.json(prefix+"user/indicators/"+uid+"/", function(rs) {
				if ($("user-count-items"))
					$("user-count-items").innerHTML = rs.items;
				if ($("user-count-friends"))
					$("user-count-friends").innerHTML = rs.friends;
				if ($("user-count-exchanges"))
					$("user-count-exchanges").innerHTML = rs.exchanges;
				if ($("user-count-groups"))
					$("user-count-groups").innerHTML = rs.groups;
				if ($("user-count-comments"))
					$("user-count-comments").innerHTML = rs.comments;
				if ($("icons-"+uid))
					request.html(prefix+"user/icons/"+uid+"/", function(h) {
						$("icons-"+uid).innerHTML = h;
					});
				Profile.updateIndicators(Queue.processIndicators);
			});
		}
	},
	acceptItem: function(id, autoclose) {
		request.html(prefix+"queue/acceptitem/"+id+"/", function(h) {
			dropAllElements($("queue-info-"+id));
			$("queue-info-"+id).innerHTML = h;
			if ($("queue-buttons-"+id))
				$("queue-buttons-"+id).style.display = "none";
			if (autoclose) {
				if ($("favorite-button-"+autoclose))
					$("favorite-button-"+autoclose).className = "faint";
				setTimeout(function() { dropElement($("queue-"+id)); }, TIMEOUT);
			}
			request.html(prefix+"item/icons/"+autoclose+"/", function(h) {
				if ($("icons-"+autoclose))
					$("icons-"+autoclose).innerHTML = h;
				request.json(prefix+"item/indicators/"+autoclose+"/", function(rs) {
					if ($("item-count-followers"))
						$("item-count-followers").innerHTML = rs.followers;
					if ($("item-count-groups"))
						$("item-count-groups").innerHTML = rs.groups;
					if ($("item-count-comments"))
						$("item-count-comments").innerHTML = rs.comments;
					Profile.updateIndicators(Queue.processIndicators);
				});
			});
		});
	},
	rejectItem: function(id) {
		this.drop(id);
	},
	acceptItemgroup: function(id, autoclose) {
		request.html(prefix+"queue/acceptitemgroup/"+id+"/", function(h) {
			dropAllElements($("queue-info-"+id));
			$("queue-info-"+id).innerHTML = h;
			if ($("queue-buttons-"+id))
				$("queue-buttons-"+id).style.display = "none";
			if (autoclose) {
				setTimeout(function() { dropElement($("queue-"+id)); }, TIMEOUT);
			}
			request.html(prefix+"item/icons/"+autoclose+"/", function(h) {
				if ($("icons-"+autoclose))
					$("icons-"+autoclose).innerHTML = h;
				request.json(prefix+"item/indicators/"+autoclose+"/", function(rs) {
					if ($("item-count-followers"))
						$("item-count-followers").innerHTML = rs.followers;
					if ($("item-count-groups"))
						$("item-count-groups").innerHTML = rs.groups;
					if ($("item-count-comments"))
						$("item-count-comments").innerHTML = rs.comments;
					Profile.updateIndicators(Queue.processIndicators);
				});
			});
		});
	},
	rejectItemgroup: function(id) {
		this.drop(id);
	},
	acceptGroup: function(id, autoclose) {
		request.html(prefix+"queue/acceptgroup/"+id+"/", function(h) {
			dropAllElements($("queue-info-"+id));
			$("queue-info-"+id).innerHTML = h;
			if ($("queue-buttons-"+id))
				$("queue-buttons-"+id).style.display = "none";
			request.html(prefix+"group/icons/"+autoclose+"/", function(h) {
				if ($("icons-"+autoclose))
					$("icons-"+autoclose).innerHTML = h;
				request.json(prefix+"group/indicators/"+autoclose+"/", function(rs) {
					if ($("group-count-items"))
						$("group-count-items").innerHTML = rs.items;
					if ($("group-count-members"))
						$("group-count-members").innerHTML = rs.members;
					if ($("group-count-comments"))
						$("group-count-comments").innerHTML = rs.comments;
					Profile.updateIndicators(Queue.processIndicators);
				});
			});
			if (autoclose) {
				if ($("join-button-"+autoclose))
					dropElement($("join-button-"+autoclose));
				setTimeout(function() { dropElement($("queue-"+id)); }, TIMEOUT);
			}
		});
	},
	rejectGroup: function(id) {
		this.drop(id);
	},
	acceptJoin: function(id, autohide) {
		request.html(prefix+"queue/acceptjoin/"+id+"/", function(h) {
			dropAllElements($("queue-info-"+id));
			$("queue-info-"+id).innerHTML = h;
			if ($("queue-buttons-"+id))
				$("queue-buttons-"+id).style.display = "none";
			if (autohide)
				 setTimeout(function() { dropElement($("queue-"+id)); }, TIMEOUT);
			request.json(prefix+"user/indicators/"+autohide+"/", function(rs) {
				if ($("user-count-items"))
					$("user-count-items").innerHTML = rs.items;
				if ($("user-count-friends"))
					$("user-count-friends").innerHTML = rs.friends;
				if ($("user-count-exchanges"))
					$("user-count-exchanges").innerHTML = rs.exchanges;
				if ($("user-count-groups"))
					$("user-count-groups").innerHTML = rs.groups;
				if ($("user-count-comments"))
					$("user-count-comments").innerHTML = rs.comments;
				request.html(prefix+"user/icons/"+autohide+"/", function(h) {
					if ($("icons-"+autohide))
						$("icons-"+autohide).innerHTML = h;
					Profile.updateIndicators(Queue.processIndicators);
				});
			});
		});
	},
	rejectJoin: function(id) {
		this.drop(id);
	},
	acceptAdd: function(id, autoclose) {
		request.html(prefix+"queue/acceptadd/"+id+"/", function(h) {
			dropAllElements($("queue-info-"+id));
			$("queue-info-"+id).innerHTML = h;
			if ($("queue-buttons-"+id))
				$("queue-buttons-"+id).style.display = "none";
			if (autoclose) {
				if ($("propose-button-"+autoclose))
					dropElement($("propose-button-"+autoclose));
				setTimeout(function() { dropElement($("queue-"+id)); }, TIMEOUT);
			}
			Profile.updateIndicators(Queue.processIndicators);
		});
	},
	rejectAdd: function(id, reason) {
		this.drop(id, reason);
	}
};

var Friends = {
	lastimporturl: "",
	proposals: [ ],
	invitations: [ ],
	drop: function(id, confirm) {
		if (confirm) {
			Tools.loading($("vcard-help-"+id), x, y);
			request.post(prefix+"friends/delete/", { id: id, confirmed: 1 }, function(rs) {
//				dropElement($("vcard-user-"+id));
				Tools.slidein($("vcard-user-"+id), 3, function() { Tools.alignColumns(); });
				Profile.updateIndicators(function(data) {
					if (data.friends<1 && $("no-friends"))
						$("no-friends").style.display = "block";
					if (data.friends_pending<1 && $("no-friends-pending"))
						$("no-friends-pending").style.display = "block";
					return true;
				});
			});
		} else {
			var x = $("vcard-info-"+id).clientWidth;
			var y = $("vcard-info-"+id).clientHeight;
			$("vcard-info-"+id).style.display = "none";
			$("vcard-controls-"+id).style.display = "none";
			$("vcard-help-"+id).style.display = "block";
			Tools.loading($("vcard-help-"+id), x, y);
			request.html(prefix+"friends/delete/"+id+"/", function(h) {
				Tools.stopLoading($("vcard-help-"+id));
				$("vcard-help-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	cancelDelete: function(id) {
		$("vcard-help-"+id).style.display = "none";
		$("vcard-info-"+id).style.display = "block";
		$("vcard-controls-"+id).style.display = "block";
		dropAllElements($("vcard-help-"+id));
	},
	sendItem: function(id,itemid,confirmed) {
		if (itemid && confirmed) {
			Tools.loading($("vcard-help-"+id), x, y);
			request.posthtml(prefix+"friends/senditem/", { id: id, itemid: itemid, confirmed: 1 }, function(h) {
				var x = $("shortvcard-help-"+id);
				if (!x)
					x = $("shortvcard-help-"+itemid);
				dropAllElements(x);
				x.innerHTML = h;
//				setTimeout(function() {
//					Friends.cancelSendItem(id);
//				}, TIMEOUT);
			});
		} else if (itemid) {
			if ($("shortitem-link-image-"+itemid))
				$("shortitem-link-image-"+itemid).src = "/img/icon-add-active.gif";
			var x = $("shortvcard-info-"+itemid).clientWidth;
			var y = $("shortvcard-info-"+itemid).clientHeight;
			$("shortvcard-info-"+itemid).style.display = "none";
			$("shortvcard-help-"+itemid).style.display = "block";
			Tools.loading($("shortvcard-help-"+itemid), x, y);
			request.html(prefix+"friends/senditem/"+id+"/"+itemid+"/", function(h) {
				Tools.stopLoading($("shortvcard-help-"+itemid));
				dropAllElements($("shortvcard-help-"+itemid));
				$("shortvcard-help-"+itemid).innerHTML = h;
				Tools.alignColumns();
			});
		} else {
			if ($("vcard-actions-"+id).style.display=="block")
				this.cancelSendItem(id);
			else
				request.html(prefix+"friends/senditem/"+id+"/", function(h) {
					dropAllElements($("vcard-actions-"+id));
					$("vcard-actions-"+id).innerHTML = h;
					$("vcard-actions-"+id).style.display = "block";
					Tools.alignColumns();
				});
		}
	},
	cancelSendItem: function(id,itemid) {
		if (itemid) {
			if ($("shortitem-link-image-"+itemid))
				$("shortitem-link-image-"+itemid).src = "/img/icon-add.gif";
			$("shortvcard-help-"+itemid).style.display = "none";
			$("shortvcard-info-"+itemid).style.display = "block";
			dropAllElements($("shortvcard-help-"+itemid));
		} else {
			dropAllElements($("vcard-actions-"+id));
			$("vcard-actions-"+id).style.display = "none";
		}
	},
	inviteGroup: function(id,groupid,confirmed) {
		if (groupid && confirmed) {
			Tools.loading($("vcard-help-"+id), x, y);
			request.posthtml(prefix+"friends/invitegroup/", { id: id, groupid: groupid, confirmed: 1 }, function(h) {
				if (h=="1") {
					Friends.cancelInviteGroup(id);
				} else {
					dropAllElements($("shortvcard-help-"+groupid));
					$("shortvcard-help-"+groupid).innerHTML = h;
					Tools.alignColumns();
//					setTimeout(function() { Friends.cancelInviteGroup(id); }, TIMEOUT);
				}
			});
		} else if (groupid) {
			if ($("shortgroup-link-image-"+groupid))
				$("shortgroup-link-image-"+groupid).src = "/img/icon-add-active.gif";
			var x = $("shortvcard-info-"+groupid).clientWidth;
			var y = $("shortvcard-info-"+groupid).clientHeight;
			$("shortvcard-info-"+groupid).style.display = "none";
			$("shortvcard-help-"+groupid).style.display = "block";
			Tools.loading($("shortvcard-help-"+groupid), x, y);
			request.html(prefix+"friends/invitegroup/"+id+"/"+groupid+"/", function(h) {
				Tools.stopLoading($("shortvcard-help-"+groupid));
				dropAllElements($("shortvcard-help-"+groupid));
				$("shortvcard-help-"+groupid).innerHTML = h;
				Tools.alignColumns();
			});
		} else {
			if ($("vcard-actions-"+id).style.display=="block")
				this.cancelInviteGroup(id);
			else
				request.html(prefix+"friends/invitegroup/"+id+"/", function(h) {
					dropAllElements($("vcard-actions-"+id));
					$("vcard-actions-"+id).innerHTML = h;
					$("vcard-actions-"+id).style.display = "block";
					Tools.alignColumns();
				});
		}
	},
	cancelInviteGroup: function(id,groupid) {
		if (groupid) {
			if ($("shortgroup-link-image-"+groupid))
				$("shortgroup-link-image-"+groupid).src = "/img/icon-add.gif";
			$("shortvcard-help-"+groupid).style.display = "none";
			$("shortvcard-info-"+groupid).style.display = "block";
			dropAllElements($("shortvcard-help-"+groupid));
		} else {
			dropAllElements($("vcard-actions-"+id));
			$("vcard-actions-"+id).style.display = "none";
		}
	},
	google: function(email, pwd) {
		request.posthtml(prefix+"friends/google/", { email: email, pwd: pwd }, function(h) {
			dropAllElements($("google-results"));
			$("google-results").innerHTML = h;
			$("google-results").style.display = "block";
			Tools.alignColumns();
		});
	},
	addLookupFriend: function(x, id, confirm) {
		if (confirm) {
			var x = $("shortvcard-info-"+id).clientWidth;
			var y = $("shortvcard-info-"+id).clientHeight;
			Tools.loading($("shortvcard-help-"+id), x, y);
			request.posthtml(prefix+"friends/lookuppropose/", { id: id, confirmed: 1 }, function(h) {
				Tools.stopLoading($("shortvcard-help-"+id));
				dropAllElements($("shortvcard-help-"+id));
				$("shortvcard-help-"+id).innerHTML = h;
				dropElement($("shortvcard-button-"+id));
				setTimeout(function() {
					$("shortvcard-info-"+id).style.display = "block";
					dropAllElements($("shortvcard-help-"+id));
				}, TIMEOUT);
			});
		} else {
			var x = $("shortvcard-info-"+id).clientWidth;
			var y = $("shortvcard-info-"+id).clientHeight;
			$("shortvcard-info-"+id).style.display = "none";
			$("shortvcard-help-"+id).style.display = "block";
			Tools.loading($("shortvcard-help-"+id), x, y);
			request.html(prefix+"friends/lookuppropose/"+id+"/", function(h) {
				Tools.stopLoading($("shortvcard-help-"+id));
				$("shortvcard-help-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	cancelAddLookupFriend: function(id) {
		$("shortvcard-help-"+id).style.display = "none";
		$("shortvcard-info-"+id).style.display = "block";
		dropAllElements($("shortvcard-help-"+id));
	},
	addInvitation: function(email, code, confirm) {
		if (confirm) {
			Tools.loading($("email-help-"+code));
			request.posthtml(prefix+"friends/addinvitation/", { email: email, confirmed: 1 }, function(h) {
				Tools.stopLoading($("email-help-"+code));
				dropAllElements($("email-help-"+code));
				$("email-help-"+code).innerHTML = h;
				$("propose-button-"+code).innerHTML = $("propose-button-"+code).className
				setTimeout(function() {
					dropAllElements($("email-help-"+code));
					$("email-help-"+code).style.display = "none";
				}, TIMEOUT);
				Profile.updateIndicators();
			});
		} else {
			$("email-help-"+code).style.display = "block";
			Tools.loading($("email-help-"+code));
			request.html(prefix+"friends/addinvitation/?email="+email, function(h) {
				Tools.stopLoading($("email-help-"+code));
				$("email-help-"+code).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	cancelInvitation: function(code) {
		dropAllElements($("email-help-"+code));
	},
	removeInvitation: function(id) {
		if (this.invitations.contains(id))
			this.invitations = this.invitations.removevalue(id);
		$("count-invitations").innerHTML = this.invitations.length;
		Tools.alignColumns();
	},
	addProposal: function(id, confirm) {
		if (confirm) {
			Tools.loading($("user-help-"+id));
			request.posthtml(prefix+"friends/addproposal/", { id: id, invited: 1, confirmed: 1 }, function(h) {
				Tools.stopLoading($("user-help-"+id));
				dropAllElements($("user-help-"+id));
				$("user-help-"+id).innerHTML = h;
				$("propose-button-"+id).className = "inactive";
				$("propose-button-"+id).removeAttribute("href");
				setTimeout(function() {
					$("user-info-"+id).style.display = "block";
					dropAllElements($("user-help-"+id));
					$("user-help-"+id).style.display = "none";
				}, TIMEOUT);
				Profile.updateIndicators();
			});
		} else {
			$("user-help-"+id).style.display = "block";
			Tools.loading($("user-help-"+id));
			request.html(prefix+"friends/addproposal/"+id+"/", function(h) {
				Tools.stopLoading($("user-help-"+id));
				$("user-help-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	removeProposal: function(id) {
		if (this.proposals.contains(id))
			this.proposals = this.proposals.removevalue(id);
		$("count-proposals").innerHTML = this.proposals.length;
		Tools.alignColumns();
	},
	sendAll: function() {
		var vars = { };
		for (var i=0; i<this.proposals.length; i++)
			vars["proposal["+i+"]"] = this.proposals[i];
		for (var i=0; i<this.invitations.length; i++)
			vars["invitation["+i+"]"] = this.invitations[i];
		request.post(prefix+"friends/sendall/", vars, function(rs) {
			window.location = prefix+"friends/invitations/";
		});
	},
	searchAll: function(school, company, country, city) {
//		this.research("?school="+school+"&company="+company+"&country="+country+"&city="+city);
		dropAllElements($("search-results"));
		Tools.loading($("search-results"));
		Tools.alignColumns();
		request.posthtml(prefix+"friends/dosearch/", { school: school, company: company, country: country, city: city }, function(h) {
			Tools.stopLoading($("search-results"));
			$("search-results").innerHTML = h;
			Tools.alignColumns();
		});
	},
	research: function(url) {
		dropAllElements($("search-results"));
		Tools.loading($("search-results"));
		Tools.alignColumns();
		request.html(prefix+"friends/dosearch/"+url, function(h) {
			Tools.stopLoading($("search-results"));
			$("search-results").innerHTML = h;
			Tools.alignColumns();
		});
	},
	findSchools: function(name, pagenum) {
		Tools.loading($("search-results"));
		request.html(prefix+"friends/dosearch/"+(pagenum?pagenum:"?school="+name), function(h) {
			Tools.stopLoading($("search-results"));
			dropAllElements($("search-results"));
			$("search-results").innerHTML = h;
			Tools.alignColumns();
		});
	},
	findCompanies: function(name, pagenum) {
		Tools.loading($("search-results"));
		request.html(prefix+"friends/dosearch/"+(pagenum?pagenum:"?company="+name), function(h) {
			Tools.stopLoading($("search-results"));
			dropAllElements($("search-results"));
			$("search-results").innerHTML = h;
			Tools.alignColumns();
		});
	},
	findCountries: function(country, city, pagenum) {
		Tools.loading($("search-results"));
		request.html(prefix+"friends/dosearch/"+(pagenum?pagenum:"?country="+country+"&city="+city), function(h) {
			Tools.stopLoading($("search-results"));
			dropAllElements($("search-results"));
			$("search-results").innerHTML = h;
			Tools.alignColumns();
		});
	},
	findOwnSchools: function(pagenum) {
		Tools.loading($("search-results"));
		request.html(prefix+"friends/dosearch/myschool/"+(pagenum?pagenum:""), function(h) {
			Tools.stopLoading($("search-results"));
			dropAllElements($("search-results"));
			$("search-results").innerHTML = h;
			Tools.alignColumns();
		});
	},
	findOwnCompanies: function(pagenum) {
		Tools.loading($("search-results"));
		request.html(prefix+"friends/dosearch/mycompany/"+(pagenum?pagenum:""), function(h) {
			Tools.stopLoading($("search-results"));
			dropAllElements($("search-results"));
			$("search-results").innerHTML = h;
			Tools.alignColumns();
		});
	},
	findOwnCountries: function(pagenum) {
		Tools.loading($("search-results"));
		request.html(prefix+"friends/dosearch/mycountry/"+(pagenum?pagenum:""), function(h) {
			Tools.stopLoading($("search-results"));
			dropAllElements($("search-results"));
			$("search-results").innerHTML = h;
			Tools.alignColumns();
		});
	},
	preview: function() {
		if ($("preview-container").childNodes.length>0) {
			dropAllElements($("preview-container"));
			return;
		}
		Tools.alignColumns();
		request.html(prefix+"friends/previewinvitation/", function(h) {
			$("preview-container").style.padding = "10px";
			$("preview-container").style.backgroundColor = "#ffffff";
			$("preview-container").style.border = "#cccccc 1px solid";
			$("preview-container").style.overflow = "auto";
			$("preview-container").style.height = "430px";
			$("preview-container").innerHTML = h;
			Tools.alignColumns();
			$("preview-container").style.position = "relative";
			$("preview-container").style.left = "-53px";
			$("preview-container").style.width = "700px";
		});
	},
	homeInvite: function() {
		var url = prefix+"friends/invite/home/";
		if ($("friends-home").childNodes.length>0 && this.lastimporturl==url) {
			this.lastimporturl = "";
			request.html(prefix+"friends/invitecontacts/home/", function(h) {
				dropAllElements($("friends-home"));
				$("friends-home").innerHTML = h;
				Tools.alignColumns();
			});
		} else {
			this.lastimporturl = prefix+"friends/invite/home/";
			request.html(url, function(h) {
				dropAllElements($("friends-home"));
				$("friends-home").innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	homeCsv: function() {
		var url = prefix+"friends/csv/home/";
		if ($("friends-home").childNodes.length>0 && this.lastimporturl==url) {
			this.lastimporturl = "";
			request.html(prefix+"friends/invitecontacts/home/", function(h) {
				dropAllElements($("friends-home"));
				$("friends-home").innerHTML = h;
				Tools.alignColumns();
			});
		} else {
			this.lastimporturl = url;
			request.html(url, function(h) {
				dropAllElements($("friends-home"));
				$("friends-home").innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	homeWebmail: function() {
		var url = prefix+"friends/webmail/home/";
		if ($("friends-home").childNodes.length>0 && this.lastimporturl==url) {
			this.lastimporturl = "";
			request.html(prefix+"friends/invitecontacts/home/", function(h) {
				dropAllElements($("friends-home"));
				$("friends-home").innerHTML = h;
				Tools.alignColumns();
			});
		} else {
			this.lastimporturl = url;
			request.html(url, function(h) {
				dropAllElements($("friends-home"));
				$("friends-home").innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	showImported: function() {
		Tools.loading($("friends-home"));
		request.html(prefix+"friends/imported/", function(h) {
			Tools.stopLoading($("friends-home"));
			dropAllElements($("friends-home"));
			$("friends-home").innerHTML = h;
			Tools.alignColumns();
		});
	},
	inviteMore: function() {
		var d = document.createElement("div");
//		d.innerHTML = $("invite-sample").innerHTML;
		d.innerHTML = "<div class=\"invite-name\"><div class=\"fancy-input\"><input type=\"text\" name=\"name[]\"/></div></div>"+
				"<div class=\"invite-email\"><div class=\"fancy-input\"><input type=\"text\" name=\"email[]\"/></div></div>";
		$("invite-list").insertBefore(d, $("invite-more"));
	},
	showSponsored: function() {
		if ($("sponsored").style.display == "block")
			$("sponsored").style.display = "none";
		else
			$("sponsored").style.display = "block";
	}
};

var Mosaic = {
	width: 0,
	cnt: 0,
	page: 0,
	method: "", //"items",
	mode: "images",
	totalcount: 0,
	ordr: "random",
	nonreg: false,
	nonregok: 0,
	force: false,
	initialize: function(method, mode, nonreg) {
		this.nonreg = nonreg;
//		this.method = method;
		if (mode) {
			this.mode = mode;
			this.page = 0;
		}
		var itemwidth = this.mode=="images" ? 83 : 168;
		this.width = 581;
		this.cnt = this.mode=="images" ? Math.floor(5*(this.width/itemwidth)) : 18;
		
		this.load(method);
	},
	load: function(method,offset) {
		if (method!=this.method) {
			if (this.method) {
				$("mosaic-mode-"+this.method).setAttribute("href", $("mosaic-mode-"+this.method).getAttribute("rel"));
				$("mosaic-mode-"+this.method).className = "";
			}
			this.method = method;
			this.page = 0;
			$("mosaic-mode-"+this.method).className = "active";
			$("mosaic-mode-"+this.method).setAttribute("rel", $("mosaic-mode-"+this.method).getAttribute("href"));
			$("mosaic-mode-"+this.method).removeAttribute("href");
		} else if (!offset && !this.force)
			return;
		this.force = false;
		if (this.ordr!="random")
			if (offset)
				if (this.page+offset>=0)
					this.page += offset;

		if (this.ordr=="random") {
			this.turnOnPrevious();
			this.turnOnNext();
		} else {
			if (this.page>0)
				this.turnOnPrevious();
			else
				this.turnOffPrevious();
		}

		dropAllElements($("mosaic"));
		Tools.loading($("mosaic"));
		request.html(prefix+"index/images/"+(this.method?this.method+"/":"")+"?count="+this.cnt+"&page="+this.page+"&mode="+this.mode+"&ordr="+this.ordr, function(h) {
			Tools.stopLoading($("mosaic"));
			dropAllElements($("mosaic"));
			$("mosaic").innerHTML = h;
			if ($("mosaic-count")) {
				Mosaic.totalcount = parseInt($("mosaic-count").className);
				dropElement($("mosaic-count"));
			}
			if (Mosaic.ordr!="random") {
				if (Mosaic.cnt*(Mosaic.page+1)<Mosaic.totalcount)
					Mosaic.turnOnNext();
				else
					Mosaic.turnOffNext();
			}
		});
	},
	register: function() {
		window.location = prefix+"register/";
	},
	next: function() {
		if (this.nonreg)
			return this.register();
		this.load(this.method, 1);
	},
	previous: function() {
		if (this.nonreg)
			return this.register();
		this.load(this.method, -1);
	},
	turnOffNext: function() {
		$("mosaic-right-inactive").style.display = "";
		$("mosaic-right-active").style.display = "none";
	},
	turnOnNext: function() {
		$("mosaic-right-inactive").style.display = "none";
		$("mosaic-right-active").style.display = "";
	},
	turnOffPrevious: function() {
		$("mosaic-left-inactive").style.display = "";
		$("mosaic-left-active").style.display = "none";
	},
	turnOnPrevious: function() {
		$("mosaic-left-inactive").style.display = "none";
		$("mosaic-left-active").style.display = "";
	},
	showTextHint: function(name, skeywords, keywords, country, city, parent, entity, side) {
		var to = $("mosaic").getAttribute("to");
		if (to)
			clearTimeout(to);
		if ($("mosaic-hint-"+entity)) {
			$("mosaic-hint-"+entity).parentNode.style.zIndex = 0;
			dropElement($("mosaic-hint-"+entity));
		}
		parent.style.zIndex = 200;
		var d = document.createElement("div");
		d.id = "mosaic-hint-"+entity;
		d.className = "mosaic-hint"+(side?" "+side:"");
		d.innerHTML = "<div class=\"help\">"+(country?"<div class=\"location\"><img src=\"/img/flags/"+country+"\" border=\"0\"/>"+city+"</div>":"")+"<h2>"+name+"</h2>"+(skeywords?"<p class=\"skeywords\">"+skeywords+"</p>":"")+"<p>"+keywords+"</p></div>";
		d.onmouseover = function() { var to = $("mosaic").getAttribute("to"); if (to) clearTimeout(to); }
		d.style.zIndex = 300;
		parent.appendChild(d);
		$("mosaic").onmouseout = function() {
			var a = setTimeout(function() {
				dropElement($("mosaic-hint-"+entity));
			},200);
			this.setAttribute("to", a);
		}
		d.onmouseout = function() {
			var a = setTimeout(function() {
				dropElement($("mosaic-hint-"+entity));
			},200);
			$("mosaic").setAttribute("to", a);
		};
	},
	showPhotoHint: function(photo, parent) {
		var to = $("mosaic").getAttribute("to");
		if (to)
			clearTimeout(to);
		if ($("mosaic-text-hint"))
			dropElement($("mosaic-text-hint"));
		var d = document.createElement("div");
		d.id = "mosaic-text-hint";
		d.innerHTML = "<div class=\"mosaic-text-hint-help\"><img src=\""+photo+"\" border=\"0\"/></div>";
		d.onmouseover = function() { var to = $("mosaic").getAttribute("to"); if (to) clearTimeout(to); }
		parent.style.position = "relative";
		d.style.left = Math.round(parent.clientWidth/2-123/2)+"px";
		parent.appendChild(d);
		$("mosaic").onmouseout = function() {
			var a = setTimeout(function() {
				dropElement($("mosaic-text-hint"));
			},200);
			this.setAttribute("to", a);
		}
	},
	switchTo: function(mode) {
		if (this.nonreg && this.nonregok)
			return this.register();
		this.nonregok ++;
		if ($("mosaic-view-"+this.mode+"-active")) {
			$("mosaic-view-"+this.mode+"-active").id = "mosaic-view-"+this.mode+"-inactive";
			un$("mosaic-view-"+this.mode+"-active");
		}
		if ($("mosaic-view-"+mode+"-inactive")) {
			$("mosaic-view-"+mode+"-inactive").id = "mosaic-view-"+mode+"-active";
			un$("mosaic-view-"+mode+"-inactive");
		}
		this.force = true;
		this.initialize(this.method, mode, this.nonreg);
	},
	setOrdr: function(ordr) {
		if (this.nonreg)
			return this.register();
		if (ordr!=this.ordr) {
			$("mosaic-order-"+ordr).className = "active";
			$("mosaic-order-"+this.ordr).className = "";
		}
		this.ordr = ordr;
		this.page = 0;
		this.force = true;
	}
};

var GoogleMap = {
	show: function(lat, long) {
		if (!$("google-map")) {
			var div = document.createElement("div");
			div.id = "google-map";
			div.className = "dialog";
			$("root").appendChild(div);
		}
		var params = "";
		if (lat && long)
			params = "?x="+lat+"&y="+long;
		$("google-map").style.width = "500px";
		$("google-map").style.height = "500px";
		$("google-map").innerHTML = "";
		showandcenter($("google-map"));
	},
	close: function() {
		dropAllElements($("google-map"));
		$("google-map").style.display = "none";
	}
};

var Journal = {
	offs: 0,
	timeout: 0,
	drop: function(id, filter) {
		request.json(prefix+"journal/delete/"+id+"/"+(filter?filter+"/":""), function(rs) {
			Tools.slidein($("log-"+id), 3, function() { Tools.alignColumns(); });
			if (rs.count<1 && $("no-journal")) {
				$("no-journal").style.display = "block";
				$("journal-more").style.display = "none";
				$("journal-no-more").style.display = "block";
			}
			if (filter && rs.count<1 && $("no-journal-"+filter)) {
				$("no-journal-"+filter).style.display = "block";
				$("journal-more").style.display = "none";
				$("journal-no-more").style.display = "block";
			}
			Tools.alignColumns();
			return true;
		});
	},
	loadMore: function(filter) {
		this.offs ++;
		request.html(prefix+"journal/more/"+(filter?filter+"/":"")+"?page="+this.offs, function(h) {
			var d = document.createElement("div");
			d.innerHTML = h;
			$("journal-container").appendChild(d);
			if ($("journal-hide-more")) {
				$("journal-more").style.display = "none";
				$("journal-no-more").style.display = "block";
			}
			Tools.alignColumns();
		});
	},
	start: function() {
		this.timeout = setTimeout(function() {
			Journal.doUpdate();
			Journal.start();
		}, 7000);
	},
	doUpdate: function() {
		request.html("/journal/update/", function(h) {
			if (!h)
				return;
			var d = document.createElement("div");
			d.innerHTML = h;
			d.style.overflow = "hidden";
			d.style.height = "1px";
			$("journal-container").insertBefore(d, $("journal-container").childNodes[0]);
			var intr = setInterval(function() {
				d.style.height = (parseInt(d.style.height)+4)+"px";
				var h = 0, cnt = 0;
				for (var i=0; i<d.childNodes.length; i++) {
					if (d.childNodes[i].id) {
						h += d.childNodes[i].clientHeight;
						cnt ++;
					}
				}
				if (parseInt(d.style.height)>=h) {
					d.style.height = (h+cnt)+"px";
					clearTimeout(intr);
				}
			}, 10);
		});
	}
};

var Exchanges = {
	items: { mine: [], his: [] },
	editid: 0,
	edititems: [ ],
	proposequeue: { his: [], mine: [] },
	propose: function(userid, itemid) {
		this.items.mine = [];
		this.items.his = [];
		Tools.loading($("exchange"), 200, 100);
		request.html(prefix+"exchanges/propose/"+userid+"/"+(itemid?itemid+"/":""), function(h) {
			Tools.stopLoading($("exchange"));
			dropAllElements($("exchange"));	
			$("exchange").innerHTML = h;
			Tools.alignColumns();
			Profile.updateIndicators();
		});
	},
	submitPropose: function(mine, his) {
	},
	cancelPropose: function() {
	},
	addItem: function(whose,id) {
		if (this.items[whose].contains(id)) {
			this.items[whose] = this.items[whose].removevalue(id);
			this.proposequeue[whose] = this.proposequeue[whose].removevalue(id);
			if (this.proposequeue[whose].length>0) {
				var x = this.proposequeue[whose][this.proposequeue[whose].length-1];
				if (whose=="mine" && $("my-image"))
					$("my-image").src = "/img/item/83/"+x+".jpg";
				if (whose=="his" && $("his-image"))
					$("his-image").src = "/img/item/83/"+x+".jpg";
			} else {
				if (whose=="mine" && $("my-image"))
					$("my-image").src = "/img/noimage-item-83.gif";
				if (whose=="his" && $("his-image"))
					$("his-image").src = "/img/noimage-item-83.gif";
			}
			$("shortitem-link-image-"+id).src = "/img/icon-add.gif";
		} else {
			if (whose=="mine" && $("my-image"))
				$("my-image").src = "/img/item/83/"+id+".jpg";
			if (whose=="his" && $("his-image"))
				$("his-image").src = "/img/item/83/"+id+".jpg";
			this.items[whose].push(id);
			this.proposequeue[whose].push(id);
			$("shortitem-link-image-"+id).src = "/img/icon-add-active.gif";
		}
		$("count-"+whose).innerHTML = this.items[whose].length;
	},
	submit: function() {
//		if (this.items.mine.length==0 || this.items.his.length==0)
//			return;
		var vars = [ ];
		for (var i=0; i<this.items.mine.length; i++)
			vars["mine["+i+"]"] = this.items.mine[i];
		for (var i=0; i<this.items.his.length; i++)
			vars["his["+i+"]"] = this.items.his[i];
		if ($("custom-checkbox-exchange-friend-1") && $("custom-checkbox-exchange-friend-1").value)
			vars["proposefriend"] = 1;
		request.post(prefix+"exchanges/create/", vars, function(rs) {
			if (rs.result=="1")
				window.location = prefix+"exchanges/";
			else
				alert("Please make sure you have made all selections correctly");
			Profile.updateIndicators();
		});
	},
	edit: function(id) {
		if ($("exchange-edit-help-"+id).childNodes.length>0)
			return this.editCancel();
		this.editCancel();
		this.editid = id;
		this.edititems = [ ];
		this.initedititems = [ ];
		dropAllElements($("exchange-edit-help-"+id));
		$("exchange-edit-"+id).style.display = "block";
		Tools.loading($("exchange-edit-help-"+id), "240", "30");
		Tools.alignColumns();
		request.json(prefix+"exchanges/editids/"+id+"/", function(rs) {
			Exchanges.edititems = rs;
			Exchanges.initedititems = rs;
			request.html(prefix+"exchanges/edit/"+id+"/", function(h) {
				Tools.stopLoading($("exchange-edit-help-"+id));
				$("exchange-edit-help-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		});
	},
	editAddItem: function(exid,id) {
		if (this.edititems.contains(id)) {
			this.edititems = this.edititems.removevalue(id);
			$("shortitem-link-image-"+id).src = "/img/icon-add.gif";
		} else {
			this.edititems.push(id);
			$("shortitem-link-image-"+id).src = "/img/icon-add-active.gif";
		}
	},
	editCancel: function() {
		if (!this.editid)
			return;
		dropAllElements($("exchange-edit-help-"+this.editid));
		$("exchange-edit-"+this.editid).style.display = "none";
		this.editid = 0;
		Tools.alignColumns();
	},
	editSubmit: function() {
		if (!this.editid)
			return;
		var unchanged = true;
		for (var i=0; i<this.edititems.length; i++)
			if (!this.initedititems.contains(this.edititems[i])) {
				unchanged = false;
				break;
			}
		for (var i=0; i<this.initedititems.length; i++)
			if (!this.edititems.contains(this.initedititems[i])) {
				unchanged = false;
				break;
			}
		if (unchanged) {
			$("exchange-edit-warning-"+this.editid).style.display = "block";
			return;
		}
		var vars = { id: this.editid }
		for (var i=0; i<this.edititems.length; i++)
			vars["item["+i+"]"] = this.edititems[i];
		request.post(prefix+"exchanges/editsave/", vars, function(rs) {
			Exchanges.editCancel();
			window.location = "./";
		});
	},
	drop: function(id) {
		if ($("exchange-container-"+id).childNodes.length>0) {
			dropAllElements($("exchange-container-"+id));
		} else {
			Reason.callback("user-reject-exchange", function(reasonid) {
				Tools.loading($("exchange-container-"+id));
				Exchanges.editCancel();
				request.html(prefix+"exchanges/delete/"+id+"/?reason_id="+reasonid, function(h) {
					dropAllElements($("exchange-container-"+id));
					Tools.stopLoading($("exchange-container-"+id));
					$("exchange-container-"+id).innerHTML = h;
					$("exchange-container-"+id).style.display = "block";
					Profile.updateIndicators();
					Tools.alignColumns();
					setTimeout(function() {
						dropElement($("exchange-"+id));
						Tools.alignColumns();
					}, TIMEOUT);
				});
				Tools.alignColumns();
			}, $("exchange-container-"+id), {hidecancel:true});
		}
	},
	accept: function(id, contact, delivery, email, phone, cellphone, address, partner) {
		if (contact) {
			if (contact.length>0 && (contact.contains("email") && email.trim() || contact.contains("phone") && phone.trim() || contact.contains("cellphone") && cellphone.trim() || contact.contains("address") && address.trim()) && delivery && (delivery=="meeting" || delivery=="laposte" && partner>0)) {
				var c = "delivery="+delivery;
				if (delivery=="laposte" && partner)
					c += "&partner="+partner;
				if (contact.contains("email") && email.trim())
					c += "&email="+email;
				if (contact.contains("phone") && phone.trim())
					c += "&phone="+phone;
				if (contact.contains("cellphone") && cellphone.trim())
					c += "&cellphone="+cellphone;
				if (contact.contains("address") && address.trim())
					c += "&address="+address;
				Tools.loading($("exchange-container-"+id));
				request.json(prefix+"exchanges/accept/"+id+"/?"+c, function(rs) {
					window.location = ""+window.location;
				});
			} else {
				$("exchange-accept-error").style.display = "block";
				Tools.alignColumns();
			}
		} else {
			if ($("exchange-container-"+id).childNodes.length>0)
				return this.cancelAccept(id);
			dropAllElements($("exchange-container-"+id));
			$("exchange-container-"+id).style.display = "block";
			Tools.loading($("exchange-container-"+id));
			Tools.alignColumns();
			request.html(prefix+"exchanges/accept/"+id+"/", function(h) {
				Tools.stopLoading($("exchange-container-"+id));
				$("exchange-container-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	cancelAccept: function(id) {
		dropAllElements($("exchange-container-"+id));
		$("exchange-container-"+id).style.display = "none";
		Tools.alignColumns();
	},
	acceptSwitchContact: function(id, value) {
		var values = $("exchange-contact-"+id).value.explode(",");
		if (values.contains(value)) {
			values = values.removevalue(value);
			$("exchange-switch-contact-"+value+"-"+id).src = "/img/radio.gif";
			$("exchange-"+value+"-div-"+id).style.display = "none";
		} else {
			values.push(value);
			$("exchange-switch-contact-"+value+"-"+id).src = "/img/radio-checked.gif";
			$("exchange-"+value+"-div-"+id).style.display = "block";
		}
		$("exchange-contact-"+id).value = values.join(",");
	},
	acceptSwitchDelivery: function(id, value) {
		var oldvalue = $("exchange-delivery-"+id).value;
		if (oldvalue && oldvalue!=value)
			$("exchange-switch-delivery-"+oldvalue+"-"+id).src = "/img/radio.gif";
		$("exchange-switch-delivery-"+value+"-"+id).src = "/img/radio-checked.gif";
		$("exchange-delivery-"+id).value = value;
	},
	acceptSwitchPartner: function(id, value) {
		var oldvalue = $("exchange-partner-"+id).value;
		if (oldvalue && oldvalue!=value)
			$("exchange-switch-partner-"+oldvalue+"-"+id).src = "/img/radio.gif";
		$("exchange-switch-partner-"+value+"-"+id).src = "/img/radio-checked.gif";
		$("exchange-partner-"+id).value = value;
	},
	finalize: function(id) {
		if (!confirm("Are you sure you want to finalize this exchange? This means that the exchange is completely done, and you and your dealer have successfully exchanged your stuff."))
			return;
		Tools.loading($("exchange-"+id), 200, 200);
		this.editCancel();
		request.json(prefix+"exchanges/finalize/"+id+"/", function(rs) {
			window.location = ""+window.location;
		});
	},
	showAllHisItems: function() {
		dropElement($("show-all-his-button"));
		$("his-all-items").style.display = "block";
		Tools.alignColumns();
	},
	showAllItems: function(id, hiddenbydefault) {
//	console.log($("rest-of-mine-"+id).style.display);
		if ($("rest-of-mine-"+id).style.display=="block" || !$("rest-of-mine-"+id).style.display && !hiddenbydefault) {
			$("rest-of-mine-"+id).style.display = "none";
			$("rest-of-his-"+id).style.display = "none";
		} else {
			$("rest-of-mine-"+id).style.display = "block";
			$("rest-of-his-"+id).style.display = "block";
		}
		Tools.alignColumns();
	},
	complete: function(id) {
		if ($("rest-of-mine-"+id))
			$("rest-of-mine-"+id).style.display = $("rest-of-mine-"+id).style.display=="block" ? "none" : "block";
		if ($("rest-of-his-"+id))
			$("rest-of-his-"+id).style.display = $("rest-of-his-"+id).style.display=="block" ? "none" : "block";
		Tools.alignColumns();
	},
	acknowledge: function(id, data, force) {
		if (data) {
			data["confirmed"] = 1;
			Tools.loading($("exchange-container-"+id));
			Tools.alignColumns();
			request.posthtml(prefix+"exchanges/acknowledge/", data, function(h) {
				Tools.stopLoading($("exchange-container-"+id));
				dropAllElements($("exchange-container-"+id));
				$("exchange-container-"+id).innerHTML = h;
				Profile.updateIndicators();
				Tools.alignColumns();
				window.location = "#e"+id;
			});
		} else {
			if ($("exchange-container-"+id).childNodes.length>0 && (typeof force == 'undefined' || !force))
				return this.cancelAcknowledge(id);
			dropAllElements($("exchange-container-"+id));
			$("exchange-container-"+id).style.display = "block";
			Tools.loading($("exchange-container-"+id));
			Tools.alignColumns();
			request.html(prefix+"exchanges/acknowledge/"+id+"/", function(h) {
				Tools.stopLoading($("exchange-container-"+id));
				$("exchange-container-"+id).innerHTML = h;
				$("exchange-container-"+id).style.display = "block";
				Tools.alignColumns();
			});
		}
	},
	cancelAcknowledge: function(id) {
		dropAllElements($("exchange-container-"+id));
		$("exchange-container-"+id).style.display = "none";
		Tools.alignColumns();
	},
	acknowledgeSwitchOverall: function(id, value) {
		var oldvalue = $("exchange-acknowledge-overall-"+id).value;
		if (oldvalue!=value) {
			$("exchange-switch-overall-"+oldvalue+"-"+id).src = "/img/radio.gif";
			$("exchange-switch-overall-"+value+"-"+id).src = "/img/radio-checked.gif";
			$("exchange-acknowledge-overall-"+id).value = value;
		}
	},
	acknowledgeSwitchDelivery: function(id, value) {
		var oldvalue = $("exchange-acknowledge-delivery-"+id).value;
		if (oldvalue!=value) {
			$("exchange-switch-delivery-"+oldvalue+"-"+id).src = "/img/radio.gif";
			$("exchange-switch-delivery-"+value+"-"+id).src = "/img/radio-checked.gif";
			$("exchange-acknowledge-delivery-"+id).value = value;
		}
	},
	acknowledgeSwitchAnswer: function(id, answer, value) {
		var oldvalue = $("exchange-acknowledge-answer-"+answer+"-"+id).value;
		if (oldvalue!=value) {
			$("exchange-switch-answer-"+answer+"-"+oldvalue+"-"+id).src = "/img/radio.gif";
			$("exchange-switch-answer-"+answer+"-"+value+"-"+id).src = "/img/radio-checked.gif";
			$("exchange-acknowledge-answer-"+answer+"-"+id).value = value;
		}
	},
	save: function(id) {
		window.location = prefix+"exchanges/saved/";
	},
	forget: function(id) {
		request.json(prefix+"exchanges/forget/"+id+"/", function(rs) {
			dropElement($("exchange-"+id));
			Tools.alignColumns();
		});
	},
	history: function(id, altdesign) {
		var dest = $("exchange-container-"+id) ? $("exchange-container-"+id) : $("details-"+id);
		if (dest.style.display=="block") {
			removeClassName($("nu-history-"+id), "history-clicked");
			dest.style.display = "none";
			Tools.alignColumns();
		} else {
			addClassName($("nu-history-"+id), "history-clicked");
			dest.style.display = "block";
			Tools.loading(dest);
			Tools.alignColumns();
			request.html(prefix+"exchanges/history/"+id+"/"+(altdesign?"1/":""), function(h) {
				Tools.stopLoading(dest);
				dropAllElements(dest);
				dest.innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	showUser: function(id, exid) {
		if ($("exchange-container-"+exid).style.display=="none") {
			$("exchange-container-"+exid).style.display = "";
			Tools.loading($("exchange-container-"+exid), "100%", "100");
			request.html(prefix+"user/single/"+id+"/", function(h) {
				Tools.stopLoading($("exchange-container-"+exid));
				dropAllElements($("exchange-container-"+exid));
				$("exchange-container-"+exid).innerHTML = h;
				Tools.alignColumns();
			});
		} else
			$("exchange-container-"+exid).style.display = "none";
	},
	eco: function(id, el, google) {
		if (!el)
			el = $("exchange-container-"+id);
		if (el.style.display=="block") {
			el.style.display = "none";
			Tools.alignColumns();
		} else {
			el.style.display = "block";
			Tools.loading(el);
			Tools.alignColumns();
			request.html(prefix+"exchanges/eco/"+id+"/?google="+(google?1:0), function(h) {
				Tools.stopLoading(el);
				dropAllElements(el);
				el.innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	provideContacts: function(id, contact, email, phone, cellphone, address) {
		if (contact) {
			var c = "";
			if (contact.contains("email") && email.trim())
				c += "&email="+email;
			if (contact.contains("phone") && phone.trim())
				c += "&phone="+phone;
			if (contact.contains("cellphone") && cellphone.trim())
				c += "&cellphone="+cellphone;
			if (contact.contains("address") && address.trim())
				c += "&address="+address;
			Tools.loading($("exchange-container-"+id));
			request.json(prefix+"exchanges/providecontacts/"+id+"/?"+c, function(rs) {
				window.location = ""+window.location;
			});
		} else {
			if ($("exchange-container-"+id).childNodes.length>0)
				return this.cancelAccept(id);
			dropAllElements($("exchange-container-"+id));
			$("exchange-container-"+id).style.display = "block";
			Tools.loading($("exchange-container-"+id));
			Tools.alignColumns();
			request.html(prefix+"exchanges/providecontacts/"+id+"/", function(h) {
				Tools.stopLoading($("exchange-container-"+id));
				$("exchange-container-"+id).innerHTML = h;
				$("exchange-container-"+id).style.display = "block";
				Tools.alignColumns();
			});
		}
	},
	cancelProvideContacts: function(id) {
		dropAllElements($("exchange-container-"+id));
		$("exchange-container-"+id).style.display = "none";
		Tools.alignColumns();
	},
	seeContacts: function(id) {
		if ($("exchange-container-"+id).childNodes.length>0) {
			dropAllElements($("exchange-container-"+id));
			$("exchange-container-"+id).style.display = "none";
			Tools.alignColumns();
			return;
		}
		request.html(prefix+"exchanges/contacts/"+id+"/", function(h) {
			dropAllElements($("exchange-container-"+id));
			$("exchange-container-"+id).innerHTML = h;
			$("exchange-container-"+id).style.display = "block";
			Tools.alignColumns();
		});
	}
};

var Dailymatch = {
	drop: function(id) {
		request.html(prefix+"dailymatch/delete/"+id+"/", function(h) {
			if ($("match-"+id))
				dropElement($("match-"+id));
			if ($("left-dailymatch")) {
				dropElement($("left-match-"+id));
				dropAllElements($("left-dailymatch"));
				$("left-dailymatch").innerHTML = h;
			}
		});
	},
	showFriends: function(id) {
		$("widget-dailymatch-friends-"+id).style.display = "block";
		$("widget-dailymatch-allfriends-"+id).style.display = "none";
	}
};

var Profile = {
	updateIndicators: function(preprocess) {
		request.json(prefix+"profile/indicators/", function(rs) {
			if (!rs)
				return;
			if (preprocess) {
				if (!preprocess(rs))
					return;
			}
			$("left-profile-interests").innerHTML = rs.keywords;
			$("profile-indicator-items").innerHTML = rs.items;
			if ($("title-indicator-items"))
				$("title-indicator-items").innerHTML = rs.items;
			if ($("title-indicator-own-items"))
				$("title-indicator-own-items").innerHTML = rs.items_own;
			if ($("title-indicator-favorites"))
				$("title-indicator-favorites").innerHTML = rs.items_favorites;
			$("profile-indicator-friends").innerHTML = rs.friends;
			if ($("title-indicator-friends"))
				$("title-indicator-friends").innerHTML = rs.friends;
			$("profile-indicator-groups").innerHTML = rs.groups;
			if ($("title-indicator-groups"))
				$("title-indicator-groups").innerHTML = rs.groups;
			$("profile-indicator-exchanges").innerHTML = rs.exchanges;
			if ($("title-indicator-exchanges"))
				$("title-indicator-exchanges").innerHTML = rs.exchanges;
			if (parseInt(rs.requests_items)) {
				$("profile-items-requests").innerHTML = "+"+rs.requests_items;
				$("profile-items-requests").style.display = "";
			} else
				$("profile-items-requests").style.display = "none";
			if (parseInt(rs.requests_friends)) {
				$("profile-friends-requests").innerHTML = "+"+rs.requests_friends;
				$("profile-friends-requests").style.display = "";
			} else
				$("profile-friends-requests").style.display = "none";
			if (parseInt(rs.requests_groups)) {
				$("profile-groups-requests").innerHTML = "+"+rs.requests_groups;
				$("profile-groups-requests").style.display = "";
			} else
				$("profile-groups-requests").style.display = "none";
			if (parseInt(rs.requests_exchanges)) {
				$("profile-exchanges-requests").innerHTML = "+"+rs.requests_exchanges;
				$("profile-exchanges-requests").style.display = "";
			} else
				$("profile-exchanges-requests").style.display = "none";
			if ($("title-indicator-exchanges-current"))
				$("title-indicator-exchanges-current").innerHTML = rs.exchanges_current;
			if ($("title-indicator-exchanges-accepted"))
				$("title-indicator-exchanges-accepted").innerHTML = rs.exchanges_accepted;
			if ($("title-indicator-exchanges-saved"))
				$("title-indicator-exchanges-saved").innerHTML = rs.exchanges_saved;
			if ($("title-indicator-exchanges-rejected"))
				$("title-indicator-exchanges-rejected").innerHTML = rs.exchanges_rejected;
			if ($("title-indicator-invitations"))
				$("title-indicator-invitations").innerHTML = rs.invitations;
			$("profile-indicator-name").innerHTML = rs.name;
			if ($("profile-indicator-flag"))
				$("profile-indicator-flag").src = rs.flag;
			$("left-profile-location").innerHTML = rs.city;
			$("left-profile-interests").innerHTML = rs.keywords;
		});
	},
	widget: function(name, force) {
		if ($("left-widget").style.display=="block" && $("left-widget").getAttribute("widget")==name && !force) {
			dropAllElements($("left-widget"));
			$("left-widget").style.display = "none";
			removeClassName($("left-profile-eco"), "active");
			removeClassName($("left-profile-comments"), "active");
			removeClassName($("left-profile-experience"), "active");
		} else {
			$("left-widget").setAttribute("widget", name);
			addClassName($("left-profile-"+name), "active");
			request.html(prefix+"profile/widget/"+name+"/", function(h) {
				dropAllElements($("left-widget"));
				if (h=="null") {
					$("left-widget").style.display = "none";
				} else {
					$("left-widget").innerHTML = h;
					$("left-widget").style.display = "block";
				}
				if (name=="comments") {
					var cnt = parseInt($("left-profile-comments").innerHTML.rplace("+",""));
					if (cnt>1) {
						$("left-profile-comments").innerHTML = "+"+(cnt-1);
					} else
						$("left-profile-comments").innerHTML = "...";
				}
			});
		}
	},
	show: function(section,alias,peer) {
		if (!alias)
			alias = section;
		if ($("profile-"+alias).className=="profile-selected") {
			$("profile-edit-"+section).style.display = "none";
			if (peer)
				$("profile-edit-"+peer).style.display = "none";
			$("profile-"+alias).className = "";
		} else {
			$("profile-edit-"+section).style.display = "block";
			$("profile-"+alias).className = "profile-selected";
			if ($("profile-error-"+section))
				$("profile-error-"+section).innerHTML = "";
		}
		Tools.alignColumns();
	},
	saveName: function(fname, lname) {
		request.post(prefix+"profile/saveparam/", { op: "name", first_name: fname, last_name: lname }, function(rs) {
			if (rs.error)
				$("profile-error-name").innerHTML = rs.error;
			else {
				$("profile-value-name").innerHTML = rs.result;
				$("profile-edit-name").style.display = "none";
				$("profile-name").className = "";
			}
			Tools.alignColumns();
			Profile.updateIndicators();
		});
	},
	saveBirthdate: function(day, month, year) {
		request.post(prefix+"profile/saveparam/", { op: "birthdate", day: day, month: month, year: year }, function(rs) {
			$("profile-value-birthdate").innerHTML = rs.result;
			$("profile-edit-birthdate").style.display = "none";
			$("profile-birthdate").className = "";
			Tools.alignColumns();
		});
	},
	saveGender: function(gender) {
		request.post(prefix+"profile/saveparam/", { op: "gender", gender: gender }, function(rs) {
			$("profile-value-gender").innerHTML = rs.result;
			$("profile-edit-gender").style.display = "none";
			$("profile-gender").className = "";
			Tools.alignColumns();
		});
	},
	saveAddress: function(address1, country, city, zip) {
		request.post(prefix+"profile/saveparam/", { op: "address", address1: address1, country: country, city: city, zip: zip }, function(rs) {
			$("profile-value-address").innerHTML = rs.result;
			$("profile-edit-address").style.display = "none";
			$("profile-address").className = "";
			$("location-iframe").src = prefix+"googlemap/auto/?random="+Math.random();
			Tools.alignColumns();
		});
	},
	saveCountry: function(country, city, zip) {
		request.post(prefix+"profile/saveparam/", { op: "country", country: country, city: city, zip: zip }, function(rs) {
			$("profile-value-country").innerHTML = rs.result;
			$("profile-edit-country").style.display = "none";
			$("profile-country").className = "";
			$("location-iframe").src = prefix+"googlemap/auto/?random="+Math.random();
			Tools.alignColumns();
			Profile.updateIndicators();
		});
	},
	savePhone: function(phone, cellphone) {
		request.post(prefix+"profile/saveparam/", { op: "phone", phone: phone, cellphone: cellphone }, function(rs) {
			$("profile-value-phone").innerHTML = rs.result;
			$("profile-edit-phone").style.display = "none";
			$("profile-phone").className = "";
			Tools.alignColumns();
		});
	},
	saveEmail: function(email, confirm) {
		request.post(prefix+"profile/saveparam/", { op: "email", email: email, confirm: confirm }, function(rs) {
			if (rs.error)
				$("profile-error-email").innerHTML = rs.error;
			else {
				$("profile-value-email").innerHTML = rs.result;
				$("profile-edit-email").style.display = "none";
				$("profile-email").className = "";
			}
			Tools.alignColumns();
		});
	},
	savePassword: function(oldp, newp, confirm) {
		request.post(prefix+"profile/saveparam/", { op: "pwd", oldpassword: oldp, newpassword: newp, confirm: confirm }, function(rs) {
			if (rs.error)
				$("profile-error-password").innerHTML = rs.error;
			else {
				$("profile-edit-password").style.display = "none";
				$("profile-password").className = "";
			}
			Tools.alignColumns();
		});
	},
	saveStatus: function(status) {
		request.post(prefix+"profile/saveparam/", { op: "status", status: status }, function(rs) {
			$("profile-value-status").innerHTML = rs.result;
			$("profile-edit-status").style.display = "none";
			$("profile-status").className = "";
			Tools.alignColumns();
		});
	},
	saveLanguage: function(language) {
		request.post(prefix+"profile/saveparam/", { op: "language", language: language }, function(rs) {
			window.location = rs.url;
		});
	},
	saveNotifications: function(vars) {
		vars["op"] = "notifications";
		request.post(prefix+"profile/saveparam/", vars, function(rs) {
//			$("profile-value-notifications").innerHTML = rs.result;
			$("profile-edit-notifications").style.display = "none";
			$("profile-notifications").className = "";
			Tools.alignColumns();
		});
	},
	savePrivacy: function(vars) {
		vars["op"] = "privacy";
		request.post(prefix+"profile/saveparam/", vars, function(rs) {
			$("profile-edit-privacy").style.display = "none";
			$("profile-privacy").className = "";
			Tools.alignColumns();
		});
	},
	addKeyword: function(word) {
		request.posthtml(prefix+"profile/addkeyword/", { word: word }, function(h) {
			dropAllElements($("container-keywords"));
			$("container-keywords").innerHTML = h;
			Tools.suggestable($("profile-input-keyword-add"), prefix+"keywords/lookup/?pattern=%s");
			var i = 1;
			while ($("profile-input-keyword-"+i)) {
				Tools.suggestable($("profile-input-keyword-"+i), prefix+"keywords/lookup/?pattern=%s");
				i++;
			}
			Profile.updateIndicators();
			Tools.alignColumns();
		});
	},
	saveKeywords: function(keywords) {
		var vars = { };
		for (var i=0; i<keywords["keyword"].length; i++)
			vars["keyword["+i+"]"] = keywords["keyword"][i];
		request.post(prefix+"profile/savekeywords/", vars, function(rs) {
			$("profile-value-keywords").innerHTML = rs.result;
			$("profile-edit-keywords").style.display = "none";
			$("profile-keywords").className = "";
			request.html(prefix+"profile/keywords/inline/", function(h) {
				dropAllElements($("container-keywords"));
				$("container-keywords").innerHTML = h;
				Profile.initKeywords();
				Profile.updateIndicators();
				Tools.alignColumns();
			});
		});
	},
	addSchool: function(name, startmn, startyr, endmn, endyr, stillthere) {
		var start = startmn && startyr ? startyr+"-"+startmn : "";
		var end = stillthere ? "" : (endmn && endyr ? endyr+"-"+endmn : "");
		if (!name || !startmn || !startyr || !endmn || !endyr || !stillthere) {
			$("add-school-error").style.display = "block";
			return;
		} else
			$("add-school-error").style.display = "none";
		request.post(prefix+"profile/addschool/", { name: name, start: start, end: end, stillthere: stillthere?1:0 }, function(rs) {
			request.html(prefix+"profile/background/schools/", function(h) {
				dropAllElements($("container-schools"));
				$("container-schools").innerHTML = h;
				$("edit-schools-button").style.display = "block";
				$("add-school-button").className = "edit-button-second";
				Tools.alignColumns();
			});

			$("profile-value-schools").innerHTML = rs.result;
			$("profile-edit-addschool").style.display = "none";
			$("profile-schools").className = "";
			
			$("profile-input-add-school").value = "";
			$("profile-input-add-school-start-month").selectedIndex = 0;
			$("profile-input-add-school-start-year").selectedIndex = 0;
			$("profile-input-add-school-end-month").selectedIndex = 0;
			$("profile-input-add-school-end-year").selectedIndex = 0;
			$("profile-input-add-school-stillthere").checked = false;
			$("profile-input-add-school-end-month").disabled = false;
			$("profile-input-add-school-end-year").disabled = false;
		});
	},
	saveSchools: function(idx) {
		var vars = { };
		var deletes = [ ];
		for (var i=1; i<=idx; i++) {
			var start = "";
			var end = "";
			if ($("custom-dropdown-edit-school-start-month-"+i).value && $("custom-dropdown-edit-school-start-year-"+i).value)
				start =  $("custom-dropdown-edit-school-start-year-"+i).value+"-"+$("custom-dropdown-edit-school-start-month-"+i).value;
			if (!$("custom-checkbox-edit-school-stillthere-"+i+"-1").value && $("custom-dropdown-edit-school-end-month-"+i).value && $("custom-dropdown-edit-school-end-year-"+i).value)
				end = $("custom-dropdown-edit-school-end-year-"+i).value+"-"+$("custom-dropdown-edit-school-end-month-"+i).value;
			vars["delete["+i+"]"] = $("custom-checkbox-edit-school-delete-"+i+"-1").value ? 1 : 0;
			vars["name["+i+"]"] = $("profile-input-edit-school-"+i).value;
			vars["start["+i+"]"] = start;
			vars["end["+i+"]"] = end;
			vars["stillthere["+i+"]"] = $("custom-checkbox-edit-school-stillthere-"+i+"-1").value ? 1 : 0;
			if ($("custom-checkbox-edit-school-delete-"+i+"-1").value)
				deletes.push(i);
		}
		if (deletes.length==idx) {
			$("edit-schools-button").style.display = "none";
			$("add-school-button").className = "edit-button";
		} else {
			$("edit-schools-button").style.display = "block";
			$("add-school-button").className = "edit-button-second";
		}
		request.post(prefix+"profile/saveschools/", vars, function(rs) {
			$("profile-value-schools").innerHTML = rs.result;
			$("profile-edit-editschool").style.display = "none";
			$("profile-schools").className = "";
			request.html(prefix+"profile/background/schools/", function(h) {
				dropAllElements($("container-schools"));
				$("container-schools").innerHTML = h;
				Tools.alignColumns();
			});
		});
	},
	addCompany: function(name, startmn, startyr, endmn, endyr, stillthere) {
		var start = startmn && startyr ? startyr+"-"+startmn : "";
		var end = stillthere ? "" : (endmn && endyr ? endyr+"-"+endmn : "");
		if (!name || !startmn || !startyr || !endmn || !endyr || !stillthere) {
			$("add-company-error").style.display = "block";
			return;
		} else
			$("add-company-error").style.display = "none";
		request.post(prefix+"profile/addcompany/", { name: name, start: start, end: end, stillthere: stillthere?1:0 }, function(rs) {
			request.html(prefix+"profile/background/companies/", function(h) {
				dropAllElements($("container-companies"));
				$("container-companies").innerHTML = h;
				$("edit-companies-button").style.display = "block";
				$("add-company-button").className = "edit-button-second";
				Tools.alignColumns();
			});

			$("profile-value-companies").innerHTML = rs.result;
			$("profile-edit-addcompany").style.display = "none";
			$("profile-companies").className = "";

			$("profile-input-add-company").value = "";
			$("profile-input-add-company-start-month").selectedIndex = 0;
			$("profile-input-add-company-start-year").selectedIndex = 0;
			$("profile-input-add-company-end-month").selectedIndex = 0;
			$("profile-input-add-company-end-year").selectedIndex = 0;
			$("profile-input-add-company-stillthere").checked = false;
			$("profile-input-add-company-end-month").disabled = false;
			$("profile-input-add-company-end-year").disabled = false;
		});
	},
	saveCompanies: function(idx) {
		var vars = { };
		var deletes = [ ]
		for (var i=1; i<=idx; i++) {
			var start = "";
			var end = "";
			if ($("custom-dropdown-edit-company-start-month-"+i).value && $("custom-dropdown-edit-company-start-year-"+i).value)
				start =  $("custom-dropdown-edit-company-start-year-"+i).value+"-"+$("custom-dropdown-edit-company-start-month-"+i).value;
			if (!$("custom-checkbox-edit-company-stillthere-"+i+"-1").value && $("custom-dropdown-edit-company-end-month-"+i).value && $("custom-dropdown-edit-company-end-year-"+i).value)
				end = $("custom-dropdown-edit-company-end-year-"+i).value+"-"+$("custom-dropdown-edit-company-end-month-"+i).value;
			vars["delete["+i+"]"] = $("custom-checkbox-edit-company-delete-"+i+"-1").value ? 1 : 0;
			vars["name["+i+"]"] = $("profile-input-edit-company-"+i).value;
			vars["start["+i+"]"] = start;
			vars["end["+i+"]"] = end;
			vars["stillthere["+i+"]"] = $("custom-checkbox-edit-company-stillthere-"+i+"-1").value ? 1 : 0;
			if ($("custom-checkbox-edit-company-delete-"+i+"-1").value)
				deletes.push(i);
		}
		if (deletes.length==idx) {
			$("edit-companies-button").style.display = "none";
			$("add-company-button").className = "edit-button";
		} else {
			$("edit-companies-button").style.display = "block";
			$("add-company-button").className = "edit-button-second";
		}
		request.post(prefix+"profile/savecompanies/", vars, function(rs) {
			$("profile-value-companies").innerHTML = rs.result;
			$("profile-edit-editcompany").style.display = "none";
			$("profile-companies").className = "";

			request.html(prefix+"profile/background/companies/", function(h) {
				dropAllElements($("container-companies"));
				$("container-companies").innerHTML = h;
				Tools.alignColumns();
			});
		});
	},
	cities: function(country, zip, simple) {
		request.json(prefix+"profile/cities/?country="+country+"&zip="+zip, function(rs) {
			if (rs.length) {
				var options = [ ];
				for (var i=0; i<rs.length; i++)
					options.push({ value: rs[i], text: rs[i] });
				if (typeof simple != "undefined" && simple) {
					dropAllElements($("profile-div-city"));
					$("profile-div-city").innerHTML = "<select name=\"city\" id=\"profile-input-city\"></select>";
					for (var i=0; i<rs.length; i++) {
						var o = new Option();
						o.value = rs[i];
						o.text = rs[i];
						$("profile-input-city").options[$("profile-input-city").options.length] = o;
					}
				} else {
					Dropdown.create($("profile-div-city"), 'city', options, 0, true);
					$("profile-div-city-stop").style.display = "none";
					$("profile-div-city").style.display = "block";
				}
			} else {
				if (typeof simple != "undefined" && simple) {
					$("profile-div-city").innerHTML = "<input type=\"text\" name=\"city\"/>";
				} else {
					$("profile-div-city").style.display = "none";
					$("profile-div-city-stop").style.display = "block";
					if ($("custom-dropdown-city"))
						$("custom-dropdown-city").value = "";
				}
			}
		});
	},
	initKeywords: function() {
		for (i=0; i<document.forms.profilekeywords.elements.length; i++)
			if (document.forms.profilekeywords.elements[i].name="keyword[]")
				Tools.suggestable(document.forms.profilekeywords.elements[i], prefix+"keywords/lookup/?pattern=%s");
	},
	addMoreKeywords: function(el) {
		var d = document.createElement("div");
		d.className = "fancy-input keywords-input";
		var inp = document.createElement("input");
		inp.type = "text";
		inp.name = "keyword[]";
		d.appendChild(inp);
		$("container-keywords").insertBefore(d, $("add-more-keywords"));
		Tools.suggestable(inp, prefix+"keywords/lookup/?pattern=%s");
	},
	switchHints: function() {
		request.json(prefix+"profile/switchhints/", function(rs) {
			$("profile-text-hints").innerHTML = rs.result;
			hints = parseInt(rs.js) ? 1 : 0;
		});
	},
	switchSubscription: function() {
		request.json(prefix+"profile/switchsubscription/", function(rs) {
			$("profile-text-subscription").innerHTML = rs.result;
		});
	},
	leave: function() {
		if ($("profile-edit-leave").childNodes.length>0) {
			dropAllElements($("profile-edit-leave"));
//			$("user-leave-button").style.display = "block";
			Tools.alignColumns();
		} else {
//			$("user-leave-button").style.display = "none";
			Reason.link('user-leave', prefix+"profile/leave/confirm/", $("profile-edit-leave"), { oncancel: function() { window.location = prefix+"profile/"; } });
		}
	},
	editProfileKeywords: function() {
		if ($("left-profile-interests-edit").style.display=="block") {
			$("left-profile-interests-edit").style.display = "none";
			$("left-profile-interests").style.display = "";
		} else {
			$("left-profile-interests").style.display = "none";
			$("left-profile-interests-edit").style.display = "block";
			$("left-profile-interests-edit").focus();
		}
	},
	saveProfileKeywords: function() {
		var keys = $("left-profile-interests-input").value.trim().explode(",");
		var vars = { };
		for (var i=0; i<keys.length; i++)
			if (keys[i].trim())
				vars["keyword["+i+"]"] = keys[i].trim();
		request.post(prefix+"profile/savekeywords/", vars, function(rs) {
			$("left-profile-interests").innerHTML = rs.result;
			$("left-profile-interests").style.display = "";
			$("left-profile-interests-input").value = rs.clear;
			$("left-profile-interests-edit").style.display = "none";
		});
	}
}

var Filter = {
	filter: { },
	init: function(arr) {
		this.filter = arr;
	},
	textarea: function(name, title, value) {
		return this.text(name, title, value);
	},
	text: function(name, title, value, options, allowempty, allownonempty) {
		request.html("/admin/filter/text/?name="+name+"&title="+title+"&value="+value+(options?"&options="+options:"")+(allowempty?"&allowempty=1":"")+(allownonempty?"&allownonempty=1":""), function(h) {
			if (!$("filter")) {
				var div = document.createElement("div");
				div.id = "filter-body";
				$("root").appendChild(div);
			}
			dropAllElements($("filter-body"));
			$("filter-body").innerHTML = h;
			showandcenter($("filter-body"));
		});
	},
	date: function(name, title, value) {
		request.html("/admin/filter/date/?name="+name+"&title="+title+"&from="+value.from+"&to="+value.to, function(h) {
			if (!$("filter")) {
				var div = document.createElement("div");
				div.id = "filter-body";
				$("root").appendChild(div);
			}
			dropAllElements($("filter-body"));
			$("filter-body").innerHTML = h;
			showandcenter($("filter-body"));
		});
	},
	integer: function(name, title, value) {
		request.html("/admin/filter/integer/?name="+name+"&title="+title+"&from="+value.from+"&to="+value.to, function(h) {
			if (!$("filter")) {
				var div = document.createElement("div");
				div.id = "filter-body";
				$("root").appendChild(div);
			}
			dropAllElements($("filter-body"));
			$("filter-body").innerHTML = h;
			showandcenter($("filter-body"));
		});
	},
	choice: function(name, title, value, table, allowempty) {
		request.html("/admin/filter/choice/?name="+name+"&title="+title+"&value="+value+(table?"&table="+table:'')+(allowempty?"&allowempty=1":""), function(h) {
			if (!$("filter")) {
				var div = document.createElement("div");
				div.id = "filter-body";
				$("root").appendChild(div);
			}
			dropAllElements($("filter-body"));
			$("filter-body").innerHTML = h;
			showandcenter($("filter-body"));
		});
	},
	boolean: function(name, title, value) {
		request.html("/admin/filter/boolean/?name="+name+"&title="+title+"&value="+value, function(h) {
			if (!$("filter")) {
				var div = document.createElement("div");
				div.id = "filter-body";
				$("root").appendChild(div);
			}
			dropAllElements($("filter-body"));
			$("filter-body").innerHTML = h;
			showandcenter($("filter-body"));
		});
	},
	add: function(name, value, dontgo, nonempty) {
		if (typeof nonempty != "undefined" && nonempty)
			this.filter[name] = "-nonempty-";
		else
			this.filter[name] = value;
		if (!dontgo)
			this.redirect();
	},
	remove: function(name, dontgo) {
		var x = { };
		walk(this.filter, function(a,b) {
			if (a!=name)
				x[a] = b;
		});
		this.filter = x;
		if (!dontgo)
			this.redirect();
	},
	redirect: function() {
		var path = "";
		walk(this.filter, function(a,b) {
			path += (path?"&":"")+a+"="+b;
		});
		var idx = 1;
		while ($("filter-option-"+idx)) {
			if ($("filter-option-"+idx).checked)
				path += (path?"&":"?")+"option[]="+$("filter-option-"+idx).value;
			idx ++;
		}
		window.location = "./"+(path?"?":"")+path;
	},
	cancel: function() {
		$("filter-body").style.display = "none";
		dropAllElements($("filter-body"));
	}
};

var Keywords = {
	current: 0,
	copy: function(id, word) {
		this.current = id;
	},
	paste: function(id) {
		if (!this.current)
			return;
		window.location = "../move/?id="+this.current+"&to="+id;
	}
}

var Reason = {
	url: "",
	handle: null,
	oncancel: null,
	onsubmit: null,
	link: function(id, url, element, params) {
		this.url = url;
		this.handle = null;
		var popup = false;
		if (params && params.oncancel)
			this.oncancel = params.oncancel;
		if (params && params.onsubmit)
			this.onsubmit = params.onsubmit;
		if (!element) {
			if (!$("reason-dialog")) {
				var d = document.createElement("div");
				d.id = "reason-dialog";
				d.className = "dialog";
				$("root").appendChild(d);
			}
			element = $("reason-dialog");
			popup = true;
		}
		if (!popup) {
			dropAllElements(element);
			Tools.loading(element);
			Tools.alignColumns();
		}
		request.html(prefix+"reason/"+id+"/"+(popup?"popup/":"?element="+element.id+(params&&params.hidecancel?"&hc=1":"")), function(h) {
			if (!popup)
				Tools.stopLoading(element);
			dropAllElements(element);
			element.innerHTML = h;
			if (popup)
				showandcenter(element);
			else {
				element.style.display = "block";
				Tools.alignColumns();
			}
		});
	},
	callback: function(id, handle, element, params) {
		this.handle = handle;
		this.url = "";
		var popup = false;
		if (params && params.oncancel)
			this.oncancel = params.oncancel;
		if (params && params.onsubmit)
			this.onsubmit = params.onsubmit;
		if (!element) {
			if (!$("reason-dialog")) {
				var d = document.createElement("div");
				d.id = "reason-dialog";
				d.className = "dialog";
				$("root").appendChild(d);
			}
			element = $("reason-dialog");
			popup = true;
		}
		if (!popup) {
			dropAllElements(element);
			element.style.display = "block";
			Tools.loading(element);
			Tools.alignColumns();
		}
		request.html(prefix+"reason/"+id+"/"+(popup?"popup/":"?element="+element.id+(params&&params.hidecancel?"&hc=1":"")), function(h) {
			if (!popup)
				Tools.stopLoading(element);
			dropAllElements(element);
			element.innerHTML = h;
			if (popup)
				showandcenter(element);
			else {
				element.style.display = "block";
				Tools.alignColumns();
			}
		});
	},
	submit: function(id, element) {
		var value = $("reason-value-"+id).value;
		if (!element)
			element = $("reason-dialog");
		element.style.display = "none";
		dropAllElements(element);
		if (this.url)
			window.location = this.url+(-1<this.url.indexOf("?")?"&":"?")+"reason_id="+value+"&referer="+window.location;
		else if (this.handle)
			this.handle(value);
	},
	setValue: function(id, value) {
		var oldvalue = $("reason-value-"+id).value;
		if (oldvalue!=value) {
			$("reason-value-"+oldvalue+"-"+id).src = "/img/radio.gif";
			$("reason-value-"+value+"-"+id).src = "/img/radio-checked.gif";
			$("reason-value-"+id).value = value;
		}
	},
	cancel: function(id) {
		if (this.oncancel)
			this.oncancel();
		else {
			if (!id)
				id = "reason-dialog";
			$(id).style.display = "none";
			dropAllElements($(id));
		}
	}
};

var FAQ = {
	current: 0,
	copy: function(id) {
		this.current = id;
	},
	paste: function(id) {
		if (!this.current)
			return;
		window.location = "./move/?id="+this.current+"&section_id="+id;
	},
	switchSection: function(id) {
		var d = $("faq-section-"+id);
		d.style.display = d.style.display=="none" ? "block" : "none";
		Tools.alignColumns();
	},
	switchEntry: function(id) {
		var d = $("faq-entry-"+id);
		d.style.display = d.style.display=="block" ? "none" : "block";
		Tools.alignColumns();
	}
};

var Contact = {
	switchto: function(type) {
		request.html(prefix+"admin/contact/schedulemessages/?type="+type, function(h) {
			$("message-container").innerHTML = h;
		});
//		$("dt-container").style.display = type=="email" ? "" : "none";
	},
	addRecipient: function() {
		for (var i=0; i<3; i++) {
			var tr = document.createElement("tr");
			var td1 = document.createElement("td");
			td1.innerHTML = "<input type=\"text\" name=\"first_name[]\"/>";
			var td2 = document.createElement("td");
			td2.innerHTML = "<input type=\"text\" name=\"last_name[]\"/>";
			var td3 = document.createElement("td");
			td3.innerHTML = "<input type=\"text\" name=\"email[]\"/>";
			tr.appendChild(td1);
			tr.appendChild(td2);
			tr.appendChild(td3);
			$("table").insertBefore(tr, $("before"));
		}
	}
};

var SendToFriend = {
	group: function(id) {
		this.dialog('group',id);
	},
	item: function(id) {
		this.dialog('item',id);
	},
	dialog: function(entity,id) {
		if ($("vcard-actions-"+id).innerHTML) {
			dropAllElements($("vcard-actions-"+id));
		} else {
			dropAllElements($("vcard-actions-"+id));
			$("vcard-actions-"+id).style.display = "block";
			Tools.loading($("vcard-actions-"+id));
			Tools.alignColumns();
			request.html(prefix+"sendtofriend/"+entity+"/"+id+"/", function(h) {
				Tools.stopLoading($("vcard-actions-"+id));
				$("vcard-actions-"+id).innerHTML = h;
				Tools.alignColumns();
			});
		}
	},
	send: function(params) {
		var id = params.group ? params.group : params.item;
		var vars = {
			group: params.group,
			item: params.item,
			myname: $("sendtofriend-myname").value,
			hisname: $("sendtofriend-hisname").value,
			hisemail: $("sendtofriend-hisemail").value,
			captcha: $("sendtofriend-captcha").value
		};
		dropAllElements($("vcard-actions-"+id));
		Tools.loading($("vcard-actions-"+id));
		request.posthtml(prefix+"sendtofriend/send/", vars, function(h) {
			Tools.stopLoading($("vcard-actions-"+id));
			$("vcard-actions-"+id).innerHTML = h;
			if ($("sendtofriend-thanks"))
				setTimeout(function() { SendToFriend.cancel(id); }, TIMEOUT);
		});
	},
	cancel: function(id) {
		$("vcard-actions-"+id).style.display = "none";
		dropAllElements($("vcard-actions-"+id));
	}
};

var Slideshow = {
	list: [],
	index: 0,
	active: false,
	start: function(entity, id) {
		if (entity!="user" && entity!="item" && entity!="group")
			return;
		if (!$("slideshow-blocker")) {
			var d = document.createElement("div");
			d.id = "slideshow-blocker";
			$("root").appendChild(d);
		}
		if (!$("slideshow-body")) {
			var d = document.createElement("div");
			d.id = "slideshow-body";
			$("root").appendChild(d);
			
			var im = document.createElement("div");
			im.id = "slideshow-image";
			d.appendChild(im);
			
			var ct = document.createElement("div");
			ct.id = "slideshow-controls";
			d.appendChild(ct);
		}
		this.blocker(true);
		Tools.loading($("slideshow-image"), 400, 400, true);
		request.json(prefix+"slideshow/"+entity+"/"+parseInt(id)+"/", function(rs) {
			Slideshow.list = rs;
			Slideshow.show(0);
		});
	},
	next: function() {
		this.index ++;
		if (this.index>=this.list.length)
			this.index = 0;
		this.show(this.index);
	},
	previous: function() {
		this.index --;
		if (this.index<0)
			this.index = this.list.length-1;
		this.show(this.index);
	},
	show: function(idx) {
		this.index = idx;
		if (!this.list || !this.list[idx])
			return;
		this.blocker(true);
		Tools.loading($("slideshow-image"), 400, 400, true);
		var i = new Image();
		i.onload = function() {
			$("slideshow-image").style.background = "url("+this.src+") center no-repeat";
		}
		i.src = this.list[idx];
		this.navigation();
		showandcenter($("slideshow-body"));
	},
	close: function() {
		this.blocker(false);
		$("slideshow-image").style.background = "transparent";
		$("slideshow-body").style.display = "none";
	},
	blocker: function(mode) {
		if (!$("slideshow-blocker"))
			return;
		if (mode) {
			var height = Math.max($("root").clientHeight, document.documentElement.clientHeight);
			$("slideshow-blocker").style.left = "0px";
			$("slideshow-blocker").style.top = "0px";
			$("slideshow-blocker").style.width = $("root").clientWidth+"px";
			$("slideshow-blocker").style.height = (height+20)+"px";
			$("slideshow-blocker").style.display = "block";
		} else
			$("slideshow-blocker").style.display = "none";
	},
	navigation: function() {
		var html = "<div id=\"slideshow-control-close\"><a href=\"javascript:void(0)\" onclick=\"Slideshow.close();return false;\">Close</a></div>";
		if (this.list.length>1) {
			var start = this.index - 5;
			var finish = this.index + 5;
			if (start<0) {
				finish -= start;
				if (finish>this.list.length-1)
					finish = this.list.length-1;
				start = 0;
			}
			if (finish>this.list.length-1) {
				start -= finish-this.list.length;
				finish = this.list.length-1;
				if (start<0)
					start = 0;
			}
			html += "<ul><li><a href=\"javascript:void(0)\" onclick=\"Slideshow.previous();return false;\">&larr;</a></li>";
			for (var i=start;i<=finish;i++)
				html += "<li><a "+(i==this.index?"class=\"active\" ":"")+"href=\"javascript:void(0)\" onclick=\"Slideshow.show("+i+");return false;\">"+(i+1)+"</a></li>";
			html += "<li><a href=\"javascript:void(0)\" onclick=\"Slideshow.next();return false;\">&rarr;</a></li></ul>";
		}
		dropAllElements($("slideshow-controls"));
		$("slideshow-controls").innerHTML = html;
	}
}

var ImageUpload = {
	maxid: 0,
	info: { },
	width: 146,
	height: 146,
	currentdflt: 0,
	defid: 0,
	defimage: "",
	zoom: { },
	rotate: { },
	dflt: function(x, id, image) {
		image = image.rplace("/tmp/items/", "").rplace("-small.jpg", "");
		if (this.currentdflt)
			removeClassName($("oldimage-"+this.currentdflt), "uploaded-image-thumb-default");
		this.currentdflt = id;
		addClassName($("oldimage-"+id), "uploaded-image-thumb-default");
		
		this.defimage = image;
		this.defid = id;
		
		$("iu-default-image").value = this.defimage;
		$("iu-default-id").value = this.defid;
	},
	fakeCreate: function(container, form, name, type, multiimage) {
		var id = ++this.maxid;
		this.info[id] = { container: container, form: form, name: name, type: type, multiimage: multiimage };
		return id;
	},
	create: function(container, form, name, type, multiimage) {
		var id = ++this.maxid;
		this.info[id] = { container: container, form: form, name: name, type: type, multiimage: multiimage };
		var html = "<div class=\"image-upload\">"+
						"<div class=\"image-upload-container\" id=\"image-upload-container-"+id+"\">"+
							"<img src=\"about:blank\" class=\"image-upload-image\" id=\"image-upload-image-"+id+"\" onmousedown=\"ImageUpload.dragOn('"+id+"',this,event);return false;\"/>"+
							"<a class=\"image-upload-link-upload\" id=\"image-upload-link-upload-"+id+"\" href=\"javascript:void(0)\" onclick=\"ImageUpload.start("+id+");this.blur();return false;\"><img src=\"/img/importjpg.gif\" border=\"0\"/>"+dictionary["IMAGE_UPLOAD_LABEL"]+"</a>"+
							"<div class=\"image-upload-controls\" id=\"image-upload-controls-"+id+"\">"+
								"<a href=\"javascript:void(0)\" id=\"image-upload-control-left-"+id+"\" class=\"image-upload-control-left\" onclick=\"ImageUpload.left("+id+");this.blur();return false;\"><img src=\"/img/arrow-left.png\" border=\"0\"/></a>"+
								"<a href=\"javascript:void(0)\" id=\"image-upload-control-right-"+id+"\" class=\"image-upload-control-right\" onclick=\"ImageUpload.right("+id+");this.blur();return false;\"><img src=\"/img/arrow-right.png\" border=\"0\"/></a>"+
								"<a href=\"javascript:void(0)\" id=\"image-upload-control-up-"+id+"\" class=\"image-upload-control-up\" onclick=\"ImageUpload.up("+id+");this.blur();return false;\"><img src=\"/img/arrow-up.png\" border=\"0\"/></a>"+
								"<a href=\"javascript:void(0)\" id=\"image-upload-control-down-"+id+"\" class=\"image-upload-control-down\" onclick=\"ImageUpload.down("+id+");this.blur();return false;\"><img src=\"/img/arrow-down.png\" border=\"0\"/></a>"+
								"<a href=\"javascript:void(0)\" id=\"image-upload-control-zoomin-"+id+"\" class=\"image-upload-control-zoomin\" onclick=\"ImageUpload.zoomin("+id+");this.blur();return false;\"><img src=\"/img/zoom-in.png\" border=\"0\"/></a>"+
								"<a href=\"javascript:void(0)\" id=\"image-upload-control-zoomout-"+id+"\" class=\"image-upload-control-zoomout\" onclick=\"ImageUpload.zoomout("+id+");this.blur();return false;\"><img src=\"/img/zoom-out.png\" border=\"0\"/></a>"+
								"<a href=\"javascript:void(0)\" id=\"image-upload-control-turncw-"+id+"\" class=\"image-upload-control-turncw\" onclick=\"ImageUpload.turncw("+id+");this.blur();return false;\"><img src=\"/img/turn-cw.png\" border=\"0\"/></a>"+
								"<a href=\"javascript:void(0)\" id=\"image-upload-control-turnccw-"+id+"\" class=\"image-upload-control-turnccw\" onclick=\"ImageUpload.turnccw("+id+");this.blur();return false;\"><img src=\"/img/turn-ccw.png\" border=\"0\"/></a>"+
								"<a href=\"javascript:void(0)\" class=\"image-upload-control-ok\" onclick=\"ImageUpload.save("+id+");this.blur();return false;\"><img src=\"/img/zoom.png\" border=\"0\"/></a>"+
							"</div>"+
						"</div>"+
					"</div>"+
					"<div class=\"image-upload-input\" id=\"image-upload-input-"+id+"\">"+
						"<form name=\"upl-"+id+"\" action=\""+prefix+"imageupload/upload/\" method=\"post\" enctype=\"multipart/form-data\" target=\"hiddenframe\">"+
						"<input type=\"hidden\" name=\"type\" value=\""+type+"\"/>"+
						"<input type=\"hidden\" name=\"name\" value=\""+name+"\"/>"+
						"<input type=\"hidden\" name=\"form\" value=\""+form+"\"/>"+
						"<input type=\"hidden\" name=\"id\" value=\""+id+"\"/>"+
						"<input id=\"image-input\" type=\"file\" name=\"image\" size=\"8\" onchange=\"ImageUpload.upload("+id+");\" style=\"width:100%\"/>"+
						"</form>"+
					"</div>";
		var d = document.createElement("div");
		d.id = "image-upload-"+id;
		d.innerHTML = html;
		$(container).appendChild(d);
		
		this.zoom[id] = 1;
		
		$("image-upload-link-more").style.display = "none";
		Tools.alignColumns();
		return id;
	},
	dragOn: function(id, img, evt) {
		if (!evt)
			evt = window.event;
		img.setAttribute("x", evt.clientX);
		img.setAttribute("y", evt.clientY);
		document.onmousemove = function(e) {
			if (!e)
				e = window.event;
			var dx = parseInt(e.clientX)-parseInt(img.getAttribute("x"));
			var dy = parseInt(e.clientY)-parseInt(img.getAttribute("y"));
			img.setAttribute("x", e.clientX);
			img.setAttribute("y", e.clientY);
			
			ImageUpload.info[id].dx = img.offsetLeft+dx;
			ImageUpload.info[id].dy = img.offsetTop+dy;

			if (ImageUpload.info[id].dx < ImageUpload.width-ImageUpload.info[id].prewidth*ImageUpload.zoom[id])
				ImageUpload.info[id].dx = ImageUpload.width-ImageUpload.info[id].prewidth*ImageUpload.zoom[id];
			if (ImageUpload.info[id].dx > 0)
				ImageUpload.info[id].dx = 0;
			if (ImageUpload.info[id].dy < ImageUpload.height-ImageUpload.info[id].preheight*ImageUpload.zoom[id])
				ImageUpload.info[id].dy = ImageUpload.height-ImageUpload.info[id].preheight*ImageUpload.zoom[id];
			if (ImageUpload.info[id].dy > 0)
				ImageUpload.info[id].dy = 0;
	
			img.style.left = ImageUpload.info[id].dx+"px";
			img.style.top = ImageUpload.info[id].dy+"px";
			
			try { e.cancelBubble = true; } catch (e) { }
			try { e.stopPropagation(); } catch (e) { }
			return false;
		}
		document.onmouseup = function(e) {
			document.onmousemove = function() { }
			document.onmouseup = function() { }
		}
	},
	start: function(id) {
		if ($("image-upload-input-"+id).style.display=="block")
			$("image-upload-input-"+id).style.display = "none";
		else
			$("image-upload-input-"+id).style.display = "block";
	},
	upload: function(id) {
		document.forms["upl-"+id].submit();
		$("image-upload-link-upload-"+id).style.display = "none";
		$("image-upload-container-"+id).style.backgroundImage = "url(/img/uploading.gif)";
	},
	edit: function(id, preview, image, prewidth, preheight, width, height) {
		this.info[id].image = image;
		this.info[id].preview = preview;
		this.info[id].prewidth = prewidth;
		this.info[id].preheight = preheight;
		this.info[id].width = width;
		this.info[id].height = height;
		this.info[id].dx = ((this.width-prewidth)/2);
		this.info[id].dy = ((this.height-preheight)/2);
		this.zoom[id] = 1;

		$("image-upload-link-upload-"+id).style.display = "none";

		$("image-upload-container-"+id).style.background = "transparent";
		$("image-upload-image-"+id).src = preview;
		$("image-upload-image-"+id).style.left = this.info[id].dx+"px";
		$("image-upload-image-"+id).style.top = this.info[id].dy+"px";
		$("image-upload-image-"+id).style.display = "block";
		$("image-upload-controls-"+id).style.display = "block";
		$("image-upload-input-"+id).style.display = "none";
		
		if (false && this.info[id].dx==0) {
			$("image-upload-control-left-"+id).style.display = "none";
			$("image-upload-control-right-"+id).style.display = "none";
			$("image-upload-control-up-"+id).style.display = "block";
			$("image-upload-control-down-"+id).style.display = "block";
		} else if (false) {
			$("image-upload-control-left-"+id).style.display = "block";
			$("image-upload-control-right-"+id).style.display = "block";
			$("image-upload-control-up-"+id).style.display = "none";
			$("image-upload-control-down-"+id).style.display = "none";
		}

		var i1 = document.createElement("input");
		i1.type = "hidden";
		i1.id = "iu-"+this.info[id].name;
		i1.name = this.info[id].name;
		i1.value = this.info[id].image;
		document.forms[this.info[id].form].appendChild(i1);
		
		if ($("saved-images"))
			request.html(prefix+"imageupload/thumb/?id="+id+"&image="+image, function(h) {
				var x = $("saved-images").innerHTML;
				dropAllElements($("saved-images"));
				$("saved-images").innerHTML = x+h;
				Tools.alignColumns();
			});
	},
	left: function(id, amt) {
		this.info[id].dx = parseFloat(this.info[id].dx)-1;//--;
		if (this.info[id].dx < this.width-this.info[id].prewidth*ImageUpload.zoom[id])
			this.info[id].dx = this.width-this.info[id].prewidth*ImageUpload.zoom[id];
		$("image-upload-image-"+id).style.left = this.info[id].dx+"px";
		$("image-upload-image-"+id).style.top = this.info[id].dy+"px";
	},
	right: function(id) {
		this.info[id].dx ++;
		if (this.info[id].dx > 0)
			this.info[id].dx = 0;
		$("image-upload-image-"+id).style.left = this.info[id].dx+"px";
		$("image-upload-image-"+id).style.top = this.info[id].dy+"px";
	},
	up: function(id) {
		this.info[id].dy --;
		if (this.info[id].dy < this.height-this.info[id].preheight*this.zoom[id])
			this.info[id].dy = this.height-this.info[id].preheight*ImageUpload.zoom[id];
		$("image-upload-image-"+id).style.left = this.info[id].dx+"px";
		$("image-upload-image-"+id).style.top = this.info[id].dy+"px";
	},
	down: function(id) {
		this.info[id].dy ++;
		if (this.info[id].dy > 0)
			this.info[id].dy = 0;
		$("image-upload-image-"+id).style.left = this.info[id].dx+"px";
		$("image-upload-image-"+id).style.top = this.info[id].dy+"px";
	},
	zoomin: function(id) {
		this.zoom[id] = parseFloat(this.zoom[id])+0.1;
		if (this.zoom[id]>100)
			this.zoom[id] = 100;
		
		this.info[id].dx = -Math.abs(this.width-this.info[id].prewidth*this.zoom[id])/2;
		this.info[id].dy = -Math.abs(this.height-this.info[id].preheight*this.zoom[id])/2;

		$("image-upload-image-"+id).style.left = this.info[id].dx+"px";
		$("image-upload-image-"+id).style.top = this.info[id].dy+"px";
		
		$("image-upload-image-"+id).style.width = (this.info[id].prewidth * this.zoom[id])+"px";
		$("image-upload-image-"+id).style.height = (this.info[id].preheight * this.zoom[id])+"px";
	},
	zoomout: function(id) {
		if (this.info[id].prewidth * (this.zoom[id]-0.1) < this.width)
			return;
		if (this.info[id].preheight * (this.zoom[id]-0.1) < this.height)
			return;
		this.zoom[id] = parseFloat(this.zoom[id])-0.1;
		if (this.zoom[id]<0.1)
			this.zoom[id] = 0.1;
		
		this.info[id].dx = -Math.abs(this.width-this.info[id].prewidth*this.zoom[id])/2;
		this.info[id].dy = -Math.abs(this.height-this.info[id].preheight*this.zoom[id])/2;

		$("image-upload-image-"+id).style.left = this.info[id].dx+"px";
		$("image-upload-image-"+id).style.top = this.info[id].dy+"px";

		$("image-upload-image-"+id).style.width = (this.info[id].prewidth * this.zoom[id])+"px";
		$("image-upload-image-"+id).style.height = (this.info[id].preheight * this.zoom[id])+"px";
	},
	turncw: function(id) {
		request.post(prefix+"imageupload/rotate/", { file: this.info[id].image, dir: "cw" }, function(rs) {
			if (rs.result=="ok") {
				var x;
				
				x = parseInt($("image-upload-image-"+id).style.left);
				$("image-upload-image-"+id).style.left = parseInt($("image-upload-image-"+id).style.top)+"px";
				$("image-upload-image-"+id).style.top = x+"px";

				x = parseInt($("image-upload-image-"+id).clientWidth);
				$("image-upload-image-"+id).style.width = parseInt($("image-upload-image-"+id).clientHeight)+"px";
				$("image-upload-image-"+id).style.height = x+"px";

				$("image-upload-image-"+id).src = "/img/px.gif";

				x = ImageUpload.info[id].dx;
				ImageUpload.info[id].dx = ImageUpload.info[id].dy;
				ImageUpload.info[id].dy = x;
				
				x = ImageUpload.info[id].prewidth;
				ImageUpload.info[id].prewidth = ImageUpload.info[id].preheight;
				ImageUpload.info[id].preheight = x;
				
				x = ImageUpload.info[id].width;
				ImageUpload.info[id].width = ImageUpload.info[id].height;
				ImageUpload.info[id].height = x;
				
				$("image-upload-image-"+id).src = "/tmp/items/"+ImageUpload.info[id].image+"?"+Math.random();
			}
		});
	},
	turnccw: function(id) {
		request.post(prefix+"imageupload/rotate/", { file: this.info[id].image, dir: "ccw" }, function(rs) {
			if (rs.result=="ok") {
				var x;
				
				x = parseInt($("image-upload-image-"+id).style.left);
				$("image-upload-image-"+id).style.left = parseInt($("image-upload-image-"+id).style.top)+"px";
				$("image-upload-image-"+id).style.top = x+"px";

				x = parseInt($("image-upload-image-"+id).clientWidth);
				$("image-upload-image-"+id).style.width = parseInt($("image-upload-image-"+id).clientHeight)+"px";
				$("image-upload-image-"+id).style.height = x+"px";

				$("image-upload-image-"+id).src = "/img/px.gif";

				x = ImageUpload.info[id].dx;
				ImageUpload.info[id].dx = ImageUpload.info[id].dy;
				ImageUpload.info[id].dy = x;
				
				x = ImageUpload.info[id].prewidth;
				ImageUpload.info[id].prewidth = ImageUpload.info[id].preheight;
				ImageUpload.info[id].preheight = x;
				
				x = ImageUpload.info[id].width;
				ImageUpload.info[id].width = ImageUpload.info[id].height;
				ImageUpload.info[id].height = x;
				
				$("image-upload-image-"+id).src = "/tmp/items/"+ImageUpload.info[id].image+"?"+Math.random();
			}
		});
	},
	save: function(id) {
		var i2 = document.createElement("input");
		i2.type = "hidden";
		i2.id = "iu-crop_x_"+this.info[id].name;
		i2.name = "crop_x_"+this.info[id].name;
		i2.value = Math.round(Math.abs(this.info[id].dx*(this.info[id].width/this.info[id].prewidth)));
		document.forms[this.info[id].form].appendChild(i2);
		
		var i3 = document.createElement("input");
		i3.type = "hidden";
		i3.id = "iu-crop_y_"+this.info[id].name;
		i3.name = "crop_y_"+this.info[id].name;
		i3.value = Math.round(Math.abs(this.info[id].dy*(this.info[id].height/this.info[id].preheight)));
		document.forms[this.info[id].form].appendChild(i3);
		
		var i4 = document.createElement("input");
		i4.type = "hidden";
		i4.id = "iu-zoom_"+this.info[id].name;
		i4.name =" zoom_"+this.info[id].name;
		i4.value = this.zoom[id];
		document.forms[this.info[id].form].appendChild(i4);
		
		$("image-upload-controls-"+id).style.display = "none";
		if (this.info[id].multiimage)
			$("image-upload-link-more").style.display = "block";
			
		if ($("oldimage-"+id)) {
			$("oldimage-"+id).style.display = "block";
			dropAllElements($(this.info[id].container));
			this.create(this.info[id].container, this.info[id].form, this.info[id].name, this.info[id].type, this.info[id].multiimage);
			$("saved-images-hint").style.display = "block";
			Tools.alignColumns();
		
			request.html(prefix+"imageupload/thumb/?id="+id+"&image="+this.info[id].image+"&x="+i2.value+"&y="+i3.value+"&zoom="+i4.value, function(h) {
				dropElement($("oldimage-"+id));
				var x = $("saved-images").innerHTML;
				dropAllElements($("saved-images"));
				$("saved-images").innerHTML = x+h;
				$("oldimage-"+id).style.display = "block";
				Tools.alignColumns();
			});
		}

		Tools.alignColumns();
	},
	prefill: function(id, image, folder, ratio, crop_x, crop_y, multiimage) {
		this.info[id].image = image;
		this.info[id].preview = image+"-small.jpg";
		this.info[id].dx = -1*Math.abs(Math.round(crop_x/ratio));
		this.info[id].dy = -1*Math.abs(Math.round(crop_y/ratio));
		if (multiimage) {
			request.html(prefix+"imageupload/thumb/?id="+id+"&image="+image, function(h) {
				var x = $("saved-images").innerHTML;
				dropAllElements($("saved-images"));
				$("saved-images").innerHTML = x+h;
				$("oldimage-"+id).style.display = "block";
				$("saved-images-hint").style.display = "block";
				Tools.alignColumns();
			});
		} else {
			$("image-upload-link-upload-"+id).style.display = "none";
			$("image-upload-container-"+id).style.background = "transparent";
			$("image-upload-image-"+id).src = folder+image+"-small.jpg";
			$("image-upload-image-"+id).style.left = this.info[id].dx+"px";
			$("image-upload-image-"+id).style.top = this.info[id].dy+"px";
			$("image-upload-image-"+id).style.display = "block";
			$("image-upload-input-"+id).style.display = "none";
		}

		var i1 = document.createElement("input");
		i1.type = "hidden";
		i1.id = "iu-"+this.info[id].name;
		i1.name = this.info[id].name;
		i1.value = this.info[id].image;
		document.forms[this.info[id].form].appendChild(i1);

		if (ratio) {
			var i2 = document.createElement("input");
			i2.type = "hidden";
			i2.id = "iu-crop_x_"+this.info[id].name;
			i2.name = "crop_x_"+this.info[id].name;
			i2.value = crop_x;
			document.forms[this.info[id].form].appendChild(i2);
		
			var i3 = document.createElement("input");
			i3.type = "hidden";
			i3.id = "iu-crop_y_"+this.info[id].name;
			i3.name = "crop_y_"+this.info[id].name;
			i3.value = crop_y;
			document.forms[this.info[id].form].appendChild(i3);
		}

//		if (multiimage)
//			$("image-upload-link-more").style.display = "block";

		Tools.alignColumns();
	}
};

var Radiobutton = {
	cache: { },
	create: function(el, name, value, checked, onclick, immediate) {
		if (onclick) {
			if (!this.cache[name])
				this.cache[name] = { };
			this.cache[name][value] = onclick;
		}
		var func = function() {
			dropAllElements(el);
			var html = "<a id=\"custom-radio-"+name+"-"+value+"\" class=\"custom-radio"+(checked?" custom-radio-checked":"")+"\" onclick=\"Radiobutton.click('"+name+"','"+value+"');this.blur();return false;\">&nbsp;</a>";
			if (name && !$("custom-radio-"+name))
				html += "<input type=\"hidden\" name=\""+name+"\" id=\"custom-radio-"+name+"\" value=\""+(checked?value:"")+"\"/>";
			if (checked && name && $("custom-radio-"+name))
				$("custom-radio-"+name).value = value;
			el.innerHTML = html;
		};
		if (immediate)
			func();
		else
			window.queueload.push(func);
	},
	click: function(name, value) {
		if ($("custom-radio-"+name)) {
			var oldvalue = $("custom-radio-"+name).value;
			if (oldvalue!=value) {
				if ($("custom-radio-"+name+"-"+oldvalue))
					$("custom-radio-"+name+"-"+oldvalue).className = "custom-radio";
				$("custom-radio-"+name).value = value;
				$("custom-radio-"+name+"-"+value).className = "custom-radio custom-radio-checked";
			}
			if (this.cache[name] && this.cache[name][value])
				this.cache[name][value]();
		}
	}
}

var Checkbox = {
	cache: { },
	disabled: function(el) {
		dropAllElements(el);
		el.innerHTML = "<a class=\"custom-radio custom-radio-disabled\">&nbsp;</a>";
	},
	create: function(el, name, value, checked, onclick, immediate) {
		if (onclick) {
			if (!this.cache[name])
				this.cache[name] = { };
			this.cache[name][value] = onclick;
		}
		var func = function() {
			dropAllElements(el);
			var safevalue = Checkbox.safe(value);
			var html = "<a id=\"custom-radio-handle-"+name+"-"+safevalue+"\" class=\"custom-radio"+(checked?" custom-radio-checked":"")+"\" onclick=\"Checkbox.click('"+name+"','"+value+"');this.blur();return false;\">&nbsp;</a>";
			if (name && !$("custom-radio-"+name))
				html += "<input type=\"hidden\" name=\""+name+"\" id=\"custom-checkbox-"+name+"-"+safevalue+"\""+(checked?" value=\""+value+"\"":"")+" class=\""+value+"\"/>";
			el.innerHTML = html;
		};
		if (immediate)
			func();
		else
			window.queueload.push(func);
	},
	click: function(name, value) {
		var safevalue = this.safe(value);
		var el = $("custom-checkbox-"+name+"-"+safevalue);
		if (el) {
			if (el.value) {
				el.value = "";
				removeClassName($("custom-radio-handle-"+name+"-"+safevalue), "custom-radio-checked");
			} else {
				el.value = el.className;
				addClassName($("custom-radio-handle-"+name+"-"+safevalue), "custom-radio-checked");
			}
		}
		if (this.cache[name] && this.cache[name][value])
			this.cache[name][value]();
	},
	safe: function(str) {
		return escape(str);
		return str.replace(/\W/, "");
	}
}

var Dropdown = {
	cache: { },
	callbacks: { },
	disabled: { },
	drag: { },
	dragy: 0,
	dragremainder: 0,
	scrollpos: { },
	scroll: function(name, by) {
		var curr = isNaN(parseInt($("custom-dropdown-table-"+name).style.top)) ? 0 : parseInt($("custom-dropdown-table-"+name).style.top);
		var hgt = $("custom-dropdown-table-"+name).clientHeight;
		
		if (curr + by > 0)
			return;
		if (curr + by + hgt - 212 < 0)
			return;
		var newpos = curr + by;
		
		var indicator = -(newpos/(hgt-212))*172;
		this.scrollpos[name] = -indicator;
		$("custom-dropdown-scrollbar-indicator-"+name).style.backgroundPosition = "0px "+indicator+"px";

		$("custom-dropdown-table-"+name).style.top = (curr + by)+"px";
	},
	startDrag: function(name, event) {
		this.drag[name] = true;
		if (!event)
			event = window.event;
		this.dragy = event.clientY;
		document.onmousemove = function(ev) {
			if (!ev)
				ev = window.event;
			var indidiff = Dropdown.dragy - ev.clientY;
			
			var newipos = (Dropdown.scrollpos[name]?Dropdown.scrollpos[name]:0) + indidiff;
			if (newipos>0 || newipos<-172)
				return;
			
			$("custom-dropdown-scrollbar-indicator-"+name).style.backgroundPosition = "0px "+(-newipos)+"px";
			Dropdown.scrollpos[name] = newipos;

			var hgt = $("custom-dropdown-table-"+name).clientHeight;
			var newpos = ((hgt-212)*newipos)/172;

			$("custom-dropdown-table-"+name).style.top = newpos+"px";

			Dropdown.dragy = ev.clientY;
			Tools.deselect();
			return false;
		}
		document.onmouseup = function() { Dropdown.stopDrag(name); }
	},
	stopDrag: function(name) {
		this.drag[name] = false;
		document.onmousemove = function() { }
	},
	create: function(el, name, values, defidx, immediate) {
		if (!values[defidx])
			defidx = 0;
		this.cache[name] = values;
		var func = function() {
			if (typeof debugmode != 'undefined' && debugmode)
				alert(name);
			dropAllElements(el);
			var html = "";
			html += "<div class=\"custom-dropdown-handle\" id=\"custom-dropdown-handle-"+name+"\"><a id=\"custom-dropdown-handle-help-"+name+"\" href=\"javascript:void(0)\" class=\"custom-dropdown-handle-help\" onclick=\"Dropdown.drop('"+name+"');this.blur();return false;\">"+(Dropdown.disabled[name]?"&nbsp;&nbsp;&nbsp;&nbsp;":values[defidx].text)+"</a></div>";
			html += "<input type=\"hidden\" id=\"custom-dropdown-"+name+"\" name=\""+name+"\" value=\""+(Dropdown.disabled[name]?"":values[defidx].value)+"\"/>";
			html += "<div id=\"custom-dropdown-dropdown-"+name+"\" class=\"custom-dropdown-dropdown\"><div class=\"custom-dropdown-dropdown-help\">";
			if (values.length>12) {
				html += "<div class=\"custom-dropdown-scrollbar\">";
				html += "<a class=\"custom-dropdown-scrollbar-up\" href=\"javascript:void(0)\" onmousedown=\"this.to=setInterval(function(){Dropdown.scroll('"+name+"',10);},100);this.blur();\" onmouseup=\"if(this.to)clearTimeout(to)\" onclick=\"Dropdown.scroll('"+name+"',10);this.blur();return false;\"></a>";
				html += "<div id=\"custom-dropdown-scrollbar-indicator-"+name+"\" class=\"custom-dropdown-scrollbar-indicator\" onmousedown=\"Dropdown.startDrag('"+name+"',event)\" onmouseup=\"Dropdown.stopDrag('"+name+"')\"></div>";
				html += "<a class=\"custom-dropdown-scrollbar-down\" href=\"javascript:void(0)\" onmousedown=\"this.to=setInterval(function(){Dropdown.scroll('"+name+"',-10);},100);this.blur();\" onmouseup=\"if(this.to)clearTimeout(to)\" onclick=\"Dropdown.scroll('"+name+"',-10);this.blur();return false;\"></a>";
				html += "</div>";
				html += "<div class=\"custom-dropdown-dropdown-crop\" id=\"custom-dropdown-dropdown-crop-"+name+"\">";
			}
			html += "<table id=\"custom-dropdown-table-"+name+"\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
			for (var i=0; i<values.length; i++) {
				html += "<tr>"+
							"<td><a href=\"javascript:void(0)\" onclick=\"Dropdown.select('"+name+"',"+i+");this.blur();return false;\"><img id=\"custom-dropdown-radio-"+name+"-"+i+"\" src=\"/img/radio"+(defidx==i?"-checked":"")+".gif\" border=\"0\"/></a></td>"+
							"<td><a href=\"javascript:void(0)\" onclick=\"Dropdown.select('"+name+"',"+i+");this.blur();return false;\">"+values[i].text+"</a></td>"+
						"</tr>";
			}
			html += "</table>";
			if (values.length>12)
				html += "</div>";
			html += "</div></div>";
			el.innerHTML = html;

			var xparams = el.getAttribute("rel");
			var params = { };
			if (xparams) {
				xparams = xparams.explode(",");
				for (var i=0; i<xparams.length; i++) {
					xparams[i] = xparams[i].explode("=");
					params[xparams[i][0]] = xparams[i][1];
				}
			}
			if (params.width) {
				$("custom-dropdown-handle-help-"+name).style.width = params.width+"px";
				$("custom-dropdown-handle-help-"+name).setAttribute("x-width", params.width);
			}
		}
		if (immediate)
			func();
		else
			window.queueload.push(func);
		return {
			name: name,
			onchange: function(func) {
				Dropdown.callbacks[this.name] = func;
			},
			disabled: function(value) {
				Dropdown.disabled[this.name] = value;
				if ($("custom-dropdown-handle-help-"+this.name))
					$("custom-dropdown-handle-help-"+this.name).innerHTML = value ? Dropdown.cache[this.name][0].text : "&nbsp;&nbsp;&nbsp;&nbsp;";
				if ($("custom-dropdown-"+this.name))
					$("custom-dropdown-"+this.name).value = value ? Dropdown.cache[this.name][0].value : "";
			}
		}
	},
	disable: function(name, value) {
		this.disabled[name] = value;
		if ($("custom-dropdown-handle-help-"+name))
			$("custom-dropdown-handle-help-"+name).innerHTML = value ? this.cache[name][0].text : "&nbsp;&nbsp;&nbsp;&nbsp;";
		if ($("custom-dropdown-"+name))
			$("custom-dropdown-"+name).value = value ? this.cache[name][0].value : "";
	},
	drop: function(name) {
		if (this.disabled[name])
			return;
		var dd = $("custom-dropdown-dropdown-"+name);
		var dh = $("custom-dropdown-handle-"+name);
		var dhh = $("custom-dropdown-handle-help-"+name);
		dd.style.display = "block";
		dd.onmousedown = function(e) {
			if (window.event)
				window.event.cancelBubble = true;
			if (e && e.stopPropagation)
				e.stopPropagation();
		}
		document.onmousedown = function() {
			dd.style.display = "none";
			document.onmousedown = function() { }
			dhh.style.width = dhh.getAttribute("x-width") ? dhh.getAttribute("x-width")+"px" : "auto";
		}

		dd.style.width = "100px";
		var width = Math.max(dh.clientWidth, dd.clientWidth);
		dhh.style.width = (width-25)+"px";
		dd.style.width = width+"px";
	},
	select: function(name, idx) {
		var oldvalue = $("custom-dropdown-"+name).value;
		if (oldvalue!=this.cache[name][idx].value) {
			for (var i=0; i<this.cache[name].length; i++)
				if (this.cache[name][i].value==oldvalue) {
					oldidx = i;
					break;
				}
			$("custom-dropdown-radio-"+name+"-"+oldidx).src = "/img/radio.gif";
			$("custom-dropdown-"+name).value = this.cache[name][idx].value;
			$("custom-dropdown-handle-help-"+name).innerHTML = this.cache[name][idx].text;
			$("custom-dropdown-radio-"+name+"-"+idx).src = "/img/radio-checked.gif";
			if (this.callbacks[name])
				this.callbacks[name](this.cache[name][idx].value);
		}
		this.hide(name);
	},
	selectvalue: function(name, val) {
		for (var i=0; i<this.cache[name].length; i++) {
			if (this.cache[name][i].value==val) {
				this.select(name, i);
				return;
			}
		}
	},
	hide: function(name) {
		$("custom-dropdown-handle-help-"+name).style.width = $("custom-dropdown-handle-help-"+name).getAttribute("x-width") ? $("custom-dropdown-handle-help-"+name).getAttribute("x-width")+"px" : "auto";
		$("custom-dropdown-dropdown-"+name).style.display = "none";
	}
}

var Scrollable = {
	heights: { },
	drag: { },
	dragy: 0,
	dragremainder: 0,
	scrollpos: { },
	scroll: function(name, by) {
		var x212 = this.heights[name];
		var x172 = this.heights[name]-30;
		var curr = isNaN(parseInt($("custom-scrollable-"+name).style.top)) ? 0 : parseInt($("custom-scrollable-"+name).style.top);
		var hgt = $("custom-scrollable-"+name).clientHeight;
		
		if (curr + by > 0)
			return;
		if (curr + by + hgt - x212 < 0) {
			return;
			by = x212 - curr - hgt;
		}
		var newpos = curr + by;
		
		var indicator = -(newpos/(hgt-x212))*x172;
		this.scrollpos[name] = -indicator;
		$("custom-scrollable-scrollbar-indicator-"+name).style.backgroundPosition = "0px "+indicator+"px";

		$("custom-scrollable-"+name).style.top = (curr + by)+"px";
	},
	startDrag: function(name, event) {
		var x212 = this.heights[name];
		var x172 = this.heights[name]-30;
		this.drag[name] = true;
		if (!event)
			event = window.event;
		this.dragy = event.clientY;
		document.onmousemove = function(ev) {
			if (!ev)
				ev = window.event;
			var indidiff = Scrollable.dragy - ev.clientY;
			
			var newipos = (Scrollable.scrollpos[name]?Scrollable.scrollpos[name]:0) + indidiff;
			if (newipos>0)
				newipos = 0;
			if (newipos<-x172)
				newipos = -x172;
//			if (newipos>0 || newipos<-x172)
//				return;
			
			$("custom-scrollable-scrollbar-indicator-"+name).style.backgroundPosition = "0px "+(-newipos)+"px";
			Scrollable.scrollpos[name] = newipos;

			var hgt = $("custom-scrollable-"+name).clientHeight;
			var newpos = ((hgt-x212)*newipos)/x172;

			$("custom-scrollable-"+name).style.top = newpos+"px";

			Scrollable.dragy = ev.clientY;
			Tools.deselect();
			return false;
		}
		document.onmouseup = function() { Scrollable.stopDrag(name); }
	},
	stopDrag: function(name) {
		this.drag[name] = false;
		document.onmousemove = function() { }
	},
	create: function(el, name, height, immediate) {
		this.heights[name] = height;
		var func = function() {
			var hg = Math.max(el.clientHeight, el.offsetHeight);
			if (hg<height)
				return;
			var cont = el.cloneNode(true);
			cont.id = "custom-scrollable-"+name;
			cont.className = "custom-scrollable-content";
			
			el.className = "custom-scrollable";
			el.style.height = height+"px";
			el.style.clip = "rect(0px, auto, "+height+"px, auto)";
			dropAllElements(el);
			el.appendChild(cont);
			
			el.onscroll = function() {
					var offset;// = 0;
					if (self.pageXOffset)
						offset = self.pageXOffset;
					else if (document.documentElement && document.documentElement.scrollTop)
						offset = document.documentElement.scrollLeft;
					else if (document.body)
						offset = document.body.scrollLeft;
					console.log(offset);
			}

			var scroll = document.createElement("div");
			scroll.className = "custom-scrollable-scrollbar";
			html = "";
			html += "<div class=\"custom-scrollable-scrollbar\">";
			html += "<a class=\"custom-scrollable-scrollbar-up\" href=\"javascript:void(0)\" onmousedown=\"this.to=setInterval(function(){Scrollable.scroll('"+name+"',10);},100);this.blur();\" onmouseup=\"if(this.to)clearTimeout(to)\" onclick=\"Scrollable.scroll('"+name+"',10);this.blur();return false;\"></a>";
			html += "<div style=\"height:"+(height-10)+"px\" id=\"custom-scrollable-scrollbar-indicator-"+name+"\" class=\"custom-scrollable-scrollbar-indicator\" onmousedown=\"Scrollable.startDrag('"+name+"',event)\" onmouseup=\"Scrollable.stopDrag('"+name+"')\"></div>";
			html += "<a style=\"top:"+(height)+"px\" class=\"custom-scrollable-scrollbar-down\" href=\"javascript:void(0)\" onmousedown=\"this.to=setInterval(function(){Scrollable.scroll('"+name+"',-10);},100);this.blur();\" onmouseup=\"if(this.to)clearTimeout(to)\" onclick=\"Scrollable.scroll('"+name+"',-10);this.blur();return false;\"></a>";
			html += "</div>";
			scroll.innerHTML = html;
			
			el.appendChild(scroll);
		}
		if (immediate)
			func();
		else
			window.queueload.push(func);
	}
}

var Account = {
	select: function(amount, custom) {
		var oldvalue = $("paypal-amount").value;
		if (oldvalue!=amount) {
			$("paypal-amount").value = amount;
			$("paypal-custom").value = custom;
			$("account-"+oldvalue).src = "/img/account-radio.gif";
			$("account-"+amount).src = "/img/account-radio-active.gif";
		}
	}
};

var Stub = {
	submitTester: function(vars) {
		if (!vars.pro_first_name || !vars.pro_last_name || !vars.pro_email) {
			$("register-firstname-tr").className = vars.pro_first_name ? "" : "error";
			$("register-lastname-tr").className = vars.pro_last_name ? "" : "error";
			$("register-email-tr").className = vars.pro_email ? "" : "error";
		} else {
			$("errors-tester").style.display = "none";
			request.post(prefix+"welcome/tester/", vars, function(rs) {
				if (rs.result=="1")
					$("form-tester").innerHTML = rs.message;
				else
					$("errors-tester").style.display = "block";
			});
		}
	},
	submitPartner: function(vars) {
		if (!vars.prt_name || !vars.prt_url || !vars.prt_description || !vars.prt_contact) {
			$("partner-company-tr").className = vars.prt_name ? "" : "error";
			$("partner-url-tr").className = vars.prt_url ? "" : "error";
			$("partner-project-tr").className = vars.prt_description ? "" : "error";
			$("partner-name-tr").className = vars.prt_contact ? "" : "error";
		} else {
			$("errors-partner").style.display = "none";
			request.post(prefix+"welcome/partner/", vars, function(rs) {
				if (rs.result=="1")
					$("form-partner").innerHTML = rs.message;
				else
					$("errors-partner").style.display = "block";
			});
		}
	}
};


var Tooltip = {
	displayed: false,
	on: function(evt) {
		if (evt == undefined)
			evt = window.event;
		Tooltip.displayed = true;
		$("hint").style.visibility = "visible";
	},
	off: function() {
		Tooltip.displayed = false;
		$("hint").style.visibility = "hidden";
		this.onmousemove = function() { }
		this.onmouseout = function() { }
	},
	move: function(evt) {
		if (Tooltip.displayed) {
			if (document.addEventListener) {
				$("hint").style.left = ((evt.clientX + window.pageXOffset)+5) + 'px';
				$("hint").style.top = ((evt.clientY + window.pageYOffset)-45) + 'px';
			} else if (window.opera) {
				$("hint").style.left = ((evt.clientX + window.pageXOffset)+5) + 'px';
				$("hint").style.top = ((evt.clientY + window.pageYOffset)-45) + 'px';
			} else if (window.event) {
				if (document.compatMode && document.compatMode!= 'BackCompat') {
					$("hint").style.left = ((event.clientX + document.documentElement.scrollLeft)+5) + 'px';
					$("hint").style.top = ((event.clientY + document.documentElement.scrollTop)-45) + 'px';
				} else {
					$("hint").style.left = ((event.clientX + document.body.scrollLeft)+5) + 'px';
					$("hint").style.top = ((event.clientY + document.body.scrollTop)-45) + 'px';
				}
			} else if (document.layers) {
				$("hint").left = evt.pageX+5;
				$("hint").top = evt.pageY+45;
			}
		}
	}
}

var Beta = {
	showForm: function() {
		$("beta-form").style.display = "block";
		$("beta-shadow").style.display = "block";
		$("beta-content").focus();
	},
	submit: function() {
		request.post(prefix+"beta/submit/", { content: $("beta-content").value }, function(h) {
			$("beta-content").value = "";
			$("beta-form").style.display = "none";
			Beta.thanks();
		});
		$("beta-form").style.display = "none";
	},
	cancel: function() {
		$("beta-form").style.display = "none";
		$("beta-shadow").style.display = "none";
	},
	thanks: function() {
		$("beta-thanks").style.display = "block";
		setTimeout(function() {
			$("beta-thanks").style.display = "none";
			$("beta-shadow").style.display = "none";
		}, TIMEOUT);
	}
};

var Wizard = {
	lasturl: "",
	load: function(url, hideanyway) {
		if (this.lasturl==url || hideanyway && $("wizard-container").childNodes.length>0) {
			dropAllElements($("wizard-container"));
			this.lasturl = "";
		} else {
			this.lasturl = url;
			request.html(url, function(h) {
				dropAllElements($("wizard-container"));
				$("wizard-container").innerHTML = h;
				Tools.alignColumns();
				setTimeout(function() { window.runxload(); }, 300);
			});
		}
	},
	showBase: function(hideanyway) {
		this.load(prefix+"wizard/journal/", hideanyway);
	},
	showProfile: function(hideanyway) {
		this.load(prefix+"wizard/profile/journal/", hideanyway);
	},
	showFriends: function(hideanyway) {
		this.load(prefix+"wizard/friends/journal/", hideanyway);
	},
	showItems: function(hideanyway) {
		this.load(prefix+"wizard/items/journal/", hideanyway);
	},
	showTutorial: function(hideanyway) {
		this.load(prefix+"wizard/tutorial/journal/", hideanyway);
	},
	saveItem: function(form) {
		var vars = dumpform(form);
		if (vars["image"])
			for (var i=0; i<vars["image"].length; i++)
				vars["image["+i+"]"] = vars["image"][i];
		if (vars["crop_x_image"])
			for (var i=0; i<vars["crop_x_image"].length; i++)
				vars["crop_x_image["+i+"]"] = vars["crop_x_image"][i];
		if (vars["crop_y_image"])
			for (var i=0; i<vars["crop_y_image"].length; i++)
				vars["crop_y_image["+i+"]"] = vars["crop_y_image"][i];
		request.posthtml(prefix+"wizard/saveitem/", vars, function(h) {
			dropAllElements($("wizard-container"));
			$("wizard-container").innerHTML = h;
			Tools.alignColumns();
		});
	},
	search: function(sr, onlyresults) {
		request.html(prefix+"wizard/friends/journal/?search="+sr+(onlyresults?"&onlyresults=1":""), function(h) {
			dropAllElements($("wizard-container"));
			$("wizard-container").innerHTML = h;
			Tools.alignColumns();
		});
	},
	addProposal: function(id, confirm) {
		if (confirm) {
			Tools.loading($("vcard-help-"+id));
			request.posthtml(prefix+"friends/addproposal/", { id: id, invited: 1, confirmed: 1 }, function(h) {
				Tools.stopLoading($("vcard-help-"+id));
				dropAllElements($("vcard-help-"+id));
				$("vcard-help-"+id).innerHTML = h;
				$("propose-button-"+id).className = "inactive";
				$("propose-button-"+id).removeAttribute("href");
				setTimeout(function() {
					$("vcard-info-"+id).style.display = "block";
					$("vcard-controls-"+id).style.display = "block";
					dropAllElements($("vcard-help-"+id));
					$("vcard-help-"+id).style.display = "none";
				}, TIMEOUT);
				Profile.updateIndicators();
				Tools.alignColumns();
			});
		} else {
			$("vcard-help-"+id).style.display = "block";
			Tools.loading($("vcard-help-"+id));
			request.html(prefix+"friends/addproposal/"+id+"/wizard/", function(h) {
				Tools.stopLoading($("vcard-help-"+id));
				$("vcard-help-"+id).innerHTML = h;
				$("vcard-info-"+id).style.display = "none";
				$("vcard-controls-"+id).style.display = "none";
				Tools.alignColumns();
			});
		}
	},
	cancelFriendPropose: function(id) {
		dropAllElements($("vcard-help-"+id));
		$("vcard-info-"+id).style.display = "block";
		$("vcard-controls-"+id).style.display = "block";
		Tools.alignColumns();
	}
};

var Partners = {
	more: function(id) {
		$("shorter-content-"+id).style.display = "none";
		$("normal-content-"+id).style.display = "block";
		Tools.alignColumns();
	}
};

var Comments = {
	hideform: false,
	updateCounter: function() {
		setTimeout(function() {
			$("post-comment-input").value = $("post-comment-input").value.substr(0, 200);
			$("post-comment-counter-value").innerHTML = parseInt(200-$("post-comment-input").value.length);
		}, 10);
	},
	add: function(dest, message, mode) {
		if (!(""+message).trim())
			return;

		var module = dest.item_id ? "item" : (dest.user_id ? "user" : "group");
		var id = dest.item_id ? dest.item_id : (dest.user_id ? dest.user_id : dest.group_id);
		var vars = dest;
		vars.message = message;
		vars.mode = mode;
		vars.parent_id = $("comment-parent").value;
		request.posthtml(prefix+"comments/postcomment/", vars, function(h) {
			if (module=="item") {
				request.json(prefix+"item/indicators/"+id+"/", function(rs) {
					if ($("item-count-followers"))
						$("item-count-followers").innerHTML = rs.followers;
					if ($("item-count-groups"))
						$("item-count-groups").innerHTML = rs.groups;
					if ($("item-count-comments"))
						$("item-count-comments").innerHTML = rs.comments;
				});
			} else if (module=="user") {
				request.json(prefix+"user/indicators/"+id+"/", function(rs) {
					if ($("user-count-items"))
						$("user-count-items").innerHTML = rs.items;
					if ($("user-count-friends"))
						$("user-count-friends").innerHTML = rs.friends;
					if ($("user-count-exchanges"))
						$("user-count-exchanges").innerHTML = rs.exchanges;
					if ($("user-count-groups"))
						$("user-count-groups").innerHTML = rs.groups;
					if ($("user-count-comments"))
						$("user-count-comments").innerHTML = rs.comments;
				});
			} else if (module=="group") {
				request.json(prefix+"group/indicators/"+id+"/", function(rs) {
					if ($("group-count-items"))
						$("group-count-items").innerHTML = rs.items;
					if ($("group-count-members"))
						$("group-count-members").innerHTML = rs.members;
					if ($("group-count-comments"))
						$("group-count-comments").innerHTML = rs.comments;
				});
			}
			
			if ($("no-comments"))
				dropElement($("no-comments"));

			var d = document.createElement("div");
			d.innerHTML = h;
			if (vars.parent_id)
				$("replies-"+vars.parent_id).insertBefore(d, $("replies-"+vars.parent_id).childNodes[0]?$("replies-"+vars.parent_id).childNodes[0]:null);
			else
				$("comments").insertBefore(d, $("comments").childNodes[0]?$("comments").childNodes[0]:null);

			$("post-comment-input").value = "";
			
			if (Comments.hideform)
				$("post-comment").style.display = "none";
			
			Tools.alignColumns();
		});
	},
	deleteComment: function(id, dest) {
		var module = dest.item_id ? "item" : (dest.user_id ? "user" : "group");
		var iid = dest.item_id ? dest.item_id : (dest.user_id ? dest.user_id : dest.group_id);
		request.post(prefix+"comments/deletecomment/", { id: id }, function(rs) {
			if (module=="item") {
				request.json(prefix+"item/indicators/"+iid+"/", function(rs) {
					if ($("item-count-followers"))
						$("item-count-followers").innerHTML = rs.followers;
					if ($("item-count-groups"))
						$("item-count-groups").innerHTML = rs.groups;
					if ($("item-count-comments"))
						$("item-count-comments").innerHTML = rs.comments;
				});
			} else if (module=="user") {
				request.json(prefix+"user/indicators/"+iid+"/", function(rs) {
					if ($("user-count-items"))
						$("user-count-items").innerHTML = rs.items;
					if ($("user-count-friends"))
						$("user-count-friends").innerHTML = rs.friends;
					if ($("user-count-exchanges"))
						$("user-count-exchanges").innerHTML = rs.exchanges;
					if ($("user-count-groups"))
						$("user-count-groups").innerHTML = rs.groups;
					if ($("user-count-comments"))
						$("user-count-comments").innerHTML = rs.comments;
				});
			} else if (module=="group") {
				request.json(prefix+"group/indicators/"+iid+"/", function(rs) {
					if ($("group-count-items"))
						$("group-count-items").innerHTML = rs.items;
					if ($("group-count-members"))
						$("group-count-members").innerHTML = rs.members;
					if ($("group-count-comments"))
						$("group-count-comments").innerHTML = rs.comments;
				});
			}
			if (rs.result) {
//				dropElement($("comment-"+id));
				Tools.slidein($("comment-"+id), 3, function() { Tools.alignColumns(); });
			}
		});
	},
	switchMode: function(value) {
		var oldvalue = $("comment-mode").value;
		if (oldvalue!=value) {
			$("comment-switch-mode-"+oldvalue).src = "/img/icon-add.gif";
			$("comment-switch-mode-"+value).src = "/img/icon-add-active.gif";
		}
		$("comment-mode").value = value;
	},
	reply: function(id) {
		if ($("post-comment").style.display=="block") {
			$("post-comment").style.display = "none";
		} else {
			$("post-comment").style.display = "block";
			$("comment-parent").value = id;
			$("reply-to-h2").innerHTML = $("reply-to-h2").getAttribute("name").rplace("{1}", $("comment-name-"+id).innerHTML);
			$("post-comment-input").focus();
			
			$("post-comment").parentNode.removeChild($("post-comment"));
			$("comments").insertBefore($("post-comment"), $("comment-"+id).nextSibling);
			Tools.alignColumns();
			
			this.hideform = true;
		}
	}
};

var Home = {
	switchComment: function(idx) {
		if ($("full-comment-"+idx).style.display=="block") {
			$("full-comment-"+idx).style.display = "none";
			$("shorter-comment-"+idx).style.display = "block";
		} else {
			$("full-comment-"+idx).style.display = "none";
			$("shorter-comment-"+idx).style.display = "block";
		}
	}
}