// Auto-Resize-Window. last-update: 2006.01.11.
	// (c) snow-materia "http://sm.useyan.com/"
	// This script create new window resized by the automatic operation.


//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	Setting
var A_R_W_set = {
	// class-name to get link elements
	trigger_class : 'resize',

	// id-name to set blocks
	image_id : 'image',	// image
	comment_id : 'comment',	// comment
	window_name : 'hoge',	// default-window-name

	// visit to the image once ? [1:yes, 0:no]
	visit : 1,	// (*) the image should be in the same domain

	// window-open-status
	status : 'left=,top=20,width=88,height=31,resizable=yes,scrollbars=no,'+
			 'menubar=no,toolbar=no,location=no,directories=no,status=no,favorites=no',

	// event-name of window.close
	window_close : 'dblclick,contextmenu',

	// style-sheet ?
	style :
//		'<link rel="stylesheet" type="text/css" media="screen,tv" href="" />\n'+
		'<style type="text/css">\n'+
		'	body { overflow:hidden; }\n'+
		'	* { margin:0; padding:0; }\n'+
		'</style>\n'
};




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	Script
// Auto-Resize-Window set
var A_R_W = function (obj) {
	if (!obj) return null;
	this.resized = false;

	// href
	this.src = obj.getAttribute('href') || obj.getAttribute('src') || null;
	if (!this.src) return null;

	// title
	this.title = obj.getAttribute('title') || '';
	var text = getInnerText(obj);
	if (text) this.title = text +' - '+ this.title;

	// comment
	this.comment = getInnerComment(obj) || '';;

	// window-open
	this.window = window.open(
		(A_R_W.set.visit) ? this.src : '', A_R_W.set.window_name, A_R_W.set.status
	);
	if (!this.window) return null;

	// document-write
	this._create_html();

	// focus
	if (this.window.focus) this.window.focus();

	// image onload
	var img = this.window.document.getElementsByTagName('IMG');
	if (img && img[0]) {
		this.image = img[0];
		if (this.image.complete) {
			this.resize('image.complete');
		} else {
			A_R_W._addEvent('load',
				function (o) { return function () { o.resize('image.onload'); } }(this),
					false, this.image);
		}
	}

	// onload event
	A_R_W._addEvent('load',
		function (o) { return function () { o.resize('window.onload'); } }(this),
			false, this.window);

	// close event
	if (A_R_W.set.window_close) {
		var events = A_R_W.set.window_close.split(/,/);
		for (var i = events.length; i--;) {
			A_R_W._addEvent(events[i],
				function (w) { return function () { w.close(); return false; } }(this.window),
					false, this.window.document);
		}
	}
};


// resize ?
A_R_W.prototype.resize = function (t) {
	if (!this.window || !this.window.resizeTo || this.resized) return;
	this.resized = true;

	// top-element
	var top_element = getTopElement(this.window);

	// image-resize
	var iwidth  = this.image.width  || 0;
	var iheight = this.image.height || 0;
	if (iwidth && iheight) this.window.resizeTo(iwidth, iheight);

	// size
	var width  = 0;
	var height = 0;
	if (top_element) {
		// document-size
		width  = (top_element.scrollWidth  > top_element.offsetWidth) ?
					top_element.scrollWidth  : top_element.offsetWidth;
		height = (top_element.scrollHeight > top_element.offsetHeight) ?
					top_element.scrollHeight : top_element.offsetHeight;
	}
	if (!width  || width  < iwidth)  width  = iwidth;
	if (!height || height < iheight) height = iheight;
	if (!width  || !height) return;

	// resize to
	this.window.resizeTo(width, height);

	// window-correction
	var x = top_element.clientWidth  || this.window.innerWidth;
	var y = top_element.clientHeight || this.window.innerHeight;
	if (!x || !y) return;

	// resize by
	this.window.resizeBy(width - x, height - y);
};


