/**************
 * ESTATE MAP
 **************/
var Region = Class.create();
Region.prototype = {
	titleHeight:12,
	initialize: function(li, zone) {
		this.id = li.id.substring(23);
		this.zone = zone;
		this.selected = false;
		this.mapRegion = new MapRegion(this, li);
		this.listRegions = [];
		if ($('estate_list_region_'+this.id)) {
			this.listRegions.push(new ListRegion(this, this.id));
		}
		$('estate_list_zone_'+this.zone.id).down('.estate_list_regions').childElements().each(function(r) {
			var id = r.id.substring(19);
			if (id < 0) {
				if (r.down('.estate_list_estates').childElements().find(function(e) {
					return e.readAttribute('rel').split(',').include(this.id);
				}.bind(this))) this.listRegions.push(new ListRegion(this, id));
			}
		}.bind(this));
		
	},
	show: function() {
		this.listRegions.invoke('show');
	},
	hide: function() {
		this.listRegions.invoke('hide');
	},
	select: function() {
		if (!this.selected) {
			this.zone.deselectRegions();
			this.mapRegion.select();
			this.listRegions.invoke('select');
			this.selected = true;
		}
	},
	deselect: function() {
		if (this.selected) {
			this.mapRegion.deselect();
			this.listRegions.invoke('deselect');
			this.selected = false;
		}
	}
};
var MapRegion = Class.create();
MapRegion.prototype = {
	initialize: function(region, li) {
		this.region = region;
		this.link = li.down();
		Event.observe(this.link, 'click', this.showClick.bindAsEventListener(this));
	},
	showClick: function(event) {
		Event.stop(event);
		if (this.region.selected) {
			this.region.deselect();
		} else {
			this.region.select();
		}
	},
	select: function() {
		this.link.setStyle('color:#fff;');
	},
	deselect: function() {
		this.link.setStyle('color:#ccc;');
	}
};
var ListRegion = Class.create();
ListRegion.prototype = {
	titleHeight:12,
	initialize: function(region, id) {
		this.region = region;
		this.el = $('estate_list_region_'+id);
		this.title = this.el.down('h4');
		this.content = this.el.down('.estate_list_estates');
	},
	show: function() {
		// indent estates list
		this.content.morph('padding-left:10px;', { duration:0.5, queue:{ scope:'zonescope'+this.region.zone.id } });
		// make space for title
		this.title.morph('height:'+this.titleHeight+'px;', { duration:0.5, queue:{ scope:'zonescope'+this.region.zone.id } });
		// show title
		new Effect.Appear(this.title.down(), { duration:0.5, queue:{ scope:'zonescope'+this.region.zone.id } });
		// darken estates links
		Effect.multiple(this.content.select('a'), Effect.Morph, {
			style:'color:#666;',
			duration:0.5
		});
	},
	hide: function() {
		// un-indent estates list
		this.content.morph('padding-left:0px;', { duration:0.5, queue:{ scope:'zonescope'+this.region.zone.id } });
		// remove space for title
		this.title.morph('height:0px;', { duration:0.5, queue:{ scope:'zonescope'+this.region.zone.id } });
		// hide title
		new Effect.Fade(this.title.down(), { duration:0.5, queue:{ scope:'zonescope'+this.region.zone.id } });
		// lighten estates links
		Effect.multiple(this.content.select('a'), Effect.Morph, {
			style:'color:#999;',
			duration:0.5
		});
	},
	select: function() {
		// darken title
		this.title.down().setStyle('color:#666;');
		// darken estates links
		this.content.select('a').invoke('setStyle', 'color:#000;');
	},
	deselect: function() {
		// lighten title
		this.title.down().setStyle('color:#ccc;');
		// lighten estates links
		this.content.select('a').invoke('setStyle', 'color:#666;');
	}
};
var Zone = Class.create();
Zone.prototype = {
	titleHeight:24,
	initialize: function(overlay, map) {
		this.id = overlay.id.substring(16);
		this.regions = [];
		overlay.down('.estate_map_list_regions').childElements().each(function(li) {
			this.regions.push(new Region(li, this));
		}.bind(this));
		this.selected = false;
		this.mapZone = new MapZone(this, overlay, map);
		this.listZone = new ListZone(this);
	},
	select: function() {
		if (!this.selected) {
			Effect.Queues.get('zonescope'+this.id).each(function(effect) { effect.cancel(); });
			this.mapZone.open();
			this.listZone.open();
			this.selected = true;
		}
	},
	deselect: function(event) {
		if (this.selected) {
			Effect.Queues.get('zonescope'+this.id).each(function(effect) { effect.cancel(); });
			this.mapZone.close();
			this.listZone.close();
			this.deselectRegions();
			this.selected = false;
		}
	},
	deselectRegions: function() {
		this.regions.invoke('deselect');
	}
};
var MapZone = Class.create();
MapZone.prototype = {
	initialize: function(zone, overlay, map) {
		this.zone = zone;
		this.map = map;
		this.el = overlay;
		this.off = overlay.down('.estate_map_zone_off');
		this.on = overlay.down('.estate_map_zone_on');
		this.content = overlay.down('.estate_map_m');
		this.on.show();
		var outerDimensions = this.on.getDimensions();
		this.dimensions = this.content.getDimensions();
		this.on.hide();
		var offset = this.el.positionedOffset();
		// if top position + height > map height, align bottom
		if (offset.top + outerDimensions.height > this.map.dimensions.height) {
			this.on.setStyle('bottom:6px;');
		} else {
			this.on.setStyle('top:-6px;');
		}
		// if left position + width > map width, align right
		if (offset.left + outerDimensions.width > this.map.dimensions.width) {
			this.on.setStyle('right:6px;');
		} else {
			this.on.setStyle('left:-6px;');
		}
		this.content.setStyle('width:13px;height:5px;');
		this.content.down('.zone_content').hide();
		Event.observe(this.off.down('a.estate_map_open'), 'click', this.openClick.bindAsEventListener(this));
		Event.observe(this.on.down('a.estate_map_close'), 'click', this.closeClick.bindAsEventListener(this));
	},
	openClick: function(event) {
		Event.stop(event);
		this.zone.select();
	},
	closeClick: function(event) {
		Event.stop(event);
		this.zone.deselect();
	},
	open: function() {
		this.bringToFront();
		this.map.deselectAll();
		this.show();
	},
	close: function() {
		this.hide();
	},
	show: function() {
		// hide button and show overlay
		this.off.hide(); this.on.show();
		// expand overlay
		new Effect.Morph(this.content, {
			style:'width:'+this.dimensions.width+'px;height:'+this.dimensions.height+'px;',
			duration:0.25,
			queue:{ position:'front', scope:'zonescope'+this.zone.id }
		});
		// show content
		new Effect.Appear(this.content.down('.zone_content'), {
			duration:0.25,
			queue:{ position:'end', scope:'zonescope'+this.zone.id }
		});
	},
	hide: function() {
		// hide content
		new Effect.Fade(this.content.down('.zone_content'), {
			duration:0.25,
			queue:{ position:'front', scope:'zonescope'+this.zone.id }
		});
		// shrink overlay, hide overlay and show button when finished
		new Effect.Morph(this.content, {
			style:'width:13px;height:5px;',
			duration:0.25,
			queue:{ position:'end', scope:'zonescope'+this.zone.id },
			afterFinish: function(){ this.on.hide(); this.off.show(); }.bind(this)
		});
	},
	bringToFront: function() {
		var parentNode = $('estate_map');
		this.el.remove();
		parentNode.appendChild(this.el);
	}
};
var ListZone = Class.create();
ListZone.prototype = {
	titleHeight:24,
	initialize: function(zone) {
		this.zone = zone;
		this.el = $('estate_list_zone_'+this.zone.id);
		this.title = this.el.down('h3');
		this.content = this.el.down('.estate_list_regions');
	},
	open: function() {
		this.show();
	},
	close: function(event) {
		this.hide();
	},
	show: function() {
		// indent regions list
		this.content.morph('padding-left:10px;padding-bottom:8px;', { duration:0.5, queue:{ scope:'zonescope'+this.zone.id } });
		// make space for zone title
		this.title.morph('height:'+this.titleHeight+'px;', { duration:0.5, queue:{ scope:'zonescope'+this.zone.id } });
		// show zone title
		new Effect.Appear(this.title.down(), { duration:0.5, queue:{ scope:'zonescope'+this.zone.id } });
		this.zone.regions.invoke('show');
	},
	hide: function() {
		// un-indent regions list
		this.content.morph('padding-left:0px;padding-bottom:0px;', { duration:0.5, queue:{ scope:'zonescope'+this.zone.id } });
		// remove space for zone title
		this.title.morph('height:0px;', { duration:0.5, queue:{ scope:'zonescope'+this.zone.id } });
		// hide zone title
		new Effect.Fade(this.title.down(), { duration:0.5, queue:{ scope:'zonescope'+this.zone.id } });
		this.zone.regions.invoke('hide');
	}
};
var Map = Class.create();
Map.prototype = {
	initialize: function() {
		// setup overlays
		this.dimensions = $('estate_map').getDimensions();
		this.zones = [];
		$$('.estate_map_zone').each(function(overlay) {
			this.zones.push(new Zone(overlay, this));
		}.bind(this));
	},
	deselectAll: function() {
		this.zones.invoke('deselect');
	}
};

