Swapper = {
	tagsToSwap: ['img'], //which types of elements should be swapped (lowercase)
	wait: 5000, //how long between swaps (milliseconds)
	swapDuration: 2, //how long should a swap take (seconds)
	useCookie: 'afrahm_swapper_index', //if not blank, store the last visible element in this cookie

	elements: [],
	currentElement: 0,
	
	swapFade: function() {
		Effect.Fade(this.getCurrentElement(), { duration: this.swapDuration, from:1.0, to:0.0 });
		this.nextElement();
		Effect.Appear(this.getCurrentElement(), { duration: this.swapDuration, from:0.0, to:1.0 });
	} ,
	
	nextElement: function() {
		this.currentElement++;
		if(this.currentElement == this.elements.length) {
			this.currentElement = 0;
		}
		
		if(this.useCookie) {
			this.setCookie(this.useCookie, "" + this.currentElement);
		}
	} ,
	
	getCurrentElement: function() {
		return this.elements[this.currentElement]
	} ,
	
	readElementsFromContainer: function(container) {
		for(var i = 0; i < container.childNodes.length; i++) {
			element = container.childNodes[i];
			
			if(element.tagName) {
				if(this.tagsToSwap.find(function(tagToSwap) { return tagToSwap == element.tagName.toLowerCase()})) {
					this.elements.push(element);
				}
			}
		}
	} ,
	
	init: function(containerId) {
		
		if(containerId != undefined) {
			this.readElementsFromContainer($(containerId));
		}
		
		if(this.useCookie) {
			if(currentElement = Number(this.getCookie(this.useCookie))) {
				/*
				Handled by PHP now!
				
				this.elements.each(function(element, index) {
					if(currentElement == index) {
						Element.show(element);
					}
					else {
						Element.hide(element);
					}
				})*/
				
				this.currentElement = currentElement;
			}
		}
		
		setInterval(Swapper.swapFade.bind(this), this.wait);
	} ,
	
	setCookie: function(cookieName, cookieValue, expires, path, domain, secure) {
		document.cookie =
			escape(cookieName) + '=' + escape(cookieValue)
			+ (expires ? '; expires=' + expires.toGMTString() : '')
			+ (path ? '; path=' + path : '')
			+ (domain ? '; domain=' + domain : '')
			+ (secure ? '; secure' : '');
	
	} ,
	
	getCookie: function(cookieName) {
		var cookieValue = '';
		var posName = document.cookie.indexOf(escape(cookieName) + '=');
		if (posName != -1) {
			var posValue = posName + (escape(cookieName) + '=').length;
			var endPos = document.cookie.indexOf(';', posValue);
			if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
			else cookieValue = unescape(document.cookie.substring(posValue));
		}
		return (cookieValue);
	}

}