var JokeBox = function(options) {
	
	this.options = options;
	this.datas = new Array();	
	this.container = $(options.containerId).appendChild(document.createElement("DIV"));
	this.container.className = "joke_info";
	this.collections = {"" : "[Pasirinkite rinkinį]"};
	this.active = 0;
	this.last_more = null;
	
}

JokeBox.prototype.add = function(data) {
	data.position = this.datas.length;
	this.datas.push(data);	
}

JokeBox.prototype.addCollection = function(value, title) {
	this.collections[value] = title;
}

JokeBox.prototype.setVisible = function(position, visible) {
	this.active = position;
	var node_more = $("__joke_" + position + "_more");
	if (this.last_more) {
		this.last_more.className = "more";
	}
	this.last_more = node_more;
	
	if (visible) {
		this.renderJoke(this.datas[position - 1]);
		var node = JokeBox.getInstance().container;	
		var offset = node_more.cumulativeOffset();
		node.style.top = offset.top + "px";
		node.style.left = (offset.left - 181) + "px";
		document.body.onmousemove = function(event) {
			if (!event) {
				event = window.event;
			}
			var x = Event.pointerX(event);
			var y = Event.pointerY(event);
			var node = JokeBox.getInstance().container;
			var xy = node.cumulativeOffset();	
			if (
				xy.left > x || xy.left + node.getWidth() < x || 
				xy.top > y || xy.top + node.getHeight() < y
			) {				
				this.onmousemove = null;
				JokeBox.getInstance().setVisible(null, false);
			}		
		}
	}
	this.container.style.display = visible ? "" : "none";
	node_more.className = "more" + (visible ? " more_active" : "");
}

JokeBox.prototype.showCollections = function(event) {
	var el = Event.element(event);
	el.style.display = "none";
	el.nextSibling.style.display = "";
}

JokeBox.prototype.onChangeCollections = function(event) {
	var el = Event.element(event);
	sendAJAX('rinkinys/bajeris/prideti/' + el.value + '/' + this.datas[this.active - 1].id, true);
	el.style.display = "none";
	el.previousSibling.style.display = "";
}

JokeBox.prototype.renderJoke = function(data) {
	var contents = [];
	if (data.deadly) {
		contents.push("<b>Mirtinas :)</b>");
	}
	if (data.tags) {
		contents.push("Raktažodžiai: <i>" + data.tags + "</i>");
	}
	contents.push("Pagrindinė kalba: <b>" + data.language + "</b>");
	contents.push("<a href=\"bajeriai/panasus/" + data.id + "\">Panašūs</a><br />");
	contents.push("<a href=\"siusti/bajeri/" + data.id + "\">Siųsti draugui</a>");
	contents.push(
		"<a href=\"javascript:sendJokeBad('bajeris/blogas/" + data.id + "')\">Pranešti kad blogas</a>"
	);
	if (this.collections.length > 1) {
		var parts = "";
		for (var i in this.collections) {
			parts += "<option value=\"" + i + "\">" + this.collections[i] + "</option>";
		}
		contents.push(
			"<a href=\"javascript:void(0)\" onclick=\"JokeBox.getInstance().showCollections(event)\">" +
				"Įdėti į rinkinį</a>" +
			"<select size=\"6\" style=\"width: 100%;display:none\" onchange=\"" + 
				"JokeBox.getInstance().onChangeCollections(event)\">" + parts + "</select>"
		);
	}
	if (data.edit) {
		var parts = []
		if (data.status) {
			parts.push(
				"<a title=\"Tvirtinti\" href=\"javascript:sendAJAX('bajeris/keisti_statusa/" + data.id + "/1', true);hideElement('__joke_" + (data.position + 1) + "');\">[T]</a>"
			);
			parts.push(
				"<a title=\"Atmesti\" href=\"javascript:rejectJoke('bajeris/keisti_statusa/" + data.id + "/0', '__joke_" + (data.position + 1) + "');\">[A]</a>"
			);
		}
		parts.push("<a title=\"Redaguoti\" href=\"bajeris/redaguoti/" + data.id + "');\">[R]</a>");
		contents.push(parts.join(""));
	}
	this.container.innerHTML = contents.join("<br/>");
}

JokeBox.instance = null;
JokeBox.getInstance = function(options) {
	if (options) {
		JokeBox.instance = new JokeBox(options);
	}
	return JokeBox.instance;
}