/**************
 * SLIDESHOW
 **************/
var Slideshow = Class.create();
Slideshow.prototype = {
	images: [],
	paused: false,
	last_index: -1,
	width:0,
	height:0,
	initialize: function(container, controls) {
		this.container = $(container);
		this.controls = $(controls);
		this.setup();
	},
	setup: function() {
		this.width = this.container.getWidth();
		this.height = this.container.getHeight();
		this.container.setStyle({ 'width': this.width + 'px', 'height': this.height + 'px' });
		this.container.childElements().each(function(li){
			li.setStyle({'position':'absolute', 'top':0, 'left':0});
			this.add(li);
		}.bind(this));
		this.controls.insert({after:'<div class="corners tl"></div><div class="corners tr"></div><div class="corners bl"></div><div class="corners br"></div>'});
		this.index = 0;
		if (this.images.length > 1) {
			Event.observe(this.controls.down('.prev a'), 'click', function(event){ this.slide(-1); Event.stop(event); }.bindAsEventListener(this));
			Event.observe(this.controls.down('.next a'), 'click', function(event){ this.slide(1); Event.stop(event); }.bindAsEventListener(this));
			Event.observe(this.controls.down('.zoom a'), 'click', function(event){
				window.lightbox.start(this.images[this.index].down());
				Event.stop(event);
			}.bindAsEventListener(this));
			this.controls.show();
		}
	},
	add: function(image_container) {
		var image = image_container.down('img');
		var i = new Image();
		i.src = image.src;
		this.images.push(image_container);
	},
	slide: function(dir) {
		if (this.last_index >= 0) {
			this.last_fade.cancel();
			this.last_appear.cancel();
			this.images[this.last_index].hide();
		}
		this.last_index = this.index;
		var old_d = this.images[this.index];
		this.index = ( this.index + this.images.length + dir ) % this.images.length;
		var new_d = this.images[this.index];
		this.last_fade = Effect.Fade(old_d);
		this.last_appear = Effect.Appear(new_d);
	}
}

