/**
 * ---------------------------
 * Client: Tap Uitvaartzorg
 * URL: http://www.tap-uitvaart.nl
 * Author: Harmen Janssen
 *
 * ---------------------------
 */

/**
 * General namespace
 */
var tap = {
	/**
	 * Initialize all objects
	 */
	init: function () {
		for (var i in this) {
			if (typeof this[i].init == 'function') {
				this[i].init ();
			}
		}
	},	
	/**
	 * Fold-out menu behaviour
	 */
	menu: {
		currentOpen: null,
		init: function () {
			var mainnav = $('main-navigation');
			if (!mainnav) {
				return false;
			}
			var lis = mainnav.getElementsByClassName ('open','li');
			lis = tap.menu.toArray (lis);
			for (var i=0,li; li=lis[i]; i++) {
				li=$(li);
				if (!li.hasClass('active')) {
					li.removeClass ('open');
					li.open = false;
					tap.menu.preload(li);
					tap.menu.shrinkImage(li);
					li.addEvent('click',function(e){
						if (!this.open) {
							if (tap.menu.currentOpen) {
								tap.menu.currentOpen.removeClass ('open');
								tap.menu.currentOpen.open = false;
								tap.menu.shrinkImage (tap.menu.currentOpen);
								tap.menu.currentOpen = null;
							}
							this.addClass('open');
							this.open = true;
							tap.menu.blowUpImage (this);
							tap.menu.currentOpen = this;
						} else {
							// delegate event. If the event was triggered on a link, return true
							var theTarget = What.Event.getTarget (e);
							if (theTarget !== this) {
								return true;
							}
							this.removeClass ('open');
							this.open = false;
							tap.menu.shrinkImage (this);
							tap.menu.currentOpen = null;
						}
						What.Event.preventDefault (e);
						return false;
					});
				}
			}
		},
		toArray: function (collection) {
			var _return = [];
			for (var i=0; i<collection.length; i++) {
				_return.push (collection[i]);
			}
			return _return;
		},
		convertToSpan: function (li) {
			var a = $(li.$$('a')[0]);
			var textnode = a.firstChild;
			while (textnode.nodeType != 3){
				textnode = textnode.firstChild;
			}
			var span = What.Element.create('span',{},[textnode]);
			a.parentNode.insertBefore (span,a);
			a.remove();
		},
		preload: function (li) {
			var theImage = li.$$('img');
			if (theImage.length == 0){
				return;
			}
			theImage = theImage[0];
			var theSrc = theImage.src;
			if (theSrc.indexOf ('_klein.jpg') != -1) {
				theSrc = theSrc.split ('_klein.jpg')[0];
				var img = new Image();
				img.src = theSrc + '_groot.jpg';
			}
		},
		blowUpImage: function (li) {
			var theImage = li.$$('img');
			if (theImage.length == 0){
				return;
			}
			theImage = theImage[0];
			var theSrc = theImage.src;
			if (theSrc.indexOf ('_klein.jpg') != -1) {
				theSrc = theSrc.split ('_klein.jpg')[0];
				theImage.setAttribute ('src', theSrc + '_groot.jpg');
			}
		},
		shrinkImage: function (li) {
			var theImage = li.$$('img');
			if (theImage.length == 0){
				return;
			}
			theImage = theImage[0];
			var theSrc = theImage.src;
			if (theSrc.indexOf ('_groot.jpg') != -1) {
				theSrc = theSrc.split ('_groot.jpg')[0];
				theImage.setAttribute ('src', theSrc + '_klein.jpg');
			}
		}
	},
	/**
	 * Create clusters of image that can be zoomed in the sidebar
	 */
	zoomcluster: {
		init: function () {
			var zclusters = document.getElementsByClassName('zoomcluster','div');
			for (var i=0,z; z=zclusters[i]; i++) {
				tap.zoomcluster.initElement($(z));
			}
		},
		initElement: function (z) {
			var imgs = z.$$('img');
			for (var k=0,img; img = imgs[k]; k++) {
				img = $(img);
				img.j = k;
				img.addEvent('mouseover',function(){
					if (!this.biggie) {
						var self = this;
						this.style.cursor = 'pointer';
						// create new image
						var theSrc = this.src;
						// test availability of image (loaded yet?)
						var testImg = new Image (), divCon;
						testImg.onload = function (){
							divCon = tap.zoomcluster.createZoom ( self, z );
							divCon.style.visibility = '';
							divCon.style.cursor = 'pointer';
							// save element as property of original image
							self.biggie = divCon;
							tap.zoomcluster.hideAnythingBut (self.biggie);
							self.biggie.addEvent('mouseout',function (){
								this.style.display = 'none';
							});
						};
						testImg.src = theSrc;
					} else {
						this.biggie.style.display = 'block';
						tap.zoomcluster.hideAnythingBut (this.biggie);
					}
				});
			}
		},
		createZoom: function (originalImg, parent) {
			var theSrc = originalImg.src;
			var imgs = $(parent.$$('img'));
			var newImage = What.Element.create('img',{src:theSrc});
			newImage.addClass ('biggie');
			// create image container
			var divCon = What.Element.create('div',{});
			divCon.className = 'biggieCon';
			divCon.appendChild(newImage);
			divCon.style.visibility = 'hidden';
			document.body.appendChild(divCon);
			// position container
			var coords = originalImg.getPerimeter();
			divCon.style.position = 'absolute';
			var theWidth = newImage.offsetWidth;
			var theHeight = newImage.offsetHeight;
			divCon.style.width = theWidth+'px';
			divCon.style.height = theHeight+'px';
			var theOffsetWidth = theWidth + 6;
			var theOffsetHeight = theHeight + 5;
			// determine when images should be layed out to the top or the bottom
			var c = imgs.length;
			var n = imgs.length/2;
			if (this.j >= n && c > 2) {
				divCon.style.left = (coords.bottomright.x - theOffsetWidth) + 'px';
				divCon.style.top = (coords.topleft.y) + 'px';
			} else {
				divCon.style.left = (coords.bottomright.x - theOffsetWidth) + 'px';
				divCon.style.top = (coords.bottomleft.y - theOffsetHeight) + 'px';
			}
			return divCon;
		},
		hideAnythingBut: function (elm) {
			var biggies = document.getElementsByClassName('biggieCon','div');
			for (var i=0,biggie; biggie = biggies[i]; i++) {
				if (biggie !== elm && biggie.style.display == 'block') {
					biggie.style.display = 'none';
				}
			}
		}
	},
	/**
	 * Show/hide the poll
	 */
	togglePollView: {
		init: function () {
			var p;
			if (p = $('poll-box')) {
				var pollquestion = p.$$('p')[0].innerHTML || 'Geef uw mening';
				var a = What.Element.create ('a',{href:'#open-poll',className:'poll-opener'},[pollquestion]);
				a.addEvent ('click',function (e){
					p.style.display = 'block';
					this.parentNode.remove ();
					What.Event.preventDefault (e);
					return false;
				});
				var div = What.Element.create ('div',{className:'widget'});
				div.appendChild (a);
				p.style.display = 'none';
				p.parentNode.insertBefore (div,p);
			}
		}
	}
};

/**
 * Initialize general namespace
 */
tap.init ();
