
function updateDimensions ()
{	
	_trace('updatedim');
	if (parseInt(navigator.appVersion)>3) {
	 if ( this.isNetscape() ) {
	  this.clientWidth = window.innerWidth;
	  this.clientHeight = window.innerHeight;
	 }
	 if ( this.isExplorer() ) {		
	  this.clientWidth = document.body.offsetWidth;
	  this.clientHeight = document.body.offsetHeight;
	 }
	}	
}

function isNetscape()
{
	return navigator.appName=="Netscape";
}

function IEIntervalHack( id )
{
	var callback = browser.intervals[ id ];
	//trace( "IEIntervalHack( id ) " + "callback.target." + callback.member + "()" );
	eval( "callback.target." + callback.member + "()" );
}

function Browser()
{
	/**********************************
	 * Member Functions
	 *********************************/
	 
	this.updateDimensions = updateDimensions;
	
	this.isNetscape = function ()
	{
		/* opera is for now treated as firefox */
		return navigator.appName=="Netscape" || navigator.appName.indexOf("Opera")!=-1;
	}	
	this.isExplorer = function ()
	{
		return navigator.appName.indexOf("Microsoft")!=-1;
	}
	
	this.setInterval = function( target, member, interval )
	{
		if( this.isNetscape() )
		{
			
			/* use wrapper hack */
			return window.setInterval ( function( object, func ) { eval( "object."+func+"()" ); }, interval, target, member );
		}
		if( this.isExplorer() )
		{
			if( this.intervals == undefined )
			{
				this.intervals = new Array();
			}
			var callback = new Object();
			callback.target = target;
			callback.member = member;
			
			/* make unique id! */
			var id = this.intervallIds++;	
			
			this.intervals[ id ] = callback;
			callback.handle = window.setInterval ( "IEIntervalHack("+id+")", interval );						
			return id;
		}
	}
	
	this.clearInterval = function( id )
	{		
		if( this.isNetscape() )
		{			
			window.clearInterval( id );
		}
		if( this.isExplorer() )
		{
			window.clearInterval( this.intervals[ id ].handle );
			this.intervals[ id ] = undefined;
			trace('clearing interval: ' + id );
		}
	}
	
	this.enableDebugging = function( on )
	{
		trace('hm');
		if( on )
		{
			this.traceDiv.style.display = "block";
			this.debugging = true;
		}
		else
		{
			this.traceDiv.style.display = "none";
			this.debugging = false;
		}
	}
	
	this.setWindowSize = function( width, height )
	{
		window.resizeTo( width, height);
	}
	
	this.enableVerboseMode = function( on )
	{
		this.verboseMode = on;
	}
	
	this.initialize = function()
	{
		this.verboseMode = false;
		this.traceDiv = document.createElement("div");		
		this.traceDiv.id = "traceDiv";
		this.traceDiv.style.zIndex = 100000;
		this.traceDiv.style.position = "absolute";
		this.traceDiv.style.padding = "3px";
		this.traceDiv.style.overflow = "hidden";
		this.traceDiv.style.left = "0px";
		this.traceDiv.style.fontFamily = "Arial";
		this.traceDiv.style.fontSize = "12px";
		this.traceDiv.style.top = "0px";
		this.traceDiv.style.width = "200px";
		this.traceDiv.style.background = "#ffaaaa";		
		this.traceDiv.clear = function()
		{
			this.traceDivText.innerHTML = "";
			trace('Tracelog cleared.');
		}
		
		this.traceMaxlen = 30;
		
		this.traceDiv.innerHTML += "<div align=right> res: "+
		"<a href='javascript: browser.setWindowSize( 1680, 1050 ); ' style='color: #000000'>1680x1050</a> &nbsp;" +
		"<a href='javascript: browser.setWindowSize( 1024, 768 ); ' style='color: #000000'>1024x768</a> &nbsp;" +
		"<a href='javascript: browser.setWindowSize( 800, 600 ); ' style='color: #000000'>800x600</a></div>";
		
		
		this.traceDiv.innerHTML += "<div align=right> "+
		"<a href='javascript: document.getElementById(\"traceDiv\").clear(); ' style='color: #000000'>clear</a> &nbsp;&nbsp;&nbsp;&nbsp;" +
		"<a href='javascript: browser.enableDebugging( false ); ' style='color: #000000'>disable</a></div>";
		
		this.traceDiv.innerHTML += "<hr style='border:0;border-top:1px solid #000000;'>";
		
		
		//this.traceDiv.innerHTML += "<div id='traceDivText'> </div>";
		this.traceDivText = document.createElement("div");
		this.traceDiv.appendChild( this.traceDivText );
		this.traceDiv.traceDivText = this.traceDivText;
		
		if( this.isNetscape() )
		{
			this.traceDiv.style.opacity = "0.4";
		}
		if( this.isExplorer() )
		{
			this.traceDiv.style.filter = "Alpha( opacity=40 )";
		}
		
		
		
		document.body.appendChild( this.traceDiv );
		
		this.enableDebugging( true );
		
		trace('Tracelog started...');
		//document.innerHTML += '<div id="traceDiv" style="z-index: 10000; display:none; position: absolute; top: 0; left: 0; height: 100px; width: 300px;"></div>';
		//this.traceDiv = document.getElementById("traceDiv");
		this.intervallIds = 0;
		
		this.updateDimensions();		
		// TODO: seems not necessary:
		//window.captureEvents( Event.RESIZE );		
		window.onresize = function() { browser.updateDimensions() };
		
		
	}

}





