// ALL PURPOSE FUNCTIONS
var Class = function(properties){
	var klass = function(){
		if (this.initialize && arguments[0] != 'noinit') return this.initialize.apply(this, arguments);
		else return this;
	};
	for (var property in this) klass[property] = this[property];
	klass.prototype = properties;
	return klass;
};


Class.empty = function(){};

Class.prototype = {

	extend: function(properties){
		var pr0t0typ3 = new this('noinit');

		var parentize = function(previous, current){
			if (!previous.apply || !current.apply) return false;
			return function(){
				this.parent = previous;
				return current.apply(this, arguments);
			};
		};

		for (var property in properties){
			var previous = pr0t0typ3[property];
			var current = properties[property];
			if (previous && previous != current) current = parentize(previous, current) || current;
			pr0t0typ3[property] = current;
		}
		return new Class(pr0t0typ3);
	},

	implement: function(properties){
		for (var property in properties) this.prototype[property] = properties[property];
	}

};



Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}

Function.prototype.bindAsEventListener = function(object) {
  var __method = this;
  return function(event) {
    return __method.call(object, event || window.event);
  }
}

Object.extend = function(){
	var args = arguments;
	args = (args[1]) ? [args[0], args[1]] : [this, args[0]];
	for (var property in args[1]) args[0][property] = args[1][property];
	return args[0];
};

Object.Native = function(){
	for (var i = 0; i < arguments.length; i++) arguments[i].extend = Class.prototype.implement;
};

new Object.Native(Function, Array, String, Number, Class);

Function.extend({

	create: function(options){
		var fn = this;
		options = Object.extend({
			'bind': fn,
			'event': false,
			'arguments': null,
			'delay': false,
			'periodical': false,
			'attempt': false
		}, options || {});
		if (options.arguments != null && typeof options.arguments != 'undefined' && !(options.arguments instanceof Array))
			options.arguments = [options.arguments];
		return function(event){
			var args = options.arguments || arguments;
			if (options.event){
				event = (options.event === true) ? event || window.event : new options.event(event);
				args = [event].concat(args);
			}
			var returns = function(){
				return fn.apply(options.bind, args);
			};
			if (options.delay) return setTimeout(returns, options.delay);
			if (options.periodical) return setInterval(returns, options.periodical);
			if (options.attempt){
				try {
					var result = returns();
				} catch(err){
					result = err;
				} finally {
					return result;
				}
			} else return returns();
		};
	},

	pass: function(args, bind){
		return this.create({'arguments': args, 'bind': bind});
	},
	
	attempt: function(args, bind){
		return this.create({'arguments': args, 'bind': bind, 'attempt': true})();
	},

	bind: function(bind, args){
		return this.create({'bind': bind, 'arguments': args});
	},

	bindAsEventListener: function(bind, args){
		return this.create({'bind': bind, 'event': true, 'arguments': args});
	},

	delay: function(ms, bind, args){
		return this.create({'delay': ms, 'bind': bind, 'arguments': args})();
	},

	periodical: function(ms, bind, args){
		return this.create({'periodical': ms, 'bind': bind, 'arguments': args})();
	}

});


function id() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}
/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com

*/

// ============ ADD EVENTS ================ //
function addEvent(obj, evType, fn){
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, true);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
	    return false;
    }
}

//*******************************************************************************************************************************************


var slideshow = {	
	
	currentItem : 0,
	nextItem : 1,
	currentOpac : 100,
	timeout: 0,
	
	init:function(){		
		var self = slideshow;
		var imgs = id('fadeImg').getElementsByTagName('img');
		slideshow.max  = imgs.length-1;
		slideshow.imgs = imgs;
		//slideshow.prevItem = slideshow.max;
		
		// SET UP IMAGES, OPACITIES, ETC
		slideshow.setUpImages();
		
		// SET UP FIRST IMAGE		
		slideshow.imgs[0].xOpacity = 100;
		slideshow.setOpacity(slideshow.imgs[0]);
		slideshow.imgs[0].style.display = 'block';
		slideshow.imgs[0].style.className = 'current';	
		//id('more-info').innerHTML = slideshow.imgs[0].alt;	
		
		slideshow.interval = 5000; // SET INTERVAL
		slideshow.timer = slideshow.fade.delay(slideshow.interval);
			
	},	
	
	setUpImages:function(){
		for(var b=0; b<slideshow.imgs.length; b++){
			//slideshow.imgs[b].style.display = 'block';
			slideshow.imgs[b].xOpacity = 0;
			slideshow.imgs[b].id = 'image-'+b;
		}
	},	
	
	fade:function(slides){
		slideshow.nextItem = (slideshow.currentItem == slideshow.max) ? 0 : slideshow.currentItem+1;		
		
		slideshow.imgs[slideshow.nextItem].xOpacity = 0;
		slideshow.setOpacity(slideshow.imgs[slideshow.nextItem]);
		slideshow.imgs[slideshow.nextItem].style.display = 'block';
		
				
		// FADE OUT
		var timer = 0;
	    for(i = 100; i >= 0; i--) {
	        setTimeout("changeOpac("+i+", '"+id(slideshow.imgs[slideshow.currentItem]).id+"')", (timer*(slideshow.interval/300)));
	        timer++;
	    }	    
	    
	    // FADE NEXT ITEM IN
	    var innie = 0;
	    for(a = 0; a<=100; a++){
	    	setTimeout("changeOpac("+a+", '"+id(slideshow.imgs[slideshow.nextItem]).id+"')", (innie*(slideshow.interval/200)));
	    	innie++;
	    }
	   	slideshow.interval = 5000; // SET INTERVAL
	    slideshow.prepNextSlide();
	    slideshow.timer = slideshow.fade.delay(slideshow.interval);	    	
	},	
	
	prepNextSlide:function(){
		//id('more-info').innerHTML = id(slideshow.imgs[slideshow.currentItem]).alt;
		slideshow.currentItem = slideshow.nextItem;
		slideshow.nextItem = (slideshow.imgs[slideshow.currentItem+1]) ? slideshow.currentItem+1 : 0;
		slideshow.prevItem = (slideshow.currentItem == 0) ? slideshow.max : slideshow.currentItem-1;		
	},
		
	setOpacity:function(obj){
		obj.style.opacity = (obj.xOpacity/100);
		obj.style.MozOpacity = (obj.xOpacity/100)
		obj.style.filter = 'alpha(opacity=' + obj.xOpacity + ')';		
	}
	
}

function changeOpac(opacity, el) {
    var object = id(el).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

addEvent(window, 'load', slideshow.init);