/**
 * CMProto.js
 *
 * A simple wrapper class for displayin popover content, usually containing
 * several items such as menus for output.
 *
 * @copyright Coalmarch Productions LLC 2007 All Rights Reserved
 * @author Thomas Ingham <tingham@coalmarch.com>
 * @authors Ken Winters <kwinters@coalmarch.com>
 * @version 0.1
 * @license MIT
 *
 **/
var anchorDisplayZIndex = 200;

CMBase = Class.create();
CMBase.prototype = {

	//NOTE: objects extending CMBase need to not have member vars declared here!  create inside the initialize func instead

	toString: function()
	{
		var retString = "";
		for(i=0;i<this.carousel.length;i++)
		{
			//retString += this.carousel[i] + ": ...";
			retString += "Object #" + i + ": " +
				"\n  domAnchor = "+ this.carousel[i].domAnchor +
				"\n  domAnchor.id = "+ this.carousel[i].domAnchor.id +
				"\n  domObject = "+ this.carousel[i].domObject +
				"\n  domObject.id = "+ this.carousel[i].domObject.id +
				//"\n  strContents = "+ this.carousel[i].strContents.substring(0,30) +
				"\n  strAjax = "+ this.carousel[i].strAjax +
				"\n  strState = "+ this.carousel[i].strState +
				"\n  domParent = "+ this.carousel[i].domParent +
				"\n  boolAjax = "+ this.carousel[i].boolAjax + "\n\n";
		}
		return retString;
	},

	initialize: function()
	{
		/**
		 * @var Container of all objects of this type
		 **/
		this.carousel = new Array();
		if( !document.getElementsByTagName ){ return; }
		var anchors = document.getElementsByTagName("a");
		for(var i=0;i<anchors.length;i++)
		{
			var anchor = anchors[i];
			var boolObjectExists = false;
			if( anchor.getAttribute("type") != this.typeValue ){ continue; }
			var theDomObject = null;
			try
			{
				theDomObject = document.getElementById(this.domObjectPrefix+anchor.id);
			}
			catch(e){ window.prompt("Error setting up source component.",e); }

			if (theDomObject == null)
			{
				alert("No dom object found for "+this.domObjectPrefix+anchor.id);
			}

			var dataLocation = anchor.getAttribute("rel");
			/* todo: The loading in of objects via ajax needs serious repair
			 presently it isn't initializing the new form properly here
			 such that successive clicks on a sub overlay fail after reload
			 through ajax. */
			var objBase = { domAnchor:anchor,
							   domObject:theDomObject,
							   strContents:null,
							   strAjax:null,
							   strState:"hidden",
							   domParent:theDomObject.parentNode,
							   boolAjax:false,
							   boolCenter:false,
							   boolDirty:false,
							   objPosition:null,
							   manageAnchorText: function(){
							   	var matchVerbsHidden = new Array("Show","Open","[+]");
							   	var matchVerbsVisible = new Array("Hide","Close","[-]");
							   	try
							   	{
									var baseString = this.domAnchor.textContent;
									if( typeof(baseString) == "undefined" )
									{
										baseString = this.domAnchor.innerText;
									}
								}catch(e){ window.prompt("Exception stripping text.",this.domAnchor.textContent); }
							   	for(var t=0;t<matchVerbsHidden.length;t++)
							   	{
							   		if( baseString.indexOf(matchVerbsHidden[t]) > -1 )
							   		{
							   			baseString = baseString.split(matchVerbsHidden[t]);
							   			baseString = baseString.join(matchVerbsVisible[t]);
						   				this.domAnchor.innerHTML = baseString;
							   			return;
							   		}
							   		else if( baseString.indexOf(matchVerbsVisible[t]) > -1 )
							   		{
							   			baseString = baseString.split(matchVerbsVisible[t]);
							   			baseString = baseString.join(matchVerbsHidden[t]);
							   			this.domAnchor.innerHTML = baseString;
							   			return;
							   		}
							   	}
							   } };
			if( dataLocation.indexOf("#") > -1 )
			{
				var tmpContentID = dataLocation.substring(1,dataLocation.length);
				try
				{
					var tmpContent = document.getElementById(tmpContentID);
					objBase.strContents = '<div id="'+this.domObjectPrefix+''+anchor.id+'_cmp"'
										+'class="'+tmpContent.className
										+'" style="'+tmpContent.getAttribute("style")
										+'">'+tmpContent.innerHTML+'</div>';
				}catch(e){ objBase.strContents = "<span class=\"error\">Cannot Resolve Content Location: \""+tmpContentID+"\".</span>"; }
			}
			else if( dataLocation.indexOf("http:") > -1 ||
					 dataLocation.indexOf("https:") > -1 ||
					 dataLocation.indexOf("file:") > -1 )
			{
				objBase.strAjax = dataLocation;
				objBase.boolAjax = true;
			}

			for(var e=0;e<this.carousel.length;e++)
			{
				if( this.carousel[e].domAnchor )
				{
					if( this.carousel[e].domObject.id == objBase.domObject.id )
					{
						this.carousel[e] = objBase;
						boolObjectExists = true;
						break;
					}
				}
			}
			if( !boolObjectExists && objBase ){ this.carousel[this.carousel.length] = objBase; }
		}//for each anchor
	},
	getEventObject: function(evt,controller)
	{
		var domEventTarget;
		//IE requires getting the event from the window
		if (! evt) evt = window.event;
		if (evt)
		{
			//in IE it's srcElement
			domEventTarget = evt.target || evt.srcElement;
		}
		if ((! domEventTarget || ! domEventTarget.id) && this.id)
		{
			//called directly
			domEventTarget = this;
		}
		for(var e=0;e<controller.carousel.length;e++)
		{
			if( domEventTarget.id == controller.carousel[e].domAnchor.id )
			{
				return controller.carousel[e];
			}
		}
		return;
	},
	setObjectInnerHTML: function(obj)
	{
		if( obj.boolAjax )
		{
			var params = '';
			var ajax = new Ajax.Request(obj.strAjax,
										{method:'get',
										 parameters:params,
										 onFailure:null,
										 asynchronous:false}
										);
			var strAjaxResult =
				ajax.transport.responseText;
			if( strAjaxResult.toLowerCase().indexOf("<renderable>") > -1 )
			{
				var renderable = "";
				var script = "";
				try
				{
					renderable = ajax.transport.responseXML.documentElement.getElementsByTagName('renderable')[0].firstChild.nodeValue;
					obj.domObject.innerHTML = renderable;
				}catch(e){ window.prompt("Error In Ajax Renderable Assignment",e); }
				try
				{
					if( strAjaxResult.indexOf("<script>") > -1 )
					{
						script = ajax.transport.responseXML.documentElement.getElementsByTagName('script')[0].firstChild.nodeValue;
						if( script.length > 0 )
						{
							installScript(script);
						}
					}
				}
				catch(e){ window.prompt("Error In Ajax Script Execution",e); }
			}
			else
			{
				obj.domObject.innerHTML = strAjaxResult;
			}
		}
		else
		{
			obj.domObject.innerHTML = obj.strContents;
		}
	}
};


Effect.SwapContents = Class.create();
Object.extend(Object.extend(Effect.SwapContents.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    if(!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
		endHTML: '',
		duration: 0.0,
		from: 1.0,
		to:   1.0
		}, arguments[1] || {});
    this.start(options);
  },
  setup: function() {
    this.element.innerHTML = this.options.endHTML;
  }
});

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
