function prepNav() {
	if (!$defined($('main-nav')))
		return;
	
	$('main-nav').removeClass('hide');

	var topNav = $('drop-nav-list');
	var topNavSize = null;
	var topNavFx = new Fx.Morph(topNav, {
		duration: 500
	});

	window.addEvent('load', function() {
		topNavSize = topNav.getSize();
	});


	var active = null;

	var uls = topNav.getElements('ul');

	uls.each(function(ul, i) {
		var opener = ul.getParent().getElement('a');
		var origSize = ul.getSize();

		var fx = new Fx.Morph(ul, {
			duration: 500
		});

		opener.addEvent('click', function(e) {
			if (!$defined(active) || active !== i)
				e.stop();

			if ($defined(active)) {
				var fxActive = new Fx.Morph(uls[active], {
					duration: 250
				});

				fxActive.start({
					height: 0
				});
			}

			active = i;
			ul.setStyle('display', 'block');

			fx.start({
				height: [0, origSize.y]
			}).chain(function() {

			});
		});

		ul.setStyles({
			display: 'none',
			overflow: 'hidden',
			height: 0
		});
	});

	var navigate = $('main-nav');

	function positionNav () {
		var topOffset = 6;

		if (Browser.Engine.trident) {
			topOffset = 36;
		}

		topNav.setStyles({
			left: navigate.getPosition($('container')).x+8,
			top: navigate.getPosition($('container')).y+topOffset
		});
	};

	navigate.addEvent('click', function() {
		topNav.toggleClass('not-visible');

		function close (e) {
			if (e.target.getParents().match('ul#drop-nav-list').indexOf(true) === -1) {
				topNav.addClass('not-visible');
				$(document).removeEvent(this);
			}
		}

		$(document).addEvent('click', close);

		positionNav();

		return false;
	});
}

window.addEvent('domready', function() {
	var bodyId = $(document.body).get('id');

	// Prepare navigation
	prepNav();

	if (bodyId === 'home' || bodyId === 'honeybee') {
		// Inject edible logo flash
		if ($defined($('edible-logo'))) {
			var logoSwf = new Swiff('/static/swf/ediblelogo.swf', {
				width: 300,
				height: 400
			});

			$('edible-logo').adopt(logoSwf);
		}
		
		// Inject petition flash
		if ($defined($('petition'))) {
			var petitionSwf = new Swiff('/static/swf/petition.swf', {
				width: 245,
				height: 180
			});

			$('petition').adopt(petitionSwf);
		}
		

	}
	if (bodyId === 'honeybee') {
	// Inject bee flash
	if ($defined($('honey-bee'))) {
		var beeSwf = new Swiff('/static/swf/bee.swf', {
			width: 980,
			height: 100
		});

		$('honey-bee').adopt(beeSwf);
	}
}

	if (bodyId === 'screensaver') {
		// Inject screensaver flash
		if ($defined($('screensaverSmall'))) {
			var screensaverSwf = new Swiff('/static/swf/screensaver_small.swf', {
				width: 400,
				height: 320
			});

			$('screensaverSmall').adopt(screensaverSwf);
		}

	}
	
	if (bodyId === 'about' || bodyId === 'press') {
		// Inject simpleviewer flash
		if ($defined($('simpleviewer'))) {
			var simpleviewerSwf = new Swiff('/static/swf/simpleviewer.swf', {
				width: 535,
				height: 785,
				vars: {
					xmlDataPath: '/static/script/flickrViewer.php?setid=72157615313734715'
				}
			});

			$('simpleviewer').adopt(simpleviewerSwf);
		}

	}

	// Add tab functionality
	if ($defined($('nav-tabs'))) tabs.prep();

	var topnav = $('top-nav');

	if ($defined(topnav)) {
		topnav.getElements('option').each(function(option) {
			if (window.location.href === option.get('value'))
				option.set('selected', 'selected');
		});
		topnav.addEvent('change', function () {
			var newloc = topnav.get('value');

			if (window.location.href !== newloc)
				window.location = newloc;
		});
	}

});

