//Z.DHTML
//Z WORLD
//CONTACT www.zexe.net

//VARIABLES DE COMPORTAMENT:
//sempre entre limits 1 i 50 aprox..

var camina=10 //defecte 10
var neteja=7 //defecte 5
var vola=10 //defecte 10
var nervis=10 //defecte 10

/////REMOTE
remoteDir="http://www.zexe.net/Z/";

var z;
var zimages;

function fly(lay){
	this.lay=new DynLayer(lay);
	this.update=flyUpdate;
	this.zstatus="quiet";
	this.fly=flyFly;
	this.walk=flyWalk;
	this.ztarget=0;
	this.walkim=true;
	this.ang=0;
	
}

function flyUpdate(){
	//en qualsevol cas si s'apropa volar a un altre lloc
	dd=mouseP.distance(new point(this.lay.x,this.lay.y));
	
	//status=this.zstatus;
	
	if(dd<50 && (this.zstatus=="quiet" || this.zstatus=="walk") ){
		//nomes el 50% dels temps..
		if(Math.random()<0.5) return;
		//empaitada...
		this.fly();
		return;
	}
	switch (this.zstatus){
		case "quiet":
			if(Math.random()*100<(camina/10.0)){
				//alert("walk");
				this.walk();
				break;
			}
			
			if(Math.random()*100<(neteja/20.0)){
				this.zstatus="clean";
				break;
				
			}
			setImage(0,getDir(this.ang));
			if(Math.random()*1000<(vola/2+nervis/5.0)){
				this.fly();
				break;
			}
			
			
			
			
		break;
		
		case "clean":
			if(this.walkim==true){
				setImage(3,getDir(this.ang));
			}else{
				setImage(4,getDir(this.ang));
			}
			this.walkim=!this.walkim;
			if(Math.random()*100<2){
				this.zstatus="quiet";
				
			}
			break;
		
		case "fly":
			//moure cap a ztarget.. rapid
			cp=new point(this.lay.x,this.lay.y);
			ang=this.ztarget.angle(cp);
			dd=this.ztarget.distance(cp);
			//this.ang=ang;
			pnext=cp.polar(ang+(dd/1000),100);
			
			
			
			this.lay.moveTo(pnext.x,pnext.y);
			if(cp.distance(this.ztarget)<100){
				this.zstatus="quiet";
				this.lay.moveTo(this.ztarget.x,this.ztarget.y);
			}
			break;
		
	
		
		case "walk":
			
			cp=new point(this.lay.x,this.lay.y);
			ang=this.ztarget.angle(cp);
	
			if(this.walkim==true){
				setImage(1,getDir(this.ang));
			}else{
				setImage(2,getDir(this.ang));
			}
			this.walkim=!this.walkim;
			
			pnext=cp.polar(ang,10);
			
			this.lay.moveTo(pnext.x,pnext.y);
			if(cp.distance(this.ztarget)<12){
				if(Math.random()*100<(nervis)){
					this.walk();
				}else{
					this.zstatus="quiet";
				}
			}
				

			
		break;
		
		
	}


}

function flyFly(){
	//look for a new point in the screen...
	this.ztarget=new point( Math.round(Math.random()*windowW),Math.round(Math.random()*windowH) );
	ang=(new point(this.lay.x,this.lay.y)).angle(this.ztarget);
	this.ang=ang;
	setImage(0,getDir(ang));
	this.zstatus="fly";
}

function setImage(n,ang){
	if(ang==1) ang=2;
	if(ang==5) ang=4;
	if(ang==7) ang=8;
	if(ang==11) ang=10;
	
	
	//nomes si realment la ha carregat
	if(zimages[n][ang].complete)
		GetImageFromLayer("z","zimg").src=zimages[n][ang].src;
}

function isOut(p){
	if (p.x<0 || p.x>windowW) return true;
	if (p.y<0 || p.y>windowH) return true;
	return false;
	
}

function flyWalk(){
	
	out=true;
	while(out){
		this.ztarget=new point(this.lay.x+Math.random()*100-50,this.lay.y+Math.random()*100-50 );
		out=isOut(this.ztarget);
	}
	ang=(new point(this.lay.x,this.lay.y)).angle(this.ztarget);
	this.ang=ang;
	setImage(1,getDir(ang));
	
	this.zstatus="walk";

}

function incAngle(ang,inc){
	res=ang+inc;
	if( res>Math.PI*2) res=res-Math.PI*2;
	if( res<0 ) res=res+Math.PI*2;
	return res;
	

}

function update(){
	z.update();
	setTimeout("update()",40);
}


var windowH,windowW;

