// *DisplayObject* 3D Transformation Facade for Webkit
// (I'm and ActionScript engineer; give me a break)
//
// author: Alex Bustin 
//
// 		email - bustin {at} gmail.com
//		  web - http://thebackbutton.com
//
// Copyright © 2009 Alex Bustin. All Rights Reserved.
 


HTMLElement.prototype.__defineGetter__("display", function() {
	if (this.displayObject == undefined) this.displayObject = new DisplayObject(this);
	return this.displayObject;
});

function DisplayObject(target) {
	
	var x = 0;
	var y = 0;
	var z = 0;
	var rotationX = 0;
	var rotationY = 0;
	var rotationZ = 0;
	var scaleX = 1;
	var scaleY = 1;
	var scaleZ = 1;
	var alpha = 1;
	
	var matrixT = new WebKitCSSMatrix();
	var matrixR = new WebKitCSSMatrix();
	var matrixS = new WebKitCSSMatrix();
		
	this.update = function() {
		target.style.webkitTransform = matrixS.multiply(matrixR.multiply(matrixT));
	}
	
	this.__defineGetter__("alpha", getAlpha);
	this.__defineSetter__("alpha", setAlpha);
	function getAlpha() {
		return alpha;
	}
	function setAlpha(v) {
		alpha = v;
		target.style.opacity = v;
	}
	
	this.__defineGetter__("x", getX);
	this.__defineSetter__("x", setX);
	function getX() { return x }
	function setX(v) {
		x = v;
		matrixT.m41 = v;
	}
	
	this.__defineGetter__("y", getY);
	this.__defineSetter__("y", setY);
	function getY() { return y }
	function setY(v) { 
		y = v;
		matrixT.m42 = v;
	}
	
	this.__defineGetter__("z", getZ);
	this.__defineSetter__("z", setZ);
	function getZ() { return z }
	function setZ(v) { 
		z = v;
		matrixT.m43 = v;
	}
	
	this.__defineGetter__("rotationX", getRotationX);
	this.__defineSetter__("rotationX", setRotationX);
	function getRotationX() { return rotationX }
	function setRotationX(v) { 
		var delta = v-rotationX;
		rotationX = v;
		matrixR = matrixR.rotate(delta,0,0);
	}

	this.__defineGetter__("rotationY", getRotationY);
	this.__defineSetter__("rotationY", setRotationY);
	function getRotationY() { return rotationY }
	function setRotationY(v) { 
		var delta = v-rotationY;
		rotationY = v;
		matrixR = matrixR.rotate(0,delta,0);
	}

	this.__defineGetter__("rotationZ", getRotationZ);
	this.__defineSetter__("rotationZ", setRotationZ);
	function getRotationZ() { return rotationZ }
	function setRotationZ(v) { 
		var delta = v-rotationZ;
		rotationZ = v;
		matrixR = matrixR.rotate(0,0,delta);
	}
	
	this.__defineGetter__("scaleX", getScaleX);
	this.__defineSetter__("scaleX", setScaleX);
	function getScaleX() { return scaleX }
	function setScaleX(v) { 
		scaleX = v;
		matrixS.m11 = v;
	}

	this.__defineGetter__("scaleY", getScaleY);
	this.__defineSetter__("scaleY", setScaleY);
	function getScaleY() { return scaleY }
	function setScaleY(v) { 
		scaleY = v;
		matrixS.m22 = v;
	}

	this.__defineGetter__("scaleZ", getScaleZ);
	this.__defineSetter__("scaleZ", setScaleZ);
	function getScaleZ() { return scaleZ }
	function setScaleZ(v) { 
		scaleY = v;
		matrixS.m33 = v;
	}
	
	function scale(v) {
		matrixS.m11 = v;
		matrixS.m22 = v;
		matrixS.m33 = v;
	}
	
}
