﻿/*	objects.js contains:

	PetObjects Superclass - holds and manages array of PetObject(s)
	PetObject  Subclass   - holds a image object, and image properties

	about:
	
	loads objects.xml
	<petobjects>
		<petobject name="petimg" width="128" height="128" />
		<petobject name="imgFood" image="food.PNG" width="30" height="12" />
		<petobject name="imgWater" image="water.PNG" width="30" height="12" />
		<petobject name="imgPoop" image="poop.PNG" width="13" height="10" />
	</petobjects>
	
	every objects.xml file MUST contain a petobject with the name 'petimg'
	all other objects are optional.
	
	objects are simply images used by digipet, the images may move, appear and disapear according
	to actions what the pet performs in 'tasks.xml' or menu items clicked from 'menu.xml'
*/

function PetObjects(petobj, xmlobj){
	// main class variables
	var me = this;
	this.petobj = petobj;
	var petobject = new Array();
	var petobjectcount = 0;
	var petimg = null;
	/*-------------------
		constructor
		read the objects.xml file and create seperate objects
	  -------------------*/	
	  
	constructor();
	function constructor(){
		var tmpobj = xmlobj.getElementsByTagName('petobject');
		//var loadcount=0,inloop=true;
		for(var i=0; i<tmpobj.length; i++){
			petobject[petobjectcount] = new petObject(petobj.rootfolder+"objects"+petobj.dirslash, petobjectcount, tmpobj[i], me);
			if(petobject[petobjectcount].name=="petimg") petimg = petobject[petobjectcount];
			petobjectcount++;
		}
		updatePosRelative();		
	}
	
	/*-------------------
		main functions
		grab, edit and destroy the objects created within the constructor
	  -------------------*/			
	this.updatePosRelative = updatePosRelative;
	function updatePosRelative(){
		if(petimg==null) return;
		for(var i=0; i<petobjectcount; i++){
		if(petobject[i].name!="petimg") petobject[i].updatePosRelative(petimg);
		}
		var posY=petimg.getY();
		if(posY < 20) posY=20; else posY-=10;
		if(compat_window){
		petobj.lblpetname.style.left=petimg.getX();
		petobj.lblpetname.style.top=posY;
		}else{
		petobj.lblpetname.x=petimg.getX();
		petobj.lblpetname.y=posY;			
		}
	}
	
	this.getObject = function(index){
		return petobject[index];
	}
	
	this.getObjectByName = function(key){
		for(var i=0; i<petobjectcount; i++){
			if(petobject[i].name==key)
				return petobject[i];
		}
		return null;
	}
	
	this.getObjectCount = function(){
		return petobjectcount;	
	}
	
	this.destroy = destroy;
	function destroy(){
		for(var i=0; i<petobjectcount; i++){
			petobject[i].destroy();
			petobject[i] = null;
		}
		petobjectcount=0 ;
	}
}

function petObject(path, index, objxml, objects){
	// global variables to be used in this class.
	var me = this;
	this.index = index;
	this.name="";
	this.element=null;
	// variables used to store the properties of the element object
	var memx=0, memy=0, memwidth=0, memheight=0, memsrc="",memvisible=false;
	var petx=0, pety=0, memvalue=10, memclick, memonout;

	constructor();
	function constructor(){
		if(!(me.name=objxml.getAttribute("name"))) me.name="";
		if(!(memsrc=objxml.getAttribute("image"))) memsrc="";
		if(!(memwidth=objxml.getAttribute("width"))) memwidth=16;
		if(!(memheight=objxml.getAttribute("height"))) memheight=16;
		if(!(memclick=objxml.getAttribute("onclickrender"))) memclick="";
		if(!(memonout=objxml.getAttribute("onmouseoutrender"))) memonout="";
		if(!(memx=parseInt(objxml.getAttribute("x")))) memx=0;
		if(!(memy=parseInt(objxml.getAttribute("y")))) memy=0;

		if(memsrc!="") memsrc=path+memsrc;
		me.element=appendHTMLElement(view, 
		"<img enabled='true' visible='false' x='"+memx+"' y='"+memy+"' width='"+memwidth+"' height='"+memheight+"' src='"+memsrc+"' />"
		);
		if(memclick!="") me.element.onclick = objectclicked;
		if(memonout!="") me.element.onmouseout = objectmouseout; 
	}

	this.objectclicked = objectclicked;
	function objectclicked(){
		objects.petobj.brain.eventhandler("objectclicked","render",memclick,"1");
	}
	
	this.objectmouseout = objectmouseout;
	function objectmouseout(){
		objects.petobj.brain.eventhandler("objectclicked","render",memonout,"2");			
	}
	
	this.updatePosRelative = updatePosRelative;
	function updatePosRelative(petimg){
		if(me.name=="petimg"){ petx=0; pety=0; return; }
		petx = petimg.getX();
		pety = petimg.getY();
		if(compat_window){ 
			me.element.style.left=memx+petx;
			me.element.style.top=memy+pety;
		}else{
			me.element.x=memx+petx; 
			me.element.y=memy+pety;	 				
		}
	}
	
	this.setVisible =  setVisible;
	function setVisible(newValue){ 
		memvisible = newValue;
		if(newValue==true){
			objects.petobj.brain.eventhandler("objectadded",me.name,memvalue,"");
			if(compat_window) me.element.style.visibility="visible"; else me.element.visible="true";	 
		}else{
			objects.petobj.brain.eventhandler("objectremoved",me.name,memvalue,"");
			if(compat_window) me.element.style.visibility="hidden"; else me.element.visible="false";	 	
		}
	}
	
	this.getVisible = getVisible;
	function getVisible(){
		return memvisible;		
	}
	
	this.setX = setX;
	function setX(newValue){
		memx = newValue;
		if(compat_window) me.element.style.left=newValue+petx; else me.element.x=newValue+petx;	 		
		if(me.name=="petimg"){ objects.updatePosRelative(); }		
	}
	
	this.getX = getX;
	function getX(){
		return memx;		
	}
	
	this.setY = setY;
	function setY(newValue){
		memy = newValue;
		if(compat_window) me.element.style.top=newValue+pety; else me.element.y=newValue+pety;	 		
		if(me.name=="petimg"){ objects.updatePosRelative(); }		
	}
	
	this.getY = getY;
	function getY(){
		return memy;		
	}
	
	this.setWidth = setWidth;
	function setWidth(newValue){
		memwidth=newValue;
		if(compat_window) me.element.style.width=newValue; else me.element.width=newValue;	 				
	}
	
	this.getWidth = getWidth;
	function getWidth(){
		return memwidth;		
	}

	this.setHeight = setHeight;
	function setHeight(newValue){
		memheight = newvalue;	
		if(compat_window) me.element.style.height=newValue; else me.element.height=newValue;	 				
	}
	
	this.getHeight = getHeight;
	function getHeight(){
		return memheight;		
	}
	
	this.setImage = setImage;
	function setImage(newValue){
		memsrc=newValue;
		me.element.src=newValue; 						
	}
	
	this.getImage = getImage;
	function getImage(){
		return memsrc;
	}	
	
	
	this.setValue = setValue;
	function setValue(newValue){
		memvalue = newValue;
		if(newValue==0) setVisible(false); else setVisible(true);
	}
	
	this.getValue = getValue;
	function getValue(){
		return memvalue;
	}
	
	this.destroy = destroy;
	function destroy(){
		if(compat_window)	view.removeChild(me.element); else 	view.removeElement(me.element);
		me.element=null;
	}
}