window.addEvent('load', function() {
	var topPromo = $('top-promo');
	// Make all columns equal height
	if ($defined(topPromo)) {
		var largest = 0;

		var cols = topPromo.getElements('.col');
		
		cols.each(function(elem) {
			var height = elem.getSize().y;
			largest = (height > largest) ? height : largest;
		});

		cols.each(function(elem) {
			elem.setStyle('height', largest);
		});
	}

	// Collapse footer nav
	var footernav = $('footer-nav');
	if ($defined(footernav)) {
		// Make external links open in new window
		footernav.getElements('a').each(function (a, i) {
			if (a.get('href').indexOf('http://') !== -1 || a.get('text').toLowerCase() === 'international site') {
				a.set('target', '_blank');
			}
		});

		footernav.addClass('close');
		var footFx = new Fx.Morph(footernav, {
			duration: 250,
			transition: Fx.Transitions.Elastic.easeOut
		});

		var footShow = true;
		var footPadding = footernav.getStyles('padding-top', 'padding-bottom');
		footPadding = footPadding['padding-top'].toInt(10)+footPadding['padding-bottom'].toInt(10);
		var footSize = footernav.getSize().y-footPadding;

		var close = new Element('a', {
			href: '#',
			text: 'Close',
			'class': 'close'
		});

		close.addEvent('click', function (){
			var size = 0;
			var show = !footShow;

			if (!footShow) {
				size = footSize;
				show = true;
			}

			footFx.start({
				height: size
			}).chain(function () {
				close.toggleClass('open');
				footernav.getElements('div').toggleClass('hide');
				footShow = show;
			});

			return false;
		});

		close.inject(footernav, 'top');
	}
});

var tabs = {
	activeTab: null,
	activeSlide: null,

	tabs: {},
	slides: {},

	prep: function () {
		this.slideshow = $('slideshow');

		var custom = ($defined($('custom')) && $('custom').getElement('div'));

		if (!custom) {
			var locseg = window.location.href.split('/');

			// Check to see if we are on a recipe page
			var recipePage = (locseg.contains('our-recipes')) ? true : false;

			// Prepare tabs
			var tabElems = $('nav-tabs').getElements('li > a');
			tabElems.each(function(tab) {
				var tn = tab.getParent().get('id').substr(3); // Get tab number from parent li element
				this.tabs[tn] = tab;

				// If we are on a recipe page dont add mouse over event
				if (!recipePage && tab.getAttribute('id') !== 'main-nav') {
					tab.addEvent('mouseover', this.tabOver.bind(this, tn));
					tab.addEvent('mouseout', this.tabOut.bind(this, tn));
				}

				// Split the tab url up
				var tabseg = tab.get('href').split('/');

				// Set active tab if we are in the our-recipe page and the same category
				if (tabseg.contains(locseg[3]) && tabseg.contains(locseg[4])) {
					this.setActiveTab(tn);
				}

			}, this);

			// Get tab slides
			if ($defined($('tab-slides'))) {
				this.slidecont = $('tab-slides');
				var slides = this.slidecont.getElements('.tabslide');
				slides.each(function(slide){
					var sn = slide.get('id').substr(9);
					this.slides[sn] = slide;

					if (!slide.hasClass('hide')) {
						this.activeSlide = sn;
					}

				}, this);
			}

			if ($(document.body).getAttribute('id') == 'home') {
				$(document).addEvent('slideshow:loaded', function() {
					this.slideshowCls = new mSlideShow(this.slideshow, '.slide');
				}.bind(this));
			} else {
				if ($defined(this.slideshow))
					this.slideshow.addClass('hide');
			}
		} else {
			$('custom').addClass('isset');
			$('slideshow').destroy();
		}
	},

	tabOver: function (n) {		
		if ($defined(this.slideshowCls)) {
			this.slideshowCls.pauseShow();
		}
		
		if (this.slideshow) {
			this.slideshow.addClass('hide');
		}

		this.unsetActiveTab();
		this.setActiveTab(n);

		this.showSlide(n);
	},

	tabOut: function (n) {		
		if ($defined(this.slideshow) && $defined(this.slideshowCls)) {
			this.slideshowCls.pauseShow();
			this.slides[this.activeSlide].addClass('hide');
		}
		
		if (this.slideshow) {
			this.slideshow.removeClass('hide');
		}
		
		this.unsetActiveTab();
	},

	setActiveTab: function (n) {
		this.activeTab = n;
		this.tabs[this.activeTab].addClass('active');
	},

	unsetActiveTab: function () {
		if (this.activeTab !== null) {
			this.tabs[this.activeTab].removeClass('active');
			this.activeTab = null;
		}
	},

	showSlide: function (n) {
		if ($defined(this.slides[n])) {
			this.slidecont.removeClass('hide');
			this.slides[n].removeClass('hide');

			this.hideActiveSlide(n);
			this.activeSlide = n;
		}
	},

	hideActiveSlide: function (n) {
		if (this.activeSlide !== null) {
			if (this.activeSlide !== n)
				this.slides[this.activeSlide].addClass('hide');
		}
	}
};
