
WindowMaker = Class.create();
WindowMaker.prototype =
{
	initialize: function (DivToClone, Title, Options)
	{
		this.DivToClone = $(DivToClone);
		this.Title = Title;

		this.Options = {
			CloseImg: '/com/img/close.png',
			OverlayImg: '/com/img/pixel.png',
			CloseImgHeight: '16px',
			CloseCallback: null,
			ClassDiv: 'WindowMakerDiv',
			ClassHeader: 'WindowMakerHeader',
			AutoFocusOffset: -1,
			Top: '10%',
			Left: '25%',
			Right: '25%',
			Bottom: '15%'
		};
		Object.extend(this.Options, Options || {});

		this.WindowId = 'WinMaker-' + Math.floor(Math.random() * Math.pow(10, 6)) + '-';
		this.Window = null;
		
		this.AutoFocus = null;
		
		this.PrepareWin();
	},
	PrepareWin: function()
	{
		this.CreateOverlayDiv();
		
		this.Window = new Element('DIV'); 
			
		this.Header = new Element('DIV');
		this.Header.className = this.Options.ClassHeader;
		Element.setStyle(this.Header, {
			'height': this.Options.CloseImgHeight,
			'marginBottom': '5px',
			'padding': '5px'
			});

		this.CloseImg = new Image();
		this.CloseImg.src = this.Options.CloseImg;
		this.CloseImgDiv = new Element('DIV');
		Element.setStyle(this.CloseImgDiv, {
			'cssFloat': 'right',
			'cursor': 'pointer',
			'position': 'relative',
			'right': 0,
			'top': 0
			});

		Event.observe(this.CloseImgDiv, 'click', this.Close.bindAsEventListener(this));
		this.CloseImgDiv.appendChild(this.CloseImg);

		this.TitleDiv = new Element('DIV');
		this.TitleDiv.update(this.Title);
		Element.setStyle(this.TitleDiv, {
			'cssFloat': 'left',
			'position': 'relative',
			'top': 0,
			'left': 0
			});

		this.Header.appendChild(this.TitleDiv);
		this.Header.appendChild(this.CloseImgDiv);
		this.Window.appendChild(this.Header);

		this.Window.className = this.Options.ClassDiv;
		this.Window.id = this.WindowId + 'main-window';
		Element.setStyle(this.Window, {
			'position': 'absolute',
			'display': 'none',
			'top': this.Options.Top,
			'left': this.Options.Left,
			'right': this.Options.Right,
			'bottom': this.Options.Bottom,
			'overflow': 'hidden'
			});

		if(Prototype.Browser.IE)
		{
			Node2Import = this.DivToClone.cloneNode(true);
		}
		else
		{
			Node2Import = document.importNode(this.DivToClone, true);
		}

		if(Prototype.Browser.IE)
		{
			Node2Import.id = this.WindowId + Node2Import.id;
		}
		else
		{
			Node2Import.setAttribute('id', this.WindowId + Node2Import.id);
		}
		Node2Import.style.display = 'block';
		Node2Import.style.clear = 'both';

		AllImported = Node2Import.descendants();
		for(var e = 0; e < AllImported.length; e++)
		{
			if(!Prototype.Browser.IE && AllImported[e].hasAttribute('id') )
			{
				AllImported[e].setAttribute('id', this.WindowId + AllImported[e].id);
			}
			if(Prototype.Browser.IE && AllImported[e].id != 'undefined' && AllImported[e].id != '')
			{
				AllImported[e].id = this.WindowId + AllImported[e].id;
			}
		}
		this.WinContent = Node2Import;

		this.Window.appendChild(this.WinContent);

		PageBody = document.getElementsByTagName('BODY')[0];
		PageBody.appendChild(this.Window);

		if(this.Options.AutoFocusOffset != -1)
		{
			var CurrentFormElements = Form.getElements(this.Window);
			this.AutoFocus = CurrentFormElements[this.Options.AutoFocusOffset];
		}
	},
	
	CreateOverlayDiv: function()
	{
		this.Overlay = new Element('DIV');
		this.Overlay.id = this.WindowId + 'overlay';
		Element.setStyle(this.Overlay, {
			'position':				'absolute',
			'top':					0,
			'left':					0,
			'background-repeat':	'repeat',
			'background-image':		'url("' + this.Options.OverlayImg  + '")',
			'width':				'100%',
			'height':				'100%',
			'filter':				'alpha(opacity=50)',
			'-moz-opacity':			.50,
			'opacity':				.50,
			'display':				'none'
			});

		var innerImg = new Image();
		innerImg.src = this.Options.OverlayImg;
		Element.setStyle(innerImg, {
				'width':	'100%',
				'height':	'100%'
				});

		this.Overlay.appendChild(innerImg);
		
		PageBody = document.getElementsByTagName('BODY')[0];
		PageBody.appendChild(this.Overlay);
	},

	PutContent: function(HtmlBlock)
	{
		this.WinContent.innerHTML = HtmlBlock;
	},
	
	Set: function(SetValues)	//////////// Set the values in the imported Node
	{
		for(ValueName in SetValues)
		{
			if($(this.WindowId + ValueName) == null)
			{
				continue;
			}

			if	(
					$(this.WindowId + ValueName).tagName == 'INPUT'
					&&
					(
						$(this.WindowId + ValueName).type == 'checkbox'
						||
						$(this.WindowId + ValueName).type == 'radio'
					)
				)
			{
				$(this.WindowId + ValueName).checked = !!SetValues[ValueName];
			}
			else if (
						$(this.WindowId + ValueName).tagName != 'INPUT'
						&&
						$(this.WindowId + ValueName).tagName != 'SELECT'
						&&
						$(this.WindowId + ValueName).tagName != 'BUTTON'
						&&
						$(this.WindowId + ValueName).tagName != 'TEXTAREA'
					)
			{
				$(this.WindowId + ValueName).innerHTML = SetValues[ValueName];
			}
			else
			{
				$(this.WindowId + ValueName).value = SetValues[ValueName];
			}
		}
	},
	Get: function() //////////// Get the values from the imported Node
	{
		var FormValues = new Object();
		// var FormElements = new Array('INPUT', 'BUTTON', 'SELECT', 'TEXTAREA');
		var CurrentFormElements = Form.getElements(this.Window);

		for(var i = 0; i < CurrentFormElements.length; i++)
		{
			if(CurrentFormElements[i].name != '')	// If the user used a NAME attr use that instead of the ID. Be HTML 4.01 compliant.
			{
				idx = CurrentFormElements[i].name.replace(this.WindowId, '');
			}
			else
			{
				idx = CurrentFormElements[i].id.replace(this.WindowId, '');
			}

			if(idx == '') continue;

			FormValues[idx] = $F(CurrentFormElements[i]);
		}

		return FormValues;
	},
	GetAsQueryString: function()
	{
		return $H( this.Get() ).toQueryString();
	},
	Show: function()
	{
		/**
		 * Search for the biggest z-index
		 */
		var Els = $$('*');
		var ZIndex = new Array();
		for(var i = 0, k = 0, l = Els.length; i < l; i++ )
		{
			
			var s = parseInt(Els[i].style.zIndex);
			if(!isNaN(s))
			{
				ZIndex[k] = s;
				++k;
			}
		}
		
		ZIndex = ZIndex.max();
		if(isNaN(ZIndex))
		{
			ZIndex = 1;
		}

		this.Overlay.style.zIndex = ++ZIndex;
		this.Overlay.style.display = 'block';

		this.Window.style.zIndex = ++ZIndex;
		this.Window.style.display = 'block';

		if(this.AutoFocus && this.AutoFocus !== null)
		{
			this.AutoFocus.focus();
		}

		this.CachedWatchEsc = this.WatchEsc.bindAsEventListener(this);
		Event.observe(document, 'keyup', this.CachedWatchEsc);
	},
	Refresh: function()
	{
		// this is to redraw... but I don't know ho to do it for now !!!
	},
	WatchEsc: function(event)
	{
		if(event.keyCode == Event.KEY_ESC)
		{
			this.Close();
		}
	},
	Close: function()
	{
		Event.stopObserving(document, 'keyup', this.CachedWatchEsc);

		if(this.Options.CloseCallback != null)
		{
			(this.Options.CloseCallback)();
		}

		this.Window.parentNode.removeChild(this.Window);
		this.Overlay.parentNode.removeChild(this.Overlay);
	}
}

