

var SWindow = {

	swindows: { },

	_findRootElement: function(element)
	{
		while (element.tagName.toUpperCase() != "BODY")
		{  
			if(element.id && SWindow.swindows[element.id]) return element;
			element = element.parentNode;
		}
	},
	
	create: function(name)
	{
		element = 'SWindow_'+name;
		if(SWindow.swindows[element]) return false;
		
		var options = Object.extend({ 
			name:		name,
			url:		'',
			innerHTML:	'',
			element:	element,
			title:		'Untitled SWindow',
			width:		500,
			height:		300,
			top:		'auto',
			left:		'auto',
			position:	'fixed',
			zindex:		1000
		}, arguments[1] || { });
		
		SWindow.swindows[options.element] = options;
	
		// Tworzymy glowny div
		var mainDiv = $(document.createElement('div'));
		mainDiv.id = 'SWindow_'+name;
		var position = this.calculatePosition(options);
		mainDiv.setStyle({
			position: options.position,
			width: options.width+'px',
			height: options.height+'px',
			top: position.top+'px',
			left: position.left+'px',
			zIndex: options.zindex
		});
		mainDiv.className = 'SWindow';

		// Tworzymy pasek
		var topbarDiv = $(document.createElement('div'));
		topbarDiv.id = 'SWindowTopBar_'+name;
		topbarDiv.className = 'SWindowTopBar';
		mainDiv.appendChild(topbarDiv);

		// Tworzymy przyciski
		var buttonsDiv = $(document.createElement('div'));
		buttonsDiv.id = 'SWindowButtons_'+name;
		buttonsDiv.className = 'SWindowButtons';
		buttonsDiv.setStyle({
			'cssFloat': 'right'
		});
		topbarDiv.appendChild(buttonsDiv);
		
		var closeButtonDiv = $(document.createElement('div'));
		closeButtonDiv.className = 'SWindowButton';
		closeButtonDiv.innerHTML = 'X';
		buttonsDiv.appendChild(closeButtonDiv);
		closeButtonDiv.observe('click',this.close);
		
		// Tworzymy tytul
		var titleDiv = $(document.createElement('div'));
		titleDiv.id = 'SWindowTitle_'+name;
		titleDiv.className = 'SWindowTitle';
		titleDiv.innerHTML = options.title;
		topbarDiv.appendChild(titleDiv);
		
		// Tworzymy content
		var contentDiv = $(document.createElement('div'));
		contentDiv.id = 'SWindowContent_'+name;
		contentDiv.className = 'SWindowContent';
		contentDiv.innerHTML = options.innerHTML;
		contentDiv.setStyle({
			width: options.width+'px', 
			overflow: 'auto'
		});
		mainDiv.appendChild(contentDiv);

		
		document.body.appendChild(mainDiv);
		
		contentDiv.setStyle({height: options.height-titleDiv.getHeight()+'px'})
		
		new Draggable(mainDiv.id,{handle: titleDiv, zindex: options.zindex});
		if(options.url)
		{
			new Ajax.Updater(contentDiv.id,options.url,{evalScripts: true, onComplete: SWindow.onLoad.bind(options)})
		}
	
	},
	calculatePosition: function(options)
	{
		position = new Object();
		position.top = (document.viewport.getHeight()-options.height)/2;
		position.left = (document.viewport.getWidth()-options.width)/2;
		if(options.position=='absolute')
		{
			position.top+=document.viewport.getScrollOffsets().top;
			position.left+=document.viewport.getScrollOffsets().left;
		}
		return position;
	},
	close: function(event)
	{
		element = SWindow._findRootElement(event.findElement());
		element.parentNode.removeChild(element);
		SWindow.swindows[element.id] = undefined;
		return false;
	},
	onLoad: function(event)
	{
		element = $(this.element);
		forms = element.getElementsByClassName('FormularzUFGA');
		for(var i=0;i<forms.length;i++)
		{
			forms[i].observe('submit',SWindow.formSubmit.bind(this));
			inputs = forms[i].getElementsByTagName('input');
			for(var j=0;j<inputs.length;i++)
			{
				if(inputs[i].className=='anuluj')
				{
					inputs[i].observe('click',SWindow.close);
				}
			}
		}
	},
	formSubmit: function(event)
	{
		var form = event.findElement();
		new Ajax.Updater('SWindowContent_'+this.name,form.action,{
			method: 'post',
			parameters: form.serialize(),
			evalScripts: true, 
			onComplete: SWindow.onLoad.bind(this)
		})
		Event.stop(event);
	},
	catchLink: function(event)
	{
		Event.stop(event);
		var element = event.findElement();
		SWindow.create('window',{url: element.href, title: element.title});
		
	},
	rewriteLinks: function()
	{
		var links = document.getElementsByTagName('a');
		for(var i=0;i<links.length;i++)
		{
			if(links[i].getAttribute('rel')=='swindow')
			{
				Event.observe(links[i],'click', SWindow.catchLink);
			}
		}
	}
}

Event.observe(window,'load',SWindow.rewriteLinks)

