// JavaScript Document

var jsWindowZIndex = 1;

var jsWindow = new Class.create();
jsWindow.prototype = {
	initialize: function(url, options)
	{
		this.options = Object.extend(
		{
			identifier: 'divFloater',
			height: 100,
			width: 100,
			fondoClass: 'fondo',
			containerClass: 'container',
			barClass: 'bar',
			contentClass: 'content',
			blockBackground: false,
			title: '',
			positionX:'left',
			positionY:'top',
			onClose: function(){}
		}, options || {});
		
		this.baseZIndex = 9000;
		this.offsetX = 0;
		this.offsetY = 0;
		if ( document.all ) this.ie = true;
		else this.ie = false;

		this.url = url;
		
		this.container = document.createElement("div");
		this.bar = document.createElement("div");
		this.closeButton = document.createElement('span');
		this.closeButton.className = 'floater_close';
		this.closeButton.innerHTML = '&nbsp;';
		this.closeButton.onclick = this.closeWindow.bind(this);
		
		this.floaterTitle = document.createElement('span');
		this.floaterTitle.className = 'floater_title';
		this.floaterTitle.innerHTML = this.options.title;
		
		this.content = document.createElement("div");
		
		this.container.className = this.options.containerClass;
		this.container.id = this.options.identifier;
			
		this.content.className = this.options.contentClass;
		this.content.style.height = (this.options.height - 24) + 'px';
		this.container.style.height = this.options.height + 'px';
		this.container.style.width = this.options.width + 'px';
		
		this.bar.className = this.options.barClass;
		this.bar.style.height = '24px';
		this.bar.appendChild(this.floaterTitle);
		this.bar.appendChild(this.closeButton);
		
		this.container.onmousedown = this.sendToFront.bind(this);
		this.container.appendChild(this.bar);
		this.container.appendChild(this.content);
		this.container.style.display='none';

		document.body.appendChild(this.container);
		if ( this.options.blockBackground ) this.blockBackground();
		this.sendToFront();
		this.loadWindow();
		this.reposition(this.options.positionX,this.options.positionY);	
		new Effect.Appear(this.container,{duration:0.5});
		//Event.observe(window,'resize', this.reposition );
		//Event.observe(window,'scroll',this.reposition);
	},
	
	blockBackground: function()
	{
		this.fondo = document.createElement("div");
		this.fondo.id = 'floaterBackground';
		this.fondo.className = this.options.fondoClass;
		this.fondo.onclick = this.closeWindow.bind(this);
		if ( !this.ie ) this.fondo.style.zIndex = this.baseZIndex + 'px';
		this.fondo.style.display='none';
		document.body.appendChild(this.fondo);
		Element.show(this.fondo);
	},
	
	setBackgroundSize: function()
	{
		this.fondo.style.width = this.getWidth() + 'px';
		this.fondo.style.height = this.getHeight() + 'px';
	},
	
	reposition: function( positionX, positionY)
	{
		if( !this.fondo )
		{
			this.fondo = $('floaterBackground');
		}
		var clientWidth=self.innerWidth?(self.innerWidth-document.documentElement.clientWidth<20?document.documentElement.clientWidth:self.innerWidth):(document.documentElement.clientWidth||document.body.clientWidth);
		var clientHeight=self.innerHeight?(self.innerHeight-document.documentElement.clientHeight<20?document.documentElement.clientHeight:self.innerHeight):(document.documentElement.clientHeight||document.body.clientHeight);
		var scrollLeft=self.pageXOffset||document.documentElement.scrollLeft||document.body.scrollLeft;
		var scrollTop=self.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop;
		var scrollHeight=[document.body.scrollHeight,document.documentElement.scrollHeight,document.body.offsetHeight].max();
		var scrollWidth=[document.body.scrollWidth,document.documentElement.scrollWidth,document.body.offsetWidth].max();
		
		if( this.container )
		{
			var height = this.options.height;
			var width = this.options.width;
			var margin = 30;
				
			switch(positionY)
			{
				case'top':
					y = margin;	
				break;
				
				case'bottom':
					y = clientHeight - height - margin;
				break;
				
				case'middle':
					y = (clientHeight - height)/2
				break;
				
				default:
					y = positionY;
				break;
			}
			
			switch( positionX )
			{
				case'left':
					x = margin;
				break;
				
				case'right':
					x = clientWidth - width - margin;
				break;
				
				case 'center':
					x = (clientWidth - width)/2
				break;
				
				default:
					x = positionX;
				break;
			}
		}
		else
		{
			container = $('loginFloater');
			var x=(clientWidth-container.offsetWidth)/2+scrollLeft;
			var y=(clientHeight-container.offsetHeight)/2+scrollTop;
		}
		
		Element.setCumulativeOffset(this.container,x,y);
		
		if ( this.fondo )
		{
			this.fondo.style.width = [scrollWidth,clientWidth+scrollLeft].max() + 'px';
			this.fondo.style.height = [scrollHeight,clientHeight+scrollTop].max() + 'px';
		}
	},
	
	sendToFront: function()
	{
		jsWindowZIndex++;
		this.container.style.zIndex = (this.baseZIndex + jsWindowZIndex);
	},
	
	loadWindow: function(title, url, options)
	{
		//new  Ajax.Updater(this.content, this.url, {evalScripts: true, asynchronous:true});
		this.content.innerHTML = '<iframe src="' + this.url + '" width="' + this.options.width + '" height="' + this.options.height + '"></iframe>';
	},
	
	closeWindow: function()
	{
		new Effect.Fade(this.container,{duration:0.5});
		document.body.removeChild(this.container);
		this.container = null;
		
		if ( this.options.blockBackground ) 
		{
			Effect.Fade(this.fondo,{duration:0.3});
			document.body.removeChild(this.fondo);
			this.fondo = null;
		}
		this.options.onClose();
	}
};
