// File: bubbles.js
// Requires: prototype.js

var BubblesController = Class.create();

BubblesController.prototype = {
	listCtrlsBubbles : null,
	eltCurrentBubble : null,

	/** Constructor. Does the following:
	 * - Argument list for each element/bubble combo
	 * -
	 */
	initialize : function(/*{idBubble: string (element), idTarg: string (bubble element), eventOn: string (display event), eventOff: string (hide event), callbackOn : function (), callbackOff : function ()}, ...*/)
	{
		var i, eltTarg, eltBbl, linkClose, list;
		this.listCtrlsBubbles = new Object();
		
		// show bubble on load
        if (defaultBubble !== undefined) {
            this.showBubble(document.getElementById(defaultBubble));
        }
		/*
        var fname = document.getElementById("first_name");
		if(fname){
			this.showBubble('first_nameHelp');
		}
		
		var fav_sport1 = document.getElementById("fav_sport1Help");
		if(fav_sport1){
			this.showBubble('fav_sport1Help');
		}
		*/
        
		for(i = 0; i < arguments.length; i++){
			linkClose = null;

			if(typeof(arguments[i]) == "object" &&
				typeof(arguments[i].idTarg) == "string" && typeof(arguments[i].idBubble) == "string" &&
				arguments[i].idTarg && arguments[i].idBubble &&
				(eltTarg = $(arguments[i].idTarg)) != null && (eltBbl = $(arguments[i].idBubble)) != null){

				if(typeof(arguments[i].eventOn) == "string"){
					Event.observe(eltTarg, arguments[i].eventOn, this.handleBubbleShow.bindAsEventListener(this), false);
				}
				if(typeof(arguments[i].eventOff) == "string"){
					Event.observe(eltTarg, arguments[i].eventOff, this.handleBubbleHide.bindAsEventListener(this), false);
				}
				// Get Close Link in bubble and attach a close bubble handler
				//if((list = $$("#" + eltBbl.id + " .closebox")) != null){ // Note: Close Bubble links always have a class of "closebox"
				//	Event.observe(list[0], "click", this.handleCloseLink.bindAsEventListener(this), false);
				//}

				this.listCtrlsBubbles[arguments[i].idTarg] = new Object();
				this.listCtrlsBubbles[arguments[i].idTarg].bubble = eltBbl;

				if(typeof(arguments[i].callbackOn) == "function"){
					this.listCtrlsBubbles[arguments[i].idTarg].callbackOn = arguments[i].callbackOn;
				}
				if(typeof(arguments[i].callbackOff) == "function"){
					this.listCtrlsBubbles[arguments[i].idTarg].callbackOff = arguments[i].callbackOff;
				}

//				{bubble : eltBbl, callbackOn : arguments[i].callbackOn, callbackOff : arguments[i].callbackOff}; // append to list
			}
		}
	},
	handleBubbleShow : function(evnt)
	{
		var eltTarg = Event.element(evnt);
		var eltLink = Event.findElement(evnt, "A");
		var bRet = true, eltBubble;

		if(evnt.type == "click" && eltLink && typeof(eltLink.tagName) != "undefined" && eltLink.tagName == "A"){
			bRet = false;
		}
        //alert(eltTarg.id);
            
		if(typeof(this.listCtrlsBubbles[eltTarg.id].callbackOn) != "function" || this.listCtrlsBubbles[eltTarg.id].callbackOn()){

			eltBubble = this.listCtrlsBubbles[eltTarg.id].bubble;
            if (window.keepBubbleHidden === undefined || keepBubbleHidden[eltTarg.id] === undefined) {
    			this.showBubble(eltBubble);
            }
        }

		return bRet;
	},
	handleBubbleHide : function(evnt)
	{
		var eltTarg = Event.element(evnt);
		var eltLink = Event.findElement(evnt, "A");
		var bRet = true;

		if(evnt.type == "click" && eltLink && typeof(eltLink.tagName) != "undefined" && eltLink.tagName == "A"){
			bRet = false;
		}

		if(typeof(this.listCtrlsBubbles[eltTarg.id].callbackOff) != "function" || this.listCtrlsBubbles[eltTarg.id].callbackOff()){
			eltBubble = this.listCtrlsBubbles[eltTarg.id].bubble;

			this.hideBubble(eltBubble);
		}

		return bRet;
	},
	handleCloseLink : function(evnt)
	{
		var eltLink = Event.findElement(evnt, "A");

		// Note: Operate under the assumtion that the close link is always a direct child of the bubble div.
		if(eltLink && eltLink.parentNode && eltLink.parentNode.parentNode){

			var eltBubble = eltLink.parentNode.parentNode;//this.listCtrlsBubbles[eltLink.parentNode.parentNode.id].bubble;

			this.hideBubble(eltBubble);
		}

		return false;
	},
	// ***** Utils *****
	showBubble : function(eltBubble)
	{
		if(this.eltCurrentBubble && this.eltCurrentBubble !== eltBubble){
			Element.hide(this.eltCurrentBubble);
		}
		if(eltBubble){
			Element.show(eltBubble);
		}
		this.eltCurrentBubble = eltBubble;
	},
	hideBubble : function(eltBubble)
	{
		if(eltBubble){
			Element.hide(eltBubble);
		}

		if(eltBubble === this.eltCurrentBubble){
			this.eltCurrentBubble = null;
		}
	},
	// ******************* External Show/Hide Controllers *******************
	hide : function(targId)
	{
		var eltBubble = null;

		if(typeof(eltBubble = this.listCtrlsBubbles[targId]) != "undefined"){
			eltBubble = eltBubble.bubble;
		}

		if(eltBubble){
			this.hideBubble(eltBubble);
		}
	},
	show : function(targId)
	{
		var eltBubble = null;

		if(typeof(eltBubble = this.listCtrlsBubbles[targId]) != "undefined"){
			eltBubble = eltBubble.bubble;
		}

		if(eltBubble){
			this.showBubble(eltBubble);
		}
	}
};
