var rethink = {

/*
 * Class, initialize with new rethink.ImageFader
 */
	ImageFader: function() {
		this.initialize.apply(this, arguments);
	},

	ImageMap: function(uid) {
		var map = [
			{x: 4, y: 262},// Stavanger
			{x: 50, y: 331},// Århus
			{x: 68, y: 291},// Göteborg
			{x: 78, y: 341},// København
			{x: 140, y: 256},// Stockholm
			{x: 188, y: 236},// Helsinki
			{x: 98, y: 184}// Östersund
		],
		width = 12,
		height = 12;
		function hide() {
			if (!this.hidden) {
				this.style.top = this.style.left = '-9999em';
				this.hidden = true;
			}
		};
		function show() {
			if (this.hidden) {
				this.style.top = this.style.left = '';
				this.hidden = false;
			}
		};
		function d(id) {
			return document.getElementById(id);
		};

		if (core)
			return;
		var img = d(uid+'_image').getElementsByTagName('img')[0];
		if (img) {
			var elements = {};
			for (var i = 0; i < map.length; i++) {
				hide.call(elements[i] = d(uid+'_'+i));
			}
			img.onmousemove = function(event) {
				event = event || window.event;
				var x = typeof event.layerX == 'undefined' ? event.offsetX : event.layerX;
				var y = typeof event.layerY == 'undefined' ? event.offsetY : event.layerY;
				var pos;
				var func;
				for (var i = 0; i < map.length; i++) {
					pos = map[i];
					func = hide;
					if (x >= pos.x && x <= pos.x+width &&
							y >= pos.y && y <= pos.y+height) {
						func = show;
					}
					func.call(elements[i]);
				}
			};
		}
	},

	partners: {
		initialize: function() {
			if (!window.core) {
				var ids = ['partners', 'content'];
				var me = this;
				var mouseover = function() {
					me.mouseover(this);
				};
				var mouseout = function() {
					me.mouseout(this);
				};
				for (var j = 0; j < ids.length; j++) {
					var p = document.getElementById(ids[j]);
					if (p) {
						var divs = p.getElementsByTagName('div');
						for (var i = divs.length-1; i >= 0; i--) {
							if (divs[i].className == 'partner') {
								divs[i].onmouseover = mouseover;
								divs[i].onmouseout = mouseout;
							}
						}
					}
				}
			}
		},
		mouseover: function(div) {
			div.className = 'partner hover';
		},
		mouseout: function(div) {
			div.className = 'partner';
		}
	},

	menu: {
		initialize_dont_run: function() {
			if (!window.core) {
				var ul = document.getElementById('abj_abi_menu');
				var li;
				for (var i = 0; i < ul.childNodes.length; i++) {
					li = ul.childNodes[i];
					if (li.nodeType == 1) {
						if (li.getElementsByTagName('ul').length == 0)
							li.appendChild(document.createElement('ul'));
					}
				}
			}
		}
	},

	tip: {
		toggle: function(elementId) {
			var elm = document.getElementById(elementId);
			if (elm) {
				elm.className = elm.className == 'tip_form' ? 'tip_form_active' : 'tip_form';
			}
		}
	},

	display: {
		name: 'rethink.display',
		behaviorAdded: false,
		initialize: function() {
			this.update(this.getType());
		},
		getType: function() {
			var value = 'co2';
			if (window.sessionStorage) {
				if (window.sessionStorage[this.name]) {
					value = window.sessionStorage[this.name];
				}
			} else if (document.documentElement.addBehavior) {
				with (document.documentElement) {
					if (!this.behaviorAdded) {
						addBehavior('#default#userdata');
						this.behaviorAdded = true;
					}
					load(this.name);
					value = getAttribute('value');
				}
			} else {
				value = secoya.cookie.get('display');
			}
			return value;
		},
		setType: function(type) {
			if (window.sessionStorage) {
				window.sessionStorage[this.name] = type;
			} else if (document.documentElement.addBehavior) {
				with (document.documentElement) {
					if (!this.behaviorAdded) {
						addBehavior('#default#userdata');
						this.behaviorAdded = true;
					}
					load(this.name);
					setAttribute('value', type);
					save(this.name);
				}
			} else {
				secoya.cookie.set('display', type);
			}
			this.update(type);
		},
		update: function(type) {
			var className = document.documentElement.className;
			className = className.replace(/\s*(co2|read)friendly\s*|^$/, type);
			if (className.indexOf(type) == -1) {
				className += ' '+type;
			}
			document.documentElement.className = className;
		},
		co2friendly: function() {
			this.setType('co2friendly');
		},
		readfriendly: function() {
			this.setType('readfriendly');
		}
	},

	initialize: function() {
		for (var obj in this) {
			obj = this[obj];
			if (obj && typeof obj == 'object' && obj.initialize && typeof obj.initialize == 'function') {
				obj.initialize();
			}
		}
	}

};

rethink.ImageFader.prototype = (function(){

	var
		doc = document,
		m = Math;

	function getNextIndex(random) {
		var
			index = this.imageIndex,
			count = this.images.length;
		if (this.random) {
			if (count > 2) {
				do {
					index = m.floor(m.random()*count);
				} while (index == this.imageIndex);
			}
		} else {
			index++;
			if (index >= count)
				index = 0;
		}
		return this.imageIndex = index;
	};

	function nextImage() {
		return this.images[getNextIndex.call(this)];
	};

	function set(elm, opacity) {
		lightCore.setOpacity(elm, opacity);
	};

	return {
		images: null,
		id: null,
		parent: null,
		imageIndex: -1,
		random: false,
		initialize: function(id, random) {
			this.id = id;
			this.random = random;
			this.parent = doc.getElementById(id);
			if (this.parent) {
				this.images = [];
				var children = this.parent.childNodes;
				for (var i = 0; i < children.length; i++) {
					if (children[i].nodeType == 1 && (children[i].tagName == 'A' || children[i].tagName == 'IMG')) {
						this.images.push(children[i]);
					}
				}
				children = null;
				if (this.images.length == 1 || window.core)
					return;
			}
			var
				me = this,
				background = nextImage.call(me),
				ontop = nextImage.call(me);

			ontop.className = 'ontop';
			background.className = 'background';
			var fx = new secoya.fx({
				from: 0,
				to: 100,
				type: secoya.fx.linear,
				duration: 2,
				callback: function(value) {
					set(ontop, value);
				},
				finish: function() {
					setTimeout(function(){
						background.className = '';
						background = ontop;
						background.className = 'background';
						ontop = nextImage.call(me);
						ontop.className = 'ontop';
						fx.start();	
					}, 2000);
				}
			});
			fx.start();
		}
	};
})();