/*
 * Created by: Chris Campbell <http://particletree.com>
 * Modified by: Jasper van Veghel <jasper.van.veghel@ordina.nl>
 * 
 * Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
 */

var LightboxGW = Class.create ();

LightboxGW.prototype = {
	yPos: 0, xPos: 0,

	initialize: function (ctrl) {
		this.content = ctrl.href;
		Event.observe (ctrl, 'click', this.activate.bindAsEventListener (this), false);

		ctrl.onfocus = ctrl.blur;
		ctrl.onclick = function () { return false; };
	},
	
	// Turn everything on - mainly the IE fixes

	activate: function () {
		if (Prototype.Browser.IE) {
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}

		this.displayLightbox("block");
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox

	prepareIE: function (height, overflow) {
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	// In IE, select elements hover on top of the lightbox

	hideSelects: function(visibility) {
		selects = document.getElementsByTagName('select');
		for (i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function () {
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function (x, y) {
		window.scrollTo (x, y); 
	},
	
	displayLightbox: function (display) {
		$('overlayGW').style.display = display;
		$('lightboxGW').style.display = display;
		if(display != 'none') this.loadInfo();
	},
	
	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function () {
		this.processInfo (this.content);
	},
	
	// Display Ajax response

	processInfo: function (response) {
		var info = "<div id='lbGWContent'><iframe id='kaartIFrame' src='" + response + "' name='Kaart' style='width: 690px; height: 570px;' scrolling='auto' frameborder='0'></iframe></div>";

		new Insertion.Before($('lbGWLoadMessage'), info);

		$('lightboxGW').className = "done";	
		this.actions ();		
	},
	
	// Search through new links within the lightbox, and attach click event

	actions: function () {
		lbActions = document.getElementsByClassName('lbGWAction');

		for(i = 0; i < lbActions.length; i++) {
			Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
			lbActions[i].onclick = function(){return false;};
		}
	},

	deactivate: function () {
		Element.remove($('lbGWContent'));
		
		if (Prototype.Browser.IE) {
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		
		this.displayLightbox("none");
	}
}

// Initialize the Lightbox (GW) on page load

var lightboxGWReferences = [ ];

Event.observe (window, 'load', function () {
	// Initialize the mark-up

	var overlayDiv = document.createElement ('div');
	overlayDiv.setAttribute ('id', 'overlayGW');

    document.getElementsByTagName('body')[0].appendChild (overlayDiv);

	var lightboxDiv = document.createElement ('div');
	lightboxDiv.setAttribute ('id', 'lightboxGW');
	lightboxDiv.setAttribute ('className', 'loading');

    var loadingDiv = document.createElement ('div');
	loadingDiv.setAttribute ('id', 'lbGWLoadMessage');

	var loadingParagraph = document.createElement ('p');
	loadingParagraph.appendChild (document.createTextNode ('Loading'));

	loadingDiv.appendChild (loadingParagraph);
	lightboxDiv.appendChild (loadingDiv);

	document.getElementsByTagName('body')[0].appendChild (lightboxDiv);

	// Initialize the rest

	$A(document.getElementsByClassName ('lbGWOn')).each (function (element) {
		lightboxGWReferences.push (new LightboxGW (element));
	});
}, false);