// JavaScript Document

/****************************************************
This is a generic pop up window object.  Use it to 
return Ajax info in a window floating above the
center of the page.  You may have as many Window
objects on a page as you want but each Window object
can only have one window open at a time.  Windows
always open in the center of the page and cannot be
moved.  The window will be as small as it can so you
should add any padding or styling on your ajax page.

/---------------------------------------------------\
| constructor - initializes the Window object.		|
|---------------------------------------------------|
| Parameters:										|
| 	String url => The url of your ajax file.		|
| Returns:											|
| 	nothing											|
| Example:											|
|	var win = new Window('www.byu.edu');			|
\---------------------------------------------------/

/---------------------------------------------------\
| open() - appends a new div to the en of the page	|
|   and populates it with an ajax call.				|
|---------------------------------------------------|
| Parameters:										|
| 	String or Hash params => The paramaters to be	|
|	  passed to the ajax file.  This can either be	|
|     a URL-like query string (ex 'id=15&name=Bob') |
|     or a hash (ex {id: 15, name: 'Bob'}).			|
| Returns:											|
| 	nothing											|
| Example:											|
|	win.open({id: 15, name: 'Bob'});				|
\---------------------------------------------------/

/---------------------------------------------------\
| close() - removes the previously created div.		|
|---------------------------------------------------|
| Parameters:										|
| 	nothing											|
| Returns:											|
| 	nothing											|
| Example:											|
|	win.close();									|
\---------------------------------------------------/


****************************************************/


function isSString() {
	if (typeof arguments[0] == 'string')
		return true;
	if (typeof arguments[0] == 'object'){
		var criterion = arguments[0].constructor.toString().match(/string/i); 
 		return (criterion != null);
	}return false;
}

var Window = Class.create({
	initialize: function(url){
		this.IE = !!(window.attachEvent && !window.opera);
		this.url = url;
		this.win = null;
		this.shadow = null;
		this.container = null;
		this.minWidth = null;
		this.isOpen = false;
		this.scrolls = new Array();
		
		// Customization Options
		this.fadeBackground = false;
		this.shadowOpacity = '0.25';
	},
	
	open: function(params){
		var json;
		
		if(isSString(params))
			json = params.evalJSON();
		else
			json = params;
			
		if(json.html){
			this.displayWindow(json.html);
		}else{
			this.displayWindow("<img src=\"/images/loadingCircles.gif\" />&nbsp;Loading...");
			new Ajax.Request(this.url,{
				method: 'post',
				parameters: params,
				evalJS: true,
				onSuccess: function(transport) {
					this.displayWindow(transport.responseText);
				}.bind(this),
				onFailure: function(){
					alert('There was an error loading this page.');
				}.bind(this)
			});
		}
	},
	
	close: function(){
		if (this.isOpen){
			this.win.remove();
			this.isOpen = false;
			this.scrolls.each(function(s){
				s.style.overflow = 'scroll';
			});
		}
		return false;
	},
	
	displayWindow: function(content){
		
		this.close();
		if (content != ''){
			this.win = document.createElement('div');
			this.shadow = document.createElement('div');
			this.container = document.createElement('div');
			this.minWidth = document.createElement('span');
			
			Element.extend(this.win);
			Element.extend(this.shadow);
			Element.extend(this.container);
			Element.extend(this.minWidth);
			
			document.body.appendChild(this.win);
			this.win.appendChild(this.shadow);
			this.win.appendChild(this.container);
			this.container.appendChild(this.minWidth);
			
			this.minWidth.update(content);
			
			this.container.style.position = 'absolute';
			this.container.style.top = '0px';
			this.container.style.left = '0px';
			this.container.style.backgroundColor = '#ffffff';
			this.container.style.border = '1px solid #333333';
			
			var scrollOffset = document.viewport.getScrollOffsets();
			
			this.win.style.width = this.minWidth.getWidth() + 5 + 'px';
			this.win.style.position = 'absolute';
			this.win.style.top = scrollOffset.top + document.viewport.getHeight()/2 - (this.container.getHeight()/2) + 'px';
			this.win.style.left = scrollOffset.left + document.viewport.getWidth()/2 - (this.container.getWidth()/2) + 'px';
			this.win.style.zIndex = 1002;
			
			if (!this.fadeBackground){
				this.shadow.style.height = this.container.getHeight() + 'px';
				this.shadow.style.width = this.container.getWidth() + 'px';
				this.shadow.style.position = 'absolute';
				this.shadow.style.top = '5px';
				this.shadow.style.left = '5px';
				this.shadow.style.backgroundColor = '#000000';
				this.shadow.setOpacity(this.shadowOpacity);
			}
			
			$$('div').each(function(s,i){
				if (s.style.overflow == 'scroll'){
					this.scrolls[i] = s;
					s.style.overflow = 'auto';
				}
			}.bind(this));
			this.scrolls = this.scrolls.compact();
			
			this.isOpen = true;
			
			// Customization Options
			if (this.fadeBackground){
				this.shadow.style.opacity = '0';
				this.background = document.createElement('div');
				Element.extend(this.background);
				this.background.style.position = 'fixed';
				this.background.style.top = '0px';
				this.background.style.left = '0px';
				this.background.style.height = document.viewport.getHeight()+'px';
				this.background.style.width = document.viewport.getWidth()+'px';
				this.background.style.backgroundColor = '#000000';
				if (this.IE)
					this.background.style.filter = 'alpha(opacity='+this.shadowOpacity.split('.').last().truncate(2)+')';
				else
					this.background.style.opacity = this.shadowOpacity;
				this.win.insert({'top':this.background});
			}
		}
	},
	
	close: function(){
		if (this.isOpen){
			this.win.remove();
			this.isOpen = false;
			this.scrolls.each(function(s){
				s.style.overflow = 'scroll';
			});
		}
	}
});
			