/* ================================ +
		     Studio Parkers
		       animalsShow.js
 + ================================ */


/* 
** Made by: Thomas Lievestro
** Email:	info@lievestro.com
** Website:	www.lievestro.com
*/


/* ================================ +
              Variables
 + ================================ */

	var numberOfAnimalsOnStage = 3;
	var animals_on_stage = new Array;
	
	var animals = new Array("gans_blauw","gans_groen","gans_oranje","gans_roze","konijn_blauw","konijn_groen","konijn_oranje","konijn_roze","tor_blauw","tor_groen","tor_oranje","tor_roze","vis_blauw","vis_groen","vis_oranje","vis_roze"); 
		
	//eigenschappen van animal
	var gans = new Array("left","positief");
	var konijn = new Array("right","negatief");
	var tor = new Array("right","negatief");
	var vis = new Array("left","positief");
		
	var animals_properties = new Object;
	animals_properties.gans 	= gans;
	animals_properties.konijn 	= konijn;
	animals_properties.tor 		= tor;
	animals_properties.vis 		= vis;
		
	//positions
	var positionsLeft = new Array(-600,-550,-500,-450,-400,-350,-300,-250);
	var positionsRight = new Array(400,450,500,550,600,650,700,750,800,850,900);
	var positionsVertical = new Array(-400,-350,-300,-250,-200,-150,-100,-50,0,50,100,150,200,250,300,350,400,450,500);

	var pageSize;
	
	var interval = new Array(-1,0,1);
	var interval_left = new Array(0,1);
	var interval_right = new Array(-1,0);
	var timeOut = new Array(50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100);


/* ================================ */

function initAnimalsShow(){
	
	//staat in general.js
	setLayerHeight();
		
	for(i=0;i<numberOfAnimalsOnStage;i++) {
		
		//Pak een random animal
		animal = animals[Math.floor(Math.random() * (animals.length))];
		
		//animal name
		animal_name = animal.split('_')[0];

		//Maak animal aan
		animal_div = '<div id="'+animal+'"></div>';
		
		//Plaats de animal achter de andere animals
		var tmpChildElements = $('animals').childElements();
		var tmpLastChildElements = tmpChildElements[tmpChildElements.length-1];
		tmpLastChildElements.insert({after: animal_div });
		
		//Zorg ervoor dat de animal niet 2 keer te zien is
		animals=animals.without(animal);
		
		if(animals_properties[animal_name][0] == "left"){
			//Move the image to the new location
			$(animal).setStyle({ marginTop: positionsVertical[Math.floor(Math.random() * (positionsVertical.length))]+'px'});
			$(animal).setStyle({ marginLeft: positionsLeft[Math.floor(Math.random() * (positionsLeft.length))]+'px'});			
			
			TopInterval = interval[Math.floor(Math.random() * (interval.length))];
			LeftInterval = interval_left[Math.floor(Math.random() * (interval_left.length))];
			thisTimeOut = timeOut[Math.floor(Math.random() * (timeOut.length))];

			getAnimalsMove(animal,TopInterval,LeftInterval,'',thisTimeOut);
	
			
		}else{
			//Move the image to the new location
			$(animal).setStyle({ marginTop: positionsVertical[Math.floor(Math.random() * (positionsVertical.length))]+'px'});
			$(animal).setStyle({ marginLeft: (positionsRight[Math.floor(Math.random() * (positionsRight.length))] + (pageSize[0] - 1000) )+'px'}); //pageSize: kijk hoe breed de pagina is
			
			TopInterval = interval[Math.floor(Math.random() * (interval.length))];
			RightInterval = interval_right[Math.floor(Math.random() * (interval_right.length))];
			thisTimeOut = timeOut[Math.floor(Math.random() * (timeOut.length))];
			
			getAnimalsMove(animal,TopInterval,'',RightInterval,thisTimeOut);
		}
		
		//push animal in array
		animals_on_stage[i] = animal;
	}	
}

function setNewAnimalOnStage(){
		
	//Pak een random animal
	animal = animals[Math.floor(Math.random() * (animals.length))];

	//animal name
	animal_name = animal.split('_')[0];
	
	//Maak animal aan
	animal_div = '<div id="'+animal+'"></div>';
	
	//Plaats de animal achter de andere animals
	var tmpChildElements = $('animals').childElements();
	var tmpLastChildElements = tmpChildElements[tmpChildElements.length-1];
	tmpLastChildElements.insert({after: animal_div });
	
	//Zorg ervoor dat de animal niet 2 keer te zien is
	animals=animals.without(animal);
	
	if(animals_properties[animal_name][0] == "left"){
		//Move the image to the new location
		$(animal).setStyle({ marginTop: positionsVertical[Math.floor(Math.random() * (positionsVertical.length))]+'px'});
		$(animal).setStyle({ marginLeft: -710+'px'});			
		
		TopInterval = interval[Math.floor(Math.random() * (interval.length))];
		LeftInterval = interval_left[Math.floor(Math.random() * (interval_left.length))];
		thisTimeOut = timeOut[Math.floor(Math.random() * (timeOut.length))];

		getAnimalsMove(animal,TopInterval,LeftInterval,'',thisTimeOut);

	}else{
		//Move the image to the new location
		$(animal).setStyle({ marginTop: positionsVertical[Math.floor(Math.random() * (positionsVertical.length))]+'px'});
		$(animal).setStyle({ marginLeft: (pageSize[0] )+'px'}); //pageSize: kijk hoe breed de pagina is
		
		TopInterval = interval[Math.floor(Math.random() * (interval.length))];
		RightInterval = interval_right[Math.floor(Math.random() * (interval_right.length))];
		thisTimeOut = timeOut[Math.floor(Math.random() * (timeOut.length))];
		
		getAnimalsMove(animal,TopInterval,'',RightInterval,thisTimeOut);


	}
	
	//push animal in array
	animals_on_stage.push(animal);
	
}


function getAnimalsMove(animal,TopInterval,LeftInterval,RightInterval,thisTimeOut){
	x = parseInt($(animal).getStyle('marginLeft'));
	y = parseInt($(animal).getStyle('marginTop'))  
	
	if(LeftInterval != ''){
		if((TopInterval != 0) && (LeftInterval != 0)){
			x = x + parseInt(LeftInterval); 
		 	y = y + parseInt(TopInterval);
		}else{		
			x = x + 1;
		 	y = y + parseInt(TopInterval);
		}
	}else{
		if((TopInterval != 0) && (RightInterval != 0)){
			x = x + parseInt(RightInterval); 
		 	y = y + parseInt(TopInterval);
		}else{		
			x = x + -1;
		 	y = y + parseInt(TopInterval);
		}		
	}

	//Move the image to the new location
	$(animal).setStyle({ marginTop: y+'px'});
	$(animal).setStyle({ marginLeft: x+'px'});
	
	//Check of de animal al buiten beeld is
	if(((x+710) < 0) || ((y+770) < 0) || (x > pageSize[0]) || (y > pageSize[1])){

		//verwijder animal
		$(animal).remove();
		
		//Haal animal uit array
		animals_on_stage=animals_on_stage.without(animal); 
		
		//Zet animal terug in array
		animals.push(animal);
		
		window.setTimeout("setNewAnimalOnStage()", 2000);
	}else{
		window.setTimeout("getAnimalsMove(\"" + animal + "\",\"" + TopInterval + "\",\"" + LeftInterval + "\",\"" + RightInterval + "\",\"" + thisTimeOut + "\")",thisTimeOut);
	}
}


Behaviour.addLoadEvent(function(){ 
	initAnimalsShow();	
});


