﻿/*------------------------------------------
  DIGIPET - XMAS 07 SANTA BETA
  By: Martin Sykes
  Site: www.mydigipet.com / www.martrinex.net

  Custom javascript desktop pets for you to download, feed and look after, try to keep them happy
  so they live a long healthy life.

  Digipet is a platform to display basic virtual pets on your desktop and or browser,
  the platform is javascript based and simply uses pet xml files to describe how each pet works
  and then display a sequance of static images showing an animated pet. The system is dynamic
  their are no predifined, moods, menus, objects or actions so this project should be able to handle
  a large amount of radicly different pets simply by downloading them from mydigipet.com
  ------------------------------------------
  Modules:
  name			classes					description
  main.js		none					inialises the application, reads 1 passport and loads 1 pet
  global.js		Debug					loads all external deps, pretends to be GD if running in browser
  passport.js	Passport				creates/reads pet information stored on the site
  pet.js		Pet						maintains 1 pet, all its menu's, objects etc...
  task.js		Brain					maintains job list, works out how to move from 1 animation to another
  mood.js		PetMood, PetMoodObj		holds each mood (happy, hunger, tired etc..) sends changes as xml
  objects.js	PetObjects, PetObject	holds any pet objects (ie food, water, mess) must also hold petimg
  menu.js		PetMenu, PetSubMenu		holds custom menu for the user to interact with
  ------------------------------------------*/
//var debug = null;
var VERSION = "1a";
var petpassport=null; 
var pet=null;
var statusimg=null;
var lblreset=null;

var mempp, memu, memp;
function webbrowserinit(debugging, passportcode,username,password, petwidth,petheight){
	if(checkCompatibility()==false) return;
	browserHook(debugging);
	init(debugging, passportcode,username,password, petwidth, petheight)
}

function gdresize(){
	pet.resizePet();
}

function googledesktopinit(){
	if(checkCompatibility()==false) return;
	fso = getFSO();
	init(true, "","","", 0, 0)
}

function init(debugging, passportcode,username,password, petwidth, petheight) {
	mempp = passportcode;
	memu =username;
	memp =password;
	// create status image
	statusimg = appendHTMLElement(view, "<img enabled='true' width='100%' height='100%' src='images/loading.JPG' />");
	lblreset = appendHTMLElement(view,"<label enabled='true' y='0' >Reset</label>");
	lblreset.onclick = masterreset;
	
	// Check for XMLHTTP and XMLDOM on the local system
	if(checkXMLHTTP()==false){ statusimg.src="images/activex.JPG"; return; }
	//debug.trace(webbrowser);
	//petpassport = new Passport(passportcode,username,password, gotPassport);
	pet = new Pet(passportcode,username,password);
}
/*
function gotPassport(){
	pet = new Pet(petpassport);
}*/

/*-----------------------------
  OPTIONS WINDOW
 ------------------------------*/

function masterreset(){
	pet.destroy();
	pet=null;
	pet=new Pet(mempp,memu,memp);
}

var divoptions=null;
var lbl1=null, lbl2=null,lbl3=null,lbl4=null,lbl5=null,lbl6=null;
var txtpp =null, txtnm=null, txtbr=null;
var btnload =null,btnadd=null, btncancel=null;
var optloaded=false;

