/* JavaScript by Lumpio- (Matti Virkkunen)
 * This JavaScript may be freely used and edited for NON COMMERCIAL
 * purposes as long as this header is included.
 ***/

function PickerPopup(p, pos) {
	if (!p.popupable) {
		alert('PickerPopup: This object isn\'t pop-upable');
		return;
	}
	this.pos = pos || PickerPopup.BELOWFIELD;
	this.p = p;
	this.p.elInput.style.display = '';
	this.p.el.style.position = 'absolute';
	this.p.el.style.zIndex = 100;
	this.p.el.style.display = 'none';
	this.p.el.onmouseover = PickerPopup.handleMouseOver;
	this.p.el.onmouseout = PickerPopup.handleMouseOut;
	this.ponPick = this.p.onpick;
	this.p.onpick = PickerPopup.handleDatePick;

	this.elButton = document.createElement("a");
	this.elButton.href = "#";
	this.elButton.className = "pickerbutton " + this.p.popupClassName;
	this.elButton.onclick = PickerPopup.handleButtonClick;
	
	this.p._PP_obj = this;
	this.p.elInput._PP_obj = this;

	this.open = false;

	if (this.p.elInput.nextSibling) {
		this.p.elInput.parentNode.insertBefore(this.elButton, this.p.elInput.nextSibling);
	} else {
		this.p.elInput.parentNode.appendChild(this.elButton);
	}
}

PickerPopup.BELOWBUTTON = 1;
PickerPopup.BELOWFIELD = 2;
PickerPopup.ABOVEBUTTON = 3;
PickerPopup.ABOVEFIELD = 4;

PickerPopup.handleButtonClick = function(e) {
	var obj = this.previousSibling._PP_obj;
	var e = e || window.event;
	if (obj.open) {
		obj.close();
	} else {
		obj.popup();
	}
	e.cancelBubble = true;
	if (e.stopPropagation) {
		e.stopPropagation();
	}
	return false;
};

PickerPopup.handleMouseOver = function() {
	var obj = this.nextSibling._PP_obj;
	obj.mouseOver = true;
};

PickerPopup.handleMouseOut = function() {
	var obj = this.nextSibling._PP_obj;
	obj.mouseOver = false;
};

PickerPopup.handleDocumentClick = function() {
	var obj = document._PP_obj;
	if (obj.mouseOver == false) {
		obj.close();
	}
};

PickerPopup.handleDatePick = function() {
	var obj = this._PP_obj;
	obj.close();
	if (obj.ponPick) {
		obj.ponPick();
	}
};

PickerPopup.prototype.popup = function() {
	switch (this.pos) {
		case PickerPopup.BELOWBUTTON:
			//this.p.el.style.left = this.elButton.offsetLeft + 4 + "px";
			//this.p.el.style.top = this.elButton.offsetTop + this.elButton.offsetHeight + "px";
			this.p.el.style.left = "550px";
			this.p.el.style.top = "350px";
			break;
		case PickerPopup.BELOWFIELD:
			//this.p.el.style.left = this.p.elInput.offsetLeft + 4 + "px";
			//this.p.el.style.top = this.p.elInput.offsetTop + this.p.elInput.offsetHeight + "px";
			this.p.el.style.left = "550px";
			this.p.el.style.top = "350px";
			break;
	}
	this.mouseOver = false;
	document._PP_obj = this;
	document.onclick = PickerPopup.handleDocumentClick;
	this.open = true;
	this.p.el.style.display = '';
	switch (this.pos) {
		case PickerPopup.ABOVEBUTTON:
			this.p.el.style.left = this.elButton.offsetLeft + 4 + "px";
			this.p.el.style.top = this.elButton.offsetTop - this.p.el.offsetHeight + "px";
			break;
		case PickerPopup.ABOVEFIELD:
			this.p.el.style.left = this.p.elInput.offsetLeft + 4 + "px";
			this.p.el.style.top = this.p.elInput.offsetTop - this.p.el.offsetHeight + "px";
			break;
	}
};

PickerPopup.prototype.close = function() {
	document.onclick = null;
	document._PP_obj = null;
	this.open = false;
	this.p.el.style.display = 'none';
}

// Copied from lutil.js

function $e(e) {
	if (typeof e == 'string') {
		return document.getElementById(e);
	} else {
		return e;
	}
}

