/*	
	daz_auto_window 1.1.1  (formerly autosize_open)
	A JavaScript object for opening images in popup windows 
		that automatically resize themselves to fit.
	
	Copyright 2003 Thomas Peri
	
	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 a copy of your changes.
	(ispleen at freeshell dot org)
	

	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.

*/

	onerror = function(e,u,l)
	{
		alert(l+','+e);
	}

var daz_auto_window = new Object();
/* usage is just like window.open(), but with an optional extra argument:
	title is the titlebar title of the popup window */
daz_auto_window.open = function(img_href,name,features,replace,title)
{
	/*	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.
	*/
	
	/* allow omission of 2nd slash after protocol, to account for local
		files.  Safari only puts one slash after file: URLs. */
	var uri = /^(\w+:\/+[^\s\/]+)\/(\S+\/)*/;
	var page_uri = window.location.href.match(uri);
	
	/* if the image href is relative to the server document root,
		then prepend the protocol and server name */
	if (img_href.charAt(0) == "/")
		img_href = page_uri[1] + 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 = page_uri[0] + img_href;
			
	/* if title is missing, use the url of the image */
	if (!title) title = img_href;
	
	/* strip spaces from features, and 
		determine whether the popup is to have scrollbars */
	features = features.replace(/\s/g,"");
	var scroll = (features.search(/scrollbars($|,|=yes)/) >= 0);
	
	/* grab the values of features that have to have values to exist */
	var feat = new Object();
	var featstat = "screenX screenY left top".split(" ");
	for (var i = 0; i < featstat.length; i++)
		feat[featstat[i]] = this.extract(features,featstat[i]);
	
	var posx = this.compare(feat,"left","screenX");
	var posy = this.compare(feat,"top","screenY");
	
	/* 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>');
	p('function loaded(img) {');
	p('opener.daz_auto_window.resize(img,window,'+posx+','+posy+');');
	p('}');
	p('</script>');
	p('</head><body style="background:#fff;margin:0px;">');
	/* if the window is not supposed to scroll, make sure it doesn't. */
	p('<div style="text-align:center;'+
		(scroll?'':'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 style="color:#000;">');
		p('<br>Loading Image...</span></div>');
	p('</body></html>');
	
	/* open the window and print the html to it */
	return window.open("javascript:'"+escape(html)+"';",
		name,features,replace);
}
daz_auto_window.extract = function(features,feature)
{
	/* parse the feature list for the desired features 
		(must be features that have values) */
	var check = features.match(new RegExp(feature+"=([^, ]+)","i"));
		/* n.b.: the "i" is necessary, as the feature 
			might be in a different case */
	return check?check[1]:null;
}
daz_auto_window.compare = function(feats,feat1,feat2)
{
	/* Accept an object of features, along with two feature names.
		Compare them, and return the first one that is defined.
		Issue an alert if they are both defined, but different. */
	var feat = null;
	var msg = "  Whenever you specify either of these features, you "+
		"should also specify the other with the same value.";
	/* only proceed if at least one is defined */
	if (feats[feat1] || feats[feat2])
	{
		/* only proceed if both are defined */
		if (!feats[feat1] || !feats[feat2])
			alert('You have specified either the "'+feat1+
				'" or "'+feat2+'" feature, but not both.'+msg);
		/* only proceed if both are equal */
		else if (feats[feat1] != feats[feat2])
			alert('You have specified different values for the "'+
				feat1+'" and "'+feat2+'" features.'+msg);
		/* set one of them to be returned */
		else
			feat = feats[feat1];
	}
	return feat;
}
daz_auto_window.resize = function(img,win,posx,posy)
{
	/* move the window as necessary for the resize.
		default to centered, and always keep window on-screen */
	
	/* where to move to (start at requested location) */
	var movex = posx, movey = posy;	
	
	/* stash screen size */
	var swidth = screen.availWidth;
	var sheight = screen.availHeight;

	/* fall back to defaults (centered and on-screen) */	
	/* X position */
	if (posx == null) 
		movex = Math.round((swidth - img.width) / 2);
	else if (posx + img.width > swidth)
		movex = swidth - img.width; 
	/* Y position */
	if (posy == null)
		movey = Math.round((sheight - img.height) / 2);
	else if (posy + img.height > sheight)
		movey = sheight - img.height; 

	/* perform the move */
	win.moveTo(movex,movey);

	/* get as close as possible to the desired size */
	win.resizeTo(img.width,img.height);
	
	/* 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)
		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";
	}
}

