window.addEvent('domready', function () {
	$$('body')[0].addClass('hasJS');
	Browser.isIE6 = (Browser.Engine.trident && typeof XMLHttpRequest == 'undefined');

	//get a list of all the search results in the delegates section
	var collections   = $$('.delegates-collection li', '.companies-collection li');
	var searchResults = $$('#search-results .result');
	var programItems  = $$('.program-item-list .three');
	var timelineItems = $$('.timeline > div');

	//fix :hover & :first-child implementation for IE6
	if (Browser.isIE6) {
		var hoverFix = {
			'mouseenter': function () { this.addClass('mouseover'); },
			'mouseleave': function () { this.removeClass('mouseover'); }
		}

		$$('#navigation > li').addEvents({
			'mouseenter': function () { this.getElement('span').addClass('mouseover'); },
			'mouseleave': function () { this.getElement('span').removeClass('mouseover');	}
		});

		$$('#navigation > li', collections, searchResults, programItems, timelineItems).addEvents(hoverFix);

		$$('.tabbed .tabs li:first-child').setStyle('margin-left', 0);

		$$('li.timeline').addEvents({
			'mouseenter': function () { this.getElement('.details').setStyle('display', 'block')},
			'mouseleave': function () { this.getElement('.details').setStyle('display', '')}
		});
	}

	$$(collections, searchResults, programItems).addEvent('mouseup', function (e) {
		if (e.target.href) return;
		document.location.href = $(this).getElement('a').get('href');
	});

	if (document.id('searchresults')) {
   	document.id('searchresults').addEvent('mouseup', function(e){
   		if (e.target.href) return;

   		$target = document.id(e.target);

   		if (($target.get('tag') == 'p' && $target.hasClass('three')) || ($target.getParent().get('tag') == 'p' && $target.getParent().hasClass('three'))) {
   			document.location.href = $target.getElement('a').get('href');
   		}
   	});
   }

	//Replace <h1> contents with flash
	sIFR.activate();
	sIFR.replace({src: '/flash/helvetica-neue.swf'}, {
		elements: $$('h1'),
		wmode: 'transparent',
		css: {
			'.sIFR-root': {
				'color': '#FFFF00',
				'text-transform': 'uppercase'
			}
		},
		tuneHeight: -7,
		offsetTop: 2
	});

	//homepage flash thingy
	if (document.id('flash')) {
		swfobject.embedSWF('/flash/ADE_home.swf', 'flash', 756, 235, "9.0.0", '', {}, {'wmode': 'transparent'});
	}

	//conference flash thingy
	if (document.id('conference-flash')) {
		swfobject.embedSWF('/flash/mainpage-conference.swf', 'conference-flash', 372, 250, "9.0.0", '', {}, {'wmode': 'transparent'});
	}

	//festival flash thingy
	if (document.id('festival-flash')) {
		swfobject.embedSWF('/flash/mainpage-festival.swf', 'festival-flash', 372, 250, "9.0.0", '', {}, {'wmode': 'transparent'});
	}

	//ADE next flash thingy
	if (document.id('ade-next-flash')) {
		swfobject.embedSWF('/flash/mainpage-ade-next.swf', 'ade-next-flash', 372, 250, "9.0.0", '', {}, {'wmode': 'transparent'});
	}

	if (document.id('subscriptions')) {
		document.id('subscriptions').setStyle('display', 'none');
		$$('#name', '#email').addEvent('focus', function(){
			document.id('subscriptions').setStyle('display', '');
		});
	}

	$$('.expandable-stripped').each(function (stripped) {
		var full = stripped.getNext('.expandable-full');
		var expandPar = new Element('p');
		var expandBtn = new Element('span', {'class': 'expand', 'html': 'Show more &raquo;'});

		expandBtn.addEvent('click', function () {
			if (stripped.getStyle('display') == 'none') {
				stripped.setStyle('display', '');
				full.setStyle('display', 'none');
				this.set('html', 'Show more &raquo;')
			} else {
				stripped.setStyle('display', 'none');
				full.setStyle('display', '');
				this.set('html', '&laquo; Show less')
			}
		});

		expandPar.grab(expandBtn)
		expandPar.inject(full, 'after');

		full.setStyle('display', 'none');
	});

	if (document.location.pathname == '/program') {
		$$('a[href^=/program]').addEvent('click', function () {
			var queryString = this.href.split('#')[1];
			ProgramSearch.doSearch(queryString);
			ProgramSearch.setButtonsActive(queryString);
		});
	}
});