window.addEvent('domready', function () {
	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')) {
   			document.location.href = $target.getElement('a').get('href');

			} else if ($target.getParent().get('tag') == 'p' && $target.getParent().hasClass('three')) {
				document.location.href = $target.getParent().getElement('a').get('href');
			}
   	});
   }
/*
  var cufonReplace = [
    'h1', 'h3', '#navigation span a', '#search label', '#content .program-item-list .event',
    '#content .program-item-list .time', '.maplocations h2', '.newsupdates .meta-label',
    '.newsupdates .tabs li'
  ]

	Cufon.replace([cufonReplace.join(', ')]);
  Cufon.now();
*/
	//homepage flash thingy
	if (document.id('flash')) {
		swfobject.embedSWF('/flash/brb-home.swf', 'flash', 720, 260, "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')) {
		var subscriptions = document.id('subscriptions');
		subscriptions.setStyle('display', 'none');

		subscriptions.getParent('form').getElements('input').addEvent('focus', function() {
			if (subscriptions.getStyle('display') == 'none') {
				var height;

				subscriptions.setStyle('display', '');
				height = subscriptions.getSize().y;
				subscriptions.setStyles({'height': 0, 'overflow': 'hidden'});

				subscriptions.set('tween', {'property': 'height'});
				subscriptions.tween(height);
			}
		});
	}

	$$('.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);
		});
	}
});

var resizeBgImage = function () {
  var imgRatio,
      img = $$('#background img')[0];
      bodyWidth = $$('body')[0].getSize().x,
      bodyHeight = $$('body')[0].getSize().y,
      imgWidth = img.getSize().x,
      imgHeight = img.getSize().y,
      imgRatio = imgWidth / imgHeight;

  if (bodyHeight > bodyWidth / imgRatio) {
    img.setStyles({
      'height': bodyHeight,
      'width': bodyHeight * imgRatio,
      'margin-left': ((bodyHeight * imgRatio) - bodyWidth) *-0.5,
      'margin-top': 0
    });
  } else if (bodyWidth > bodyHeight * imgRatio) {
    img.setStyles({
      'height': bodyWidth / imgRatio,
      'width': bodyWidth,
      'margin-left': 0
    });
  }
}

window.addEvents({
  'load': resizeBgImage,
  'resize': resizeBgImage
}); 
