/*--------------------------------------------------------------------------------ASonJS ver 8.9.11    a library for scripting JavaScript like ActionScript 1.0.--------------------------------------------------------------------------------License    ASonJS is published under MIT License.    Copyright (c) 2007-2008 KOJO/Yusuke Takasaki.    Permission is hereby granted, free of charge, to any person obtaining a copy    of this software and associated documentation files (the "Software"), to deal    in the Software without restriction, including without limitation the rights    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell    copies of the Software, and to permit persons to whom the Software is    furnished to do so, subject to the following conditions:    The above copyright notice and this permission notice shall be included in    all copies or substantial portions of the Software.    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN    THE SOFTWARE. --------------------------------------------------------------------------------Documents (still written in Japanese only.)    http://www.grkt.com/iphone/document.html--------------------------------------------------------------------------------History     8.9.11         available for Google Chrome.     8.9.02         Minimized default canvas size.     8.7.11         fixed the timing of changing _visible property of MovieClip.         fixed the margin of error of rotating.     8.7.4        __draw and __ef method optimized.    8.6.18        MovieClip._rotation available.(test)    8.6.10        TimeLine script available. >> MovieClip.setTimeLineScript( frame:Number, func:Function )--------------------------------------------------------------------------------*/var trace = function(val){}if(window.console){	trace = function(val){		window.console.log(val);	}}Object.merge = function(obj1,obj2){	for(var i in obj2){		obj1[i] = obj2[i];	}	return obj1;}var DisplayObject = function(){};DisplayObject.prototype.__defineGetter__("_x",function(){return this.__x});DisplayObject.prototype.__defineSetter__("_x",function(val){this.__x = val; this._element.style.left = val;});DisplayObject.prototype.__defineGetter__("_y",function(){return this.__y});DisplayObject.prototype.__defineSetter__("_y",function(val){this.__y = val; this._element.style.top = val;});DisplayObject.prototype.__defineGetter__("_alpha",function(){return this.__alpha});DisplayObject.prototype.__defineSetter__("_alpha",function(val){this.__alpha = val; this._element.style.opacity = val/100;});DisplayObject.prototype.__defineGetter__("_visible",function(){return this.__visible;});DisplayObject.prototype.__defineSetter__("_visible",	function(val){		this.__visible = val;		this._element.style.display = this.__visible ? 'block' : 'none';	});DisplayObject.prototype.remove = function(){	if(typeof(this['onUnload'])=='function'){		this['onUnload']();	}	this._parent.removeChild(this);}var TextField = function(opt){	this._element = document.createElement('div');	this._element.style.position = 'absolute';		var params = Object.merge({		_x:0,		_y:0,		_width:100,		_height:16,		_visible:true,		_xscale:100,		_yscale:100,		_alpha:100,		background: false,		backgroundColor: '#ffffff'	},opt || {});	for(var i in params){		this[i] = params[i];	}	}TextField.prototype = new DisplayObject();TextField.prototype.removeTextField = function(){	this.remove();}TextField.prototype.__defineGetter__("text",function(){return this._element.innerHTML});TextField.prototype.__defineSetter__("text",function(val){this._element.innerHTML = val});TextField.prototype.__defineGetter__("background",function(){return this._background});TextField.prototype.__defineSetter__("background",function(val){	this._background=val;	if(val){		this._element.style.backgroundColor = this._backgroundColor;	}else{		this._element.style.backgroundColor = null;	}});TextField.prototype.__defineGetter__("backgroundColor",function(){return this._backgroundColor});TextField.prototype.__defineSetter__("backgroundColor",function(val){	if(this.background){		this._backgroundColor=val;		this._element.style.backgroundColor = this._backgroundColor;	}});TextField.prototype.__defineGetter__("_width",function(){return this.__width});TextField.prototype.__defineSetter__("_width",function(val){this.__width = val; this._element.style.width = val;});TextField.prototype.__defineGetter__("_height",function(){return this.__height});TextField.prototype.__defineSetter__("_height",function(val){this.__height = val; this._element.style.height = val;});var MovieClip = function(opt){	this._element = document.createElement('DIV');	this._element.style.position = 'absolute';		this.canvas = document.createElement('canvas');	this.canvas.style.position = 'absolute';	this.canvas.width = 1;	this.canvas.height = 1;	this.canvas.style.left = 0;//to delete 'pt'	this.canvas.style.top = 0;//to delete 'pt'	this._element.appendChild(this.canvas);		this._currentframe = 1;	this._currentimage = undefined;	this.isPlaying = true;	this.timeline = new TimeLine(this);	this.children = [];		this.__rotation;	this.__depth = 0;		this.__setCanvasLoc;}MovieClip.prototype = new DisplayObject();MovieClip.prototype.createTextField = function(name,depth,x,y,width,height){	if(this.children[depth]) this.children[depth].remove();	if(this[name]) this[name].remove();		var opt = {		name: name,		depth: depth,		_x: x,		_y: y,		_width: width,		_height: height	}		var tf = new TextField(opt);		tf._name = name;	tf._parent = this;	this.children[depth] = tf;	this[name] = tf;	this._element.appendChild(tf._element);	tf._element.style.zIndex = depth;	tf.__depth = depth;		return tf;}MovieClip.prototype.createEmptyMovieClip = function(name,depth){		if(this.children[depth]) this.children[depth].remove();		if(this[name]) this[name].remove();		var mc = new MovieClip();		mc._name = name;	mc._parent = this;	this.children[depth] = mc;	this[name] = mc;	this._element.appendChild(mc._element);	mc._element.style.zIndex = depth;	mc.__depth = depth;	mc._element.style.position = 'absolute';		return mc;}MovieClip.prototype.attachMovie = function(id,name,depth,opt){		var mc = this.createEmptyMovieClip(name,depth);		var params = Object.merge({		_x:0,		_y:0,		_width:1,		_height:1,		_visible:true,		_xscale:100,		_yscale:100,		_alpha:100,		_rotation: 0,		_origin: 'top-left',		__canvas_x: 0,		__canvas_y: 0	},opt || {});	for(var i in params){		mc[i] = params[i];	}		mc.timeline.make(id);		var img = mc.timeline.getImage(1);	if(img){		mc.__draw(img);	}		//setTimeout to be able to call onLoad which is defined to returned MovieClip from attatchMovie()	setTimeout((function(){		var obj = this;		return function(){			(function(){				if(typeof(this['onLoad'])=='function'){					this['onLoad']();				}			}).apply(obj)		}	}).apply(mc),0)		return mc;}MovieClip.prototype.removeChild = function(obj){	this._element.removeChild(obj._element);	this[obj._name] = undefined;	this.children[obj.__depth] = undefined;}MovieClip.prototype.removeMovieClip = function(){	this.remove();}MovieClip.prototype.getNextHighestDepth = function(){	return this.children.length;}MovieClip.prototype.play = function(){	this.isPlaying = true;}MovieClip.prototype.stop = function(){	this.isPlaying = false;}MovieClip.prototype.gotoAndPlay =function(val){	this.isPlaying = true;	this.__jump(val);}MovieClip.prototype.gotoAndStop =function(val){	this.isPlaying = false;	this.__jump(val);}MovieClip.prototype.nextFrame =function(){	this.isPlaying = false;	this.__jump(this._currentframe+1);}MovieClip.prototype.prevFrame =function(){	this.isPlaying = false;	this.__jump(this._currentframe-1);}MovieClip.prototype.hitTest = function(){	//Only MovieClip to MovieClip without children. yet.	var tgt = arguments[0];	var res = false;	if(tgt){		var w1 = this._width/2;		var h1 = this._height/2;		var x1 = this._x + this.__canvas_x + w1;		var y1 = this._y + this.__canvas_y + h1;		var w2 = tgt._width/2;		var h2 = tgt._height/2;		var x2 = tgt._x + tgt.__canvas_x + w2;		var y2 = tgt._y + tgt.__canvas_y + h2;		if(Math.abs(x1 - x2)<w1+w2 && Math.abs(y1-y2)<h1+h2){			res = true;		}	}	return res;}MovieClip.prototype.__jump = function(val){	if(typeof(val) == 'string'){		val = this.timeline.getNumberOfFrameByLabel(val)	}		if(typeof(val)!='number') return false;		if(val>this._totalframes) val = this._totalframes;	if(val<1) val = 1;		if(this._currentframe != val){		this.timeline.executeFrameScript(val);		this.__draw(this.timeline.getImage(val));		this._currentframe = val;	}}MovieClip.prototype.__draw = function(img){	if(img != this._currentimage || this.__redraw){		this._element.style.display = this.__visible ? 'block' : 'none';				var rad1 = this.__rotation / 180 * Math.PI;		var rad2 = rad1 + 1.5707963267948966;//rad1 + 90/180*Math.PI				var w = img.width * this.__xscale;		var h = img.height * this.__yscale;				var x0 = w * Math.cos(rad1);		var x1 = h * Math.cos(rad2);		var x2 = x0+x1;				var y0 = h * Math.sin(rad2);		var y1 = w * Math.sin(rad1);		var y2 = y0+y1;				var offset_x = -Math.min(0,  x0, x1, x2);		var offset_y = -Math.min(0,  y0, y1, y2);				this._width =Math.abs(x0) + Math.abs(x1);		this._height = Math.abs(y0) + Math.abs(y1);				var context = this.canvas.getContext('2d');				context.translate(offset_x, offset_y);		context.rotate(rad1);		context.drawImage(img,0,0,w,h);				this.__canvas_x = -Math.floor(offset_x + x1 * this.__cH + x0 * this.__cW);		this.__canvas_y = -Math.floor(offset_y + y0 * this.__sH + y1 * this.__sW);		this.canvas.style.left = this.__canvas_x;		this.canvas.style.top = this.__canvas_y;				this._currentimage = img;	}	this.__redraw = false;}MovieClip.prototype.__ef = function(){	var c, cld = this.children;	var i = cld.length - 1;	do{		if(c = cld[i]){			var f = c.__ef;			if(typeof(f)=='function') f.apply(c);		}	}while(i-->0);		this._currentframe = (this._currentframe+Number(this.isPlaying)-1)%this._totalframes+1;		var ef = this['onEnterFrame'];	if(typeof(ef)=='function') ef.apply(this);		if(this.isPlaying) this.timeline.executeFrameScript(this._currentframe);		if(img = this.timeline.getImage(this._currentframe)) this.__draw(img);}MovieClip.prototype.addFrameScript = function(frm, func){	this.timeline.addFrameScript(frm, func);}MovieClip.prototype.__defineGetter__("_visible",function(){return this.__visible;});//overrideMovieClip.prototype.__defineSetter__("_visible",function(val){this.__visible = val;this.__redraw = true;});//overrideMovieClip.prototype.__defineGetter__("_totalframes",function(){return this.timeline._totalframes;});MovieClip.prototype.__defineSetter__("_totalframes",function(val){return false;});MovieClip.prototype.__defineGetter__("_xscale",function(){return this.__xscale*100;});MovieClip.prototype.__defineSetter__("_xscale",function(val){this.__xscale = val/100;this.__redraw = true;});MovieClip.prototype.__defineGetter__("_yscale",function(){return this.__yscale*100;});MovieClip.prototype.__defineSetter__("_yscale",function(val){this.__yscale = val/100;this.__redraw = true;});MovieClip.prototype.__defineGetter__("_width",function(){return this.__width});MovieClip.prototype.__defineSetter__("_width",function(val){this.__width = val; this._element.style.width = val;this.canvas.setAttribute('width',val);});MovieClip.prototype.__defineGetter__("_height",function(){return this.__height});MovieClip.prototype.__defineSetter__("_height",function(val){this.__height = val; this._element.style.height = val;this.canvas.setAttribute('height',val);});MovieClip.prototype.__defineGetter__("_rotation",function(){return this.__rotation});MovieClip.prototype.__defineSetter__("_rotation",function(val){this.__rotation = (val+360)%360; this.__redraw = true;});MovieClip.prototype.__defineGetter__("_origin",function(){return this.__origin});MovieClip.prototype.__defineSetter__(	"_origin",	function(val){		var org = val;		var func;		switch(org){			case 'top-left' :				this.__cH = 0;				this.__cW = 0;				this.__sH = 0;				this.__sW = 0;				break;			case 'top' :				this.__cH = 0;				this.__cW = 0.5;				this.__sH = 0;				this.__sW = 0.5;				break;			case 'top-right' :				this.__cH = 0;				this.__cW = 1;				this.__sH = 0;				this.__sW = 1;				break;			case 'left' :				this.__cH = 0.5;				this.__cW = 0;				this.__sH = 0.5;				this.__sW = 0;				break;			case 'center' :				this.__cH = 0.5;				this.__cW = 0.5;				this.__sH = 0.5;				this.__sW = 0.5;				break;			case 'right' :				this.__cH = 0.5;				this.__cW = 1;				this.__sH = 0.5;				this.__sW = 1;				break;			case 'bottom-left' :				this.__cH = 1;				this.__cW = 0;				this.__sH = 1;				this.__sW = 0;				break;			case 'bottom' :				this.__cH = 1;				this.__cW = 0.5;				this.__sH = 1;				this.__sW = 0.5;				break;			case 'bottom-right' :				this.__cH = 1;				this.__cW = 1;				this.__sH = 1;				this.__sW = 1;				break;			default:				org = 'top-left';				this.__cH = 0;				this.__cW = 0;				this.__sH = 0;				this.__sW = 0;		}		this.__origin = org;});MovieClip.prototype.__defineSetter__(	"onRelease",	function(func){		if(typeof(func)=='function'){			var obj = this;			this.canvas.onclick = function(){func.apply(obj)};		}else{			this.canvas.onclick = func;		}});MovieClip.prototype.__defineGetter__(	"onRelease",	function(){		return this.canvas.onclick;});var TimeLine = function(container){	this.container = container;	this.images =[];	this._totalframes;	this.loop;	this.labels;	this.scripts=[];	this.frames=[];};TimeLine.prototype.addFrameScript = function(frm, func){	if(typeof(func=='function')){		this.scripts[frm] = func;	}}TimeLine.prototype.executeFrameScript = function(frm){	if(this.container){		if(this.scripts[frm]){			this.scripts[frm].apply(this.container);		}	}}TimeLine.prototype.getImage = function(num){	if(num>this._totalframes || num==0) num = 1;	var img = undefined;	var sam = 0;	var len = this.images.length;	for(var i = 0; i<len; i++){		sam += this.frames[i];		if(num <= sam){			img = this.images[i];			break;		}	}	return img;}TimeLine.prototype.getNumberOfFrameByLabel = function(label){	var num = this.labels[label];	return num ? num : 1;}TimeLine.prototype.make = function(id){	this._totalframes = 0;	this.images = new Array();	this.frames = new Array();	this.labels = {};		var d = document.getElementById(id);		if(d){		var label = d.getAttribute('labels');		if(label){			this.labels = eval('(' + label + ')');		}				for(var i = 0;i < d.childNodes.length;i++){			var tag = d.childNodes[i];			if(tag.tagName=='IMG'){				var img = new Image(tag.attributes['width'].nodeValue,tag.attributes['height'].nodeValue);				img.src = tag.attributes['src'].nodeValue;				this.images.push(img);				var num = Number(tag.getAttribute('frames'));				this._totalframes += num;				this.frames.push(num);			}		}				this.scripts = new Array(this._totalframes);	}}var Stage = function(){};Stage.init = function(opt){	this._element = opt.element;	this._element.style.position = 'relative';	this.millisec = Math.floor(1000/opt.fps);		var root = this._root = new MovieClip({_name : '_level0',_width:this._element.style.width ,_height:this._element.style.height});	this._element.appendChild(root._element);		this.width = Number(this._element.style.width.match(/[0-9]*/));	this.height = Number(this._element.style.height.match(/[0-9]*/));		var r = this._root;	var __timer = function(){r.__ef()};		var me = this;	var maketimer = function(){		me.intervalId = setInterval(__timer,me.millisec);	}		setTimeout(maketimer,50);		return root;}