/**
 * Naptár készíto.
 * 
 * @author sassas (Kaprf Andras)
 * @since 0.1 - 2008.02.05.
 * @since 0.2 - 2008.02.07.
 * @since 0.3 - 2008.02.11. keyControl()
 * @since 0.4 - 2008.02.14. maxEv,maxHo,maxNap
 * @vesion 0.4
 */

/**
 * Az elso három paraméter megadása kötelezo
 * @param ev obj
 * @param ho obj
 * @param nap obj
 * @param minEv string
 * @param minHo string
 * @param minNap string
 * @return naptár forraskodja
 * @access public
 */
function Help(helpid) {
	//debug
	this.debug = true;
	
	try {
		//import
		this.ClassName = new ClassName();
		//set
		this.i_am_about_to_show = false;
		this.help = document.getElementById( helpid );
		this.iframe = null;
		this.ieBug  = false;
		this.userAgent = navigator.userAgent.toLowerCase();
		if ( document.all && this.userAgent.indexOf("opera") < 0 &&  this.userAgent.indexOf("msie 7") < 0 ) {
			this.ieBug = true;
		}	
		//create
		this.createHelpbox();
		//event
		EventListener.addEvent(document, 'keyup', this.scope('keyControl'));		
	} catch ( exception ) {
		if ( this.debug ) alert('Help.construct()' + ' - ' + exception.message);
		return null;
	}
}

