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 li');
	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);

    programItems.addEvents({
      'mouseenter': function () {
        var li = $(this),
            likeWrapper = li.getElement('.facebook-like'),
            likeUrl, iframeHTML;

        if (!li.getElement('iframe')) {    
          if (likeWrapper != null) {
            likeUrl = likeWrapper.get('data-url'),
            iframeHTML = '<iframe src="http://www.facebook.com/plugins/like.php?href=' + encodeURI(likeUrl) + '&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;action=like&amp;colorscheme=dark&amp;font&amp;height=35" scrolling="no" frameborder="0" allowTransparency="true"></iframe>';

            likeWrapper.set('html', iframeHTML);
          }
        }
      }
    });

		$$('.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.grey', '.eventdetails h2'),
    wmode: 'transparent',
    css: {
      '.sIFR-root': {
        'font-size': '20px',
        'color': '#333333',
        'text-transform': 'uppercase'
      }
    },
    offsetTop: 2,
    tuneWidth: 2
  });

	sIFR.replace({src: '/flash/helvetica-neue.swf'}, {
		elements: $$('h1'),
		wmode: 'transparent',
		css: {
			'.sIFR-root': {
				'font-size': '20px',
				'color': '#ffff00',
				'text-transform': 'uppercase'
			}
		},
		offsetTop: 2,
		tuneWidth: 2,
		tuneHeight: -7
	});	
	
	sIFR.replace({src: '/flash/helvetica-neue.swf'}, {
		elements: $$('h2.with-sifr'),
		wmode: 'transparent',
		css: {
			'.sIFR-root': {
				'font-size': '20px',
				'color': '#ffff00',
				'text-transform': 'uppercase'
			}
		},
		offsetTop: 2,
		tuneWidth: 2
	});

  document.addEvent('carouselReady', function (carousel) {
    sIFR.replace({src: '/flash/helvetica-neue.swf'}, {
      elements: carousel.element.getElements('h3'),
      wmode: 'transparent',
      css:['.sIFR-root { color: #fff000; font-size: 22px; line-height: 27px; text-transform: uppercase; font-weight: normal }',
      'a { color: #fff000; text-decoration: none; }',
      'a:hover { color: #fff000; text-decoration: none; }',
      'a:visited { color: #fff000; text-decoration: none; }']
    });

    sIFR.replace({src: '/flash/helvetica-neue.swf'}, {
      elements: carousel.element.getElements('h4'),
      wmode: 'transparent',
      css:['.sIFR-root { color: #ffffff; font-size: 15px; line-height: 21px; text-transform: uppercase; font-weight: normal }',
      'a { color: #ffffff; text-decoration: none; }',
      'a:hover { color: #ffffff; text-decoration: none; }',
      'a:visited { color: #ffffff; text-decoration: none; }']
    });
    
    sIFR.replace({src: '/flash/helvetica-neue.swf'}, {
    elements: $$('h5'),
    wmode: 'transparent',
    offsetTop: -8,
    tuneHeight: -20,
    css:[
      '.sIFR-root { color: #fff000; font-size: 35px; line-height: 32px; height: 30px; text-transform: uppercase; font-weight: normal }',
      'a { color: #fff000; text-decoration: none; }',
      'a:hover { color: #fff000; text-decoration: none; }',
      'a:visited { color: #fff000; text-decoration: none; }'
    ]
  }); 
  
  sIFR.replace({src: '/flash/helvetica-neue.swf'}, {
    elements: $$('h6'),
    wmode: 'transparent',
    offsetTop: -2,
    tuneHeight: -2,
    css:['.sIFR-root { color: #ffffff; font-size: 15px; line-height: 21px; text-transform: uppercase; }',
      'a { color: #ffffff; text-decoration: none; }',
      'a:hover { color: #ffffff; text-decoration: none; }',
      'a:visited { color: #ffffff; text-decoration: none; }']
  });
    
  })

	//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);
		});
	}

  if (/*$('added-to-program')*/ false) {
    $('added-to-program').set('tween', {
      property: 'opacity',
      onComplete: function () {
        $('added-to-program').getParent().destroy();
      },
      duration: 1000
    });

    window.setTimeout(function () {
      $('added-to-program').get('tween').start(0);
    }, 10000)
  }  
  
  //
  
  $$('#content.home .four').setStyle('border', 'none');
  $$('#content.home .four').setStyle('padding-top', '0px');
  
});

function setSearchResultBehaviours() {
  var programItems = $$('.program-item-list li');

  programItems.addEvents({
    'mouseenter': function () {
      var li = $(this),
          timeout = li.retrieve('timeout');

      li.addClass('mouseover');

      if (parseInt(timeout) > 0) {
        window.clearTimeout(timeout);
      }
    },
    'mouseleave': function () {
      var li = $(this), timeout;

      timeout = window.setTimeout(function () {
        li.removeClass('mouseover');
      }, 1000);

      li.store('timeout', timeout);
    }
  })

  programItems.addEvent('mouseenter', function () {
    var li = $(this),
        likeWrapper = li.getElement('.facebook-like'),
        likeUrl, iframeHTML;

    if (!li.getElement('iframe')) {    
      if (likeWrapper != null) {
        likeUrl = decodeURIComponent(likeWrapper.get('data-url'));

        if (likeUrl.substr(0,7) != 'http://') {
          likeUrl = 'http://' + document.location.host + likeUrl;
        }

        iframeHTML = '<iframe src="http://www.facebook.com/plugins/like.php?href=' +  encodeURIComponent(likeUrl) + '&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;action=like&amp;colorscheme=dark&amp;font&amp;height=35" scrolling="no" frameborder="0" allowTransparency="true"></iframe>';

        likeWrapper.set('html', iframeHTML);
      }
    }
  });
}
