# MATRIX(HTML)	- 逆立ちしてご覧ください. (Please stand upside down) -
#
var background = 'black' # darkmagenta midnightblue
var part = 1     # MATRIX I, II
var speed = 4    # 1:Chrome,Edge 4:Firefox
var palette = 0  # 0:All 1...:Palette No.
var width = 50   # Screen width
var tail = 30    # The unit is the display cycle
var loop = 800
BEGIN {
	initialize()
	print top
	while (--loop > 0) {
	  var s = ""
 	  for (var i = 0; i < width; i++) {
	    var r = rand()
	    var cha = (MATRIX[i] > 0) ? CHAR[int(r * charLen) + 1] : ""
	    var color = colorCode(i)
	    var rs = (cha == "" || color == "") ? "   " :
	        " <span style='color:" color "'>" cha " </span>"
	    s = s rs

	    if (MATRIX[i] == 0) PALETTE[i] = int(r * paletteNumber)	# Palette number selection
	    if (MATRIX[i] <= 0) {
	      if (++MATRIX[i] >= 0) MATRIX[i] = int(r * colorLen)	# Body length
	    } else {
	      if (--MATRIX[i] <= 0) MATRIX[i] = - int(r * tail)	# Tail length
	    }
	  }
	  gsub(/ +$/, "", s); print s "<br>"
	}
	print bottom
}
function initialize() {
	charLen = split("0123456789" " [](){}" \
	  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", CHAR, "")
	colorLen = split("#ff7f50 #ff6347 #ff4500 #ff0000 #dc143c #c71585 #ff1493 #ff69b4 " \
	  "#db7093 #ffc0cb #ffb6c1 #d8bfd8 #ff00ff #ff00ff #ee82ee #dda0dd " \
	  "#da70d6 #ba55d3 #9932cc #9400d3 #8b008b #800080 #4b0082 #483d8b " \
	  "#8a2be2 #9370db #6a5acd #7b68ee", COLOR01)
	colorLen = split("#ffffe0 #fafad2 #fffacd #f5deb3 #deb887 #d2b48c #f0e68c #ffff00 " \
	  "#ffd700 #ffa500 #f4a460 #ff8c00 #daa520 #cd853f #b8860b #d2691e " \
	  "#a0522d #8b4513 #800000 #8b0000 #a52a2a #b22222 #cd5c5c #bc8f8f " \
	  "#e9967a #f08080 #fa8072 #ffa07a", COLOR02)
	colorLen = split("#008b8b #008080 #2f4f4f #006400 #008000 #228b22 #2e8b57 #3cb371 " \
	  "#66cdaa #8fbc8f #7fffd4 #98fb98 #90ee90 #00ff7f #00fa9a #7cfc00 " \
	  "#7fff00 #adff2f #00ff00 #32cd32 #9acd32 #556b2f #6b8e23 #808000 " \
	  "#bdb76b #eee8aa #fff8dc #f5f5dc", COLOR03)
	colorLen = split("#f0f8ff #e6e6fa #b0c4de #778899 #708090 #4682b4 #4169e1 #191970 " \
	  "#000080 #00008b #0000cd #0000ff #1e90ff #6495ed #00bfff #87cefa " \
	  "#87ceeb #add8e6 #b0e0e6 #afeeee #e0ffff #00ffff #00ffff #40e0d0 " \
	  "#48d1cc #00ced1 #20b2aa #5f9ea0", COLOR04)
	colorLen = split("#000000 #696969 #808080 #a9a9a9 #c0c0c0 #d3d3d3 #dcdcdc #f5f5f5 " \
	  "#ffffff #fffafa #f8f8ff #fffaf0 #faf0e6 #faebd7 #ffefd5 #ffebcd " \
	  "#ffe4c4 #ffe4b5 #ffdead #ffdab9 #ffe4e1 #fff0f5 #fff5ee #fdf5e6 " \
	  "#fffff0 #f0fff0 #f5fffa #f0ffff", COLOR05)
	paletteNumber = 5

	#-------------------------------------------- 落ちる(drop down)
	val LAST = 'LAST'
	val scroll = "<script>var move = " speed ";/*px*/" \
	  "var interval = 1;/*sec*/" \
	  "var scrollTop = document.body.scrollTop;" \
	  "setInterval(function(){" \
	  " if (1 > window.scrollY) JumpToID('" LAST "');" \
	  " var scroll = scrollTop + move;" \
	  " window.scrollBy(0, -scroll)}, interval);" \
	  "</script>\n"

	val JumpToID = "<script>function JumpToID(element) {" \
	  "window.scrollTo(0, getPosition('" LAST "'));" \
	  "}</script>"

	val getPosition = "<script>function getPosition(element) {" \
	  "var elem = document.getElementById(element);" \
	  "return elem.getBoundingClientRect().top;" \
	  "}</script>"
	val part1 = scroll JumpToID getPosition

	#-------------------------------------------- 行ったり来たり(Back and forth)
	val scroll2 = "<script>var move = " speed ";/*px*/" \
	  "var interval = 1;/*sec*/" \
	  "var scrollTop = document.body.scrollTop;" \
	  "var lastPosition = -1;" \
	  "var sign = 1;" \
	  "setInterval(function(){" \
	  " var y = window.scrollY;" \
	  " if (1 > y) sign = 1;" \
	  " else if (lastPosition == y) sign = -1;" \
	  " lastPosition = y;" \
	  " var scroll = scrollTop + move;" \
	  " window.scrollBy(0, sign * scroll)}, interval);" \
	  "</script>\n"
	val part2 = scroll2

	val script = part == 1 ? part1: part2
	val lineHeight = "80%"
	top = "<html><body onload=function() style='background-color:" background "'>\n" \
	  script "<div style='line-height:" lineHeight "'><pre><b>"
	bottom = "<span id='" LAST "'></span></b></pre></div></body></html>"

	srand()
	for (var i = 0; i < width; i++) {
	  MATRIX[i] = - int(rand() * tail)	# Tail length
	}
}
function colorCode(i) {	# get palette color
	var p = palette == 0 ? PALETTE[i] : (palette - 1) % paletteNumber
	var x = MATRIX[i] + 1
	if (p == 0) return COLOR01[x]
	if (p == 1) return COLOR02[x]
	if (p == 2) return COLOR03[x]
	if (p == 3) return COLOR04[x]
	return COLOR05[x]
}