function loadOptions(width,height){
	if(!optloaded) optloaded=true; else destroyOptions();
	divoptions = appendHTMLElement(view, "<div visible='false' width='100%' height='100%' background='#999999' />");
	if(compat_window){
		lbl1 = appendHTMLElement(divoptions, "<label y='0' >Digipet - Options</label>");
		lbl2 = appendHTMLElement(divoptions, "<label y='20' >Use existing passport</label>");
		lbl3 = appendHTMLElement(divoptions, "<label x='5' y='40' >passport:</label>");
		lbl4 = appendHTMLElement(divoptions, "<label y='80' >Create new passport</label>");
		lbl5 = appendHTMLElement(divoptions, "<label x='5' y='100' >name:</label>");
		lbl6 = appendHTMLElement(divoptions, "<label x='5' y='120' >breed:</label>");	
		txtpp = appendHTMLElement(divoptions, "<input x='100' y='40' type='text' value='"+pet.passport.passport+"' />");	
		txtnm = appendHTMLElement(divoptions, "<input x='100' y='100' type='text' value='stnick' />");	
		txtbr = appendHTMLElement(divoptions, "<input x='100' y='120' type='text' value='santa07' />");	
		btnadd = appendHTMLElement(divoptions, "<input y='140' x='100' name='ok' value='Create_Pet' type='button' />");
		btnload = appendHTMLElement(divoptions, "<input y='60' x='100' name='ok' value='Load_Pet' type='button' />");
		btncancel = appendHTMLElement(divoptions, "<input y='160' x='100' name='cancel' value='Close_Options' type='button' />");		
	}else{
		lbl1 = appendHTMLElement(divoptions, "<label y='0' >Digipet - Options</label>");
		lbl2 = appendHTMLElement(divoptions, "<label y='15' >Use existing passport</label>");
		lbl3 = appendHTMLElement(divoptions, "<label x='5' y='30' >passport:</label>");
		lbl4 = appendHTMLElement(divoptions, "<label y='45' >Create new passport</label>");
		lbl5 = appendHTMLElement(divoptions, "<label x='5' y='60' >name:</label>");
		lbl6 = appendHTMLElement(divoptions, "<label x='5' y='75' >breed:</label>");	

		txtpp = appendHTMLElement(divoptions, "<edit x='70' y='30' width='70' height='14'  value='"+pet.passport.passport+"' />");	
		txtnm = appendHTMLElement(divoptions, "<edit x='70' y='60' width='70' height='14' value='stnick' />");	
		txtbr = appendHTMLElement(divoptions, "<edit x='70' y='75' width='70' height='14' value='santa07' />");	
		btnadd = appendHTMLElement(divoptions, "<button y='90' x='70' width='70' height='15' caption='Create Pet' downImage='stock_images\\button_down.png' image='stock_images\\button_up.png' overImage='stock_images\\button_over.png' />");
		btnload = appendHTMLElement(divoptions, "<button y='45' x='70' width='70' height='15' caption='Load Pet' downImage='stock_images\\button_down.png' image='stock_images\\button_up.png' overImage='stock_images\\button_over.png' />");
		btncancel = appendHTMLElement(divoptions, "<button y='110' x='70' width='70' height='15' caption='Close Options' downImage='stock_images\\button_down.png' image='stock_images\\button_up.png' overImage='stock_images\\button_over.png' />");		
	}
	btnadd.onclick = addppclick;
	btnload.onclick = loadppclick;
	btncancel.onclick = hideOptions;
	appendHTMLElement(view, "<label x='"+parseInt(width-20)+"' >"+VERSION+"</label>");
}
function showOptions(){
	// update controls
	try{
		txtpp.value = pet.passport.passport;
	}catch(e){
		txtpp.value="";
	}
	if(compat_window)
		divoptions.style.visibility="visible";
	else
		divoptions.visible="true";
}
function addppclick(){
	pet.destroy();
	pet = new Pet("","","",txtnm.value,txtbr.value);
	hideOptions();	
}
function loadppclick(){
	pet.destroy();
	pet = new Pet(txtpp.value,"","","","");
	hideOptions();
}
function hideOptions(){
	if(compat_window)
		divoptions.style.visibility="hidden";
	else
		divoptions.visible="false";	
}
function destroyOptions(){
	lbl1=removeHTMLElement(divoptions,lbl1);
	lbl2=removeHTMLElement(divoptions,lbl2);
	lbl3=removeHTMLElement(divoptions,lbl3);
	lbl4=removeHTMLElement(divoptions,lbl4);
	lbl5=removeHTMLElement(divoptions,lbl5);
	lbl6=removeHTMLElement(divoptions,lbl6);

	txtpp=removeHTMLElement(divoptions,txtpp);
	txtnm=removeHTMLElement(divoptions,txtnm);
	txtbr=removeHTMLElement(divoptions,txtbr);

	btnload=removeHTMLElement(divoptions,btnload);
	btnadd=removeHTMLElement(divoptions,btnadd);
	btncancel=removeHTMLElement(divoptions,btncancel);

	divoptions=removeHTMLElement(view,divoptions);
}
