var parseUri = function (source) {
	var o = parseUri.options,
		value = o.parser[o.strictMode ? "strict" : "loose"].exec(source);
	for (var i = 0, uri = {}; i < 14; i++)
		uri[o.key[i]] = value[i] || "";
	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});
	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q: {
		name: "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

if (openedBubblesArr == undefined) {
	var openedBubblesArr = [];
}

var Bubble = function(){
    this.path = "/res/js/bubble/";
    this.header = "-- header --";
    this.message = "-- message --";
    this.width = 200;
    this.height = 50;
    this.startPage = false;
    this.offsetX = 0;
    this.offsetY = 0;

    this.startPage = true;
/*
    try {
        var path = parseUri( window.location ).path.replace(/\//g, '');
        if (path == '' || path == 'en' || path == 'nl')
            this.startPage = true;
    } catch(e) { }
*/
}

Bubble.prototype.check = function(){
    try{
        if (!document.getElementById(this.output)){
            throw new Error("Output instance " + this.output + " not found.");
        }
//        if (document.getElementById(this.output).nodeName.toLowerCase() != "input"){
//            throw new Error("Output instance " + this.output + " is not input instance.");
//        }
//        if (document.getElementById(this.output).type.toLowerCase() != "text" && document.getElementById(this.output).type.toLowerCase() != "password"){
//            throw new Error("Output instance " + this.output + " is not text input instance.");
//        }
        if (this.startPage) {
            this.width = 240;
            this.height = 80;
        }
        return true;
    }
    catch (error){
        alert(error.message);
        return false;
    }
}

Bubble.prototype.bind = function(){
    if (!this.check()){
        return;
    }
    var bubble = this;

	if(document.getElementById(this.output).nodeName.toLowerCase() != 'input') {

		// trick to determine cash reg, but must be changed
		if (this.offsetY != 0) {
			document.getElementById(this.output).onclick = function() {
				bubble.show();
			}
		}

		// otherwise use mouseover handler
		else {
			document.getElementById(this.output).onmouseover = function() {
				bubble.show();
			}
			document.getElementById(this.output).onmouseout = function() {
				bubble.hide();
			}
		}
    }
    else {
	    document.getElementById(this.output).onfocus = function() {
		    bubble.show();
	    }

		document.getElementById(this.output).onblur = function() {
		    bubble.hide();
	    }
    }
}


Bubble.prototype.show = function(){
	
	// check if buule is already opened
	if (openedBubblesArr[this.output] != undefined) {
		return;
	}
	else {
		openedBubblesArr[this.output] = 1;
	}

    var instance = document.createElement("div");
    instance.id = this.output + "Bubble";
    instance.style.position = "absolute";
    instance.style.zIndex = "100";
	instance.bubble = this;

    var left = this.getAbsolute(document.getElementById(this.output), 'Left') + ( this.startPage ?  0 : 0);
    var top  = this.getAbsolute(document.getElementById(this.output), 'Top');
    instance.style.left = left - this.width + 16 + Math.round(document.getElementById(this.output).offsetWidth / 2) + this.offsetX + "px";
    instance.style.top = top - this.height - 19 + this.offsetY + "px";
    instance.style.width = this.width + "px";
    instance.style.height = this.height + "px";

    this.createCorner(instance, "left", "top");
    this.createCorner(instance, "left", "bottom");
    this.createCorner(instance, "right", "top");
    this.createCorner(instance, "right", "bottom");
    this.createHorDiv(instance);
    this.createVertDiv(instance);
    this.createTriangle(instance);
    this.createInfo(instance);
    this.createClose(instance);
    this.createHeader(instance);
    this.createMessage(instance);

    document.body.appendChild(instance);
}

Bubble.prototype.getAbsolute = function(object, param) {
    var attrname = 'offset'+param;
	var pos = object[attrname];
	while(object.offsetParent != null) {
		var parent = object.offsetParent;
		pos += parent[attrname];
		object = parent
	}
	return pos;
}

Bubble.prototype.createMessage = function(instance){
    var div = document.createElement("div");
    div.style.position = "absolute";
    div.style.left = "12px";
    div.style.top = "25px";
    div.style.width = this.width - 24 + "px";
    div.style.fontFamily = "tahoma";
    div.style.fontSize = "8pt";
    div.innerHTML = this.message;
    div.style.cursor = "default";
    instance.appendChild(div);
}

Bubble.prototype.createHeader = function(instance){
    var div = document.createElement("div");
    div.style.position = "absolute";
    div.style.left = "36px";
    div.style.top = "7px";
    div.style.width = this.width - 72 + "px";
    div.style.fontFamily = "tahoma";
    div.style.fontSize = "8pt";
    div.style.fontWeight = "bold";
    div.innerHTML = this.header;
    div.style.cursor = "default";
    instance.appendChild(div);
}

Bubble.prototype.createClose = function(instance){
    var div = document.createElement("div");
    div.style.position = "absolute";
    div.style.right = "5px";
    div.style.top = "5px";
    div.style.width = "17px";
    div.style.height = "17px";
    div.style.cursor = "pointer";
    div.style.background = 'url("' + this.path + 'bubble_close.gif") no-repeat';
	div.onclick = function() { instance.bubble.hide(); }
    instance.appendChild(div);
}

Bubble.prototype.createInfo = function(instance){
    var div = document.createElement("div");
    div.style.position = "absolute";
    div.style.left = "13px";
    div.style.top = "7px";
    div.style.width = "16px";
    div.style.height = "16px";
    div.style.background = 'url("' + this.path + 'bubble_info.gif") no-repeat';
    instance.appendChild(div);
}

Bubble.prototype.createTriangle = function(instance){
    var div = document.createElement("div");
    div.style.position = "absolute";
    div.style.right = "16px";
    div.style.bottom = "-20px";
    div.style.width = "20px";
    div.style.height = "21px";
    div.style.background = 'url("' + this.path + 'bubble_triangle.gif") no-repeat';
    instance.appendChild(div);
}

Bubble.prototype.createHorDiv = function(instance){
    var div = document.createElement("div");
    div.style.position = "absolute";
    div.style.left = "7px";
    div.style.top = "0px";
    div.style.width = (this.width - 14) + "px";
    div.style.height = (this.height - 2) + "px";
    div.style.borderTop = "1px solid #000000";
    div.style.borderBottom = "1px solid #000000";
    div.style.background = "#FFFFE1";
    instance.appendChild(div);
}

Bubble.prototype.createVertDiv = function(instance){
    var div = document.createElement("div");
    div.style.position = "absolute";
    div.style.left = "0px";
    div.style.top = "7px";
    div.style.width = (this.width - 2) + "px";
    div.style.height = (this.height - 14) + "px";
    div.style.borderLeft = "1px solid #000000";
    div.style.borderRight = "1px solid #000000";
    div.style.background = "#FFFFE1";
    instance.appendChild(div);
}

Bubble.prototype.createCorner = function(instance, hor, vert){
    var corner = document.createElement("div");
    corner.style.background = 'url("' + this.path + 'bubble_' + hor + '_' + vert + '.gif") no-repeat';
    corner.style.position = "absolute";
    corner.style[hor] = "0px";
    corner.style[vert] = "0px";
    corner.style.width = "7px";
    corner.style.height = "7px";
    corner.style.oveflow = "hidden";
    corner.style.fontSize = "0px";
    instance.appendChild(corner);
}

Bubble.prototype.hide = function(){
	openedBubblesArr[this.output] = undefined;

    var instance = document.getElementById(this.output + "Bubble");
    if (instance){
        instance.parentNode.removeChild(instance);
    }
}
