// ----------------------------------------------------------------
demoSiteOnLoad = function(boolFirstPage){//	
	//
	if(boolFirstPage!=undefined && boolFirstPage) {
		document.prel = new Object();
		document.prel.img1 = new Image();
		document.prel.img2 = new Image();
		document.prel.img3 = new Image();
		document.prel.img4 = new Image();
		document.prel.img1.src = "img/custom/leftSky.jpg";
		document.prel.img2.src = "img/custom/upperSky.jpg";
		document.prel.img3.src = "img/custom/bottomSky.jpg";
		document.prel.img4.src = "img/custom/zalaisAugs.gif";
		//
		window.setTimeout( function(){
			$('leftSky').style.display = "none";
			$('bottomSky').style.display = "none";
			$('upperSky').style.display = "none";
			$('greenTree').style.display = "none";
		}, 10);
		window.setTimeout( function(){
			Effect.BlindDown($('greenTree'), {duration:1.5});
			Effect.Appear($('leftSky'), {duration:2.5});
			Effect.Appear($('bottomSky'), {duration:2.5});	
			Effect.Appear($('upperSky'), {duration:2.5});
		}, 20);
		//
		//Effect.Move($('bottomSky'), {x:100, y:100});
		window.setTimeout( function(){ isReadyToAct=true; }, 5000);
	}
	//
	window.setTimeout( function(){ new Bird(); }, 1000);
	window.setTimeout( function(){ new Bird(); }, 2000);
	window.setTimeout( function(){ new Bird(); }, 3000);
}
// ----------------------------------------------------------------
var busyElements = new Array();
var isReadyToAct = false;
function shakeTheElement_over(el){
	return false;
	if(busyElements[el.id]) return;
	if(!isReadyToAct) return;
	busyElements[el.id]=true;
	el.style.display = "none";
	Effect.Appear(el, {duration:2.0} );
	window.setTimeout( function(){ busyElements[el.id]=false; }, 15000);
}
function shakeTheElement_out(el){ }
// ----------------------------------------------------------------
function gotoBottomOfPage( ){
	var el = $("bottomContainer");
	Effect.ScrollTo(el, {duration:0.9} );
}
// ----------------------------------------------------------------
function treeMouseOver(el){
	$('greenTree').style.backgroundImage = "url(img/custom/zalaisAugs_hover.gif)";
}
function treeMouseOut(el){
	$('greenTree').style.backgroundImage = "url(img/custom/zalaisAugs.gif)";
}
//==================================================================
var imageRollingButton = null;
var imageMegaFast = false;
var doDebug = false;
// ----------------------------------------------------------------
function imageRollingStart(el, boolMegaFast){
	if(doDebug) $('debug').innerHTML += "<br>imageRollingStart(el) - called by: " + el.id;
	if(imageRollingButton!=null) return; 
	if(boolMegaFast==undefined) boolMegaFast = false;
	imageMegaFast = boolMegaFast;
	imageRollingButton = el;
	new Effect.Opacity("imagePortfolioDiv", {duration:1.0, from:0.5, to:1.0} );
	repeatMe( );
}
// ----------------------------------------------------------------
function imageRollingStop(el){
	imageRollingButton = null;
	imageMegaFast = false;
	if(doDebug) $('debug').innerHTML += "<br>imageRollingStop(el) - canceled by: " + el.id;
}
// ----------------------------------------------------------------
function repeatMe ( ){ 
	if(doDebug) $('debug').innerHTML += "<br>repeatMe()";
	if(imageMegaFast) $('debug').innerHTML = "<br>Mega fast";
	var speed = 14; var opacity = 0.7; var goLeft = false;
	if(imageRollingButton==$("rightImageButton")) goLeft = true;
	var tb = $("imagePortfolioTable");		
		//tb.style.opacity  = opacity;
		//tb.style.filter = 'alpha(opacity=' + opacity*100 + ')';
	var left = 0; 
	if(tb.style.left!=undefined && tb.style.left!=null ) left = tb.style.left;
	
	left = left.toString().replace(/px/i,"");
	if(doDebug) $('debug').innerHTML = "Before: goLeft=" + goLeft + "; left=" + left;
	if(goLeft){ 		
		if(!(left<(-1)*(tb.offsetWidth-325))) { 
			left = Math.floor(left) - speed; 
			
		} else {
			$('rightImageButton').className = "rightButton";
			return;
		}
	} else {
		if(left < 0) {
			left = Math.floor(left) + speed;			
		} else {
			$('leftImageButton').className = "leftButton";
			return;
		}
	}
	$('rightImageButton').className = "rightButtonA";
	$('leftImageButton').className = "leftButtonA";
	if(left!=0) tb.style.left = left + "px"; else tb.style.left = "";
	if(doDebug) $('debug').innerHTML += "<br>After: goLeft=" + goLeft + "; left=" + left;
	//repeatMe();
	if(imageRollingButton!=null) window.setTimeout( repeatMe, 40 );
}
//==================================================================
// Document extension functions for DOM model
document.getFirstChild = function (element, nodeName, number){
	if(element.firstChild==undefined || element.firstChild==null) return;
	if(number==undefined) number = 1;
	return document.getNextSibling(element.firstChild, nodeName, number);
}
// ----------------------------------------------------------------
document.getLastChild = function (element, nodeName, number){
	if(element.lastChild==undefined || element.lastChild==null) return;	
	if(number==undefined) number = 1;
	return document.getPreviousSibling(element.lastChild, nodeName, number);
}
// ----------------------------------------------------------------
document.getNextSibling = function (element, nodeName, number){
	var foundElement = element;
	var currMatch = 0;
	if(number==undefined) number = 1;
	while(foundElement!=null && foundElement.nextSibling!=null){		
		//alert(foundElement);
		if(foundElement.nodeName==nodeName) {
			currMatch++;
			if(currMatch==number) return foundElement;
		}
		foundElement = foundElement.nextSibling;
	}
	return null;
}
// ----------------------------------------------------------------
document.getPreviousSibling = function (element, nodeName, number){
	var foundElement = element;
	var currMatch = 0;
	if(number==undefined) number = 1;
	while(foundElement!=null && foundElement.previousSibling!=null){
		//alert(foundElement);
		if(foundElement.nodeName==nodeName) {
			currMatch++;	
			if(currMatch==number) return foundElement;
		}
		foundElement = foundElement.previousSibling;
	}
	return null;
}
//=============================================================================