Help.prototype = {
	scope:function(method) {
		var scope = this;
		return function() {
			scope[method].apply(scope, arguments);
		}
	},
			
	keyControl:function(e) {
		try{
			
			if ( !this.help ) return;
			var esc = 27;

			if ( e.keyCode == esc ) {
				this.removeHelpbox();
				return EventListener.cancelEvent(e);
			}
			
		} catch ( exception ) {
			if ( this.debug ) alert('Help.keyControl()' + ' - ' + exception.message);
			return EventListener.cancelEvent(e);
		}
	},
	
	showHelpbox:function(obj,tartalom, cim, width) {
		try {
			if ( !this.help ) return;
			if ( cim ) {
				var adat = '<table border="0" cellspacing="0" cellpadding="5"><tr><td><div style="font-size:13px"><b>'+tartalom.cim+'</b></div></td></tr><tr><td><div style="line-height:1.2em; font-size:10px">'+tartalom.leiras+'</div></td></tr></table>';
			} else {
				var adat = '<table border="0" cellspacing="0" cellpadding="5"><tr><td><div style="line-height:1.2em; font-size:10px">'+tartalom.leiras+'</div></td></tr></table>';
			}
			this.i_am_about_to_show = true;
			obj.style.cursor="help";
			this.help.innerHTML = adat;
			var divBorder = Number(this.help.style.borderWidth.split("px").join(''));
			if (width != null) {
				this.help.style.width = String(width) + "px";
			} else {
				this.help.style.width = "350px";	
			}
			//this.help.style.width = String(800) + "px";
			/*if ( this.help.clientHeight > 240 ) {
				this.help.style.width = this.help.clientWidth + 18 + "px";
				this.help.style.height = 240 + "px";
			}*/
			
			this.help.style.left = this.localToGlobal(obj).x - (this.help.clientWidth/2) + (obj.clientWidth/2) + 26 + "px";	
			this.help.style.top = this.localToGlobal(obj).y  + obj.clientHeight + 26 + "px";
			if ( this.ieBug ) {
				this.iframe = document.createElement('iframe');
				this.iframe.style.width = this.help.clientWidth + (divBorder * 2); 
				this.iframe.style.height = this.help.clientHeight + (divBorder * 2);
				this.iframe.style.left = this.help.style.left;	
				this.iframe.style.top =this.help.style.top;
			}
		} catch ( exception ) {
			if ( this.debug ) alert('Help.showHelpbox()' + ' - ' + exception.message);
			return null;
		}		
	},
	
	// Ha "kesleletetes" nincs definiálva, akkor alapból vár 2 másodpercig, és utána tunik el.
	// Ha "kesleletetes" definiálva van és értéke > 0, akkor "kesleltetes" ezredmásodpercig vár.
	// Ha "kesleletetes" definiálva van és értéke <= 0, akkor azonnal eltunik.
	removeHelpbox:function(kesleltetes) {
		try {
			if ( !this.help ) return;

			try {
				if (helpBox === undefined) {
					this.i_am_about_to_show = false;
				} else {
					if (kesleltetes === undefined) {
						this.i_am_about_to_show = false;
						setTimeout("helpBox.removeHelpbox(0)", 2000);
						return;
					} if (parseInt(kesleltetes) > 0) {
						this.i_am_about_to_show = false;
						setTimeout("helpBox.removeHelpbox(0)", parseInt(kesleltetes));
						return;
					}
				}
			} catch ( exception ) {
				this.i_am_about_to_show = false;
			}
			
			if (this.i_am_about_to_show) {
				return;
			}
			
			this.help.style.top='-1000px';
			this.help.style.left='-1000px';
			if ( this.ieBug ) {
				this.iframe.style.left = this.help.style.left;	
				this.iframe.style.top = this.help.style.top;
			}
		} catch ( exception ) {
			if ( this.debug ) alert('Help.removeHelpbox()' + ' - ' + exception.message);
			return null;
		}		
	},
			
	createHelpbox:function() {
		try {			
			if ( this.ieBug ) {
				return;
				var divBorder = Number(this.help.style.borderWidth.split("px").join(''));
				this.iframe = document.createElement('iframe');
				this.iframe.style.zIndex = this.help.style.zIndex-1; 
				this.iframe.style.width = this.help.clientWidth + (divBorder * 2); 
				this.iframe.style.height = this.help.clientHeight + (divBorder * 2);
				this.iframe.style.position = 'absolute';
				this.iframe.style.left = this.help.style.left;	
				this.iframe.style.top = this.help.style.top;
				document.body.appendChild(this.iframe);
			}	
			
		} catch ( exception ) {
			if ( this.debug ) alert('Help.createHelpbox()' + ' - ' + exception.message);
			return null;
		}
	},
	
						
	localToGlobal:function(div) {
		try {
			var myObject = new Object();
			myObject.x = 0;
			myObject.y = 0;
			while (div != null) {
			   myObject.x += div.offsetLeft;
			   myObject.y += div.offsetTop;
			   div = div.offsetParent;
			}
			return myObject;
		} catch ( exception ) {
			if ( this.debug ) alert('Help.localToGlobal()' + ' - ' + exception.message);
		}
	},
		
	getScrollPoz:function() {
		try {
			var scrOfX = 0, scrOfY = 0;
			if( typeof( window.pageYOffset ) == 'number' ) {
				//Netscape compliant
				scrOfY = window.pageYOffset;
				scrOfX = window.pageXOffset;
			} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				//DOM compliant
				scrOfY = document.body.scrollTop;
				scrOfX = document.body.scrollLeft;
			} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				//IE6 standards compliant mode
				scrOfY = document.documentElement.scrollTop;
				scrOfX = document.documentElement.scrollLeft;
			}
			return { x:scrOfX, y:scrOfY };
		} catch ( exception ) {
			if ( this.debug ) alert('Help.getScrollPoz()' + ' - ' + exception.message);
		}
	},
	
	setScrollPoz:function(scrOfX, scrOfY) {
		try {
			var scrOfX = 0, scrOfY = 0;
			if( typeof( window.pageYOffset ) == 'number' ) {
				//Netscape compliant
				window.pageYOffset = scrOfY;
				window.pageXOffset = scrOfX;
			} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				//DOM compliant
				document.body.scrollTop = scrOfY;
				document.body.scrollLeft = scrOfX;
			} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				//IE6 standards compliant mode
				document.documentElement.scrollTop = scrOfY;
				document.documentElement.scrollLeft = scrOfX;
			}
		} catch ( exception ) {
			if ( this.debug ) alert('Help.setScrollPoz()' + ' - ' + exception.message);
		}
	}
}
