/*
 * form_help.js
 *  Used to create a pop-up 'help bubble' to aid the user
 *  when filling out forms.
 *  (effects.js is necessary)
 */

var Bubble = {
	soap : function (obj, text, help, extra) {
		// 'obj' is the input or target object
		if (typeof obj == 'string')
			var _obj = document.getElementById(obj);
		else if (typeof obj == 'object')
			var _obj = obj;
		else
			alert("object referenced doesn't exist");
			
		// 'text' is the HTML to put in the bubble
		if ((text == '') || (text == null))
			alert("nothing to put in bubble");
		
		var already_exists = document.getElementById(_obj.id+"_bubble");
		if (already_exists)
			Bubble.pop(already_exists);
		
		//_obj.onfocus = function () {
			//Bubble.blow(_obj, text);
		//};
		_obj.onblur = function () {
			Bubble.pop(_obj.id+"_bubble");
		};
		_obj.onkeypress = function () {
			Bubble.pop(_obj.id+"_bubble");
		}

		if ((help == true) || (help == null)) {
			var help_button = document.createElement("a");
			help_button.href = "javascript:;";
			help_button.className = "help_button";
			help_button.onclick = function () {
				Bubble.blow(_obj, text);
			};
			help_button.onblur = function () {
				Bubble.pop(_obj.id+"_bubble");
			};
			help_button.innerHTML = (extra) ? extra : "?";
			_obj.parentNode.appendChild(help_button);
		}
	},
	
	blow : function (_obj, text) {
		// just a little checking so we don't create any dupes
		var already_exists = document.getElementById(_obj.id+"_bubble");
		if (already_exists)
			var _bubble = already_exists;
		else {
			var _bubble = document.createElement("div");
			_bubble.id = _obj.id+"_bubble";
			_bubble.onclick = function() {
				Bubble.pop(_bubble);
			}
		}
		
		// content
		_bubble.innerHTML = 	'<img src="'+fs_url+'community/img/help_top.gif" /><br />'+
								'<div style="border-left:#47448e 1px solid;border-right:#47448e 1px solid;width:210px;background-color:#fcfbee;">'+
									'<div style="padding:0px 3px 0px 3px;">'+text+'</div>'+
								'</div>'+
								'<img src="'+fs_url+'community/img/help_bottom.gif" />';
		
		// styling
		_bubble.style.position = "absolute";
		_bubble.style.top = (_obj.offsetTop + _obj.offsetHeight + 2) + "px";
		_bubble.style.left = Bubble.getTrueX(_obj) + "px";
		_bubble.style.width = "212px";
		
		_bubble.className = "help_bubble";
		_bubble.style.cursor = "pointer";
		_bubble.style.font = "normal 7pt Verdana,Sans-Serif";
		
		// if a bubble doesn't exist, we create a new one and fade it in
		if (already_exists == null) {
			_bubble.style.opacity = "0";
			_bubble.style.filter = "alpha(opacity=0)";
			
			document.body.appendChild(_bubble);
			Effect.Appear(_bubble.id, {duration:0.5});
		}
	},
	
	pop : function (targ) {
		// targ is the bubble object to pop
		if (typeof targ == 'object') {
			Effect.Fade(targ.id, {duration:0.2, afterFinish:function (obj) {document.body.removeChild(obj.element);}});
		}
		else if (typeof targ == 'string') {
			var temp = document.getElementById(targ);
			if (temp) Effect.Fade(temp.id, {duration:0.1, to: 0.1, afterFinish:function(obj) {document.body.removeChild(obj.element);}});
		}
	},
	
	getTrueX : function (obj) {
		var c = obj;
		var x = 0;
		do {
			x += c.offsetLeft;
			c = c.parentNode;
		} while (c.parentNode);
		return x;
	},
	
	getTrueY : function (obj) {
		
	}
};