//
// bonfire.js   by CINCS <http://www.cincs.net/>
//
// supported browsers:
//   +MSIE 5.01/6.0/7.0 (5.5 may ok)
//   +Firefox 1.0/1.5/2.0
//   +Netscape 7.1/8.0
//   +Safari 3.0
//

/**************************************************************************/

/**
 * Function.prototype.bind
 *   Based on prototype.js (Prototype JavaScript framework, version 1.5.0_rc0)
 */
Function.prototype.bind = function() {
	var __method = this;
	var args = Array.getArray(arguments);
	var object = args.shift();
	return function() { return __method.apply(object, args); }
}

/**
 * Function.prototype.bindAsEventListener
 *   Based on prototype.js (Prototype JavaScript framework, version 1.5.0_rc0)
 */
Function.prototype.bindAsEventListener = function(object) {
	var __method = this;
	return function(e) { return __method.call(object, e || window.event); }
}

/**
 * Object.__extend
 *   Simple to extend class (for properties).
 *   Example: Object.__extend.call(this, ChildClass, ParentClass)
 */
Object.__extend = function(childClass, parentClass) {
	this.__super = parentClass;
	this.__super();
	this.constructor = childClass;
	delete this.__super;
}

/**
 * Array.getArray
 *   To array from iteratable object (Arguments etc...)
 */
Array.getArray = function(object) {
	if ( !object ) { return []; }
	else if ( object.toArray ) { return object.toArray(); }
	else {
		var array = [];
		for (var i=0; i<object.length; i++) {
			array.push(object[i]);
		}
		return array;
	}
}

/**
 * pxEvent:
 *   Simple to use event object.
 */
var pxEvent = {
	add: function(object, type, listener) {
		if ( object.addEventListener ) {
			/* DOM2 */
			object.addEventListener(type, listener, false);
		} else if ( object.attachEvent ) {
			/* IE5 later */
			object.attachEvent('on' + type, listener);
		}
	},
	remove: function(object, type, listener) {
		if ( object.removeEventListener ) {
			/* DOM2 */
			object.removeEventListener(type, listener, false);
		} else if ( object.detachEvent ) {
			/* IE5 later */
			object.detachEvent('on' + type, listener);
		}
	},
	dispatch: function(object, event) {
		if ( object.dispatchEvent ) {
			/* DOM2 */
			object.dispatchEvent(event);
		} else if ( object.fireEvent ) {
			/* IE5 later */
			object.fireEvent(event.type, event);
		}
	},
	cancel: function(event) {
		if ( event.preventDefault ) {
			/* DOM2 */
			event.preventDefault();
		} else if ( event.cancelBubble != undefined ) {
			/* IE5 later */
			event.returnValue = false;
		}
	}
}

/**************************************************************************/

pxEvent.add(window, 'load', bonfire_onload);

function bonfire_onload() {
	bonfire_image_update.init();
}

/**
 * bonfire_image_update:
 *   Update images in 5-10 sec interval. (random)
 */
var bonfire_image_update = {
	/*** [[public]] ***/
	init: function() {
		var found = 0;
		var elements = document.getElementsByTagName('IMG');
		if ( elements ) {
			for ( var i=0; i<elements.length; i++ ) {
				var o = elements[i];
				if ( o.className ) {
					if ( o.className == 'livemain' ) {
						o._uri = o.src;
						o._count = 0;
						this._nexttime(o,5,5);
						this._mainimg.push(o);
						found++;
					} else if ( o.className == 'liveside' ) {
						o._uri = o.src;
						o._count = 0;
						this._nexttime(o,12,5);
						this._sideimg.push(o);
						found++;
					}
				}
			}
		}
		if ( found > 0 ) {
			setInterval(this._ontimer.bind(this), 1000);
		}
	},
	add: function(o) {
		if ( !o._uri ) o._uri = o.src;
		o._count = 0;
		this._nexttime(o,5,5);
		this._others.push(o);
	},
	remove: function(o) {
		for ( var i=0; i<this._others.length; i++ ) {
			if ( this._others[i] == o ) {
				this._others.splice(i, 1);
				break;
			}
		}
	},
	enabled: true,
	/*** [[private]] ***/
	_mainimg: [],
	_sideimg: [],
	_others: [],
	_nexttime: function(o,base,range) {
		o._time = base + Math.floor(Math.random() * range);
	},
	_ontimer: function() {
		for ( var i=0; i<this._mainimg.length; i++ ) {
			var o = this._mainimg[i];
			if ( o._time-- <= 0 ) {
				if ( this.enabled ) {
					o.src = o._uri +'?'+ o._count++;
					this._nexttime(o,5,5);
				}
			}
		}
		for ( var i=0; i<this._sideimg.length; i++ ) {
			var o = this._sideimg[i];
			if ( o._time-- <= 0 ) {
				if ( this.enabled ) {
					o.src = o._uri +'?'+ o._count++;
					this._nexttime(o,12,5);
				}
			}
		}
		for ( var i=0; i<this._others.length; i++ ) {
			var o = this._others[i];
			if ( o._time-- <= 0 ) {
				o.src = o._uri +'?'+ o._count++;
				this._nexttime(o,5,5);
			}
		}
	}
}