/*****************************************************************************
 * let the object move
 * coded for muddle-code.de
 *****************************************************************************/

function log (msg)
{
	//if (console != undefined) {	console.log (msg); }
}

function randomizer (min,max)
{
	return (min+Math.floor(Math.random()*(max-min)+1));
}

function bgzoom (opts)
{
	//# valid opts {element,from,to,rate}
	var start = opts.from || 0;
	var	end = opts.to || 0;
	var fps = opts.rate || 200;
	var id = opts.element || "";
	var height = $(id).css("height");
	
	$(id).css("width",start+"px");
	//# to be able for clipping the id must be absolute
	$(id).css("position", "absolute");
	
	log("zoom (from: "+start+", to: "+end);
	
	//# this will zoom in width with clipping
	var interval = setInterval (function() {
		var	wi = parseInt ($(id).css ("width"));
		
		//# check direction
		var	finish = false;
		if (start > end) {
			wi--;
			if (wi < end)finish=true;
		}
		else {
			wi++;
			if (wi > end)finish=true;
		}
		
		if (finish==true) {
			clearInterval (interval);
			//# reverse
			setTimeout (function(){
				bgzoom({element:id,from:end,to:start,rate:fps});
			},10);
		}
		else {
			$(id).css ("width",wi+"px");
			var	cl = start>end ? end : start;
			$(id).css ("clip","rect(0 "+cl+"px "+height+" 0)");
		}
	},fps);
}

var cloud, icon, logo, ground, soldier=new Array(4)

$(document).ready(function() 
{
	$(".accordion2 h3").eq(2).addClass("active");
	$(".accordion2 h3").click(function(){
		$(this).next("p").slideToggle("slow")
		.siblings("p:visible").slideUp("slow");
		$(this).toggleClass("active");
		$(this).siblings("h3").removeClass("active");
		});
	
	//# cloud
	bgzoom ({from:800, to:1200, rate:40,element:"#cloud"});
	
	//# icon (before ground to use z-index)
	icon = new Sprite ("#topbar", "./img/icon.png",{left:100,bottom:250,visible:"none", width:100,height:101});

	//# ground
	ground = new Sprite ("#topbar", "./img/ground.png",{bottom:-1,width:800,height:25});
	
	//# logo
	logo = new Sprite ("#topbar", "./img/angertext.png",{left:400,visible:"none",width:397, height:176});
	
	//# start with logo fadein
	logo.run ({duration:3000, from:0, to:1, property:"opacity"});
	
	//# continue with falling icon
	setTimeout (function(){icon.show();icon.run ({duration:2000, from:"250px", to:"0px", property:"bottom", ease: "easeOutBounce"});},4000);
	
	//# let the soldiers run :-)
	setTimeout (function(){
		for (var i = 0; i < soldier.length; i++) {
			var opts = {duration:randomizer(4000,10000), from:"0px", to:randomizer(200,400)+"px",property:"left", reverse:true};
			soldier[i] = new Sprite ("#topbar", "./img/soldier.png", {bottom:20,visible:"none",width:80, height:16});
			soldier[i].animate({fps:100,count:4}).run (opts);
		}
	},6000);
});


