/*	
	daz_auto_window 1.2
	A JavaScript object for opening images in popup windows 
		that automatically resize themselves to fit.
	
	Copyright 2003 Thomas Peri
	http://www.infospleen.com/tools/
	
	You may copy, modify, and redistribute this script as you 
	wish, but please keep this notice and the copyright notice
	intact.  If you improve on the script, add a line or two 
	below, describing the changes you made.
	
	I'd also appreciate it if you'd e-mail me your changes and
	any bug reports.  (thomas at infospleen dot com)
	

	KNOWN ISSUES:
			
	* The script ignores any <base href="..."> tag, since it pulls from 
		the current window.location.href instead.
	
	* In Safari, the window jitters a little.  I know what causes it,
		but I haven't come up with a way to fix it without aggravating 
		an even more annoying bug: Safari's disregard of "scrollbars=no"
		in the window features.
	
*/

var daz_auto_window = new Object();
/* usage is just like window.open(), but with two optional extra arguments:
	title is the titlebar title of the popup window, and
	center is whether or not to center the popup window */

/* default message */
daz_auto_window.messageHTML = 
	'<br><div style="text-align:center;">Loading Image...<'+'/div>';
/* default style tag */
daz_auto_window.styleText = 
	'body{background:white;color:gray;font-family:"Arial Black";}';

daz_auto_window.open = function(
	/*string*/  img_href,
	/*string*/  name,
	/*string*/  features,
	/*boolean*/ replace,
	/*string*/  title,
	/*boolean*/ center)
{
	/*	Here, we make sure the image href is a full URI (converting
		it if necessary), because the popup has a "javascript:"
		url, to which an href can't be relative.
	*/
	
	/* extract important parts of page location */
	var L = window.location;
	var docroot = L.protocol + "//" + L.host;
	var prefix = (docroot + L.pathname).replace(/\/[^\/]*$/,"/");
		/* (prefix is the path to the directory the page is in) */
	
	/*	If the image href is relative to the server document root,
		then prepend the protocol and server name.
		
		In determining whether the URI is valid, accept any number 
		of slashes after protocol, to account for local "file:" 
		urls, which can have varying numbers of slashes, depending 
		on the browser.
	*/
	var uri = /^(\w+:\/+[^\s\/]+)\/(\S+\/)*/;
	if (img_href.charAt(0) == "/")
	{
		img_href = docroot + img_href;
	}

	/* otherwise, if the url we're given isn't a full URI,
		then prepend the current directory prefix */
	else if (img_href.match(uri) === null)
	{
		img_href = prefix + img_href;
	}

	
	/* if title is missing, use the url of the image */
	if (!title)
	{
		title = img_href;
	}
	
	
	/* deal with window features */
	
	features = features?features:"";
	
	var scroll = (features.search(/scrollbars($|,|=yes)/) >= 0);	

	var feats = this.explode(features);
	
	/* set some default dimensions if they haven't been set */
	if (!feats.width)
	{
		feats.width = 250;
	}
	if (!feats.height)
	{
		feats.height = 100;
	}
	
	/* if we're centering, override any left and top features */
	if (center)
	{
		feats.left = Math.round((screen.availWidth - feats.width)/2);
		feats.top = Math.round((screen.availHeight - feats.height)/2);
	}

	/* (over)write screenx/screeny features with left/top */
	if (feats.left != feats.screenx)
	{
		if (feats.left)
		{
			feats.screenx = feats.left;
		}
		else if (feats.screenx)
		{
			feats.left = feats.screenx;
		}
	}
	if (feats.top != feats.screeny)
	{
		if (feats.top)
		{
			feats.screeny = feats.top;
		}
		else if (feats.screeny)
		{
			feats.top = feats.screeny;
		}
	}
	
	// put them back into a string
	features = this.implode(feats);
	
	
	/* generate the popup window html -- NO SINGLE QUOTES! 
		the popup will then call back to functions on the issuing page. */
	var html = '';	
	var p = function(s) {html += s;};
	p('<html><head><title>'+title+'</title>');
	p('<script type="text/javascript">');
	p('function loaded(img) {');
	p('opener.daz_auto_window.resize(img,window,'+feats.left+','+feats.top+','+center+');');
	p('}');
	p('</script>');
	p('<style>'+this.styleText+'</style>');
	p('</head><body style="margin:0px;">');
	/* if the window is not supposed to scroll, make sure it doesn't. */
	p('<div'+(scroll?'':' style="width:100%;height:100%;overflow:hidden;"')+'>');
	p('<img onload="loaded(this);" style="visibility:hidden;');
		p('position:absolute;left:0px;top:0px;" ');
		p('src="'+img_href+'"><span>'+this.messageHTML+'</span></div>');
	p('</body></html>');
	
	/* escape single quotes for printing source to window*/
	html = html.replace(/'/g,'\\\'');
	
	/* open the window and print the html to it */
	return window.open("javascript:'"+escape(html)+"';",
		name,features,replace);
};
daz_auto_window.explode = function(featureList)
{
	var featureArray = featureList.toLowerCase().replace(/\s/g,"").split(',');
	var features = new Array();
	
	var pair;
	for (var i = 0; i < featureArray.length; i++)
	{
		if (featureArray[i])
		{
			pair = featureArray[i].split('=');
			features[pair[0]] = pair[1];
		}
	}
	
	return features;
};
daz_auto_window.implode = function(features)
{
	var featureList = "";
	var first = true;
	for (var i in features)
	{
		// put commas after the first pair
		if (!first) 
		{
			featureList += ',';
		}
		featureList += i;
		if (features[i] !== '') 
		{
			featureList += '=' + features[i];
		}
		first = false;
	}
	
	return featureList;
};

