/**
 * Animated Background - Version 0.1
 * Copyright (c) 2008 Mark Branly, Neural 9 Studios (neural9.com)
 *
 * AnimatedBackground is freely distributable under the terms of an MIT-style license.
 *
 */
AnimatedBackground = function( element, options ){

	var self = this;
	self.version = '0.1';
	self.position = {'x': 0,'y': 0}
	self.interval = null;
	self.duration = 0;
	self.frequency = 0;
	// do not change these settings, pass in the values you want when you instantiate the object.
	self.options = {
		'start':			{'x':0,'y':0},				// The coordinates of the background starting position
		'increment':		{'x':3,'y':3},				// The delta per frame
		'limit':			{'x':100000,'y':100000},	// The upperbound coordinates (set to the width/height of background to prevent coordinate overflow)
		'moveBy':			{'x':0,'y':0},				// The distance that you want the background moved
		'frameRate':		10,							// The number of times per second to update the background
		'duration':			null,						// The amount of time in seconds to continue the animation, leave null for infinity
		'autoStart':		true						// Whether or not the animation should begin on object creation
	}

	self.animate = function(){
		self.duration += self.frequency;
		//console.log( self.duration+" > "+(self.options.duration*1000)+" = "+( self.duration > self.options.duration*1000 ) );
		if ( self.options.duration && self.duration > self.options.duration*1000 )
			self.stop();
		self.position.x = ( self.position.x + self.options.increment.x ) % self.options.limit.x;
		self.position.y = ( self.position.y + self.options.increment.y ) % self.options.limit.y;
		self.element.style.backgroundPosition = self.position.x+"px "+self.position.y+"px";
	}

	self.start = function(){
		self.duration = 0;
		self.interval = window.setInterval( self.animate, self.frequency );
	}

	self.stop = function(){
		window.clearInterval( self.interval );
	}

	self.mergeOptions = function( source, destination ){
		for ( var i in source ){
			if( typeof destination[i] !== "undefined" ){
				if( typeof destination[i] === "object" && typeof source[i] === "object" )
					destination[i] == self.mergeOptions( source[i], destination[i] );
				else
					destination[i] = source[i];
			} else {
				if( console )
					console.log( 'AnimatedBackground:  Warning - unrecognized option, '+i+' = '+source[i] );
			}
		}
		return destination;
	}

	self.calculateMoveByDuration = function( coord ){
		console.log
		return ;
	}

	self.initialize = function( element, options ){

		// determine whether or not passed element is an object, convert as needed.
		if (typeof element !== 'object') {
			try {
				self.element = document.getElementById(element);
			}
			catch (e) {
				if( console )
					console.log("Error instantiating AnimatedBackground: " + e.description);
			}
		}
		else {
			self.element = element;
		}
		if ( options ) {
			self.options = self.mergeOptions( options, self.options );
			self.frequency = 1000/self.options.frameRate;
			// MoveBy is special!
			if ( options.moveBy ){
				if( !options.duration ){
					self.options.duration = Math.max(
						(Math.abs( self.options.moveBy.x/self.options.increment.x )*self.frequency)/1000,
						(Math.abs( self.options.moveBy.y/self.options.increment.y )*self.frequency)/1000
					);
				} else {
					self.options.increment.x = (self.frequency*options.moveBy.x)/(1000*options.duration);
					self.options.increment.y = (self.frequency*options.moveBy.y)/(1000*options.duration);
				}
			}
		}
		if( self.options.autoStart )
			self.start();
	}
	self.initialize(element, options);

};