/**************
 * INFO SHEET BUILDER
 **************/

var InfoSheet = Class.create();
InfoSheet.prototype = {
	estate: null,
	wine: null,
	vintage: null,
	initialize: function(form, args) {
		this.form = form;
		this.wineSet = this.form.down('div.info_wine');
		Event.observe(this.wineSet.down('select'), 'change', this.wineOption.bind(this));
		this.vintageSet = this.form.down('div.info_vintage');
		Event.observe(this.vintageSet.down('select'), 'change', this.vintageOption.bind(this));
		this.reviewsSet = this.form.down('div.info_reviews');
		this.estate = Estates.find(function(e){ return e.value==args.estate_id; });
		if (this.estate) {
			this.showOptions(this.wineSet, this.estate.wines, 0);
		}
		if (args.closeButton) {
			Event.observe(args.closeButton, 'click', this.close.bind(this));
		}
	},
	showOptions: function(set, opts, sel) {
		var el = set.down('select');
		el.options.length = 0;
		var count = 0;
		el.options[0] = new Option('-- Please Select --', 0, false, false);
		count++;
		for(var i=0; i<opts.length; i++) {
			if (opts[i].publish)
				el.options[count++] = new Option(opts[i].text, opts[i].value, false, (sel == opts[i].value));
		}
		set.appear({ duration: 0.5 });
	},
	showInputs: function(set, opts, sel) {
		var tbody = set.down('tbody');
    while (tbody.down()) tbody.down().remove();
		if (opts.length == 0) {
			set.down('thead').hide();
		} else {
			set.down('thead').show();
		}
		var count = 0;
		for(var i=0; i<opts.length; i++) {
			if (opts[i].publish) {
				tbody.insert({ bottom:'<tr' + ((count++==0) ? ' class="first"':'') + '><td class="checks"><input type="checkbox" name="info_reviews[]" value="' + opts[i].value + '" /></td><td>' + opts[i].text + '</td></tr>' });
			}
		}
		set.appear({ duration: 0.5 });
	},
	wineOption: function() {
		var opt = this.wineSet.down('select').value;
		this.reviewsSet.fade({ duration: 0.5 });
		this.wine = this.estate.wines.find(function(e){ return e.value==opt; });
		if (this.wine) {
			this.showOptions(this.vintageSet, this.wine.vintages, 0);
		} else {
			this.vintageSet.fade({ duration: 0.5 });
		}
	},
	vintageOption: function(opt) {
		var opt = this.vintageSet.down('select').value;
		if (!this.wine) {
			this.vintageSet.fade({ duration: 0.5 });
			this.reviewsSet.fade({ duration: 0.5 });
		} else {
			this.vintage = this.wine.vintages.find(function(e){ return e.value==opt; });
			if (this.vintage) {
				this.showInputs(this.reviewsSet, this.vintage.reviews, 0);
			} else {
				this.reviewsSet.fade({ duration: 0.5 });
			}
		}
	},
	close: function() {
		this.wineSet.down('select').selectedIndex = 0;
		this.wineOption();
	}
}

