// from Prototype by Sam Stephenson (http://prototype.conio.net) MIT-style LICENSE

var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}

Object.extend = function(destination, source) {
	for (property in source) destination[property] = source[property];
	return destination;
}

Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
}

function $() {
	if (arguments.length == 1) return get$(arguments[0]);
	var elements = [];
	$c(arguments).each(function(el){
		elements.push(get$(el));
	});
	return elements;

	function get$(el){
		if (typeof el == 'string') el = document.getElementById(el);
		return el;
	}
}

Array.prototype.each = function(func){
	for(var i=0;ob=this[i];i++) func(ob, i);
}

function $c(array){
	var nArray = [];
	for (i=0;el=array[i];i++) nArray.push(el);
	return nArray;
}

// from Moo.fx by Valerio Proietti (http://mad4milk.net) MIT-style LICENSE

if (!window.fx) fx = function() {};

fx.Opacity = Class.create();
fx.Opacity.prototype = {
	initialize: function(el, options) {
		this.el = $(el);
		this.now = 1;
		this.increase();
		this.options = {
			duration: 500,
			onComplete: ''
		}
		Object.extend(this.options, options || {});
	},

	increase: function() {
		if (this.now == 1 && (/Firefox/.test(navigator.userAgent))) this.now = 0.9999;
		this.setOpacity(this.now);
	},

	setOpacity: function(opacity) {
		if (opacity == 0 && this.el.style.visibility != "hidden") this.el.style.visibility = "hidden";
		else if (this.el.style.visibility != "visible") this.el.style.visibility = "visible";
		if (window.ActiveXObject) this.el.style.filter = "alpha(opacity=" + opacity*100 + ")";
		this.el.style.opacity = opacity;
	},

	custom: function(from, to) {
		if (this.timer != null) return;
		this.from = from;
		this.to = to;
		this.startTime = (new Date).getTime();
		this.timer = setInterval (this.step.bind(this), 13);
	},

	step: function() {
		var time  = (new Date).getTime();
		if (time >= this.options.duration+this.startTime) {
			this.now = this.to;
			clearInterval (this.timer);
			this.timer = null;
			if (this.options.onComplete) setTimeout(this.options.onComplete.bind(this), 10);
		}
		else {
			var Tpos = (time - this.startTime) / (this.options.duration);
			this.now = ((-Math.cos(Tpos*Math.PI)/2) + 0.5) * (this.to-this.from) + this.from;
		}
		this.increase();
	}
}

// from Metodus by Aeron Glemann (http://metodus.com) MIT-style LICENSE

if (!window.Metodus) Metodus = function() {};

Metodus.slideshow = Class.create();
Metodus.slideshow.prototype = {
	initialize: function(images, props) {
		if (/Opera/.test(navigator.userAgent)) return;

		this.images = images.split(',');
		if (this.images.length == 1) return; // only cycle multiple images

		this.props = {
			hu: '',
			n: '0',
			ms: 4000
		}
		Object.extend(this.props, props || {});

		a = $('aro-' + this.props.n + 'a');
		img = a.cloneNode(true);

		div = document.createElement('div');
		div.setAttribute('id', 'aro-' + this.props.n + 'b');
		div.style.width = a.width + 'px';
		div.style.height = a.height + 'px';
		div.appendChild(img);

		a.parentNode.replaceChild(div, a);
		this.a = $('aro-' + this.props.n + 'a');
		this.b = $('aro-' + this.props.n + 'b');

		this.effect = new fx.Opacity(this.a.id, { duration: this.props.ms, onComplete: this.loaded.bind(this) });

		this.curr = 1;

		this.loader = new Image();
		this.loader.src = this.props.hu + 'images/' + this.images[this.curr];

		this.preload();
	},

	preload: function() {
		if (this.loader.complete) {
			this.b.style.background = 'url(' + this.loader.src + ') no-repeat';

			this.effect.custom(1, 0);
		}
		else { setTimeout(this.preload.bind(this), 10); }
	},

	loaded: function() {
		this.a.src = this.loader.src;

		this.effect.setOpacity(1);

		(this.curr == this.images.length - 1) ? this.curr = 0 : this.curr++;

		this.loader = new Image();
		this.loader.src = this.props.hu + 'images/' + this.images[this.curr];

		this.preload();
	}
}