/**
 * Resize the popup window to the size of the image,
 * repositioning if necessary to keep it on-screen.
 */
daz_auto_window.resize = function(img,win,posx,posy,center)
{
	
	/* stash screen size */
	var swidth = screen.availWidth;
	var sheight = screen.availHeight;
	
	/* determine new window size */
	var iwidth = (img.width<=swidth) ? img.width : swidth;
	var iheight = (img.height<=sheight) ? img.height : sheight;
	
	/* override posx and posy if we're centering */
	if (center)
	{
		posx = Math.round((swidth-img.width)/2);
		posy = Math.round((sheight-img.height)/2);
	}

	/* keep it on-screen */

	/* X position */
	if (posx + img.width > swidth) 
	{
		posx = swidth - img.width;
	}
	if (posx < 0) 
	{
		posx = 0;
	}

	/* Y position */
	if (posy + img.height > sheight) 
	{
		posy = sheight - img.height; 
	}
	if (posy < 0) 
	{
		posy = 0;
	}
	
	
	/* perform the move */
	win.moveTo(posx,posy);


	/* get as close as possible to the desired size */
	win.resizeTo(iwidth,iheight);

	
	/* compensate for browser size quirks */
	var cw, ch;	/* client width and height */
	if (win.innerWidth)
	{
		cw = win.innerWidth;
		ch = win.innerHeight;
	}
	else if (win.document.body.clientWidth)
	{
		cw = win.document.body.clientWidth;
		ch = win.document.body.clientHeight;
	}
	if (cw && ch)
	{
		win.resizeBy((img.width-cw),(img.height-ch));
	}
	
	/* make image visible and "Loading Image" invisible */
	if (img.nextSibling)
	{
		img.nextSibling.style.display = "none";
	}
	if (img.style)
	{
		img.style.position = "relative";	/* jitterbug */
		img.style.visibility = "visible";
	}
};

function ezWindow(anchor, title)
{
	var win = daz_auto_window.open(
		/* image url     */ anchor.href,
		/* window name   */ "",
		/* features      */ "scrollbars=no,status=yes,resizable=yes,width=300,height=125",
		/* replace       */ false,
		/* window title  */ title,
		/* center window */ true
	);
	
	win.focus();
}
