
	$(function() {
	
			//
			// Lock header to top of window
			var header_fixed = false;
			$(window).scroll(function() {
					if( $(this).scrollTop() > 102 ) {
						$('body').addClass('fixed');
						header_fixed = true;
					} else if( header_fixed ) {
						$('body').removeClass('fixed');
						header_fixed = false;
					}
				});
			
			//
			// Box heights
			$('.match_height').each(function() {
			
					var sibling = $(this).siblings('.match_height');
					var my_height = $(this).height();
					var sibling_height = $(sibling).height();
					
					if( my_height > sibling_height ) {
						$(sibling).css( 'height', my_height );
					} else if( sibling_height > my_height ) {
						$(this).css( 'height', sibling_height );
					}
				
				});
			
			//
			// <a> form submission
			$('form a.submit').bind( 'click.dTsubmit', function(e) {
					e.preventDefault();
					$(this).parents('form').submit();
					$(this).unbind('click.dTsubmit');
				});
			
			//
			// Setup ul.related
			$('ul.related').each(function() {
					$(this).find('a').not('.grey').addClass('arrow');
				});
			
			//
			// Nav setup
			$('#nav').each(function() {
			
					$(this).find('.dropdown').each(function() {
							$(this).parent('li').hover(function() {
									$(this).addClass('hover');
								},function() {
									$(this).removeClass('hover');
								});
							$(this).find('ul:first > li:last').addClass('last');
						});
					
					$(this).find('ul:first > li.current').each(function() {
							$(this).removeClass('current');
							$(this).append(
								$('<span class="current">^</span>').css({ left: Math.floor(($(this).width()-31)/2) })
							);
						});
				
				});
			
			//
			// Back to top
			$('.back-to-top').click(function(e) {
					e.preventDefault();
					$('html,body').animate( { scrollTop: 0 }, 500 );
				});
			
			//
			// Sub nav
			$('ul.subnav').each(function() {
					
					//
					// Setup current
					$(this).find('li.current').each(function() {
							if( $(this).find('li.current').length ) return;
							var li = $(this).parents('li');
							if( li.length > 0 ) {
								$(li[li.length-1]).find('a:first').addClass('active');
							} else {
								$(this).find('a:first').addClass('active');
							}
							$(this).parents('ul').not('.subnav').removeClass('open').addClass('open');
							$(this).append('<span class="current">&gt;</span>');
							$(this).find('ul:first').show();
						});
						
					//
					// Setup ul
					$(this).find('ul').each(function() {
							plus_minus(this);
							$(this).append('<li class="line" />');
						});
					
					//
					// Open <ul>
					function open() {
						$(this).addClass('open');
						plus_minus(this);
					}
					
					//
					// Close <ul>
					function close() {
						$(this).removeClass('open');
						plus_minus(this);
					}
					
					//
					// Append +,- elements
					function plus_minus(ul) {
						var $a = $(ul).prev('a');
						$a.find('span.minus,span.plus').remove();
						if($(ul).is(':visible')) {
							$a.append(
								$('<span class="sprite minus">-</span>').click(function(e) {
										e.preventDefault();
										close.call(ul);
									})
							);
						} else {
							$a.append(
								$('<span class="sprite plus">+</span>').click(function(e) {
										e.preventDefault();
										open.call(ul);
									})
							);
						}
					}
					
				});
		
			//
			// Switchables
			$('div.switchable').each(function() {
			
					var $this = $(this);
			
					$this.find('div.target').css({ display: 'none' }).eq(0).css({ display: 'block' });
			
					$(this).find('ul.tabs > li > a').click(function(e) {
							e.preventDefault();
							$this.find('div.target').css({ display: 'none' }).eq( $(this).parent().index() ).css({ display: 'block' }).find('div.slider').dTslider('refresh');
							$(this).parents('ul').find('a.active').removeClass('active');
							$(this).addClass('active');
						});
			
				});
		
			//
			// Date picker
			$('input.date').each(function() {
					$(this).wrap('<div class="date" />');
					$(this).after(
						$('<a href="#" class="sprite calendar" />').click(function(e) { e.preventDefault(); $(this).parent().find('input').focus(); })
					);
					$(this).datepicker();
				});
			
			//
			// Login dropdown
			$('div#top a.login').click(function(e) {
					e.preventDefault();
					var $dropdown = $(this).next('.dropdown');
					$dropdown.css({ display: 'block', top: -1*$dropdown.outerHeight() }).animate( { top: 0 }, 200 );
				});
			$('div#top li.login a.close').click(function(e) {
					e.preventDefault();
					var $dropdown = $(this).parents('.dropdown');
					$dropdown.animate( { top: -1*$dropdown.outerHeight() }, 200 );
				});
			
			//
			// Share popup
			$('a.share').click(function(e) {
					e.preventDefault();
					$.dTpopup({
							'content' : '<h3>Share</h3><ul class="share"><li><a href="http://www.facebook.com/sharer.php?u=' + encodeURIComponent(window.location.href) + '" class="facebook" target="_blank">Facebook</a></li><li><a href="http://twitter.com/home?status=' + encodeURIComponent(window.location.href) + '" class="twitter" target="_blank">Twitter</a></li></ul>'
						});
				});
			
			//
			// Add to calendar popup
			$('a.add-to-calendar').click(function(e) {
					e.preventDefault();
					$.dTpopup({
							'content' : '<h3>Add Event to Calendar</h3><ul class="share"><li><a href="/events/google.dT/' + $(this).data('id') + '/" class="google" target="_blank">Google Calendar</a></li><li><a href="/events/ical.dT/' + $(this).data('id') + '/" class="outlook" target="_blank">Outlook</a></li><li><a href="/events/ical.dT/' + $(this).data('id') + '/" class="ical" target="_blank">iCal</a></li></ul>'
						});
				});
			
			//
			// Video popups
			$('.videoPlayer').click(function(e) {
					e.preventDefault();
					$.dTpopup({
							'width'		: 402,
							'height'	: 240,
							'content' 	: '<div class="video"></div>'
						});
					var video = $(this).attr('href').replace('#','');
					$('#dTpopup').find('div.content div.video').dTvideo({
							'width'	: 352,
							'height' : 240,
							'autoplay' : true,
							'loop' : false,
							'controls' : true,
							'activate' : false,
							'mp4' : '/www/video/' + video + '.mp4',
							'webm' : '/www/video/' + video + '.webm',
							'ogv' : '/www/video/' + video + '.ogv',
							'flv' : '/www/video/' + video + '.flv',
							'poster' : ''
						});
				});
			
			//
			// Use dropdowns in place of select
			$('select').dTselect();
			
			//
			// Blur inputs
			$('input').dTblur();
			
			$("form input[type='text'],form input[type='password']").keydown(function(e,ui) {
					if( e.keyCode == 13 ) {
						$(this).parents('form').submit();
					}
				});
		
		});
	
	/**
	 * UNIQUE_ID
	 *
	 * Generate a unique ID.
	 */
	function unique_id( length ) {
	
		if( length == undefined ) length = 12;
		var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		var id = '';
		
		for( var i = 0; i < length; i++ ) {
			var rand = Math.floor( Math.random() * chars.length );
			id += chars.substring( rand, rand+1 );
		}
		
		return id;
		
	}
	
	/**
	 * GET_FLASH
	 *
	 * Get flash movie by ID.
	 */
	function get_flash( name ) {
		if( navigator.appName.indexOf("Microsoft") != -1 ) {
	    	return window[name];
	    } else {
	    	return document[name];
	    }
	}

