/****
*		Hired Gun: Matthew Gaddis <matthew@spiremedia.com>
*		Created: 2006-06-29
*		File: Platform2006 JS JavaScript Edit Block UI Control 
*		Requires: prototype 1.5.0 or greater
****/

var editBlock = {};
editBlock.Base = function () {};

editBlock.Base.prototype = 
{
	baseInitialize: function(url, options)
	{
		///
		/// Setting of Public variables, i.e. the options
		///
		this.url = url;
		this.e = null;
		
		if (this.setOptions)
			this.setOptions(options);
		else
			this.options = options || {};
			
		this.options.containers = this.options.containers || [ 'form', 'text' ];
		this.options.filter = this.options.filter || 'content-block';
		this.options.controlNameSuffix = this.options.controlNameSuffix || '-editblock';
		this.options.tagName = this.options.tagName || 'div';
		this.options.editor = this.options.editor || {width: 1036, height: 700, name: 'addmode1'};
		
		///
		/// onClick event handler, reads the className information
		/// and pops a window according to the order and markup 
		/// of the content object
		///
		this.options.onClick = this.options.onClick || function(event)
		{
			this.e = Event.findElement(event, this.options.tagName);
			
			for(var i = 0; i < this.options.containers.length; i++)
			{
				if ( this.e.hasClassName(this.options.containers[i] + this.options.controlNameSuffix) )
				{
					var order = '&order=' + (/\d+/.exec(this.e.className));
					var markup = '&markup=' + this.options.containers[i];
					
					this.popUpWindow(this.url + order + markup);
					break;
				}
			}
		};
		
		///
		/// onMouseOver event handler, toggles the background to "on" state
		///
		this.options.onMouseOver = this.options.onMouseOver || function(event)
		{
			this.e = Event.findElement(event, this.options.tagName);
			
			for(var i = 0; i < this.options.containers.length; i++)
			{
				if ( this.e.hasClassName(this.options.containers[i]) )
				{
					this.e.removeClassName(this.options.containers[i]);
					this.e.addClassName(this.options.containers[i] + this.options.controlNameSuffix);
					break;
				}
			}

		};
		
		///
		/// onMouseOut event handler, toggles the background to an "off" state
		///
		this.options.onMouseOut = this.options.onMouseOut || function(event)
		{
			this.e = Event.findElement(event, this.options.tagName);
			
			for(var i = 0; i < this.options.containers.length; i++)
			{
				if ( this.e.hasClassName(this.options.containers[i] + this.options.controlNameSuffix) )
				{
					this.e.removeClassName(this.options.containers[i] + this.options.controlNameSuffix);
					this.e.addClassName(this.options.containers[i]);
					break;
				}
				
			}
		};
		
		var editBlocks = $$(this.options.tagName + ( (this.options.filter) ? '.' : '' ) + this.options.filter);
		
		
		for(var i = 0; i < editBlocks.length; i++)
		{
			Event.observe(editBlocks[i], 'click', this.options.onClick.bindAsEventListener(this));
			Event.observe(editBlocks[i], 'mouseover', this.options.onMouseOver.bindAsEventListener(this));
			Event.observe(editBlocks[i], 'mouseout', this.options.onMouseOut.bindAsEventListener(this));
			editBlocks[i].onclick = function(e) { return false; }
		}
		
		this.disableAnchorAndInputTags();
	},
	disableAnchorAndInputTags: function ()
	{
		var tags = ['a', 'input', 'select', 'textarea'];
		
		$A(tags).each
		(
			function(tagName)
			{
				var elements = $A(document.getElementsByTagName(tagName));
				
				elements.each
				(
				 	function(e)
					{
						switch(e.tagName)
						{
							case 'A':
								e.onclick = function () { return false };
								break;
							default:
								e.setAttribute('disabled', 'disabled');
								break;
						}
					}
				);
			}
		);
	},
	popUpWindow: function(url)
	{
		var popupWin = window.open 
		( 
		 	url, 
			this.options.editor.name, 
			'width=' + this.options.editor.width + 
			',height=' + this.options.editor.height +
			',toolbar=0,resizable=1,scrollbars=1,screenX=0,screenY=0,top=0,left=0'
		);
		if ( !document.all && window.focus ) popupWin.focus(); 
	}
};

editBlock.ESM = Class.create();
editBlock.ESM.prototype = Object.extend
(
 	new editBlock.Base(), 
	{
		initialize: function(url, options) 
		{
			this.baseInitialize(url, options);
		}
	}
);
