// sprite.js -- Javascript DHTML Sprite Object 
// Version 0.90 (16/12/2002)
// ------------------------------------------------------
// Copyright Webree.com Ltd, All rights reserved.
// Designed & Written by Matthew Tighe

function magicSprite( name, layer, parentObj )
{
	this.length = 28;   	// Number of properties in the object, not including this one.
		
	this.objLayer = null;
	this.szLayer = "undefined";
	this.szName = name;

	// Timers used by various ....To functions
	this.moveTimer = null;
	this.clipTimer = null;
	this.scaleTimer = null;
	
	// Properties for dispaly size and position
	this.x = 0;
	this.y = 0;
	this.z = 0;
	this.width = 0;			// Width and height are relative to CONTENT
	this.height = 0;		// cliping can alter displayed width and height
	this.alpha = 100;
	this.flags = 0;
	
	// Clip Co-ordiates
	this.l = 0;
	this.t = 0;
	this.r = 0;
	this.b = 0;

	// Methods
	this.attachDiv = sprAttachDiv;

	this.move = sprMove;
	this.moveBy = sprMoveBy;
	this.moveTo = sprMoveTo;

	this.moveC = sprMoveC;
	this.moveToC = sprMoveToC;
	
	this.clip = sprClip;
	this.clipTo = sprClipTo;

	this.blend = sprBlend;
	this.blendTo = sprBlendTo;
	this.show = sprShow;
	this.show2 = sprShow2;	

	this.resize = sprResize;
	this.scale = sprScale;

	this.setSource = sprSetSource;

	this.setMouseEvents = sprSetMouseEvents;

	if( layer )
		this.attachDiv( layer );
}

// Constructors

function sprAttachDiv( szLayer )
{
	var lyr;
	
	// Create the object and store layer name.
	lyr = getLayerObj( szLayer );

	this.objLayer = lyr;
	this.szLayer = szLayer;

	// Retrieve Initial Values

	this.x = getLayerXPos( lyr );
	this.y = getLayerYPos( lyr );
			
	this.width = getLayerDisplayWidth( lyr );
	this.height = getLayerDisplayHeight( lyr );

	this.l = clipValues( lyr, 'l' );
	this.t = clipValues( lyr, 't' );
	this.r = clipValues( lyr, 'r' );
	this.b = clipValues( lyr, 'b' );

}

function sprAttachBrowser( szLayer )
{
	var lyr;
	
	// Create the object and store layer name.
	lyr = getLayerObj( szLayer );
	this.objLayer = lyr;
	this.szLayer = szLayer;

	// Retrieve Initial Values

	this.x = getLayerXPos( lyr );
	this.y = getLayerYPos( lyr );
	
	this.width = getLayerContentWidth( lyr );
	this.height = getLayerContentHeight( lyr );

	this.l = clipValues( lyr, 'l' );
	this.t = clipValues( lyr, 't' );
	this.r = clipValues( lyr, 'r' );
	this.b = clipValues( lyr, 'b' );
}

// Movement Functions

function sprMove(x, y, z)
{
		clearTimeout( this.moveTimer);		// Stop animation timer, if any exists

		this.x = parseInt(x);
		this.y = parseInt(y);
		this.z = parseInt(z);
		move( this.objLayer, x,y,z );
}

function sprMoveC(x, y, z)
{
		var cx = 0, cy = 0, cw = 0, ch = 0;
		clearTimeout( this.moveTimer);		// Stop animation timer, if any exists

		this.x = parseInt(x);
		this.y = parseInt(y);
		this.z = parseInt(z);

		ch = nScrHeight - this.y;
		if( ch >= this.height )
			ch = this.height;
	
		cw = nScrWidth - this.x;
		if( cw >= this.width )
			cw = this.width;

		clip( this.objLayer, cx,cy, cw, ch );
		move( this.objLayer, x,y,z );
}

function sprMoveBy(x, y, z)
{
		clearTimeout( this.moveTimer);		// Stop animation timer, if any exists

		this.x = this.x + parseInt(x);
		this.y = this.y + parseInt(y);
		this.z = this.z + parseInt(z);
		move( this.objLayer, this.x, this.y, this.z );
}