// html ?
A_R_W.prototype._create_html = function () {
	if (!this || !this.window) return;

	// write document
	var d = this.window.document;
	d.open();
	d.write(

	// header
		'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"',
		' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n',
		'<html lang="ja">\n',
		'<head>\n',
		'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\n',
		'<meta http-equiv="Content-Style-Type" content="text/css" />\n',
		'<meta http-equiv="Content-Script-Type" content="text/javascript" />\n',
		'<title>' + this.title + '</title>\n',

	// style-sheet
		A_R_W.set.style,

	// body
		'</head>\n',
		'<body>\n',

		'<div id="'+ A_R_W.set.image_id +'">',
			'<img src="' + this.src + '" alt="' + this.title + '" />',
		'</div>\n',

	// text
		(this.comment) ? 
			'<div id="'+ A_R_W.set.comment_id +'">'+ this.comment +'</div>\n' :
			'',

	// footer
		'</body>\n',
		'</html>\n'
	);
	d.close();
};


// setting
A_R_W.set = A_R_W_set;
A_R_W_set = null;


// run
A_R_W._run = function (e) {
	var obj = getEventElement(this, e);
	if (!obj) return true;
	while (obj.parentNode && obj.nodeName != 'A') {
		obj = obj.parentNode;
	}
	if (obj.nodeName != 'A') return true;

	// new
	var arw = new A_R_W(obj);

	// no new window
	if (!arw || !arw.window) return true;

	// return false
	if (e.returnValue) e.returnValue = false;
	if (e.preventDefault) e.preventDefault();
	return false;
};


//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	initialize
A_R_W._init = function () {
	// old UA ?
	if (!document.getElementById) return;

	// <A> list
	var a_list = document.getElementsByTagName('A');

	// trigger-RegExp
	var trigger =
		new RegExp('(?:^|[\x20\t])'+ A_R_W.set['trigger_class'] +'(?:[\x20\t]|$)');

	// loop
	for (var i = a_list.length; i--;) {
		// className == 'resize' ?
		if (!a_list[i].href || !a_list[i].className ||
				!a_list[i].className.match(trigger)) continue;

		// set event
		A_R_W._addEvent('click', A_R_W._run, false, a_list[i]);
	}
};




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	usaful-tips
// addEvents
A_R_W._addEvent = function (type, func, capt, obj) {
	if (!obj || typeof obj != 'object') return;

	// add event
	if (window.addEventList) window.addEventList(type, func, false, obj);	// Trick ?
	else if (obj.addEventListener) obj.addEventListener(type, func, false);	// DOM2 ?
	else if (obj.attachEvent) obj.attachEvent('on'+type, func);	// ie ?
	else {	// Other
		var prev = obj['on'+type];
		obj['on'+type] = (prev) ? function (e) { prev(e); func(e); } : func;
	}
};
// get top-element
function getTopElement (w) {
	if (!w) w = window;
	var d = w.document;
	return (d.compatMode && d.compatMode != 'BackCompat' && d.documentElement) ?
				d.documentElement : d.body || d.documentElement || null;
}
// check this-object
function getEventElement (obj, e) {
	if (!obj.nodeType) {	// ie
		if (!e || typeof(e) != 'object') e = window.event || null;
		if (e && e.srcElement) obj = e.target || e.srcElement;
	}
	return (typeof(obj) == 'object') ? obj : null;
}
// get text in element
function getInnerText (obj) {
	if (!obj || obj.nodeType != 1) return null;
	var text = '';
	// children
	var child_leng = obj.childNodes.length;
	for (var i = 0; i < child_leng; i++) {
		var child = obj.childNodes[i];
		text +=
			(child.nodeType == 1) ? getInnerText(child) :
			(child.nodeType == 3) ? child.nodeValue : '';
	}
	// return text
	return text;
}
// get comment in element
function getInnerComment (obj) {
	if (!obj || obj.nodeType != 1) return null;
	var text = '';
	// children
	var child_leng = obj.childNodes.length;
	for (var i = 0; i < child_leng; i++) {
		var child = obj.childNodes[i];
		text +=
			(child.nodeType == 1) ? getInnerComment(child) :
			(child.nodeType == 8) ? child.nodeValue : '';
	}
	// return text
	return text.replace(/^[\x20\t]+/, '').replace(/[\x20\t]+$/, '');
}




// onload
A_R_W._addEvent('load', A_R_W._init, false, window);