var Bird = function() {		
	this.minLeft = 5; this.maxLeft = document.body.clientWidth - 20;
	this.minTop = 5; this.maxTop = 270;
	// ----------------------------------------------------------------
	this.Bird = function (){
		Bird.Counter++;
		this.div = document.createElement("DIV");
		this.div.className = "ptashka";
		this.div.id = "ptashka" + Bird.Counter;			
		this.behaviour = this.getRandomBehaviour();
		this.div.style.left = this.behaviour.left + "px";
		this.div.style.top = this.behaviour.top + "px";		
		this.startGoing();
	}
	// ----------------------------------------------------------------
	this.startGoing = function (){
		document.body.appendChild(this.div);
		this.div.style.display = "block";
		this.div.style.zIndex = 1000;
		this.div.Bird = this;
		this._performStep(this.div.id);
	}
	// ----------------------------------------------------------------
	this._performStep = function (id){		
		var $this = $(id).Bird;
		var left = $this.div.style.left;
		var top = $this.div.style.top;
		left = left.toString().replace(/px/i,"");
		top = top.toString().replace(/px/i,"");
		// calculate linear changed value
		left = (Math.floor(left) + $this.behaviour.hSpeed * $this.behaviour.hDirection);
		top = (Math.floor(top) + $this.behaviour.vSpeed * $this.behaviour.vDirection);
		if( ( left<$this.minLeft || left>$this.maxLeft) || (top > $this.maxTop || top < $this.minTop) ) { 
			document.body.removeChild($this.div);
			delete $this.div;
			this.Bird();
			return;
		}
		// calculate sinusoidal movement
		top = Math.abs( top + Math.cos(left*$this.behaviour.cosinus.koeficient)*$this.behaviour.cosinus.amplitude );
		// set values
		$this.div.style.left = left + "px";
		$this.div.style.top = top + "px";
		window.setTimeout( function() { $this._performStep(id); }, 450);
	}
	// ----------------------------------------------------------------
	this.getRandomBehaviour = function (){
		var p = new Object();
		p.left = this.minLeft; 
		p.top = Math.abs( (this.maxTop - this.minTop) * Math.random() / 2 + this.minTop);    /*  0 xxxxxxxx X xxxxxxx 1/2 -------- | -------- 1  */
		p.vDirection = +1; p.hDirection = +1;
		p.vSpeed = 0; p.hSpeed = 5;
		p.cosinus = new Object();
		p.cosinus.amplitude = 5; p.cosinus.koeficient = Math.PI / 50;
		p.isHorizontal = true;
		//
		var rndSize = Math.random();
		if(Math.random()<0.5) {
			p.hDirection = -1; p.left = this.maxLeft;
			if(rndSize < 0.33) { this.div.className += " pt_X"; p.hSpeed = Math.floor(rndSize*5); }
			else if(rndSize < 0.66) { this.div.className += " pt_XX"; p.hSpeed = Math.floor(rndSize*5) + 5; }
			else { this.div.className += " pt_XXX"; p.hSpeed = Math.floor(rndSize*5) + 10; }
		} else {
			if(rndSize < 0.33) { this.div.className += " pt_X_m"; p.hSpeed = Math.floor(rndSize*5); }
			else if(rndSize < 0.66) { this.div.className += " pt_XX_m"; p.hSpeed = Math.floor(rndSize*5) + 5; }
			else { this.div.className += " pt_XXX_m"; p.hSpeed = Math.floor(rndSize*5) + 10; }
		}
		p.hSpeed = Math.floor(rndSize*10);

		if(Math.random()<0.5) {
			p.vDirection = -1;
			p.top = Math.abs( (this.maxTop - this.minTop) * (1 + Math.random()) / 2);    /*  0 -------- | -------- 1/2 xxxxxxxx X xxxxxxx 1  */
		}
		p.hSpeed = Math.floor(Math.random()*10) + 3;
		return p;
	}
	this.Bird();
};
Bird.Counter = 0;
//=============================================================================
var closePopupDiv = function(){
	var but = $('buttonUpper');
	var div = $('longDescription');
	but.style.display = "none";
	div.style.display = "none";
}
var showPopupDiv = function(){
	var but = $('buttonUpper');
	var div = $('longDescription');
	but.style.display = "block";
	div.style.display = "block";
}
var sendUsMessage = function() {
	closePopupDiv();
	gotoBottomOfPage( );
}
//=============================================================================
function print_rjs(e){
	var d = ""; var i = 0;
	for(x in e) { 
		d = d + x + "='" + e[x] + "'; "; 
		if(i > 5) { 
			d = d + "\r\n"; i = 0;
		}
		i++;   
	}
	alert(d);
}
