screen.bufferDepth = 16
document.onselectstart =  function (){ return false }

var object
N   = 0
I   = 0
xm  = document.body.offsetWidth  / 2
ym  = document.body.offsetHeight / 2
I   = 0
N   = 0
nx  = 0
ny  = 0
mil = true
frm = false
dce = false
hum = true
wir = false
hiq = false
ray = false
FR  = .92

function CObj(par, I, arg, argMin, argMax, W, H, x0, y0, x1, y1, zi, I2, W2, H2, x2, y2) {
	if(W2){
		o = document.createElement("<img style='position:absolute;left=-1000;width:" + W2 + ";height:" + H2 + ";z-index:" + zi + "' src='" + I2.src + "'>")
		G0.appendChild(o)
		o.style.filter = "progid:DXImageTransform.Microsoft.matrix(FilterType=nearest, sizingMethod=\"auto expand\") alpha(opacity=30)"
		this.obj2 = o
		this.f2   = o.filters[0]
	}
	o = document.createElement("<img style='position:absolute;left=-1000;width:" + W + ";height:" + H + ";z-index:" + zi + "' src='" + I.src + "'>")
	G1.appendChild(o)
	o.style.filter = "progid:DXImageTransform.Microsoft.matrix(FilterType=nearest, sizingMethod=\"auto expand\")"
	this.obj    = o
	this.f      = o.filters[0]
	this.src    = I.src
	this.x1     = x1
	this.y1     = y1
	this.x0     = x0
	this.y0     = y0
	this.xv     = 0
	this.yv     = 0
	this.xb     = 0
	this.yb     = 0
	this.dx     = 0
	this.dy     = 0
	this.arg    = arg
	this.Ax     = Math.cos(arg * Math.PI / 180)
	this.Ay     = Math.sin(arg * Math.PI / 180)
	this.a      = 0
	this.lim    = (par && argMin != -1)
	this.argMin = argMin * Math.PI / 180
	this.argMax = argMax * Math.PI / 180
	this.rx     = x0
	this.ry     = y0
	this.par    = par
	this.L      = Math.sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1))
	this.H      = H
	this.W      = W
	this.inv    = (arg < 0 && par.arg >= 0)
	this.fr     = Math.random() / 100
	this.za     = 0
	if(W2) {
		this.src2  = I2.src
		this.W2    = W2
		this.H2    = H2
		this.rx2   = x2
		this.ry2   = y2
	} else this.W2 = 0

	this.groundContact = function (z) {
		with(this){
			xv = 0
			if(yv > 0) yv = 0
			y1 -= z
			if (par) par.groundContact(z); else main()
		}
	}

	this.main = function () {
		with (this) {

			xb  = x1
			yb  = y1
			xv *= (FR + fr)
			yv *= (FR + fr)
			x1 += xv
			y1 += yv

			if(!par) {
				x0 = xm
				y0 = ym
			} else {
				x0 = par.x1
				y0 = par.y1
			}

			dx = x1 - x0
			dy = y1 - y0

			if(!frm) dx += Ax, dy += Ay

			// convulsion
			if(dce) {
				z = Math.random() * 8
				dx += Math.cos(za += Math.random()*.2) * z
				dy += Math.sin(za += Math.random()*.2) * z
			}

			a = Math.atan2(dy, dx)

			// articular limits (thanks to Michel Pietri!)
			if(lim && mil) {
				while (a - par.a >  Math.PI ) a -= Math.PI * 2
				while (a - par.a < -Math.PI ) a += Math.PI * 2
				if (a - par.a > 0) {
					if ((inv?a - par.a: argMax) < (inv? argMax:a - par.a)) a = par.a + argMax
				} else {
					if ((inv?a - par.a:-argMin) > (inv?-argMin:a - par.a)) a = par.a - argMin
				}
			}

			x1 = x0 + Math.cos(a) * L
			y1 = y0 + Math.sin(a) * L

			// check ground
			if (y1 > ny) if (par) groundContact(y1 - ny)

			xv = x1 - xb
			yv = y1 - yb

			ct = Math.cos(a - Math.PI * .5)
			st = Math.sin(a - Math.PI * .5)
			f.M11 =  ct
			f.M12 = -st
			f.M21 =  st
			f.M22 =  ct
			obj.style.left = x0 - st * ((st > 0) ? H - ry : -ry) - ct * ((ct > 0) ? rx : rx - W)
			obj.style.top  = y0 - st * ((st > 0) ?  rx : rx - W) - ct * ((ct > 0) ? ry : ry - H)
			if(W2 && ray) {
				f2.M11 =  ct
				f2.M12 = -st
				f2.M21 =  st
				f2.M22 =  ct
				obj2.style.left = x0 - st * ((st > 0) ? H2 - ry2 : -ry2) - ct * ((ct > 0) ? rx2 : rx2 - W2)
				obj2.style.top  = y0 - st * ((st > 0) ?  rx2 : rx2 - W2) - ct * ((ct > 0) ? ry2 : ry2 - H2)
			}
		}
	}
}

function wire() {
	wir=!wir
	for(var i=1; i<object.length; i++){
		with(object[i].obj) {
			if(wir) {
				src = blank.src
				style.border = "#66FF66 solid 1px"
				ray = false
				xr.checked = false
				if(object[i].W2)object[i].obj2.style.left = -1000
			} else {
				src = object[i].src
				style.border = ""
			}
		}
	}
}

