/*
 * be - Brightness Engine
 * (c)2002 A3.boot.development s.r.o.
 * All rights reserved
 *
 * Programming is like any other "artwork",
 * THIS piece of data is an ART which has it's artworker.
 * Thus it is proteced by copyright law and international treaties !
 * You can not use any part of it, you can not modify it and then use it in any concept !
 *
 * If you do so, you can be prosecuted by maximal extend
 * under the law.
 */

function beFade( o, subo, c, t ) {
	
//	try {
	
	if( t == null )	t = 500; // 500ms default time
	if( t < 40 ) t = 40;
	if( t > 10000 ) t = 10000;
	
	if( c == null || c == '')
		c = '#FFFFFF';  // default to white
	
	if( subo == null || subo == '' ) 
		subo = "style.color"; // default to forecolor;
	
	beFadersAdd(o, subo, c, t );
	
//	}catch( e ) {
//	}

}

/*
 * Fader object
 */
var beFaders = new Array();

function beFadersAdd( obj, subo, c, t ) {
	
//	window.status = "adding fader: " + obj + ", " + subo + ", " + c + ", " + t;
	var id, o, i;
	var stcolor;
	var steps = Math.round(t / 40);
	
//	debugger;	
	// check if we are not running
	for( i = 0; i < beFaders.length; i++ )
		if( beFaders[i].obj == obj && beFaders[i].subo == subo ) {
//			window.status = "restarting obj: " + beFaders[i] + " subo: " + subo;
			beFadersDel( beFaders[i] );	// stop and restart
		}

	eval( "stcolor = obj." + subo + ";" );
	
	if( !stcolor.length || stcolor.charAt(0) != '#' ) { // no named colors !
		window.status = "Can't use color names with beFader or empty color value. color:" + stcolor + ", object name:" + obj.nodeName;
		return;
	}

	id = beGetId();
	o = new beFader(id);
	o.obj = obj;
	o.scolor = new beColor( stcolor );
	o.subo = subo;
	o.cstep = new beColor( c );
	o.cstep.sub( o.scolor );
	o.cstep.div( steps );
	o.counter = steps;
	
	beFaders.push( o );	
	o.int = window.setInterval( "beFadersHelper('" + id + "')", 40 );
}

function beFadersDel( o ) {
	clearInterval( o.int );
	var i,j;
//	debugger;
	// find it 
	for( i = 0; i < beFaders.length; i++ )
		if( beFaders[i].id == o.id ) {
			beFaders[i] = null;	// delete fader
			break;
		}
	if( i != beFaders.length ) {
		for( j = i; j < beFaders.length-1; j++ ) {
			beFaders[j] = beFaders[j+1];
			delete beFaders[j+1];
		}
		beFaders.length--;
	}
}

function beFader( id ) {
	// props
	this.id = id;
	this.obj = null;
	this.scolor = null;
	this.cstep = null;
	this.subo = "";
	this.counter = 0;
	this.int = "";
	// methods
	this.OnTimer = beFaderOnTimer;
}

function beFaderOnTimer() {
//	var a = Math.round(this.scolor).toString(16);
	this.scolor.add( this.cstep	);
	eval( "this.obj." + this.subo + " = this.scolor.get();" );
	this.counter--;
//	window.status = "counter:" + this.counter + " ,scolor: " + this.scolor.get();
	if( !this.counter )
		beFadersDel( this );
}

function beFadersHelper( id ) {
	var i;
	for( i = 0; i < beFaders.length; i++ ) {
		if( beFaders[i].id == id ) {
			beFaders[i].OnTimer();
			break;
		}
	}
}

function beGetId(){
	var a = Math.random()*1000;
	a = Math.round( a );
	return a.toString();
}

/*
 * Color object
 */
function beColor( c ) {
	if( c == null )
		c = "#FFFFFF"; // white ? 
	this.r = Number("0x" + c.substr( 1, 2 ));
	this.g = Number("0x" + c.substr( 3, 2 ));
	this.b = Number("0x" + c.substr( 5, 2 ));
	this.get = beColorGet;
	this.cmp = beColorCmp;
	this.set = beColorSetRGB;
	this.sub = beColorSub;
	this.add = beColorAdd;
	this.div = beColorDiv;
}

function beColorGet() {
	var r = Math.round(this.r).toString(16);
	var g = Math.round(this.g).toString(16);
	var b = Math.round(this.b).toString(16);
	if( r.length == 1 ) r = "0" + r;
	if( g.length == 1 ) g = "0" + g;
	if( b.length == 1 ) b = "0" + b;
	return "#" + r + g + b;
}

function beColorCmp( c ) {
	return this.r - c.r;	// :)
}

function beColorSub( c ) {
	this.r -= c.r;
	this.g -= c.g;
	this.b -= c.b;
}

function beColorAdd( c ) {
	this.r += c.r;
	this.g += c.g;
	this.b += c.b;
}

function beColorDiv( val ) {
	this.r /= val;
	this.g /= val;
	this.b /= val;
}

function beColorSetRGB( r, g, b ) {
	if( r < 0 || r > 255 ) r = 0;
	if( g < 0 || g > 255 ) g = 0;
	if( b < 0 || b > 255 ) b = 0;
	this.r = r;
	this.g = g;
	this.b = b;
}
