/**
 * CMDrawer.js
 *
 * An object specifically designed for CoalMedia page sections to make them
 * less prone to kerploding during the extension of arbitrary page sections.
 *
	<div id="holster27">
		<a href="http://hostname/xml/ajax.php?func=Distro.doSomethingToFillTheDrawer"
		   id="activator27" rel="drawerHandle">Bob Jones</a>
		<div id="drawerFor_activator27"></div>
	</div>
	Will render the results of the ajax call into the drawer, zeroing out that
	data on the reverse.
	<a href="#someDOM_ID" />
	Will render the contents of someDOM_ID instead.
 *
 * @copyright Coalmarch Productions LLC 2007 All Rights Reserved
 * @authors Thomas Ingham <tingham@coalmarch.com>
 * @version 0.3
 * @license MIT
 *
 **/

var global_cmDrawerController = null;
CMDrawer = Class.create();
Object.extend(Object.extend(CMDrawer.prototype, CMBase.prototype), {
	  /**
	   * Set up the carousel array, look through the document to find any
	   * divs that need to be linked to their controllers and set up
	   * AJAX objects for later calling.
	   * @return	[nil]
	   * @author	tingham@coalmarch.com
	   * @created	4/28/07 12:10 AM
	   */
	  initialize: function()
	  {
		/**
		 * @var value of the rel field to look for
		 **/
		this.typeValue = "text/drawerHandle";
		/**
		 * @var prepended to the link tag ID to find drawer dom object
		 **/
		this.domObjectPrefix = "drawerFor_";

		/**
		 * @var When you want the drawer to open on a click, we use click.
		 * If you want the drawer to open on something else, that's your
		 * business.
		 * For now only click is supported, not really seeing a big need for
		 * mouseover or anything but someone else could extend this once
		 * the safari event assignment on link text instead of link element thing
		 * gets fixed.
		 **/
		this.showEvent = "click";

		/**
		 * @var Speed of fades / appears. Affected by method type inline.
		 **/
		this.duration = 0.25;

	  	if( !document.getElementsByTagName ){ return; }
		CMBase.prototype.initialize.call(this);

		for(var i=0;i<this.carousel.length;i++)
		{
			var thisDrawer = this.carousel[i];
			var theDuration = this.duration;

			if( thisDrawer.domObject.className.indexOf("modal") > -1 )
			{
				thisDrawer.boolModal = true;
			}
			else
			{
				thisDrawer.boolModal = false;
			}

			var partnerDiv = document.getElementById(thisDrawer.domObject.id+'Partner');
			if (partnerDiv)
			{
				thisDrawer.partnerDiv = partnerDiv;
			}

			switch( this.showEvent )
			{
				case "click":
					var index = i;
					thisDrawer.domAnchor.onclick = function (evt) {
						var objTmpDrawer = CMBase.prototype.getEventObject.call(this,evt,global_cmDrawerController);
						try
						{
							if( objTmpDrawer.strState == "hidden" )
							{
								/**
								 * If the drawer is modal hide drawers at this level.
								 **/
								if( objTmpDrawer.boolModal )
								{
									for(var p=0;p<global_cmDrawerController.carousel.length;p++)
									{
										if( global_cmDrawerController.carousel[p].domParent ==
												objTmpDrawer.domParent &&
											global_cmDrawerController.carousel[p].strState == "visible")
										{
											global_cmDrawerController.closeDrawer( global_cmDrawerController.carousel[p].domObject.id,true );
										}
									}
								}
								CMBase.prototype.setObjectInnerHTML.call(this,objTmpDrawer);
								Effect.Appear(objTmpDrawer.domObject.id, {duration:(theDuration*2)});
								var tmpDomNode = objTmpDrawer.domObject.firstChild;
								Effect.Appear(tmpDomNode, {duration:((objTmpDrawer.boolUsesAjax)?theDuration*4:theDuration)});
								objTmpDrawer.strState = "visible";
								if (objTmpDrawer.partnerDiv)
								{
									objTmpDrawer.partnerDiv.style.display="none";
								}
								else
								{
									objTmpDrawer.manageAnchorText();
								}
							}
							else
							{
								global_cmDrawerController.closeDrawer( objTmpDrawer.domObject.id,false );
							}
						}catch(e){}
						return false;
					};
					break;
			}
		}
	},

	/**
	 * CMDrawer::closeAllDrawers
	 * Closes all open drawers.
	 */
	closeAllDrawers : function(options)
	{
		for(var i=0;i<global_cmDrawerController.carousel.length;i++)
		{
			var objTmpDrawer = global_cmDrawerController.carousel[i];
			if( objTmpDrawer.strState == "visible" )
			{
				this.closeDrawer( objTmpDrawer.domObject.id );
			}
		}
	},

	/**
	 * CMDrawer::closeDrawer
	 * Closes a single drawer.
	 * @param [String] ID of DOM element to hide.
	 * @param [BOOL] Whether or not to hide<true>, instead of fading<false>.
	 */
	closeDrawer : function(id,hideBackground)
	{
		var tmpDomNode = null;
		var objTmpDrawer = null;
		var domEventTarget = document.getElementById(id);
		for(var i=0;i<global_cmDrawerController.carousel.length;i++)
		{
			if( domEventTarget == global_cmDrawerController.carousel[i].domObject )
			{
				objTmpDrawer = global_cmDrawerController.carousel[i];
				break;
			}
		}
		try
		{
			if( hideBackground )
			{
				Element.hide(domEventTarget.id);
			}
			else
			{
				Effect.Fade(domEventTarget.id, {duration:(global_cmDrawerController.duration/2)});
			}
			tmpDomNode = domEventTarget.firstChild;
			Effect.Fade(tmpDomNode, {duration:global_cmDrawerController.duration});
			objTmpDrawer.strState = "hidden";
			if (objTmpDrawer.partnerDiv)
			{
				objTmpDrawer.partnerDiv.style.display="block";
			}
			else
			{
				objTmpDrawer.manageAnchorText();
			}
		}catch(e){}
	}
});


function initcmDrawerController() { global_cmDrawerController = new CMDrawer(); }
Event.observe(window, 'load', initcmDrawerController, false);