MW.namespace("controls.panel");

(function(){
	var Event = YAHOO.util.Event;
	var Dom = YAHOO.util.Dom;
	var logging = false;
	
	MW.controls.panel.Login = function()
	{
		if (logging)
		{
			if (!MW.controls.panel.logger)
			{
				MW.controls.panel.logger = new YAHOO.widget.LogReader(null, {draggable: true});
			}
		}
		
		if (!this.preInit()) {return;}
		this.trigger = document.getElementById("mywedding-login-panel-trigger");
		this.panel = new YAHOO.widget.Overlay("login-panel", {
			visible: false,
			context: [this.trigger,"tr","tr",["beforeShow", "windowResize"]] // login panel sits underneath 'my account' button
		});
		this.panel.hideEvent.subscribe(this.onHide,this);
		this.panel.showEvent.subscribe(this.onShow,this);
		this.panel.render();
		this.getManager().register(this.panel);
		this.visible = false;
		this.panelRegion = Dom.getRegion("login-panel");
		this.panel.isLoaded = false; // indicate the panel's content has not been loaded.
		// The trigger's handler acts as a proxy to whatever method triggerMethod
		// is referenced to. On each show, triggerMethod is set to hide, and vice
		// versa. Attempting this behavior by changing the trigger's event listener
		// directly resulted in IE havoc.
		this.triggerMethod = this.show;
		Event.addListener(this.trigger, "click", this.onTrigger, this, true);
		// The panel body has a handler that simply stops event propagation so
		// a click anywhere in it will not be registered by the window click
		// event (registered on panel open) which closes the panel.
		Event.addListener(this.panel.body, "click", function(e){Event.stopPropagation(e);});
	}
	
	MW.controls.panel.Login.prototype = {
		onTrigger: function(e)
		{
			this.triggerMethod(e);
			Event.stopEvent(e);
		},
		show: function(e)
		{
			YAHOO.log("show")
			if (!this.visible)
			{
				this.getManager().hideAll();
				this.panel.show();
				if (!this.panel.isLoaded)
				{
					var login = this;
					var loginPanel = document.getElementById("mwLoginPanel");
					mywedding.ajax.getIntoElement(
						loginPanel ? loginPanel.href : "/apps/entry/account_menu.php",
						"",
						this.panel.body,
						function() {login.panel.isLoaded = true;}
					);
				}
				Event.addListener(document, "click", this.hide, this, true);
				Event.stopEvent(e);
			}
			Event.stopPropagation(e);
		},
		hide: function(e)
		{
			YAHOO.log("hide");
			if (this.visible)
			{
				this.getManager().hideAll();
				Event.removeListener(document, "click", this.hide);
			}
		},
		onHide: function(type,args,me)
		{
			me.visible = false;
			me.triggerMethod = me.show;
		},
		onShow: function(type,args,me)
		{
			me.visible = true;
			me.triggerMethod = me.hide;
		},
		preInit: function()
		{
			if (!document.getElementById('mywedding-login-panel-trigger') || !document.getElementById('login-panel'))
			{
				return false;
			}
			else
			{
				return true;
			}
		},
		getManager: function()
		{
			if (!(MW.controls.panel.Manager instanceof YAHOO.widget.OverlayManager))
			{
				MW.controls.panel.Manager = new YAHOO.widget.OverlayManager();
			}
			return MW.controls.panel.Manager;
		}
	};
	
	Event.onDOMReady(function() {new MW.controls.panel.Login();});
}());

/**
 * Legacy.
 */

/** Called inline in the template to check for a session cookie and
  * display the appropriate account-related link.
  */
function writeLogInOut(baseUrl) {
	if (arguments.length == 0) baseUrl = "";
	var sess = mywedding.cookies.get(escape("mw_session::LOGGED_IN"));
	var loggedIn = sess != null;
	var login = document.getElementById("t-login-link");
	login.removeChild(login.firstChild);
	login.appendChild(document.createTextNode(loggedIn ? "log out" : "log in"));
	login.href = baseUrl + "/apps/" + (loggedIn ? "entry/logout.php" : "entry/entry.php");	
}

/** Sets the user's saved email address in the homepage login box.
  */
function writeHomePageEmail() {
	var email = mywedding.cookies.get("mw_home_login_email");	
	if (email != null) {		
		document.getElementById("hp_login_form").email.value = email;				
	}
}