function mutX() {
	dce = true
	if(ray) xray(), xr.checked = false
	setTimeout("dce=false;G1.innerHTML='';G0.innerHTML='';hum=!hum;struct()",5000)
}

function hiQ() {
	hiq=!hiq
	for(var i in object) {
		object[i].f.filterType = hiq?"bilinear":"nearest"
		if(object[i].W2)object[i].f2.filterType = hiq?"bilinear":"nearest"
	}
}

function xray() {
	ray=!ray
	for(var i in object) if(object[i].W2)object[i].obj2.style.left = -1000
}

function resize() {
	nx = document.body.offsetWidth / 2
	ny = document.body.offsetHeight
	ny = ny - ny * .1
	GRND.style.top = ny
}

function struct() {
	object = new Array()
	N = 0
	//                         (parent, image, angle,  AngMin, AngMax,  width, height, x0,  y0,  x1,  y1,  zIndex, image2,   w,   h,  x,  y)
	// hip
	object[N++] = O1 = new CObj(null,     s0,    90,      -1,     0,      1,   210,     1, -10,   1, 210,    40  )
	object[N++] = O2 = new CObj(  O1,     s9,    90,       0,     0,     30,    45,    15,  17,  15,  30,    25  )
	// leg 1
	object[N++] = O  = new CObj(  O2,     s1,    75,     110,    90,     20,   110,    10,   9,   8, 107,    20  ,     p1,  45, 129, 26, 20)
	object[N++] = O  = new CObj(   O,     s2,   130,       0,   160,     16,   110,     9,   9,   7, 107,    19  ,     p2,  32, 118, 19, 12)
	object[N++] = O  = new CObj(   O,     s3,     0,     150,     0,     18,    49,    16,  13,   0,   0,    19  ,     p3,  30,  54, 18, 16)
	// leg 2
	object[N++] = O  = new CObj(  O2,     s1,    50,     110,    90,     20,   110,    10,   9,   8, 107,   -10  ,     p1,  45, 129, 26, 20)
	object[N++] = O  = new CObj(   O,     s2,    90,       0,   160,     16,   110,     9,   9,   7, 107,   -11  ,     p2,  32, 118, 19, 12)
	object[N++] = O  = new CObj(   O,     s3,     0,     150,     0,     18,    49,    16,  13,   0,   0,   -11  ,     p3,  30,  54, 18, 16)
	// spinal column
	object[N++] = O  = new CObj(  O1,     s6,   -90,     160,   160,     16,    55,     8,   0,   8,  50,     0  ,     p6,  60,  90, 40, 30)
	object[N++] = O3 = new CObj(   O,     s6,   -75,      20,    40,     16,    65,     8,  -1,   8,  60,     0  )
	// thorax
	object[N++] = O  = new CObj(  O3,     s8,   -75,       1,     1,     52,    80,    40,  69,  40,  10,     0  ,     p8,  67,  90, 47, 75)
	if(hum){
	// neck + head
	object[N++] = O  = new CObj(  O3,     s6,   -60,      60,    30,     12,    40,    10,   0,  10,  36,    -1  ,     p6,  30,  50, 20, 10)
	object[N++] = O  = new CObj(   O,     s7,   -60,      20,    15,     60,    48,    40,  10,  40,   0,     0  ,     p7,  70,  60, 45, 16)
	} else {
	object[N++] = O  = new CObj(  O3,     s6,   -60,      90,    90,     12,    48,     6,   2,   6,  46,    -4  )
	object[N++] = O  = new CObj(   O,     s6,   -70,      30,    30,     12,    48,     6,   2,   6,  46,    -4  )
	object[N++] = O  = new CObj(   O,     s6,   -80,      30,    30,     12,    48,     6,   2,   6,  46,    -4  )
	object[N++] = O  = new CObj(   O,     s7,   -90,      30,    30,     51,    41,    28,  15,   0,  51,    -4  )
	object[N++] = O  = new CObj(  O3,     s6,  -120,      90,    90,     12,    48,     6,   2,   6,  46,    -5  )
	object[N++] = O  = new CObj(   O,     s6,  -110,      30,    30,     12,    48,     6,   2,   6,  46,    -5  )
	object[N++] = O  = new CObj(   O,     s6,  -100,      30,    30,     12,    48,     6,   2,   6,  46,    -5  )
	object[N++] = O  = new CObj(   O,     s7,   -90,      30,    30,     51,    41,    28,  15,   0,  51,    -5  )
	}
	// shoulders - arms
	object[N++] = O  = new CObj(  O3,     s4,    90,      -1,    -1,     16,    80,     8,   5,   8,  80,    30  ,     p1,  28,  90, 20, 15)
	object[N++] = O  = new CObj(   O,     s5,    60,     140,     0,     20,    90,     6,   6,   6,  85,    30  ,     p5,  20,  94, 6,  10)
	object[N++] = O  = new CObj(  O3,     s4,    60,      -1,    -1,     16,    80,     8,   5,   8,  80,   -15  ,     p1,  28,  90, 20, 15)
	object[N++] = O  = new CObj(   O,     s5,     0,     140,     0,     20,    90,     6,   6,   6,  85,   -15  ,     p5,  20,  94, 6,  10)

	if(wir)wir=!wir,wire()
}


function zyva() {
	document.onmousemove = function() {
		xm = window.event.clientX
		ym = window.event.clientY
	}
	resize()
	struct()
	setInterval("for(I in object)object[I].main()", 16)
}
