// Creates an object that can be used later to easily open popup windows.
// Usage:
// var SpecialWindow = new PopupWindow( 'foobar', {width:100, height: 50} );
// <a href='blah.html' onclick='SpecialWindow.popup(this);return false;'>Pop me up</a>
var PopupWindow = Class.create();
PopupWindow.prototype = {
	initialize: function( target, options ) {
		this.target = target || '_blank';
		this.options = {
			location: 0,
			statusbar: 0,
			menubar: 0,
			width: 400,
			height: 300,
			resizable: 1,
			scrollbars: 1
		};
		Object.extend( this.options, options || {} );
	},
	
	getFeatures: function() {
		return $H(this.options).map( function(pair) {
			return pair.join('=');
		}).join(',');
	},
	
	popup: function( destination ) {
		if( destination )
		{
			var url = destination.href || destination;
			var childWindow = window.open( url, this.target, this.getFeatures() );
			if(childWindow)
				childWindow.focus();
			return childWindow;
		}
	}
};

var HelpWindow = new PopupWindow('help', { width: 300, height: 470 });
var PrintWindow = new PopupWindow('print', { width: 800, height: 640 });

// Creates an image that changes when the user mouses over it.
// new HoverImage("someImage", "http://example.org/someImageUrl.jpg");
var HoverImage = Class.create();
HoverImage.prototype = {
	initialize: function(imageTag, mouseOverImage) {
		this.image = $(imageTag);
		this.offSrc = this.image.src;
		this.onSrc = mouseOverImage.replace(/&amp;/g, '&');
		
		Event.observe(this.image, "mouseover", this.onMouseOver.bindAsEventListener(this));
		Event.observe(this.image, "mouseout", this.onMouseOut.bindAsEventListener(this));
	},
	
	onMouseOver: function(evt) {
		this.image.src = this.onSrc;
	},
	
	onMouseOut: function(evt) {
		this.image.src = this.offSrc;
	}
}

// Look through the document for images with an attribute "hover" and create a hover image object for each of them.
HoverImage.parseDocument = function()
{
	var imgTags = document.getElementsByTagName("img");
	var images = $A(imgTags);
	images.each(function(image) {
		var hover = image.getAttribute("hover");
		if(hover)
			new HoverImage(image, hover);
	});
}

Event.observe(window, "load", HoverImage.parseDocument);

// This function will disable all select boxes on the screen - useful to call right before doing a postback
// if you don't want the user to try changing multiple selections
// TODO: Actually call this method when the user selects something
function disableSelects()
{
	$A(document.getElementsByTagName("select")).each(function(s)
	{
		s.disabled = true;
	});
}