var pfx = "cfg";
var sw, sh;
var mcookie = new MCookie("SLM", "cfg", location.pathname, false);

function init() {
	var ss;
	
	if (!mcookie.containsFragment("ssize")) {
		if (screen) {
			sw = screen.width;
			sh = screen.height;
		} else if (navigator && navigator.appVersion.substring(0, 4) * 100 >= 400 &&
			navigator.javaEnabled())
		{
			// screen size for 4.x+ browsers
			sw = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
			sh = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
		} else {
			// screen size for 3.x browsers or non java enabled browser
			sw = 800;
			sh = 600;
		}
	} else {
		ss = mcookie.getFragment("ssize").getValue();
		sw = ss.substring(0, ss.indexOf("x"));
		sh = ss.substring(ss.indexOf("x") + 1);
	}
}

function js_image(image, w, h, bg) {
	if (mcookie.getFragment("newwin").getValue() == "false") return true;

	var ratio = w/h, iw = w, ih = h;
	init();

	//alert(w + " " + h + " " + ratio);
	if (mcookie.getFragment("rescale").getValue() != "false") {
		if (w + 60 > sw) { w = sw - 60; h = w * 1/ratio }
		if (h + 60 > sh) { h = sh - 60; w = h * ratio }
		iw = w; ih = h;
	} else {
		if (w + 60 > sw) { w = sw - 60 }
		if (h + 60 > sh) { h = sh - 60 }
	}
	//alert(w + " " + h + " " + ratio);
	var wn = window.open('', '_blank', 'toolbar=0,location=0,directories=0,' +
		'status=0,menubar=0,scrollbars=0,resizable=0,width=' +
		(w + 40) + ',height=' + (h + 40));
	with(wn.document) {
		open('text/html');
		writeln('<html><head><title>', document.title, '</title></head>',
			'<body bgcolor="', bg, '"><center>',
			'<a href="#" onClick="self.close(); return false"><img src="', image,
			'" width="', iw, '" height="', ih, '" alt=""',
			'" border="0" title="Click to close" /></a></center></body></html>');
		close();
	}
	return false;
}

function cfgWindow(cfgdoc) {
	window.open(cfgdoc + '?slm_cg_gallery', 'SLM_CFG_WIN',
		'toolbar=0,location=0,directories=0,' +
		'status=0,menubar=0,scrollbars=0,resizable=1,width=470,height=230');
	return false;
}


function js_pchview(image, w, h) {
	var wn = window.open('', '_blank', 'toolbar=0,location=0,directories=0,' +
		'status=0,menubar=0,scrollbars=0,resizable=0,width=' +
		(w * 2 + 40) + ',height=' + (h + 100));
	with(wn.document) {
		open('text/html');
		writeln(
				'<html>',
				'<head>',
				'	<title>', document.title, '</title>',
				'	<link rel="stylesheet" type="text/css"',
				'		href="../css/tss_flat2/main.css" />',
				'	<link rel="stylesheet" type="text/css"',
				'		href="../css/tss_flat2/slm.css" />',
				'</head>',
				'<body class="pchview">',
				'<img src="../images/slm/oekaki/', image, '.png"',
				'	width="', w, '" height="', h, '" align="left" />',
				'<applet code="pch.PCHViewer" codebase="../images/slm/oekaki"',
				'	archive="pchviewer.jar"',
				'	width="', w, '" height="', (h + 25), '">',
				'<param name="pch_file" value="', image, '.pch" />',
				'<param name="image_width" value="' + w + '" />',
				'<param name="image_height" value="' + h + '" />',
				'<param name="speed" value="2" />',
				'<param name="run" value="true" />',
				'<param name="buffer_progress" value="true" />',
				'<param name="buffer_canvas" value="true" />',
				'<param name="color_back" value="#ddddcc" />',
				'<param name="color_text" value="#000000" />',
				'<param name="color_icon" value="#ddddcc" />',
				'<param name="color_frame" value="#ddddcc" />',
				'<param name="color_bar" value="#ddddcc" />',
				'<param name="color_barselect" value="#ccccdd" />',
				'</applet>',

				'<a href="#" onClick="self.close(); return false">Close</a>',
				'</body>',
				'</html>'
			);
		close();
	}
	return false;
}
