// JavaScript Document
var scroll_width = 18;

function iWindow() 
{
	
	var	_this = this;
	
	var container = null;
	
	var iframe = null;
	
	var iscreen = null;
	
	var url = null;
	
	var close_btn = null;
	
	var _height = null;
	
	var _width = null;
	
	var left = null;
	
	var top = null;
	
	var base_top = null;
	
	var iframe_height = 700;
	
	var iframe_width = 600;
	
	var min_height_offset = 55;
	
	var win_offset_width = .9;
	
	var win_offset_height = .8;

	var iscreen_opacity = .5;

	var is_active = false;
	
	var screen_pad = 50;


	this.init = function(iWindow) 
	{
		
		if (!iWindow) { return; }
		
		_this.container 																			= iWindow;
		_this.iframe 																						= iWindow.getElement('iframe');
		_this.iscreen 																					= iscreen;		
		_this.close_btn																				= iWindow.getElement('.close_iframe');		
		_this.iframe_height 															= iframe_height;
		_this.iframe_width 																= iframe_width;
		_this.min_height_offset 	= min_height_offset;
		_this.win_offset_width 		= win_offset_width;
		_this.win_offset_height 	= win_offset_height;
		_this.iscreen_opacity 		= iscreen_opacity;
		_this.base_top				= base_top;
		_this.is_active				= is_active;
		_this.screen_pad				= screen_pad;
		_this.create_screen();



	}
	
	this.open = function(url)
	{
				win = _this.get_win_cords();

		
		//alert("win cords: " + win['height'] + "  iframe_width: " + (win['width'] - iframe_width) /2);
		_this.is_active = true;
		_this.iscreen.setStyles({									
									display: 'block',
									opacity:iscreen_opacity
								});
		

		_this.position_window();
		
		_this.container.setStyles({
									display: 'block'
								   });	


		_this.iframe.src = _this.url = url;
		
		
		_this.close_btn.addEvent('click', function(){_this.close(); });
		
		window.addEvent('resize', function(){_this.resize(); });
		if (Browser.Engine.trident) {
			window.addEvent('scroll', function(){_this.scrolling(); });
		}


		
	}

	this.close = function()
	{
		
		_this.close_btn.removeEvent('click');
		window.removeEvent('resize');
		
		if (Browser.Engine.trident) {
			window.removeEvent('scroll');
		}
		
		_this.iscreen.setStyles({
			display: 'none'
		});

		_this.container.setStyles({
			display: 'none'
		});
		
		_this.iframe.src = "";
		_this.is_active = false;
		
		return false;
	}

	this.get_frame_width = function ()
	{
		width = _this.iframe_width;
		win = _this.get_win_cords();

		if (win['width'] > _this.iframe_width) 
		{			
			width = win['width'] * _this.win_offset_width;
		}
		
		//alert("Window width: " + win['width'] + "    iframe width: " + _this.iframe_width);
		return Math.round(width);
	}
	
	this.get_frame_height = function ()
	{
		height = _this.iframe_height;
		win = _this.get_win_cords();

		if (win['height'] > _this.iframe_height) 
		{
			height = win['height'] * _this.win_offset_height;
		}  
		else 
		{
			height = win['height'] - _this.min_height_offset;
		}
		return Math.round(height);	
	}
	
	this.get_frame_top = function ()
	{
		var _top = 0;
		win = _this.get_win_cords();
		if ( _this._height  < win['height']  - _this.min_height_offset) 
		{		
			_top = (win['height'] - _this._height)/2/2;
		}
		return Math.round(_top);
		
	}
	
	this.get_frame_left = function (win)
	{		
		win = _this.get_win_cords();
		return Math.round((win['width'] - _this._width) /2);
		
	}
	
	this.get_win_cords = function()
	{
		return document.getCoordinates();
	}
	
	
	
	this.create_screen = function()
	{
		_this.iscreen = new Element('div', {
											'id' 	: 'iframe_screen',	 
											'class' : 'iScreen'
											
										});
					
		_this.iscreen.inject(document.body, 'top');
		
	}
	
	this.position_window = function()
	{
		
		if (_this.is_active)
		{
		
			_this._width = _this.get_frame_width();
			_this._height = _this.get_frame_height();
			_this.left = _this.get_frame_left();
			_this.top = _this.base_top + _this.get_frame_top();
			
			//alert("OPEN SCALED WIN: " + url);
			_this.iframe.setStyles({
										"width": _this._width,
										"height":_this._height
									});		
	
			
			_this.container.setStyles({
										"margin-left": _this.left-scroll_width,
										"margin-right": _this.left,
										"top": _this.top,
										"display": 'block'
									   });	
			
			if (Browser.Engine.trident) 
			{
				
				_this.iscreen.setStyles({
										'top': _this.base_top -_this.screen_pad,
										'height': win['height'] + _this.screen_pad									
										});				
			}

			
		} //// EOF IF
	}
	
	/* ------------- EVENT HANDLERS ----- */
	
	this.resize = function()
	{
		_this.position_window();
	}

	this.scrolling = function()
	{
		
		//window.onscroll=function() { window.scrollTo(0,0); }
		
		var scroll = window.getScroll();

		_this.base_top = scroll.y;
		_this.position_window();
	}


}