
window.addEvent('domready', function() {
	['#navbar #nb1', '#navbar #nb2', '#navbar #nb3', '#navbar #nb4', '#navbar #nb5'].each(function(e) {
		e = $E(e);
		var m = new Menu(e);
		//find submenus
		$A($(e).getElementsByTagName('li')).each(function(e) {
			var sm = new Menu(e);
		});
	});


	if (/MSIE\s6/.test(navigator.userAgent)) {
		var elements = $ES('img');
		elements.each(function (e) {
			if (e.src.indexOf('.png') == -1) {
				return;
			}
			var styles = $extend(e.getStyles('width', 'height', 'top', 'left', 'margin', 'padding', 'position', 'display', 'visibility'), {
				background: 'none',
				filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod="crop", src="' + e.src + '")',
				width: e.getSize().size.x,
				height: e.getSize().size.y
			});

			var el = new Element('div', {
				styles: styles,
				'class': 'pngfix'
			}).injectAfter(e);
			e.setStyle('display', 'none');
		});
	}
});


Element.extend({
	getDimensions: function() {
		element = this;
		var display = $(element).getStyle('display');
		if (display != 'none' && display != null) {// Safari bug
			return {width: element.offsetWidth, height: element.offsetHeight};
		}

		// All *Width and *Height properties give 0 on elements with display none,
		// so enable the element temporarily (and parents as well)

		//deal with the parents
		var e = element;
		var ps = [];
		while (e.getParent() && e.getParent().getTag().toLowerCase() != 'html') {
			p = e.getParent();
			var display = $(p).getStyle('display');
			if (display == 'none' || display == null) {
				ps.push({
					'element': p,
					'visibility': p.style.visibility,
					'position': p.style.position,
					'display': display
				});
				p.style.visibility = 'hidden';
				p.style.position = 'absolute';
				p.style.display = 'block';
			}
			e = p;
		}

		var els = element.style;
		var originalVisibility = els.visibility;
		var originalPosition = els.position;
		var originalDisplay = els.display;
		els.visibility = 'hidden';
		els.position = 'absolute';
		els.display = 'block';
		var originalWidth = element.clientWidth;
		var originalHeight = element.clientHeight;
		els.display = originalDisplay;
		els.position = originalPosition;
		els.visibility = originalVisibility;

		//revert parents
		ps.each(function(p) {
			$(p.element).style.visibility = p.visibility;
			$(p.element).style.position = p.position;
			$(p.element).style.display = p.display;
		});

		return {width: originalWidth, height: originalHeight};
	}
});

var Menu = new Class ({
	options: {
	},
	initialize: function (element, options) {
		this.element = $(element);
		this.options = $extend(this.options, options || {});

		if (!this.element) {
			return;
		}

		//var divs = $(this.element).getElementsByTagName('div');
		var divs = this.element.getElements('div');
		var submenu = null;
		for (var i = 0; i < divs.length; i++) {
			var e = divs[i];
			if (e.hasClass('submenu')) {
				submenu = e;
				break;
			}
		}
		if (!submenu) {
			return;
		}

		this.submenu = submenu;
		this.tr = new Element('div', {'class': 'tr'}).injectInside(this.submenu);
		this.br = new Element('div', {'class': 'br'}).injectInside(this.submenu);
		this.b = new Element('div', {'class': 'b'}).injectInside(this.submenu);
		this.bl = new Element('div', {'class': 'bl'}).injectInside(this.submenu);

		this.isIE = /MSIE/.test(navigator.userAgent);
		this.isIE7 = /MSIE\s7/.test(navigator.userAgent);

		if (this.isIE) {
			if (this.isIE7) {
				this.prepareForIe7();
			}
			else {
				this.prepareForIe6();
			}

			//need mouseover for ie7 as well because display:none might have been
			//applied with js
			this.element.addEvent('mouseenter', function (evt) {
				this.show = true;
				this.mouseOver();
			}.bindWithEvent(this));
			this.element.addEvent('mouseleave', function (evt) {
				this.show = false;
				this.mouseOut.delay(50, this);
			}.bindWithEvent(this));
		}
	},
	prepareForIe7: function () {
		var dims = this.submenu.getDimensions();
		this.submenu.getElements('li').each(function (e) {
			e.setStyles({
				width: dims.width - 7
			});
		});
		this.b.setStyles({
			width: (dims.width - 15) + 'px'
		});
	},
	prepareForIe6: function () {
		var dims = this.submenu.getDimensions();

		this.element.getElements('span').each(function (e) {
			e.addEvent('mouseenter', function (evt) { evt.target.addClass('hover'); });
			e.addEvent('mouseleave', function (evt) { evt.target.removeClass('hover'); });
		});

		this.submenu.getElements('li').each(function (e) {
			e.setStyles({
				width: dims.width - 7
			});
		});

		this.r = new Element('div', {'class': 'r'}).injectInside(this.submenu);

		this.tr.setStyles({
			right: '',
			left: (dims.width - 7) + 'px'
		});
		this.r.setStyles({
			right: '',
			left: (dims.width - 7) + 'px',
			height: (dims.height - 16) + 'px'
		});
		this.br.setStyles({
			bottom: '',
			top: (dims.height - 8) + 'px',
			right: '',
			left: (dims.width - 7) + 'px'
		});
		this.bl.setStyles({
			bottom: '',
			top: (dims.height - 8) + 'px'
		});
		this.b.setStyles({
			width: (dims.width - 14) + 'px'
		});
	},
	mouseOver: function() {
		this.submenu.setStyles({display: 'block'});

		if (this.isIE7) {
			$(this.element).setStyles({'float': 'left'});
		}
	},
	mouseOut: function() {
		if (!this.show) {
			this.submenu.setStyles({display: 'none'});
		}
	}
});