/***************************************************************************************/
function load( url )
{
	_trace('loadImage');
	this.url = url;
	this.image = new Image();
	this.image.src = url;
	this.interval = browser.setInterval( this, "isLoaded", 100 );	
}

function isLoaded()
{
	//trace('isloaded ? ');
	if( this.image.complete )
	{
		//trace('image is loaded');		
		
		if( this.onLoad != undefined )
		{
			eval( "this.onLoad( this )" );
		}
		
		/* hope this kills us */
		browser.clearInterval( this.interval );		
	}
}

function ImageLoader( )
{
	_trace('ImageLoader');
	this.isLoaded = isLoaded;
	this.load = load;
}


/***************************************************************************************/

function tweenFrame() 
{	
	/* step in animation */
	var date = new Date();
	
	var factor = ( date.getTime() - this.start ) / this.delta;
	
	factor = factor > 1 ? 1 : factor;
	
	var pos = this.from + this.anim *  factor;

	
	//trace( "bla" );
	var call = "this.target."+this.property +" = " + this.lBrace + pos + this.rBrace;
	_trace( call );
	eval( call );
	
	if( factor == 1 )
	{
		/* animation is finished. uninstall intervall */		
		_trace( this.tname + '.animation done: ' + this.interval );
		
		if( this.finish != "" )
		{
			/* jump to 'animation finished' callback */
			eval("this.target."+this.finish +"()" ) ;
			_trace("this.target."+this.finish +"()");
		}
		
		/* do this last! garbage collector kills us here */
		browser.clearInterval( this.interval );				
	}	
	
}

function Tween( target, property, from, to, len, fps, finishCallback, tname )
{
	this.tname = tname;		
	this.to = to;
	this.from = from;
	this.finish = finishCallback;
	this.target = target;
	this.property = property;	
	this.tweenFrame = tweenFrame;	
	this.lBrace = "";
	this.rBrace = "";
	this.round = false;
	
	var date = new Date();
	this.start = date.getTime();	
	this.delta = len * 1000;
	this.end = this.start + this.delta;	
	
	_trace('tween delta: ' +this.delta );
	_trace('tween end: ' +this.end );
	
	if( browser.isExplorer() )
	{
		/* special treatment for internet explorer */
		if( this.property.toLowerCase().indexOf( "opacity" ) != -1 )
		{
			this.lBrace = "'Alpha( opacity=";
			this.rBrace = " style=0 )'";
			this.to *= 100;
			this.from *= 100;
			this.property = this.property.replace( /opacity/g, "filter" );
			this.round = true;
		}
	}
	
	this.anim = (this.to - this.from );
	
	this.tweenFrame();
	this.interval = browser.setInterval( this, "tweenFrame", 1000/fps);
}


/***************************************************************************************/

function _trace( arg )
{
	if( browser.verboseMode )
	{
		trace( arg );
	}
}

function trace( arg )
{
	if( browser.debugging )
	{
		var split = "";
		var count = 0;
		for( var i = 0; i < arg.length; i++ )
		{				
			var c = arg.charAt( i );
			if( c == " " )
			{
				count = 0;
			}
			else
			{
				count++;
			}
			
			if( count > browser.traceMaxlen )
			{
				split += " ";
				count = 0;
			}
			
			split += c;	
		}
		var tr = browser.traceDivText;
		var date = new Date();
		var str = date.getMinutes() + ":" + date.getSeconds() + ":" + date.getMilliseconds();
		tr.innerHTML = tr.innerHTML + "<b style='font-size: 9px'>" + str + "</b><br>&nbsp; " + split + "<br>";
	}
}

function error( arg )
{
	trace( "<font style='fonz-color: #ff0000'>" + arg + "</font>" );
}


/***************************************************************************************/


var browser = new Browser();

