/**
 * dogmaConsult CowThrow - ein JavaScript Werbespiel
 *
 * @author Steffen Müller
 * @version 0.1
 */

// CowThrow ///////////////////////////////////////////////////////////////////

function CowThrow (divid)
{
	this.cows = Array();
	this.window = document.getElementById(divid);
	if (!this.window)
	{
		dError("CowThrow: could not find window '"+divid+"'");
		return;
	}
	document.cowThrow = this;
	this.window.style['background'] = "url('media/weide.jpg')";

	// Physics
	this.physics = new dPhysics (divid);
	this.physics.addListener("crash", "dPlaySound('crash');");
	this.physics.addListener("crashleft", "document.cowThrow.crash('l');");
	this.physics.addListener("crashright", "document.cowThrow.crash('r');");
	this.physics.addListener("mousedown", "document.cowThrow.mouseDown()");
	this.physics.addListener("mouseup", "document.cowThrow.mouseUp()");
	this.physics.addListener("touchdown", "document.cowThrow.touchDown()");
	this.physics.addListener("takeoff", "document.cowThrow.takeOff()");

	this.physics.addFrame("defaultl", "media/cow2.gif", 100, 81, 0, 0);
	this.physics.addFrame("walkl", "media/cow_walk2.gif", 100, 72, 0, 9);
	this.physics.addFrame("longl", "media/cow_long2.gif", 100, 70, 0, 11);
	this.physics.addFrame("hangl", "media/cow_hang2.gif", 62, 100, 10, 10);
	this.physics.addFrame("flatl", "media/cow_flat2.gif", 100, 58, 0, 23);
	this.physics.addFrame("defaultr", "media/cow.gif", 100, 81, 0, 0);
	this.physics.addFrame("walkr", "media/cow_walk.gif", 100, 72, 0, 9);
	this.physics.addFrame("longr", "media/cow_long.gif", 100, 70, 0, 11);
	this.physics.addFrame("hangr", "media/cow_hang.gif", 62, 100, 10, 10);
	this.physics.addFrame("flatr", "media/cow_flat.gif", 100, 58, 0, 23);

	// Internals
	this.cows = Array();
	this.frameListener = window.setInterval(this.doFrame, 500);

	this.STAND = 1;
	this.WALK = 2;
	this.MOOH = 3;
}

CowThrow.prototype.mouseDown = function ()
{
	var cow = this.physics.listener_target;
	this.physics.setFrame(cow, 'hang'+cow.ctDir);
}

CowThrow.prototype.mouseUp = function ()
{
	var cow = this.physics.listener_target;
	this.physics.setFrame(cow, 'flat'+cow.ctDir);
}

CowThrow.prototype.touchDown = function ()
{
	var cow = this.physics.listener_target;
	if (cow.dStatus == this.physics.PASSIVE)
		this.physics.setFrame(cow, 'default'+cow.ctDir);
}

CowThrow.prototype.takeOff = function ()
{
}

CowThrow.prototype.crash = function (dir)
{
	var cow = this.physics.listener_target;
	if (dir == 'l')
		cow.ctDir = 'r';
	if (dir == 'r')
		cow.ctDir = 'l';
}


CowThrow.prototype.createCow = function (x,y,z)
{
	var cow = this.physics.addActor ('defaultr', x, y, z);
	this.cows.push(cow);

	cow.ctCounter = 0;
	cow.groundCounter = 0;
	cow.aniAlternate = false;
	cow.ctDir = "r";
	this.chooseBehave(cow);
}

CowThrow.prototype.chooseBehave = function (cow)
{
	var nb = Math.round(0.5+Math.random()*2.5);
	var resttime = Math.round(Math.random()*20);
	if (nb == this.MOOH) resttime = 9;
	cow.behave = nb;
	cow.restbehave = resttime;
	cow.firstBehave = true;
	if (Math.random() > .8)
	{
		if (cow.ctDir == 'r') cow.ctDir = 'l';
		else cow.ctDir = 'r';
	}
}

CowThrow.prototype.doFrame = function ()
{
	var c = document.cowThrow;
	for (var i = 0; i < c.cows.length; i++)
	{
		var a = c.cows[i];
		if (a.onground && a.dStatus == c.physics.PASSIVE)
			a.groundCounter++;
		else
		{
			a.groundCounter = 0;
			return;
		}

		if (a.behave == c.WALK && a.groundCounter > 3)
		{
			var am = 3;
			if (a.ctDir == 'l') am = -3;
			if (a.aniAlternate == true)
			{
				c.physics.setFrame(a, 'default'+a.ctDir);
				a.acc.x += am;
				a.aniAlternate = false;
			}
			else
			{
				c.physics.setFrame(a, 'walk'+a.ctDir);
				a.acc.x += am;
				a.aniAlternate = true;
			}
		}
		if (a.behave == c.STAND)
		{
			if (a.firstBehave)
			{
				c.physics.setFrame(a, 'flat'+a.ctDir);
			}
			a.firstBehave = false;
		}
		if (a.behave == c.MOOH)
		{
			if (a.firstBehave)
			{
				dPlaySound('mooh');
				c.physics.setFrame(a, 'long'+a.ctDir);
			}
			a.firstBehave = false;
		}

		a.restbehave--;
		if (a.restbehave <= 0)
			c.chooseBehave(a);
	}
}

