if (/MSIE/.test(navigator.userAgent)) {
//	document.write('<style type="text/css">.quote {display: none}</style>');
}

window.addEvent('domready', function() {
	new ElementRandomiser($$('.quote'));

	//activate hover state
	$$('.eventItem').each(function(e) {
		e.addEvent('mouseenter', function() {
			this.toggleClass('eventItemHover');
		});
		e.addEvent('mouseleave', function() {
			this.toggleClass('eventItemHover');
		});
	});
	$$('.speaker, .speaker2').each(function(e) {
		e.addEvent('mouseenter', function() {
			this.toggleClass('speakerHover');
		});
		e.addEvent('mouseleave', function() {
			this.toggleClass('speakerHover');
		});
	});
	$$('.library, .library2').each(function(e) {
		$(e).addEvent('mouseenter', function() {
			this.addClass('libraryHover');
		});
		$(e).addEvent('mouseleave', function() {
			this.removeClass('libraryHover');
		});
	});

	//clear login form inputs on focus
	$$('form#frmLogin input').each(function(e) {
		var inputType = e.getAttribute('type');
		if (inputType == 'text' || inputType == 'password') {
			new AutoClearingField(e);
		}
	});
	$$('form#frmReminder input').each(function(e) {
		var inputType = e.getAttribute('type');
		if (inputType == 'text' || inputType == 'password') {
			new AutoClearingField(e);
		}
	});

	if (Cookie.read('user')) {
		var el = $('nb4').getElement('a');
		var href = new String(el.href);
		el.href = href.replace('ClientLogin_23.html', 'check_user.php?id=1');
	}

	//display error if credentials are incorrect on members area login page
	if (Cookie.read('login_flag') == 'invalid_credentials') {
		showLoginError('frmLogin');
		Cookie.dispose('login_flag', {path : '/'});
	}
	else if (Cookie.read('login_flag') == 'email_sent') {
		showLoginError('frmLogin', 'A new password has been sent to ' + Cookie.read('login_msg') + '.');
		Cookie.dispose('login_flag', {path : '/'});
		Cookie.dispose('login_msg', {path : '/'});
	}

	// file details
	new GDRLightbox($$('.cityShoppingGuide .fileDetails'), {
		fixFlash: false,
		adoptOnly: 'div.rightColumn div.guideDetails'
	});
});


var ElementRandomiser = new Class({
	initialize: function(elements) {
		var elementToDisplay = $random(0, elements.length - 1);
		for (var i = 0; i < elements.length; i++ ) {
			if (i == elementToDisplay) {
				$(elements[i]).setStyle('display', 'block');
			}
			else {
				$(elements[i]).setStyle('display', 'none');
			}
		}
	}
});



function showLoginError(form, msg) {
	form = $(form);
	if (!form) return;

	msg = msg || 'The username and password provided do not match the correct details.';

	var div = new Element('div');
	div.addClass('clearing');
	div.inject(form, 'top');

	var p = new Element('p');
	p.addClass('error');
	p.appendText(msg);
	p.inject(form, 'top');
}

var AutoClearingField = new Class({
	element: null,
	cleared: false,
	initialValue: '',
	initialize: function(element) {
		this.element = $(element);
		if (!this.element) return;

		this.initialValue = this.element.value;
		this.element.addEvent('focus', function() {
			this.clearDefault();
		}.bind(this));
		this.element.addEvent('blur', function() {
			this.setToDefault();
		}.bind(this));
	},
	clearDefault: function() {
		if (this.element && !this.cleared) {
			this.element.value = '';
		}
		this.cleared = true;
	},
	setToDefault: function () {
		if (this.element.value == '') {
			this.element.value = this.initialValue;
			this.cleared = false;
		}
	}
});

/*
 * GDRLightbox
 * Requires Fx.Scroll
 */