var QuotesDisplayer = new Class({
	elements: null,
	current: 0,
	moving: false,
	options: {
		delay: 5000,
		selector: 'li',
		onStart: Class.empty,
		onComplete: Class.empty
	},
	initialize: function(element, options) {
		this.setOptions(options);

		element = $(element);
		if (!element) {
			return;
		}

		// get the elements
		this.elements = element.getElements(this.options.selector);

		// hide them all
		this.elements.each(function(e) {
			//e.setStyle('display', 'none');
			//e.setStyle('visibility', 'hidden');
			e.setStyles({
				display: 'none',
				visibility: 'hidden',
				opacity: '0'
			});
		});

		this.next();
	},
	onHideStart: function (e) {
		this.moving = true;
	},
	onHideComplete: function (e) {
		e.setStyles({
			display: 'none',
			visibility: 'hidden'
		});

		this.moving = false;
		this.fireEvent('onComplete', this.current, 10);
		this.next.delay(10, this);
		//this.next.delay(this.options.delay, this);
	},
	onShowStart: function (e) {
		e.setStyles({
			display: 'block',
			visibility: 'visible'
		});
		
		//reposition copy if too long
		var copy = e.getFirst();
		if (copy) {
			if (copy.innerHTML.length > 200) {
				e.getParent().setStyle('margin-top', '15px');
			}
			else {
				e.getParent().setStyle('margin-top', '30px');
			}
		}

		this.moving = true;
		this.fireEvent('onStart', this.current, 10);
	},
	onShowComplete: function (e) {
		this.moving = false;
		this.hide.delay(this.options.delay, this);
	},
	hide: function () {
		if (this.moving) {
			return;
		}
		var element = this.elements[this.current - 1];
		options = this.options;
		options.onStart = this.onHideStart.bind(this);
		options.onComplete = this.onHideComplete.bind(this);
		var fx = new Fx.Style(element, 'opacity', options).start(1, 0);
	},
	show: function (quote) {
		if (quote < 1 || quote > this.elements.length || this.moving) {
			return;
		}

		this.current = quote;
		var element = this.elements[this.current - 1];
		options = this.options;
		options.onStart = this.onShowStart.bind(this);
		options.onComplete = this.onShowComplete.bind(this);
		var fx = new Fx.Style(element, 'opacity', options).start(0, 1);
	},
	next: function () {
		var quote = this.current + 1;
		this.show(quote > this.elements.length ? 1 : quote)
	}
});
QuotesDisplayer.implement(new Events, new Options);



var ImagesDisplayer = new Class({
	es: new Array(),
	initialize: function(es) {
		this.es = es;

		this.es.each(function(e) {
			this.showRandomImage(e);
		}, this);
	},
	showRandomImage: function(imageInfo) {
		if ($(imageInfo.container) && imageInfo.images.length > 0) {
			imgs = $ES('img', imageInfo.container);
			if (imgs.length > 0) {
				var r = Math.floor(Math.random() * imageInfo.images.length);
				$(imgs[0]).setProperty('src', imageInfo.images[r].src);
				$(imgs[0]).setProperty('alt', imageInfo.images[r].title);
				$(imgs[0]).setProperty('width', imageInfo.images[r].width);
				$(imgs[0]).setProperty('height', imageInfo.images[r].height);
			}
		}
	}
});

var MyForm = new Class({
	f: null,
	defaultValues: {},
	initialize: function (f) {
		this.f = $(f);
		if (!this.f) return;
		this.setDefaultValues();
	},
	setDefaultValues: function() {
		this.defaultValues = {};
		$ES('input', this.f).each(function(i) {
			//eval('this.defaultValues.' + $(i).id + ' = $(i).value;');
			var defaultValue = $(i).value;

			$(i).addEvent('focus', function(evt, def) {
				if (this.value == def) this.value = '';
			}.bindWithEvent(i, defaultValue));

			$(i).addEvent('blur', function(evt, def) {
				if (this.value == '') this.value = def;
			}.bindWithEvent(i, defaultValue));
		}, this);
	}
});

function checkForm(f) {
	var errorMsg;

	var badFields = getBadFields(f);

	var badFieldsLength = badFields.length;
	if (badFieldsLength > 0) {
		errorMsg = 'Please fill in the following fields:\n';
		for (i = 0; i < badFieldsLength; i++) {
			errorMsg += '- ' + badFields[i].name + '\n';
		}
		// damn FF bug 236791
		document.getElementById(badFields[0].id).setAttribute('autocomplete', 'off');
		document.getElementById(badFields[0].id).focus();
		document.getElementById(badFields[0].id).setAttribute('autocomplete', '');
		alert(errorMsg);
		return false;
	}
	else {
		return true;
	}
}

function getBadFields(f) {
	var badFields = new Array();

	switch(f) {
		case 'newsletterForm':
			if (document.getElementById('fldName').value == '' || document.getElementById('fldName').value == 'Name') {
				badFields.push({id: 'fldName', name: 'name'});
			}
			if (document.getElementById('fldEmail').value == '' || document.getElementById('fldEmail').value == 'Email') {
				badFields.push({id: 'fldEmail', name: 'email'});
			}
			break;
	}

	return badFields;
}

