// libnk-panels.js
//
// Copyright (c) 2007 Nathan Kelly <info@nathan-kelly.com>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// This script will not work without prototype.js or scriptaculous.js
// each of these scripts are subject to their own License Agreements

var UserPanels=Class.create();
UserPanels.prototype={
	initialize: function() {
		var panels = document.getElementsByClassName('upanel');
		var toggles = document.getElementsByClassName('utoggle');
		for (var i=0; i<panels.length; i++) {
			var panel = panels[i];
			if (!$('open-upanel')) {
				// dont hide panel-message yet if it exists
				if (panel.id != 'panel-message') {
					panel.style.display='none';
				}
			}			
		}
		for (var t=0; t<toggles.length; t++) {
			var toggle = toggles[t].firstChild;
			toggle.onclick = function() {
				if ($('open-upanel')) {
					myUserPanels.moveOn(this);
				} else if (!document.getElementById('open-upanel')) {
					myUserPanels.start(this);
				}
				// need to reference this otherwise dual toggles get mixed up
				var toggleHref = String(this.getAttribute('href'));
				// return false if it's a normal toggle not if it's a special
				// toggle i.e. if the link takes me to the panel and opens it
				if (toggleHref == '#') {
					return false;
				}
			}
		}
		// set a timeout to close panel-message if it exists
		if ($('panel-message')) {
			window.setTimeout( function() {
				myUserPanels.end($('panel-message'));
			}, 6000)	
		}
		return false;
	},
	start: function(whichPanel) {
		if (!document.getElementById('open-upanel')) {
			whichPanel.id = 'open-upanel';
		}
		var openThis = whichPanel.className;
		if (!openThis) return;
		new Effect.Parallel([
			new Effect.BlindDown(openThis),
			new Effect.Appear(openThis, {
				from: 0.0,
				to: 1.0
			})
		],
			{
				duration: 0.7
			}
		);
		return false;
	},
	end: function(whichPanel) {
		if ($('open-upanel')) { $('open-upanel').id = ''; }				
		// check if the panel is a system message
		if (whichPanel.id == 'panel-message') {
			var closeThis = whichPanel.id;
		} else {
			var closeThis = whichPanel.className;
		}		
		if (!closeThis) { return; }
		
		new Effect.Parallel([
			new Effect.BlindUp(closeThis),
			new Effect.Fade(closeThis, {
				from: 1.0,
				to: 0.0
			})
		],
			{
				duration: 0.7
			}
		);		
		return false;
	},
	moveOn: function(whichPanel) {
		var openThis = whichPanel.className;
		var closeThis = $('open-upanel').className;
		if (openThis == closeThis) {
			myUserPanels.end(whichPanel);
			return;
		}
		if ($('open-upanel')) {
			new Effect.Parallel([
				new Effect.BlindUp(closeThis),
				new Effect.Fade(closeThis, {
					from: 1.0,
					to: 0.0
				})
			],
				{
					duration: 0.7,
					afterFinish: function() {
						new Effect.Parallel([
							new Effect.BlindDown(openThis),
							new Effect.Appear(openThis, {								
								from: 0.0,
								to: 1.0
							})
						],
							{
								duration: 0.7
							}
						);
					}
				}
			);
			$('open-upanel').id = '';
		}
		whichPanel.id = 'open-upanel';
		return false;
	}
};

function initUserPanels(){if(!document.getElementById('upanels')) return; myUserPanels=new UserPanels();};
/* Fire on DOM load
Event.observe(window, 'load', initPanels);
*/
Event.observe(window,'unload', Event.unloadCache);