var GDRLightbox = new Class({
	Implements: Options,
	overlay: null,
	outer: null,
	inner: null,
	loading: null,
	elements: [],
	animating: false,
	req: null,
	visible: false,
	imageload: {
		images: [],
		loaded: 0
	},
	fx: {
		overlay: null,
		outer: null,
		inner: null,
		loading: null
	},
	options: {
		append: '',
		opacity: 0.7,
		duration: 250,
		scrollDuration: 750,
		timeout: 1000,
		fixFlash: true,
		adoptOnly: ''
	},
	initialize: function (elements, options) {
		this.setOptions(options);
		this.elements = elements;

		this.setup();

		this.elements.each(function (el) {
			el.addEvent('click', this.clicked.bindWithEvent(this, el));
		}, this);
	},
	setup: function () {
		var size = $(window).getScrollSize();
		this.overlay = new Element('div', {
			id: 'lb-overlay',
			styles: {
				height: size.y,
				opacity: this.options.opacity,
				visibility: 'hidden'
			}
		}).injectInside(document.body);
		this.overlay.addEvent('click', function (evt) {
			evt.stop();
			this.hide();
		}.bind(this));

		this.outer = new Element('div', {
			id: 'lb-outer',
			styles: {
				opacity: 1,
				visibility: 'hidden'
			}
		}).injectInside(document.body);
		this.outer.addEvent('click', function (evt) {
			evt.stop();
			this.hide();
		}.bind(this));

		this.inner = new Element('div', {
			id: 'lb-inner'
		}).injectInside(this.outer);
		this.inner.addEvent('click', function (evt) {
			evt.stopPropagation();
		}.bind(this));

		this.content = new Element('div', {
			id: 'lb-content'
		}).injectInside(this.inner);

		this.loading = new Element('div', {
			id: 'lb-loading',
			styles: {
				opacity: 1,
				visibility: 'hidden'
			}
		}).injectInside(this.inner);

		this.fx.overlay = new Fx.Tween(this.overlay, {
			duration: this.options.duration
		})
		this.fx.overlay.addEvent('start', this.start.bind(this));
		this.fx.overlay.addEvent('complete', this.completed.bind(this));

		this.fx.outer = new Fx.Tween(this.outer, {
			duration: this.options.duration
		})
		this.fx.outer.addEvent('start', this.start.bind(this));
		this.fx.outer.addEvent('complete', this.completed.bind(this));

		this.fx.loading = new Fx.Tween(this.loading, {
			duration: this.options.duration
		})
		this.fx.loading.addEvent('complete', this.completed.bind(this));
		this.fx.loading.addEvent('start', this.start.bind(this));
	},
	start: function (el) {
		switch (el) {
			case this.overlay:
				if (this.visible) {
					el.setStyle('display', 'block');
					this.fixFlash(true);
				}
				break;
			case this.outer:
				if (this.visible) {
					el.setStyle('display', 'block');
				}
				break;
		}
	},
	completed: function (el) {
		var visible = el.getStyle('visibility') == 'hidden' ? false : true;
		switch (el) {
			case this.overlay:
				if (!this.visible) {
					el.setStyle('display', 'none');
					this.fixFlash();
				}
				break;
			case this.outer:
				if (!this.visible) {
					el.setStyle('display', 'none');
				}
				if (visible && this.req) {
					this.req.send();
				}
				break;
		}
	},
	imageLoaded: function () {
		this.imageload.loaded++;
		if (this.imageload.loaded == this.imageload.images.length) {
			this.show();
		}
	},
	loaded: function (tree, elements, html) {
		this.content.setStyles({
			visibility: 'hidden'
		});

		this.content.empty();
		this.content.adopt(tree);
		if (this.options.adoptOnly) {
			var elToAdopt = this.content.getElements(this.options.adoptOnly);
			if (elToAdopt) {
				var newTree = elToAdopt.clone(true, true);
				this.content.empty();
				this.content.adopt(newTree);
			}
		}
		this.content.injectInside(this.inner);

		var images = [];
		images.extend(this.content.getElements('img'));

		// load images
		this.imageload = {
			images: images,
			loaded: 0
		};
		images.each(function (el) {
			el.addEvent('load', this.imageLoaded.bind(this));
		}, this);

//		this.imageload.images.each(function(e) {
//			console.log(e);
//		});
		if (this.imageload.images.length == 0) {
			this.show();
		}
	},
	show: function () {
		this.fx.loading.start('opacity', 1, 0);
		var cover = this.content.getElementById('cover');
		if (cover) {
			var y = cover.getSize().y;
			cover.setStyle('height', 0);
			cover.tween('height', y);
		}
		var infolink = this.content.getElementById('infoLink');
		var info = this.content.getElementById('coverInfo');
		if (infolink && info) {
			infolink.addEvent('click', function (evt) {
				evt.stop();
				var parent = infolink.getParent();
				if (parent.hasClass('norm')) {
					info.setStyle('display', 'block');
					infolink.set('text', 'Close information on the file');
				}
				else {
					info.setStyle('display', 'none');
					infolink.set('text', 'Read information on the file');
				}
				parent.toggleClass('norm');
				parent.toggleClass('down');
			});
		}

		var closes = this.content.getElements('.close');
		closes.each(function (el) {
			el.addEvent('click', function (evt) {
				this.hide();
			}.bind(this));
		}, this);

		//"add to selection" link
		$$('.optionsList a.updateSelection').each(function(e) {
			oSelectionUpdater.addLink(e);
		}.bind(this));

		this.content.setStyle('visibility', 'visible');
	},
	hide: function () {
		this.visible = false;
		this.fx.overlay.start('opacity', 0)
		this.fx.outer.start('opacity', 0)
	},
	clicked: function (evt, el) {
		if (evt) {
			evt.stop();
		}
		if (Fx.Scroll) {
			new Fx.Scroll(window, {
				offset: {
					x: 0,
					y: 60
				},
				duration: this.options.scrollDuration
			}).toTop();
		}
		var url = new String(el.href);
		if (this.options.append) {
			url += url.contains('?') ? '&' : '?';
			url += this.options.append;
		}
		this.load(url)
	},
	load: function (url) {
		this.req = new Request.HTML({
			url: url,
			onSuccess: this.loaded.bind(this),
			onFailure: function () {
				alert('The page request failed, please refresh the page and try again.');
			}
		});

		// show loading
		this.visible = true;
		this.inner.empty();
		this.fx.loading.start('opacity', 1);
		this.loading.injectInside(this.inner);

		// fade in
		this.fx.overlay.start('opacity', 0, this.options.opacity)
		this.fx.outer.start('opacity', 0, 1)
	},
	fixFlash: function (disable) {
		if (this.options.fixFlash) {
			["object", /MSIE/.test(navigator.userAgent) ? "select" : "embed"].forEach(function(tag){
				Array.forEach(document.getElementsByTagName(tag), function(el){
					el.setStyle('visibility', disable ? 'hidden' : 'visible');
				});
			});
		}
	}
});