function sprMoveTo( x, y, z, step, delay, finishcode )
{
	
	if( this.x < x )
		this.x += step;

	if( this.x > x )
		if( (this.x -= step) < x)
			this.x = x;

	if( this.y < y )
		this.y += step;

	if( this.y > y )
		if( (this.y -= step) < y)
			this.y = y;

	if( this.z < z )
		this.z += step;

	if( this.z > z )
		if( (this.z -= step) < z)
			this.z = z;
	
	this.move( this.x, this.y, this.z );
		
	if( this.x != x || this.y != y || this.z != z )
		this.moveTimer = setTimeout( this.szName + ".moveTo(" + x + "," + y + "," + z + "," + step + "," + delay + ", \"" + finishcode + "\");", delay );
	else if( finishcode )
		eval( finishcode );
}

function sprMoveToC( x, y, z, step, delay, finishcode )		// Clips to edges of screen.
{
	
	if( this.x < x )
		this.x += step;

	if( this.x > x )
		if( (this.x -= step) < x)
			this.x = x;

	if( this.y < y )
		this.y += step;

	if( this.y > y )
		if( (this.y -= step) < y)
			this.y = y;

	if( this.z < z )
		this.z += step;

	if( this.z > z )
		if( (this.z -= step) < z)
			this.z = z;

	this.moveC( this.x, this.y, this.z );
		
	if( this.x != x || this.y != y || this.z != z )
		this.moveTimer = setTimeout( this.szName + ".moveToC(" + x + "," + y + "," + z + "," + step + "," + delay + ", \"" + finishcode + "\");", delay );
	else if( finishcode )
		eval( finishcode );
}

// Clipping Functions

function sprClip( l,t,r,b )
{
	clearTimeout( this.clipTimer);		// Stop animation timer, if any exists
	this.l = parseInt(l);
	this.t = parseInt(t);
	this.r = parseInt(r);
	this.b = parseInt(b);
	clip( this.objLayer, l, t, r, b );
}

function sprClipTo(l, t, r, b, step, delay, finishcode )
{
	if( this.l < l )
		this.l += step;

	if( this.l > l )
		if( (this.l -= step) < l)
			this.l = l;

	if( this.t < t )
		this.t += step;

	if( this.t > t )
		if( (this.t -= step) < t)
			this.t = t;

	if( this.r < r )
		this.r += step;

	if( this.r > r )
		if( (this.r -= step) < l)
			this.r = r;

	if( this.b < b )
		this.b += step;

	if( this.b > b )
		if( (this.b -= step) < l)
			this.b = b;
			
	this.clip( this.l, this.t, this.r, this.b );

	if( this.l != l || this.t != t || this.r != r || this.b != b)
		this.clipTimer = setTimeout( this.szName + ".clipTo(" + l + "," + t + "," + r + "," + b + "," + step + "," + delay + ",\"" + finishcode + "\");", delay );
	else if( finishcode )
		eval( finishcode );
}

// Blending/Transparency Functions

function sprBlend( alpha )
{
	clearTimeout( this.blendTimer );
	this.alpha = parseInt(alpha);

	if( this.alpha > 100 )
		this.alpha  = 100;
		
	if( this.alpha < 0 )
		this.alpha  = 0;

	blend( this.objLayer, this.alpha );
}

function sprBlendTo( alpha, step, delay, finishcode )
{
	if( this.alpha < alpha )
		this.alpha += step;

	if( this.alpha > alpha )
		if( (this.alpha -= step) < alpha)
			this.alpha = alpha;

	this.blend( this.alpha );
			
	if( this.alpha != alpha )
		this.blendTimer = setTimeout( this.szName + ".blendTo(" + alpha + ", " + step + ", " + delay + ",\"" + finishcode + "\");", delay);
	else if( finishcode )
		eval( finishcode );
}

function sprResize( w, h )
{
	this.width = w;
	this.height = h;
	setLayerContentWidth( this.objLayer, w );
	setLayerContentHeight( this.objLayer, h );
}

function sprScale( s )
{
	this.move( ( this.orgX * s ), ( this.orgY * s ), 0);
	this.size( ( this.orgWidth * s), ( this.orgHeight * s) );
}

// Source/Content Functions

function sprSetSource( szSource )
{
	layerWrite( this.szLayer, null, szSource );
}

function sprShow( bVisible )
{
	if( bVisible == true )
		showObject( this.objLayer );
	else
		hideObject( this.objLayer );
}

function sprShow2( binline )
{
	if( binline == true )
		displayObject( this.objLayer );
	else
		nodisplayObject( this.objLayer );
}
// Event Functions

function sprSetMouseEvents( argOnUp, argOnDown, argOnMove )
{

	this.objLayer.onmousedown = eval( argOnDown);
	this.objLayer.onmousemove = eval( argOnMove );
	this.objLayer.onmouseup = eval( argOnUp );
}
