
Animals = SZN.ClassMaker.makeClass({
	NAME: "animals",
	VERSION: "1.0",
	CLASS: "class"
});

Animals.prototype.$constructor = function(number) {
	this.number = number;
	this.animal= new Array();
	for(i=0;i<this.number;i++){
		this.animal[i] = new Animal(i);
	}
	this.show = false;
	this.active = false;
}
Animals.prototype.sah = function() {
	if(this.active===false){
	 	this.active = Math.round(Math.random()*(this.number-1));
	 	this.animal[this.active].active();
	}else{
		this.animal[this.active].deactive();
		this.active = false;
	}
 	
}


//----------------------------------------------------------------------------------------------
Animal = SZN.ClassMaker.makeClass({
	NAME: "animal",
	VERSION: "1.0",
	CLASS: "class"
});

Animal.prototype.$constructor = function(id) {
	this.id = id;
	this._request = false;
	this._requestXhr = false;
	this.tips = new Array();
	this.emotes = new Array();
	this.fetchStrings();
	this.show = false;
	this.dom = SZN.gEl("mAnimal"+id);
	this.domSpeech = SZN.gEl("mAnimal"+id+"Speech");
	this.domEmote = SZN.gEl("mAnimal"+id+"Emote");
	this.actived = false;
	this.iter = false;
	this.tick = SZN.bind(this, this.tick);
	this.opacity = 0;
	this.tickFreq = 100;
	this.tickStep = 0.1;
	this.fadeDirect = 1;
	if(SZN.Browser.client=="ie" || SZN.Browser.client=="safari") this.tickStep = 1;
}
Animal.prototype.fetchStrings = function() {

	var url = "./?x=mAnimals&op=fetchstrings&animal="+this.id;
	this._request = new SZN.HTTPRequest();
	this._request.setMethod("get");
	this._request.setFormat("txt");
	this._request.setMode("async");
	this._requestXhr = this._request.send(url,this,"setStrings");		
	
}
Animal.prototype.setStrings = function(txt, status) {
 	if (status == 200 && txt != "FAILED"){
 		this._requestXhr = false;
 		//alert(txt);
 		eval(txt);
		
 	}
}
Animal.prototype.active = function() {
	if(Math.random()<0.5){
		this.actived = this.domSpeech;
		this.actived.innerHTML = this.tips[Math.round(Math.random()*(this.tips.length-1))];
	}else{
		this.actived = this.domEmote;
		this.actived.innerHTML = this.emotes[Math.round(Math.random()*(this.emotes.length-1))];
	}
	
	if(this.actived){
		if(this.iter) clearInterval(this.iter);
		this.iter = setInterval(this.tick,this.tickFreq);
		Element.setOpacity(this.actived,this.opacity);
		this.actived.style.display = "block";
	}
	
}
Animal.prototype.deactive = function() {
	if(this.actived){
		if(this.iter) clearInterval(this.iter);
		this.iter = setInterval(this.tick,this.tickFreq);
		Element.setOpacity(this.actived,this.opacity);
	}
}
Animal.prototype.tick = function() {
	this.opacity = this.opacity + (this.tickStep*this.fadeDirect);
	if(this.opacity > 1) this.opacity = 1;
	if(this.opacity < 0) this.opacity = 0;
	
	Element.setOpacity(this.actived,this.opacity);
	if(this.opacity>=1){
		clearInterval(this.iter);
		this.fadeDirect = -1;
	}
	if(this.opacity<=0){
		clearInterval(this.iter);
		this.fadeDirect = 1;
		this.actived.innerHTML = "";
	}
	
}

Element.setStyle = function(element, style) {
  element = $(element);
  for(k in style) element.style[k.camelize()] = style[k];
}
Element.getOpacity = function(element){  
  var opacity;
  if (opacity = Element.getStyle(element, 'opacity'))  
    return parseFloat(opacity);  
  if (opacity = (Element.getStyle(element, 'filter') || '').match(/alpha\(opacity=(.*)\)/))  
    if(opacity[1]) return parseFloat(opacity[1]) / 100;  
  return 1.0;  
}
Element.setOpacity = function(element, value){  
  element= $(element);  
  if (value == 1){
    Element.setStyle(element, { opacity: 
      (/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ? 
      0.999999 : null });
    if(/MSIE/.test(navigator.userAgent))  
      Element.setStyle(element, {filter: Element.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'')});  
  } else {  
    if(value < 0.00001) value = 0;  
    Element.setStyle(element, {opacity: value});
    if(/MSIE/.test(navigator.userAgent))  
     Element.setStyle(element, 
       { filter: Element.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'') +
                 'alpha(opacity='+value*100+')' });  
  }   
} 