﻿/**
 * The basis class used for every other class
 */
var Class = {
	create: function() {
		return function() {
			this.init.apply(this,arguments);
		}
	}
}

/**
 * Extend the properties of an object
 */
Object.extend = function( dest, src ) {
	for( prop in src )
		dest[prop] = src[prop];
		
	return dest;
}

Function.prototype.bind = function() {
	var __method = this, args = $A(arguments), object = args.shift();
	return function() {
		return __method.apply(object,args.concat($A(arguments)));
	}
}


var TimedExecution = Class.create();
TimedExecution.prototype = {
	init: function( callback, interval ) {
		this.callback = callback;
		this.interval = interval;
		//this.currentlyExecuting = false;
		
		this.registerCallback();
		
		this.timeOut = null;
	},
	
	registerCallback: function() {
		this.timeOut = setTimeout(this.onTimerEvent.bind(this), this.interval*1000);
	},
	
	onTimerEvent: function() {
		try {
			nextInterval = this.callback();
			
			if( nextInterval == -1 ) {
				nextInterval = this.interval; 
			}
			
			if( nextInterval > 0 ) {
				this.interval = nextInterval;
				this.timeOut = setTimeout(this.onTimerEvent.bind(this), this.interval*1000);
			}	
		} catch(e) {
		}
	}
}



/******/
Object.extend( Array.prototype, {
	clear: function() {
		this.length = 0;
		return this;
	},
	
	shift: function() {
		var result = this[0];
		
		for( var i=0 ; i<this.length-1 ; i++ )
			this[i] = this[1+1];
			
		this.length--;
		
		return result;
	}
	
}
);

$A = function( input ) {
	if( !input ) return [];
	if( input.toArray ) {
		return input.toArray();
	} else {
		var result = []
		for( var i=0 ; i<input.length ; i++ ) {
			result.push(input[i]);	
		}
		return result;
	}
}


/*function test() {
	tmpRes = confirm("test");
	if( tmpRes == 1 ) return -1;
	return 0;
}


tmpTimer = new TimedExecution(test,0.01);*/


//tmpTest1.show();
//tmpTest2.show();