/*
	Copyright 2008 Hans Schmucker
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
	(function(){
		var myCrowd=null;
		var drawTarget=null;
		var drawTargetContext=null;
		var background=null;
		var clouds=null;
		var c=0;
		
		var loop=function(){
			drawTargetContext.clearRect(0,0,drawTarget.width,drawTarget.height);
			myCrowd.drawAndMove();
			c=(c+2)%drawTarget.width;
			drawTargetContext.drawImage(clouds,c,0);
			drawTargetContext.drawImage(clouds,c-drawTarget.width,0);
			drawTargetContext.globalCompositeOperation="source-over";
			drawTargetContext.drawImage(background,0,0);
			window.setTimeout(loop,25);
		}

		function main(){
			drawTarget=document.getElementById("draw");
			drawTargetContext=drawTarget.getContext("2d");
			background=document.getElementById("bg");
			clouds=document.getElementById("clouds");
			
			cc=new CanvasCollision(document.getElementById("map"),document.getElementById("sprite"));
			//FIXME ADD FOR CHARACTER COLLISION (BIG SLOWDOWN) cc.map.push(drawTarget);
			myCrowd=new Crowd(cc,document.getElementById("sprite"),drawTargetContext,150);
			loop();
		}

		window.addEventListener("load",function(){
			var c;
			c=function(){
				if(!document || !document.body){
					window.setTimeout(c,200);
					return;
				}
				
				var imgs=document.getElementsByTagName("img");
				
				var loaded=true;
				for(var i=0;i<imgs.length;i++){
					if(!imgs[i].complete)
						
						loaded=false;
					}
				if(loaded) main();
				else window.setTimeout(c,100);
			}
			c();
			
		},false);
	})();