var scale_state = 0;
var orgWidth = 0, orgHeight = 0;
var scale_margin = 160;
var resized_msg = document.createElement("div");
resized_msg.appendChild(document.createTextNode("Image rescaled"));
resized_msg.appendChild(document.createElement("br"));
resized_msg.appendChild(document.createTextNode("click to see full size"));

function resize()
{
	var img = document.getElementById("main-image");
	var resized_div = document.getElementById("rescaled-msg");
	if (!img || !resized_div) return 0;
	if (scale_state == 0) {
		orgWidth = img.width;
		orgHeight = img.height;
		scale_state = 1;
	}
	if (scale_state == 1) {
		var w = document.body.offsetWidth - scale_margin;
		if (w < 128) w = 128;
		if (w < orgWidth) {
			img.width = w;
			img.height = w * orgHeight / orgWidth;
			scale_state = 2;
			resized_div.appendChild(resized_msg);
			resized_div.style.display = "inline-block";
		}
	} else {
		img.width = orgWidth;
		img.height = orgHeight;
		scale_state = 1;
		resized_div.style.display = "none";
		resized_div.removeChild(resized_msg);
	}
	return 0;
}

@@ -1,86 +1,236 @@
-var scale_state = 0;%0Avar orgWidth = 0, orgHeight = 0;%0Avar scale_margin = 160;%0Avar 
+function resize() %7B%0A%09var w;%0A%09if (!wp.resize_state) %7B%0A%09%09wp.resize_img = document.getElementById(%22main-image%22);%0A%09%09wp.resize_div = document.getElementById(%22rescaled-msg%22);%0A%09%09if (!wp.resize_img %7C%7C !wp.resize_div) %7B return false; %7D%0A%09%09wp.
 resi
@@ -270,16 +270,21 @@
 %22div%22);%0A
+%09%09wp.
 resized_
@@ -339,24 +339,29 @@
 escaled%22));%0A
+%09%09wp.
 resized_msg.
@@ -403,16 +403,21 @@
 %22br%22));%0A
+%09%09wp.
 resized_
@@ -488,214 +488,41 @@
 ));%0A
-%0Afunction resize()%0A%7B%0A%09var img = document.getElementById(%22main-image%22);%0A%09var resized_div = document.getElementById(%22rescaled-msg%22);%0A%09if (!img %7C%7C !resized_div) return 0;%0A%09if (scale_state == 0) %7B%0A%09%09orgWidth = 
+%09%09wp.resize_orgWidth = wp.resize_
 img.
@@ -530,16 +530,26 @@
 idth;%0A%09%09
+wp.resize_
 orgHeigh
@@ -552,16 +552,26 @@
 eight = 
+wp.resize_
 img.heig
@@ -568,36 +568,40 @@
 e_img.height;%0A%09%09
-scal
+wp.resiz
 e_state = 1;%0A%09%7D%0A
@@ -605,20 +605,24 @@
 %09%7D%0A%09if (
-scal
+wp.resiz
 e_state 
@@ -627,16 +627,17 @@
 e ==
+=
  1) %7B%0A%09%09
 var 
@@ -632,20 +632,16 @@
  1) %7B%0A%09%09
-var 
 w = docu
@@ -668,20 +668,11 @@
 h - 
-scale_margin
+160
 ;%0A%09%09
@@ -683,16 +683,18 @@
 w %3C 128)
+ %7B
  w = 128
@@ -694,16 +694,18 @@
 w = 128;
+ %7D
 %0A%09%09if (w
@@ -707,16 +707,26 @@
 if (w %3C 
+wp.resize_
 orgWidth
@@ -728,24 +728,34 @@
 Width) %7B%0A%09%09%09
+wp.resize_
 img.width = 
@@ -760,16 +760,26 @@
 = w;%0A%09%09%09
+wp.resize_
 img.heig
@@ -787,16 +787,26 @@
 t = w * 
+wp.resize_
 orgHeigh
@@ -809,16 +809,26 @@
 eight / 
+wp.resize_
 orgWidth
@@ -832,20 +832,24 @@
 dth;%0A%09%09%09
-scal
+wp.resiz
 e_state 
@@ -852,31 +852,33 @@
 ate = 2;%0A%09%09%09
+wp.
 resize
-d
 _div.appendC
@@ -878,24 +878,27 @@
 appendChild(
+wp.
 resized_msg)
@@ -902,23 +902,25 @@
 sg);%0A%09%09%09
+wp.
 resize
-d
 _div.sty
@@ -964,16 +964,26 @@
 lse %7B%0A%09%09
+wp.resize_
 img.widt
@@ -986,16 +986,26 @@
 width = 
+wp.resize_
 orgWidth
@@ -1008,16 +1008,26 @@
 idth;%0A%09%09
+wp.resize_
 img.heig
@@ -1031,16 +1031,26 @@
 eight = 
+wp.resize_
 orgHeigh
@@ -1054,20 +1054,24 @@
 ight;%0A%09%09
-scal
+wp.resiz
 e_state 
@@ -1073,31 +1073,33 @@
 tate = 1;%0A%09%09
+wp.
 resize
-d
 _div.style.d
@@ -1117,23 +1117,25 @@
 one%22;%0A%09%09
+wp.
 resize
-d
 _div.rem
@@ -1143,16 +1143,19 @@
 veChild(
+wp.
 resized_
@@ -1171,13 +1171,17 @@
 %09return 
-0
+false
 ;%0A%7D%0A

Fix rescaling code style.