function WindowDims(){

	if (!document.all) {
		windowW=window.innerWidth;
		windowH=window.innerHeight;
	}else{
		windowW=document.body.clientWidth;
		windowH=document.body.clientHeight;
	}

}

function DynMouseMove(x,y){
	
	mouseP.x=x;
	mouseP.y=y;
	if(z){
		ang=(new point(z.lay.x,z.lay.y)).angle(mouseP);
		//status=getDir(ang);
	}

}

function GetImageFromLayer(idLayer,nameImage){
	if(is.ns5){
		return document.getElementById(nameImage);
	}
	if (!document.all) {
		return document.layers[idLayer].document.images[nameImage]
	}else{
		return document.all[idLayer].document.images[nameImage]
	}
}

/////////class point
function point(x,y){
	this.x=x;
	this.y=y;
	//methods
	this.distance=pointDistance;
	this.polar=pointPolar;
	this.angle=pointAngle;
	this.toString=pointToString;
	this.rounds=pointRound;
}

function pointToString(){
	return (this.x+":"+this.y);
}

function pointDistance(p2){
	return Math.sqrt( (this.x-p2.x)*(this.x-p2.x)+(this.y-p2.y)*(this.y-p2.y) );

}

//gets a new point from the current given a degree and module..
function pointPolar(angle,mod){
   return( new point( this.x+Math.sin(angle)*mod, (this.y+Math.cos(angle)*mod)) );
   
}

function pointAngle(p2){
	xx=this.x-p2.x;
	yy=-this.y+p2.y;
	
	if(xx==0 && yy>0) return 0;
	if(xx==0 && yy<0) return Math.PI;
	res=-Math.atan(-yy/xx)+Math.PI/2;
	if(xx<0 ) return (res+Math.PI);
	return res;
}

function getDir(ang){
	res=Math.round( ((ang*12)/(Math.PI*2))-0.5 );
	res=res-12;
	if ( res==-12 ) res=0;

	
	return(Math.abs(res) );

}
function getDir2(ang){
	res=Math.round( ((ang*12)/(Math.PI*2))-0.5 );
	res=res-12;
	if ( res==-12 ) res=0;
	
	res=res+6;
	if (res>12) res=res-12;
	
	return(Math.abs(res) );

}
function pointRound(){
	this.x=Math.round(this.x);
	this.y=Math.round(this.y);
	return new point(this.x,this.y);

}

var mouseP;

function init(){

	zimages=new Array(5);
	
	//carregar totes les imatges..
	//això pot portar un cert temps...
	//son 60 imatges en total!
	
	for(i=0;i<5;i++){
		zimages[i]=new Array(12);
		for(j=0;j<12;j++){
			if(j!= 1 && j!= 5 && j!=7 && j!= 11 ){
				zimages[i][j]=new Image();
				ii=i+1;
				zimages[i][j].src=remoteDir+"images/M"+ii+"_"+j+".gif";
			}
		}
	}
	
	initMouseEvents();
	WindowDims();
	
	z=new fly("z"); 
	//z.lay.moveTo(100,100);
	
	//z.lay.moveTo(Math.round(windowW/2)-20,Math.round(windowH/2)-40);
	z.lay.moveTo(Math.round( Math.random()*(windowW-40) )+20,Math.round( Math.random()*(windowH-40) )+20);
	
	mouseP=new point(0,0);
	setTimeout("update()",40);
}

function openzexe(){
	var w=open('http://www.zexe.net');
}



///Dynlayer functions
function DynLayer(id,nestref,frame) {
	if (!is.ns5 && !DynLayer.set && !frame) DynLayerInit()
	this.frame = frame || self
	if (is.ns) {
		if (is.ns4) {
			if (!frame) {
				if (!nestref) var nestref = DynLayer.nestRefArray[id]
				if (!DynLayerTest(id,nestref)) return
				this.css = (nestref)? eval("document."+nestref+".document."+id) : document.layers[id]
			}
			else this.css = (nestref)? eval("frame.document."+nestref+".document."+id) : frame.document.layers[id]
			this.elm = this.event = this.css
			this.doc = this.css.document
		}
		else if (is.ns5) {
			this.elm = document.getElementById(id)
			this.css = this.elm.style
			this.doc = document
		}
		this.x = this.css.left
		this.y = this.css.top
		this.w = this.css.clip.width
		this.h = this.css.clip.height
	}
	else if (is.ie) {
		this.elm = this.event = this.frame.document.all[id]
		this.css = this.frame.document.all[id].style
		this.doc = document
		this.x = this.elm.offsetLeft
		this.y = this.elm.offsetTop
		this.w = (is.ie4)? this.css.pixelWidth : this.elm.offsetWidth
		this.h = (is.ie4)? this.css.pixelHeight : this.elm.offsetHeight
	}
	this.id = id
	this.nestref = nestref
	this.obj = id + "DynLayer"
	eval(this.obj + "=this")
}
function DynLayerMoveTo(x,y) {
	if (x!=null) {
		this.x = x
		if (is.ns) this.css.left = this.x
		else this.css.pixelLeft = this.x
	}
	if (y!=null) {
		this.y = y
		if (is.ns) this.css.top = this.y
		else this.css.pixelTop = this.y
	}
}
function DynLayerMoveBy(x,y) {
	this.moveTo(this.x+x,this.y+y)
}
function DynLayerShow() {
	this.css.visibility = (is.ns4)? "show" : "visible"
}
function DynLayerHide() {
	this.css.visibility = (is.ns4)? "hide" : "hidden"
}
DynLayer.prototype.moveTo = DynLayerMoveTo
DynLayer.prototype.moveBy = DynLayerMoveBy
DynLayer.prototype.show = DynLayerShow
DynLayer.prototype.hide = DynLayerHide
DynLayerTest = new Function('return true')

