var ImageTesselator = new Class({
	initialize : function(container,images,w,h,rs,duration,time,steps) {
		this.rectsize = rs;
		this.width = w;
		this.height = h;
		this.duration = duration,
		this.time = time;
		this.steps = steps;

		this.container = container;
		this.images = new Array();
		for(var i = 0;i < images.length;++i){
			this.images.push(images.item(i));
		}
		this.imagea = this.images.shift();
		this.imageb = this.images.shift();
		this.init();
	},
	
	init : function(){
		var img = this.imageb;
		this.rects = new Array();
		var html = "";
		html += "<img src=\""+this.imagea.src+"\" style=\"position:absolute;top:0px;left:0px;visibility:visible;\"/>";
		
		var nx = Math.ceil(this.width / this.rectsize);
		var ny = Math.ceil(this.height / this.rectsize);
		
		for(var j = 0;j < ny;++j){
			for(var i = 0;i < nx;++i){
				var o = new Object();
				o.counter = Math.random() * 50;
				o.x = i * this.rectsize;
				o.y = j * this.rectsize;
				o.w = 1;
				o.h = 1;
				this.rects.push(o);
				html += "<img src=\""+this.imageb.src+"\" style=\"position:absolute;top:0px;visibility:hidden;\" />";
			}
		}
		this.waitcounter = 0;
		this.container.innerHTML = html;
	},

	animate : function (){
		var next = true;
		
		if(next){
			if(this.waitcounter < (this.time / this.duration)){
				next = false;
				this.waitcounter++;
			}
		}
		
		if(next){
			for(var i = 0;i < this.rects.length;++i){
				var r = this.rects[i];
				var cn = this.container.childNodes[i+1];
				
				if(r.counter > 0){
					next = false;
					r.counter--;
				}else{
					if(r.w < this.rectsize){
						r.w += this.rectsize / this.steps;
						r.h += this.rectsize / this.steps;
						next = false;
					}
					var x = r.x + (this.rectsize - r.w)/2;
					var y = r.y + (this.rectsize - r.h)/2;
					var clip = "rect(";
					clip += Math.floor(y) + "px,";
					clip += Math.ceil(x + r.w) + "px,";
					clip += Math.ceil(y + r.h) + "px,";
					clip += Math.floor(x) + "px)";
					cn.style.clip = clip;
					cn.style.visibility = "visible";
				}
			}
		}
		
		if(next){
			this.images.push(this.imagea);
			this.imagea = this.imageb;
			this.imageb = this.images.shift();
			this.init();
		}
	}

});