/**************
 * PORTFOLIO BUILDER
 **************/

var PortfolioElement = Class.create({
	initialize: function(parent, object) {
		this.isOpen = false;
		this.isSetup = false;
		this.children = [];
		this.parent = parent;
		this.object = object;
		this.container = new Element('li').update(new Element('a', { href: '#', onClick: 'return false;' }).update(this.object.text));
		Event.observe(this.container.down(), 'click', this.toggle.bind(this));
		this.parent.childList.insert(this.container);
	},
	toggle: function(e, quick) {
		if (e && e.stop) {
			this.deselectGlobals();
			e.stop();
		} else quick = e;
		if (this.isOpen) {
			this.children.each(function(c){ if(c.isOpen) c.toggle(quick); });
			this.container.removeClassName('selected');
			if (quick) this.childListContainer.hide();
			else this.childListContainer.slideUp({ duration: 0.25 });
			this.isOpen = false;
		} else {
			if (!this.isSetup) this.setup();
			this.container.addClassName('selected');
			if (quick) this.childListContainer.show();
			else this.childListContainer.slideDown({ duration: 0.25 });
			this.isOpen = true;
		}
	},
	setup: function() {
		this.container.insert('<div style="display:none;"><ul></ul></div>');
		this.childListContainer = this.container.down('div');
		this.childList = this.childListContainer.down();
		this.isSetup = true;
	},
	deselectGlobals: function() {
		if (this.container.hasClassName('pb_estate')) {
			this.container.down('.estate_actions').down('input.all').checked = false;
			this.container.down('.estate_actions').down('input.current').checked = false;
		}
		if (this.parent.deselectGlobals) this.parent.deselectGlobals();
	}
});
var PortfolioReview = Class.create(PortfolioElement, {
	initialize: function($super, parent, review) {
		$super(parent, review);
		this.container.addClassName('pb_review').update('<table><tbody><tr><td class="check"><input type="checkbox" name="reviews[' + review.value + ']" /></td><td class="content">' + review.text + '</td></tr></tbody></table>');
		Event.observe(this.container.down('td.content'), 'click', this.toggle.bind(this));
		Event.observe(this.container.down('input'), 'change', this.toggle.bind(this));
	},
	toggle: function($super, quick) {
		if (this.isOpen) {
			this.container.removeClassName('selected');
			this.container.down('input').checked = false;
			this.isOpen = false;
		} else {
			this.container.addClassName('selected');
			this.container.down('input').checked = true;
			this.isOpen = true;
		}
	}
});
var PortfolioVintage = Class.create(PortfolioElement, {
	initialize: function($super, parent, vintage) {
		if (parseInt(vintage.text) > 0) vintage.text += ' Vintage';
		else if (vintage.text.toUpperCase() == 'NV') vintage.text = 'Non-Vintage';
		else if (vintage.text.toUpperCase() == 'MV') vintage.text = 'Multi-Vintage';
		$super(parent, vintage);
		this.container.addClassName('pb_vintage').insert({ top: '<input type="checkbox" name="vintages[' + vintage.value + ']" />'});
		Event.observe(this.container.down('input'), 'change', this.toggle.bind(this));
	},
	setup: function($super) {
		$super();
		this.object.reviews.each(function(r) { if (r.publish) this.children.push(new PortfolioReview(this, r)); }.bind(this));
		if (this.children.length > 0) this.children[0].container.addClassName('first');
	},
	toggle: function($super, quick) {
		if (this.isOpen) {
			this.container.down('input').checked=false;
		} else {
			this.container.down('input').checked=true;
		}
		$super(quick);
	}
});
var PortfolioWine = Class.create(PortfolioElement, {
	initialize: function($super, parent, wine) {
		$super(parent, wine);
		this.container.addClassName('pb_wine');
	},
	setup: function($super) {
		$super();
		this.object.vintages.each(function(v) { if (v.publish) this.children.push(new PortfolioVintage(this, v)); }.bind(this));
	}
});
var PortfolioEstate = Class.create(PortfolioElement, {
	initialize: function($super, parent, estate) {
		$super(parent, estate);
		this.container.addClassName('pb_estate').insert('<span class="estate_actions"><input type="checkbox" class="all" name="estate_all[' + estate.value + ']"/> all vintages <input type="checkbox" class="current" name="estate_current[' + estate.value + ']"/> current vintages <input type="checkbox" class="reviews" name="estate_reviews[' + estate.value + ']"/> all reviews </span>');
		Event.observe(this.container.down('.estate_actions').down('input.all'), 'click', this.toggleVintages.curry('all').bind(this));
		Event.observe(this.container.down('.estate_actions').down('input.current'), 'click', this.toggleVintages.curry('current').bind(this));
		Event.observe(this.container.down('.estate_actions').down('input.reviews'), 'click', this.toggleVintages.curry('reviews').bind(this));
	},
	setup: function($super) {
		$super();
		this.object.wines.each(function(w) { if (w.publish && w.vintages.length>0) this.children.push(new PortfolioWine(this, w)); }.bind(this));
	},
	toggleVintages: function(which) {
		if (!this.isSetup) this.setup();
		if (this.container.down('.estate_actions').down('input.'+which).checked) {
			if (which!='all') this.container.down('.estate_actions').down('input.all').checked = false;
			if (which!='current') this.container.down('.estate_actions').down('input.current').checked = false;
			if (which!='reviews') this.container.down('.estate_actions').down('input.reviews').checked = false;
			this.children.each(function(wine){
				if (!wine.isSetup) wine.setup();
				if (which == 'current') {
					wine.children.each(function(vintage){
						if (!vintage.isSetup) vintage.setup();
						if (!vintage.isOpen && vintage==this.children[0]) vintage.toggle(true);
						else if (vintage.isOpen && vintage!=this.children[0]) vintage.toggle(true);
					}.bind(wine));
				} else {
					wine.children.each(function(vintage){
						if (!vintage.isSetup) vintage.setup();
						if (!vintage.isOpen) vintage.toggle(true);
						if (which == 'reviews') vintage.children.each(function(review){
							if (!review.isSetup) review.setup();
							if (!review.isOpen) review.toggle(true);
						});
					});
				}
				if (!wine.isOpen) wine.toggle(true);
			});
			if (!this.isOpen) this.toggle();
			this.container.down('.estate_actions').down('input.'+which).checked = true;
		} else {
			if (this.isOpen) this.toggle();
			this.children.each(function(wine){
				if (!wine.isSetup) wine.setup();
				if (wine.isOpen) wine.toggle(true);
				if (which == 'current') {
					wine.children.each(function(vintage){
						if (!vintage.isSetup) vintage.setup();
						if (vintage.isOpen && vintage==this.children[0]) vintage.toggle(true);
						else if (!vintage.isOpen && vintage!=this.children[0]) vintage.toggle(true);
					}.bind(wine));
				} else {
					wine.children.each(function(vintage){
						if (!vintage.isSetup) vintage.setup(); if (vintage.isOpen) vintage.toggle(true);
						if (which == 'reviews') vintage.children.each(function(review){
							if (!review.isSetup) review.setup(); if (review.isOpen) review.toggle(true);
						});
					});
				}
			});
		}
	}
});
var PortfolioBuilder = Class.create();
PortfolioBuilder.prototype = {
	children: [],
	initialize: function(action, container) {
		this.container = container;
		this.container.insert('<form method="POST" action="' + action + '"><ul></ul></form>');
		this.childList = this.container.down('ul');
		Estates.each(function(e) { if (e.publish && e.wines.length>0) this.children.push(new PortfolioEstate(this, e)); }.bind(this));
		this.container.insert({ top:'<span class="portfolio_actions"><input type="submit" class="submit" name="portfolio_create" value="Create"/> <input type="checkbox" class="all" name="portfolio_all"/> all vintages <input type="checkbox" class="current" name="portfolio_current"/> current vintages <input type="checkbox" class="reviews" name="portfolio_reviews"/> all reviews </span>' });
		Event.observe(this.container.down('.portfolio_actions').down('input.submit'), 'click', this.validate.bindAsEventListener(this));
		Event.observe(this.container.down('.portfolio_actions').down('input.all'), 'click', this.toggleVintages.curry('all').bind(this));
		Event.observe(this.container.down('.portfolio_actions').down('input.current'), 'click', this.toggleVintages.curry('current').bind(this));
		Event.observe(this.container.down('.portfolio_actions').down('input.reviews'), 'click', this.toggleVintages.curry('reviews').bind(this));
	},
	toggleVintages: function(which) {
		if (this.container.down('.portfolio_actions').down('input.'+which).checked) {
			if (which!='all') this.container.down('.portfolio_actions').down('input.all').checked = false;
			if (which!='current') this.container.down('.portfolio_actions').down('input.current').checked = false;
			if (which!='reviews') this.container.down('.portfolio_actions').down('input.reviews').checked = false;
			this.children.each(function(e){ e.container.down('.estate_actions').down('input.'+which).checked = true; });
			this.children.each(function(e){ e.toggleVintages(which); });
			//recheck
			this.container.down('.portfolio_actions').down('input.'+which).checked = true;
		} else {
			this.children.each(function(e){ e.container.down('.estate_actions').down('input.'+which).checked = false; });
			this.children.each(function(e){ e.toggleVintages(which); });
		}
	},
	deselectGlobals: function() {
		this.container.down('.portfolio_actions').down('input.all').checked = false;
		this.container.down('.portfolio_actions').down('input.current').checked = false;
	},
	validate: function(e) {
		var somethingChecked = this.children.find(function(e){ return e.children.find(function(w){ return w.children.find(function(v){ return v.isOpen; }); }); });
		if (somethingChecked) {
			this.container.down('form').submit();
		} else {
			alert("Please select one or more vintages to continue");
			e.stop();
		}
	}
}

