MAX_BALLONNEN=13;
SPEEDS=new Array(50,75,92);		
TOP_MARGE=new Array(-25,25);

function playSound(){
	document.firstSound.play(false);
}
function rand( min, max ) {
    var argc = arguments.length;
    if (argc == 0) {
        min = 0;
        max = 2147483647;
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getHeight(){
	var viewportheight;	
	if (typeof window.innerWidth != 'undefined'){
		viewportheight = window.innerHeight;
	}	
	else if (typeof document.documentElement != 'undefined'
	 && typeof document.documentElement.clientWidth !=
	 'undefined' && document.documentElement.clientWidth != 0){
		viewportheight = document.documentElement.clientHeight;
	}	
	else{
	   viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}
	return viewportheight;
}
IE = document.all?true:false;
function doEvent(target,ev,func){
	//alert(target);	
	if(IE){
		target.attachEvent("on" + ev,func);
	}
	else{
		target.addEventListener(ev,func,false);
	}
}

function upBallon(nr,sec,hor,origml,defdir,origheight){
	dir=defdir;
	bl=document.getElementById('ballon_' + nr);
	curtop=parseFloat(bl.style.top);
	//document.getElementById('top_' + nr).value=curtop + "-" + origheight;
	//change=rand(5,7);
	bl.style.top=(curtop - 6) + "px";
	curml=parseFloat(bl.style.marginLeft);
	
	
	if(curml<= (origml - hor) && dir=="left"){
		dir="right";
	}
	else if(curml >= (origml + hor) && dir=="right"){
		dir="left";
	}
	
	if(dir=="left"){
		bl.style.marginLeft=(curml -1) + "px";
	}
	else{
		bl.style.marginLeft=(curml +1) + "px";
	}
	
	if(curtop<=-100){
		/*bl=document.getElementById('ballon_' + nr);
		bl.src="art2009/img/balonnen/ballon_" + rand(1,16) + ".png";
		bl.style.top=origheight + "px";
		bl.style.display='';
		
		if(bl.src.indexOf('_16')>-1){
			bl.style.cursor="crosshair";
			doEvent(bl,"click",detectBallon);
			sec=40;
		}
		else{		
			speeds=new Array(50,75,92);
			sec=speeds[rand(0,2)];
		}*/
		clearTimeout(self["upb_" + nr]);
		document.getElementById('balonnen').removeChild(bl);
		createBallon(nr);
		return;
	}
	self["upb_" + nr]=setTimeout("upBallon("  + nr +  "," + sec + "," + hor + "," + origml + ",'"  + dir +  "',"  + origheight +  ")",sec);
}
function fadeout(ball,opac){
	bln=document.getElementById('ballon_' + ball);
	if(opac>0){
		//alert(opac);
		op=opac-.02;
		ieopac=opac*100;
		bln.style.opacity=op;
		bln.style.filter="alpha(opacity="  + ieopac + ")";
		faw=setTimeout("fadeout("  + ball +  ","  + op +  ")",50);
	}
	else{
		clearTimeout(faw);
		document.getElementById('balonnen').removeChild(bln);
		createBallon(ball);
		return;
	}
}
function detectBallon(e){
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // opvangen Safari bug
		targ = targ.parentNode;
		makeS();
		idar=targ.id.split('_');
		//targ.src="art2009/img/balonnen/ster.png";
		clearTimeout(self["upb_" + idar[1]]);
		targ.style.cursor="";	
		//sf=setTimeout("fadeout(" + idar[1] + ",1)",1000);
		//targ.style.display='none';
		document.getElementById('balonnen').removeChild(targ);
		createBallon(idar[1]);
		
}
function freeBalloons(){
	for(i=1; i<=MAX_BALLONNEN; i++){
		createBallon(i);
	}
}
function createBallon(i){
	j=i;
	helft=Math.floor(MAX_BALLONNEN / 2);
	if(i>helft){j=j-helft;}
	startml=(i<=helft)?-83:1015;
	newB=document.createElement("img");
	newB.className="ballon";
	rndnr=rand(1,16);
	newB.src="art2009/img/balonnen/ballon_" + rndnr + ".png";
	BALML=(startml + rand(-10,10));
	newB.style.marginLeft=BALML + "px";		
	BALTOP=(getHeight() + (150 * j) + TOP_MARGE[rand(0,1)]);
	newB.style.top=BALTOP + "px";
	newB.id="ballon_" + i;
	document.getElementById('balonnen').appendChild(newB);
	if(newB.src.indexOf('_16')>-1){
		newB.style.cursor="crosshair";
		doEvent(newB,"click",detectBallon);
	}
	tmo=(rndnr==16)?40:SPEEDS[rand(0,2)];
	hormarge=rand(5,25);
	lr=new Array("left","right");
	defdir=lr[rand(0,1)];
	setTimeout("upBallon("  + i +  ","  + tmo +  ","  + hormarge + "," + BALML +  ",'"  + defdir +  "',"  + BALTOP +  ")",tmo);
}
