function BubbleMessage(title, message, image) {
	this.title = title;
	this.message = message;
	this.image = image;
};

BubbleMessage.prototype.bind = function(target) {

	var pos = BubbleMessage.getElementPosition(target);
	var container = document.createElement("div");
	if (this.message.indexOf('Out of stock')>0) {
		this.message = '* Out of stock';
		container.setAttribute('id', 'bubblemessage-error');
	} else {
		container.setAttribute('id', 'bubblemessage');
	}
	if (this.message.indexOf('<br')>0) {
		this.message = this.message.substring(0, this.message.indexOf('<br'));
	}

	var main = document.createElement("div");
	main.innerHTML = '<div id="bubblemessage-image"><img src="' + this.image + '"></div><div id="bubblemessage-content"><img src="/res/close.png" id="bubblemessage-close"><br style="clear: both;" />' + this.message + '</div>';
	
	container.appendChild(main);
	container.style.position = "absolute";
	container.style.left = (pos.x) + "px";

	document.body.appendChild(container);
	container.style.top = (pos.y) + "px";

	container.onclick = function() {
		container.parentNode.removeChild(container);
	};

	setTimeout(function(){ $('#bubblemessage').fadeOut('slow'); }, 2000);

};

BubbleMessage.getElementPosition = function(elm) {
	var el = elm;
	var ol = el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }

	var el = elm;
	var ot = el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }

	return {
		x : ol,
		y : ot
	}
};