Subscribe = {
	contentHeight: 0,
	contentWidth: 235,
	form: null,
	start: function() {
		this.form = $('subscribe').down('form');
		Event.observe($('subscribe_open'), 'click', this.open.bindAsEventListener(this));
		Event.observe($('subscribe_close'), 'click', this.close.bindAsEventListener(this));
		Event.observe(this.form, 'submit', this.submit.bindAsEventListener(this), true);
		$('subscribe').setStyle('width:'+this.contentWidth+'px;');
		this.contentHeight = $('subscribe_content').show().getHeight();
		$('subscribe_content').hide();
		$('subscribe_on').hide();
		$('subscribe').setStyle('top:0px;width:81px;');
		$('subscribe').hide();
		Effect.SlideDown('subscribe', {duration:0.5});
	},
	open: function(e) {
		Event.stop(e);
		// hide button and show overlay
		$('subscribe_off').hide(); $('subscribe_on').show();
		// expand overlay
		new Effect.Morph($('subscribe'), {
			style:'width:'+this.contentWidth+'px;',
			duration:0.25,
			queue:{ scope:'subscribe' }
		});
		new Effect.Morph($('subscribe_m'), {
			style:'height:' + this.contentHeight + 'px;',
			duration:0.25,
			queue:{ scope:'subscribe' }
		});
		// show content
		new Effect.Appear($('subscribe_content'), {
			duration:0.25,
			queue:{ position:'end', scope:'subscribe' }
		});
	},
	close: function(e) {
		Event.stop(e);
		// hide content
		new Effect.Fade($('subscribe_content'), {
			duration:0.25,
			afterFinish: function(){
				// shrink overlay, hide overlay and show button when finished
				new Effect.Morph($('subscribe_m'), {
					style:'height:16px;',
					duration:0.25
				});
				new Effect.Morph($('subscribe'), {
					style:'width:81px;',
					duration:0.25,
					afterFinish: function(){ $('subscribe_on').hide(); $('subscribe_off').show(); }.bind(this)
				});
			}
		});
	},
	submit: function(e) {
		if (!this.hasValidEmail()) {
			var emailField = this.form.findFirstElement();
			emailField.value = 'Email is required';
			emailField.focus();
			new Effect.Highlight(emailField, { duration:2.0 });
			Event.stop(e);
		}
	},
	hasValidEmail: function() {
		var email = this.form.findFirstElement().value;
		var filter = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		if (email.length == 0 || !filter.test(email)) {
			return false;
		}
		return true;
	}
}
Event.observe(window, 'load', function() {
//	Subscribe.start();
});

function showCredits() {
	$('credits').clonePosition($('credits_popper'), { offsetLeft:$('credits_popper').getWidth(), offsetTop:-64 });
	$('credits').appear({duration:0.25});
}
function hideCredits() {
	$('credits').fade({duration:0.25});
}
