/**
 * (c) Firma JAKUBIAK 2007
 * Antoni Jakubiak
 * http://www.jakubiak.biz/
 */
 
 
/**
 * Pokazywanie dymkow
 */
function Puff(id,on,offsetX,offsetY,enabled,hint) {
	this.id = id;
	this.element = document.getElementById(id);
	this.on = on;
	this.onElement = document.getElementById(on);
	this.offsetX = offsetX;
	this.offsetY = offsetY;
	this.enabled = enabled;
	this.hint = hint;
	
	this.hide();
	
	var tmpOnload = window.onload;
	var oThis = this;
	// gdy tylko nie jestesmy onmouseover
	if (!this.hint) {
		// to zapalamy sie zaraz po zaladowaniu strony
		var tmpFn = function() {
			if("function" == typeof tmpOnload) {
				tmpOnload();
			}
			oThis.show();
		}
	} else {
		// jednak, gdy jestesmy hintem to zapalamy sie i gasniemy
		// gdy ktos nachodzi mysza na element on
		// 1. rejestracja onmouseover
		var oldOnMouseOver = this.onElement.onmouseover;
		var tmpOnMouseOver = function() {
			if("function" == typeof oldOnMouseOver) {
				oldOnMouseOver();
			}
			oThis.show();
		}
		this.onElement.onmouseover = tmpOnMouseOver;
		// 2. rejestracja onmouseout
		var oldOnMouseOut = this.onElement.onmouseout;
		var tmpOnMouseOut = function() {
			if("function" == typeof oldOnMouseOut) {
				oldOnMouseOut();
			}
			oThis.hide();
		}
		this.onElement.onmouseout = tmpOnMouseOut;
	}
		
	try {		
		window.onload = tmpFn;
	} catch(e) {}
	// po kliknieciu znikam
	if (this.enabled) {
		var tmpHideFn = function() {
			oThis.hide();
		}
		this.element.onclick = tmpHideFn;
	}
	// timeout sledzenie pozycji rodzica
	this._autoRecenterTimeout = null;
}

/**
 * Ukrycie dymka
 */
Puff.prototype.hide = function() {
	this.element.style.display = "none";
	clearTimeout(this._autoRecenterTimeout);	
}

Puff.prototype.show = function() {
	s = this.element.style;
	s.position = "absolute";
	s.zIndex = "1000";
	s.display = "block";
	if (this.enabled) {
		s.cursor = "pointer"; // po kliknieciu znikam
	}
	this.autoRecenter();
}
/**
 * Przesuniecie elementu wraz z rodzicem
 */
Puff.prototype.recenter = function() {
	var pos = this.getAbsolutePos(this.onElement);
	s = this.element.style;
	s.left = (parseInt(pos.right) + parseInt(this.offsetX)) + "px";
	s.top = (parseInt(pos.top) + parseInt(this.offsetY)) + "px";
}

Puff.prototype.autoRecenter = function() {
	this.recenter();
	
	clearTimeout(this._autoRecenterTimeout);
	var oThis = this;
	var fn = function() {
		oThis.autoRecenter();
	}
	this._autoRecenterTimeout = setTimeout(fn,1000);
}
	



	



/**
 * Absolutna pozycja dla elementy
 */
Puff.prototype.getAbsolutePos = function( oElem ) {
	var oTemp = typeof oElem == 'string' ? document.getElementById( oElem ) : oElem;
	oElem = oTemp;
	var absLeft = 0, absTop = 0;
	while ( oTemp ) 
	{
		absTop += oTemp.offsetTop;
		absLeft += oTemp.offsetLeft;
		oTemp = oTemp.offsetParent;
	}
	var r = new Object;
	r.left   = absLeft;
	r.top    = absTop;
	r.right  = absLeft + oElem.offsetWidth;
	r.bottom = absTop + oElem.offsetHeight;
	r.width  = oElem.offsetWidth;
	r.height = oElem.offsetHeight;
	return r;
}


