// window.image.js
// Displays an image in a window, requires xilinus windows.js, prototype.js and scriptaculous effects.js
//
// Copyright (c) 2007 Modern Webspace, Inc. (http://modernwebspace.com, http://bgok.net)
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice, associated urls and this permission notice
// shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
Object.extend(Window.prototype, {
	maxWindowWidth: function(padding) {
		if (!padding)
			padding = 0;
		return Math.min(
			this.options.maxWidth,
			WindowUtilities.getPageSize().windowWidth - (this.widthW + this.widthE + (padding * 2))
		);
	},
	maxWindowHeight: function(padding) {
		if (!padding)
			padding = 0;
		return Math.min(
			this.options.maxHeight,
			WindowUtilities.getPageSize().windowHeight - (this.heightN + this.heightS + (padding * 2))
		);
	},
	widthTotal: function() {
		return this.width + this.widthW + this.widthE;
	},
	heightTotal: function() {
		return this.height + this.heightN + this.heightS;
	},
	setScale: function(factor) {
		this.setSize(Math.round(this.options.maxWidth * factor), Math.round(this.options.maxHeight * factor));
	},
	getWidthScale: function(size) {
		return(size/this.options.maxWidth);
	},
	getHeightScale: function(size) {
		return(size/this.options.maxHeight);
	},
	centerInBrowser: function(top, left) {
		this._center(top, left);
	}
});

Dialog.image = function(image, opts) {
	var options = Object.extend({
		className:		'darkX',
		width:			320,
		height:			180,
		padding:		10,
		zIndex:			Math.max(100, Windows.maxZIndex + 1),
		resizable:		false,
		title:			'Image',
		draggable:		true,
		wiredDrag:		false
	}, opts || {});
	var w = new Window({
		className: options.className,
		destroyOnClose: true,
		maxWidth: options.width,
		maxHeight: options.height,
		title: options.title,
		resizable: options.resizable,
		zIndex: options.zIndex
	});
	w.setScale(Math.min(
		w.getWidthScale(w.maxWindowWidth(options.padding)),
		w.getHeightScale(w.maxWindowHeight(options.padding))
	));
	w.centerInBrowser();
	var id = w.getId() + "_image";
	w.setHTMLContent('<img id="'+ id + '" src="' + image + '" width="' + w.width + '" height="' + w.height + '" />');
	id = $(id);
	id.setStyle({cursor:'pointer'});
	// if the image is clicked, maximize it
	Event.observe(id, 'click', function() {
		w.maximize();
	});
	ob = {
		onMaximize: function(eventName, win) {
			if (win == w) {
				if (win.isMaximized()) {
					win.setSize(win.options.maxWidth, win.options.maxHeight, true);
				} else {
					win.setScale(Math.min(
						win.getWidthScale(win.maxWindowWidth(options.padding)),
						win.getHeightScale(win.maxWindowHeight(options.padding))
					));
				}
				// resize the image to fill the window
				id.height = win.height;
				id.width = win.width;
			}
		},
		onResize: function(eventName, win) {
			if (win == w) {
				// maintain the proper aspect ratio
				win.setScale(Math.max(
					win.getWidthScale(win.width),
					win.getHeightScale(win.height)
				));
				// Resize the image
				id.height = win.height;
				id.width = win.width;
			}
		},
		onDestroy: function(eventName, win) {
			if (win == w) 
				Windows.removeObserver(this);
		}
	}
	Windows.addObserver(ob);
	w.show(); 
}
