/*
	preview.js: image previews on links with the 'preview="previewimg"'
	            attribute. Requires the Prototype and Scriptaculous
	            javascript libraries from <http://www.prototypejs.com/>
	            and <http://script.aculo.us/>.
	Copyright (c) 2007 Kalle Räisänen.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


	See <http://vcm.fivebyfive.be/js/preview_usage.txt> for documentation.
*/

var preview_options = {
	bounded:      true,
	fade:         true,
	fadeduration: 1.0,
	fadestart:    0.0,
	fadeend:      1.0,
	imagedir:     '',
	pagecontainer: 'page'
};

var preview_images = [];

function preview_mkimage(impath)
{
	return Builder.node(
		'img', {
			src:   preview_options.imagedir + impath,
			alt:   impath,
			style: 'margin: 0;'
		}
	);
}

function preview_preload_images()
{
	$A(arguments).each( function(arg) {
		if(typeof preview_images[arg] == 'undefined')
			preview_images[arg] = preview_mkimage(arg);
	});
}

var preview_visible = null;

function preview_show(e)
{
	var el     = $(Event.findElement(e, 'a'));
	var lid    = el.id;
	var impath = el.getAttribute('p:preview');

	if(impath == null) {
		alert(el +' WTF?');
		return false;
	}

	if(typeof preview_images[impath] == 'undefined') {
		preview_images[impath] = preview_mkimage(impath);
	}

	var div = Builder.node(
		'div', {
			className: 'preview-div',
			id:        'img-div-' + impath.replace(/\.(png|gif|jpe?g)$/i, ''),
			style:     'left: -800px; top: -800px; position: absolute;'
		},
		[ preview_images[impath] ]
	);
	
	preview_visible  = div.id;
	Event.observe(el, 'mousemove', preview_mouse_moved);
	Event.observe(el, 'click',     preview_hide);

	$(preview_options.pagecontainer).appendChild(div);

	if(typeof Effect != 'undefined' && preview_options.fade == true) {
		div.setAttribute(
			'style',
			'display: none;'
		);
		Effect.Appear(
			div.id, {
				duration: preview_options.fadeduration,
				from:     preview_options.fadestart,
				to:       preview_options.fadeend
			}
		);
	}

	preview_mouse_moved(e);
}

function preview_hide(e)
{
	var el     = $(Event.findElement(e, 'a'));
	if($(preview_visible))
		$(preview_options.pagecontainer).removeChild($(preview_visible));

	preview_visible  = null;

	Event.stopObserving(el, 'mousemove', preview_mouse_moved);

	return true;
}

function preview_mouse_moved(e)
{
	if(preview_visible != null) {
		var pos = {'x': Event.pointerX(e), 'y': Event.pointerY(e)}; //_get_mouse_pos(e);
		pos['y'] += 5;

		if(preview_options.bounded == true) {
			var ih = $(preview_visible).getElementsByTagName('img')[0].height;
			var iw = $(preview_visible).getElementsByTagName('img')[0].width;

			var b_dist =
				window.innerHeight - ((pos['y'] - document.documentElement.scrollTop) + ih + 10);

			if(b_dist <= 0)
				pos['y'] += (b_dist);

			b_dist =
				window.innerWidth - ((pos['x'] - document.documentElement.scrollLeft) + iw + 40);
			if(b_dist <= 0)
				pos['x'] += (b_dist);
		}

		$(preview_visible).style.left = (pos['x'] + 10) + 'px';
		$(preview_visible).style.top  = (pos['y']) + 'px';
	}
}

function preview_init(opts)
{
	if(typeof opts != 'undefined') {
		$H(opts).each(function(pair) {
			if(typeof preview_options[pair.key] != 'undefined')
				preview_options[pair.key] = pair.value;
		});
	}
	$$('[p:preview]').each( function(el) {
		var prev = el.getAttribute('p:preview');
		Event.observe(el, 'mouseover', preview_show);
		Event.observe(el, 'mouseout',  preview_hide);
		preview_preload_images(prev);
	});
}