// DynLayerInit Function
function DynLayerInit(nestref) {
	if (!DynLayer.set) DynLayer.set = true
	if (is.ns) {
		if (nestref) ref = eval('document.'+nestref+'.document')
		else {nestref = ''; ref = document;}
		for (var i=0; i<ref.layers.length; i++) {
			var divname = ref.layers[i].name
			DynLayer.nestRefArray[divname] = nestref
			var index = divname.indexOf("Div")
			if (index > 0) {
				eval(divname.substr(0,index)+' = new DynLayer("'+divname+'","'+nestref+'")')
			}
			if (ref.layers[i].document.layers.length > 0) {
				DynLayer.refArray[DynLayer.refArray.length] = (nestref=='')? ref.layers[i].name : nestref+'.document.'+ref.layers[i].name
			}
		}
		if (DynLayer.refArray.i < DynLayer.refArray.length) {
			DynLayerInit(DynLayer.refArray[DynLayer.refArray.i++])
		}
	}
	else if (is.ie) {
		for (var i=0; i<document.all.tags("DIV").length; i++) {
			var divname = document.all.tags("DIV")[i].id
			var index = divname.indexOf("Div")
			if (index > 0) {
				eval(divname.substr(0,index)+' = new DynLayer("'+divname+'")')
			}
		}
	}
	return true
}
DynLayer.nestRefArray = new Array()
DynLayer.refArray = new Array()
DynLayer.refArray.i = 0
DynLayer.set = false;
/////////////////
function initMouseEvents() {
	document.onmousedown = mouseDown
	document.onmousemove = mouseMove
	document.onmouseup = mouseUp
	if (is.ns) document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP)
}
function mouseDown(e) {
	if ((is.ns && e.which!=1) || (is.ie && event.button!=1)) return true
	var x = (is.ns)? e.pageX : event.x+document.body.scrollLeft
	var y = (is.ns)? e.pageY : event.y+document.body.scrollTop
	if (is.ns && e.target!=document) routeEvent(e)
	if (Scroll && ScrollTestActive()) return false
	else if (Drag && drag.mouseDown(x,y)) return false
	else return DynMouseDown(x,y)
}

function mouseMove(e) {
	var x = (is.ns)? e.pageX : event.x+document.body.scrollLeft
	var y = (is.ns)? e.pageY : event.y+document.body.scrollTop
	if (is.ns && e.target!=document) routeEvent(e)
	if (Scroll && ScrollTestActive()) return false
	else if (Drag && drag.mouseMove(x,y)) return false
	else return DynMouseMove(x,y)
}
function mouseUp(e) {
	var x = (is.ns)? e.pageX : event.x+document.body.scrollLeft
	var y = (is.ns)? e.pageY : event.y+document.body.scrollTop
	if (is.ns && e.target!=document) routeEvent(e)
	if (Drag && drag.mouseUp(x,y)) return false
	else return DynMouseUp(x,y)
}

// overwrite these functions in your html source to do other mouse handling
function DynMouseDown(x,y) {return true}
//function DynMouseMove(x,y) {return true}
function DynMouseUp(x,y) {return true}

// include drag.js and/or scroll2.js after this file to overwrite these variables
Drag = null
Scroll = null

// BrowserCheck Object
function BrowserCheck() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.version = navigator.appVersion
	this.v = parseInt(this.version)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)
	this.min = (this.ns||this.ie)
}
is = new BrowserCheck()


function clicamosca(){
	//alert("Bien!");	
}







