     //  _  __ __  __ _____ __    _____ __  __ _____ 
    //  / |/ // / / //_  _// /   / ___// /_/ // ___/
   //  /    // /_/ /  / / / /__ / _/  /___  //__  /
  //  /_/|_/ |____/  /_/ /____//____/ /____//____/
 // Copyright (c) Nutley and Nutley 2009
// pumpy.js
//
// OO code to control the pumpy fish on the Ocean Ward home page
// requires animation.js

function Pumpy(parent)
{
	this.active = true;
	this.lock = false;	// If the active state should be locked.
	this.frame = 0;
	this.dir = 1;
	this.muzzle = false;
	this.bubbleCount = 1;
	
	this.lastMousePos = new Object;
	
	this.parent = parent;
	document.pumpy = this; 
	
	this.div = IEFIX_CreateImg('/images/pumpy01.png');
	this.div.id = 'pumpyDiv';
	with (this.div.style) {
		position = 'absolute';
		left = '0px';
		top = '285px';
		width = '46px';
		height = '44px';
	}
	this.lastMousePos.x = 0;
	this.lastMousePos.y = 285;
	
	parent.appendChild(this.div);
	if (this.div.attachEvent) this.div.attachEvent('onclick', PUM_Click);
	else this.div.addEventListener('click', PUM_Click, false);
	
	
	this.BlowBubble = PUM_BlowBubble;
	this.MouseMove = PUM_MouseMove;
	this.Nuzzle = PUM_Nuzzle;
	this.Show = PUM_Show;
	this.Hide = PUM_Hide;
	this.Update = PUM_Update;	
	
	animationCtrl.transitions.bubbleW = 'Math.sin(obj.step / 4.0)'; // For the bubble pumpy will blow.
	animationCtrl.transitions.bubbleH = 'Math.cos(obj.step / 4.0)';
}

function PUM_Click()
{
	pumpy.Hide(true);
}

function PUM_Update()
{
	this.frame = (this.frame + 1) % 2;
	IEFIX_SetImg(this.div, '/images/pumpy' + this.frame + this.dir + '.png');
	//this.div.style.backgroundImage = 'url(images/pumpy' + this.frame + this.dir + '.png)';
}

function PUM_MouseMove(mouse)
{
	if (mouse != null) this.lastMousePos = mouse;
	if (!this.active) return;
	this.muzzle = this.dir == 1; 
	
	animationCtrl.SetTransition(null, 'linear', 'linear', null, null, null);
	animationCtrl.std.numSteps = 20;
	
	var x = parseInt(document.getElementById('pumpyDiv').style.left) + 23;
	if (x < this.lastMousePos.x) {
		this.dir = 1;
		animationCtrl.SetWanted('pumpyDiv', this.lastMousePos.x - 56, Math.max(20, this.lastMousePos.y - 20), null, null, null, PUM_Nuzzle);
	} else {
		this.dir = -1;
		animationCtrl.SetWanted('pumpyDiv', this.lastMousePos.x + 10, Math.max(20, this.lastMousePos.y - 20), null, null, null, PUM_Nuzzle);
	}
	animationCtrl.Start();
} 

function PUM_Nuzzle()
{
	animationCtrl.SetTransition(null, 'fast', 'fast', null, null, null); 
	animationCtrl.std.numSteps = 10;
	var dir = '+5';
	if (this.muzzle) dir = '-5';
	animationCtrl.SetWanted('pumpyDiv', dir, null, null, null, null, PUM_Nuzzle);	  
	this.muzzle = !this.muzzle;
	animationCtrl.Start();
}

function PUM_Show(unlock)
{
	if (this.lock && unlock) this.lock = false;
	if (this.lock) return; // Don't show because pumpy is locked away   		
	this.active = true;
	this.MouseMove(null); // Make pumpy appear 	
}

function PUM_Hide(lock)
{
	if (!lock && this.lock) return;
	this.lock = lock;
	this.active = false;
  this.dir = -1;
  var p = animationCtrl.SetWanted('pumpyDiv', -50, null, null, null, null, null);
	p.numSteps = 20;
	animationCtrl.Start();
}

function PUM_BlowBubble()
{
	if (!this.active) return;
	var d = IEFIX_CreateImg('/images/bubble.png');
	d.id = 'fshbble_' + this.bubbleCount++;
	d.style.position = 'absolute';
	d.style.overflow = 'hidden';
	
	var top = parseInt(this.div.style.top) + 12;
	
	d.style.top = top + 'px';
	if (this.dir == 1) d.style.left = (parseInt(this.div.style.left) + 42) + 'px';
	else d.style.left = (parseInt(this.div.style.left) + 4) + 'px';
	d.style.width = d.style.height = '7px'; 
	this.parent.appendChild(d);
	animationCtrl.SetWanted(d.id, '+20', 19, null, null, null, PUM_DelBubble);
	animationCtrl.SetTransition(d.id, 'bubbleW', 'linear', 'bubbleW', 'bubbleH', null);
	d.numSteps = Math.max(2, Math.round((top - 18) / 12));
 
	animationCtrl.Start();
}

function PUM_DelBubble(obj)
{
	pumpy.parent.removeChild(obj);
}

