var get = {
	id : function(i) {
		return document.getElementById(i);
	},
	tag : function(t, p) {
		p = p || document;
		return p.getElementsByTagName(t);
	},
	object : function(obj) {	
		return (typeof obj == 'string') ? get.id(obj) : obj;
	}
};
var go = {
	self : function(l) {
		window.location.href = l;
	},
	blank : function(l) {
		window.open(l);
		return false;
	}
};
var swap = {
	init : function(obj, v) {
		this.object = get.object(obj);
		this.v = v;
	},
	className : function(obj, v) {
		this.init(obj, v);
		this.o = this.object.className;
		this.object.className = this.v || this.object.className + '_hover';
		this.object.onmouseout = function() {
			swap.object.className = swap.o;
		};
	},
	display : function(obj, r) {
		this.init(obj);
		if(this.object.style.display !== 'block')
			this.object.style.display = 'block';
		else
			this.object.style.display = 'none';
		if(r == true)
			this.object.reset();
		return false;
	}
};
var array = {
	elements : Array,
	className : String,
	filter : function(a, c) {
		this.elements = [];
		this.className = c;
		for(var i = 0; i < a.length; i++) {
			if(a[i].className == this.className)
				this.elements.push(a[i]);
		};
		return this.elements;
	}
}
var products = {
	container : Object,
	thumbnails : [],
	object : Object,
	image : Object,
	message : String,
	counter : Number,
	formats : {
		thumbnail : 'product_gallery_small',
		preview: 'product_gallery_large',
		popup: 'popup'
	},
	gallery : function(c, m) {
		this.container = get.id(c);
		this.message = m || 'Er zijn geen productfoto\'s beschikbaar.';
		this.limit = 3;
		this.counter = null;
		if(this.container.hasChildNodes()) {
			while(this.container.hasChildNodes()) {
				var node = this.container.firstChild;
				if(node.className == 'image_gallery_thumbnail' && this.counter < this.limit) {
					this.thumbnails.push(node);
					this.counter++;
				};
				this.container.removeChild(node);
			};
			this.setThumbnails();
			this.setPreview();
		} else {
			this.container.parentNode.appendChild(document.createElement('p').appendChild(document.createTextNode(this.message)));
			this.container.parentNode.removeChild(this.container);
		};
	},
	setThumbnails : function() {
		this.object  = document.createElement('div');
		this.object.id = 'product_gallery_thumbnails';
		for(var i = 0; i < this.thumbnails.length; i++) {
			this.thumbnails[i].onclick = this.setPreview;
			this.thumbnails[i].firstChild.src = this.replaceFormat(this.thumbnails[i].firstChild.src, this.formats.thumbnail);
			this.object.appendChild(this.thumbnails[i]);
		};
		this.container.appendChild(this.object);
	},
	setPreview : function() {
		if(get.id('product_gallery_preview')) {
			products.object = get.id('product_gallery_preview');
			products.object.firstChild.href = products.replaceFormat(this.href, products.formats.popup);
			products.object.firstChild.firstChild.src = products.replaceFormat(this.href, products.formats.preview);
			return false;
		} else {
			this.object  = document.createElement('div');
			this.object.id = 'product_gallery_preview';
			this.image = this.thumbnails[0].cloneNode(true);
			this.image.firstChild.src = this.replaceFormat(this.image.firstChild.src, this.formats.preview);
			this.image.rel = 'lightbox';
			this.image.href = this.replaceFormat(this.image.href, this.formats.popup);
			this.object.appendChild(this.image);
			this.container.appendChild(this.object);
		};
	},
	replaceFormat : function(s, f) {
		return s.substring(0, s.indexOf('&format=')) + '&format=' + f;
	},
	imageNotFound : function(obj, t, c, s) {
		this.object = get.id(obj);
		this.elements = array.filter(get.tag(t, this.object), c);
		for(i = 0, j = this.elements.length; i < j; i++) {
			if(get.tag('img', this.elements[i]).length == 0) {
				this.image = document.createElement('img');
				this.image.src = s;
				this.elements[i].appendChild(this.image);
			};
		};
	},
	checkDesc : function(o) {
		var o = get.id(o);
		var t = o.firstChild.nodeValue;
		if(t == '" "' || t == '""') {
			o.innerHTML = '&nbsp;';
		};
	}
};
var cart = {
	object : Object,
	quantity : Number,
	message : String,
	replace : function(c, t) {
		if(get.id(c)) {
			this.quantity = get.id(c).firstChild.nodeValue;
			this.object = get.id(t);
			if(this.quantity == 1)
				this.object.firstChild.nodeValue = 'artikel';
			else if(this.quantity > 1)
				this.object.firstChild.nodeValue = 'artikelen';
		};
	},
	del : function(t, l) {
		switch(l) {
			case 'de':
				this.message = t ? 'Sind Sie sicher, dass Sie das Produkt "' + t + '" aus Ihrem Einkaufskorb entfernen möchten?' : 'Sind Sie sicher, dass Sie das Produkt # aus Ihrem Einkaufskorb entfernen möchten?';
				break;
			default:
				this.message = t ? 'Weet u zeker dat u het product "' + t + '" wilt verwijderen?' : 'Weet u zeker dat u dit product wilt verwijderen?';
		};
		if(confirm(this.message))
			return true
		else
			return false;
	},
	tooltip : function(o, t, m) {
		this.object = get.object(o);
		var target = get.id(t);
		this.message = m;
		var oMessage = target.innerHTML;
		target.innerHTML = '<strong>' + this.message + '</strong>';
		this.object.onmouseout = function() {
			target.innerHTML = oMessage;
		};
	}
};
var order = {
	target : Object,
	elements : Array,
	setAltAddress : function(t) {
		this.target = get.id(t);
		this.elements = get.tag('input', this.target);
		if(this.target.style.display !== 'block') {
			this.target.style.display = 'block'
			for(var i = 0, j = this.elements.length; i < j; i++) {
				if(this.elements[i].getAttribute('_error') !== 'Input invalid, no description specified!')
					this.elements[i].setAttribute('_required', false);
			};
		} else {
			this.target.style.display = 'none';
			for(var i = 0, j = this.elements.length; i < j; i++) {
				this.elements[i].removeAttribute('_required');
			};
		};
	},
	cancel : function(t, l) {
		switch(l) {
			case 'de':
				var q = confirm('Sind Sie sicher, dass Sie annullieren möchten? Die ausgefüllten Felder werden gelöscht.');
				break;
			default:
				var q = confirm('Weet u zeker dat u wilt annuleren? De ingevulde velden worden gewist.');
		};
		this.target = get.id(t);
		if(q) {
			swap.display(this.target);
			this.target.reset();
		};
	}
};