var moreLink = $('<a href="#" class="more moreClosed"><span class="open">More</span><span class="closed">Less</span></a>');
moreLink.click(function(evt){
  evt.stopPropagation();
  evt.preventDefault();
  var trigger = this;
  $('#intro .extra').slideToggle(function(){
    $(trigger).toggleClass('moreOpen').toggleClass('moreClosed');
  });  
});
$("#intro .extra").show().hide().after(moreLink);

$('#frame').after('<div id="slideshow-nav">').cycle({ 
  fx:      'scrollLeft',
  speed:   750, 
  timeout: 5000, 
  pager:   '#slideshow-nav',
  easing:  'easeOutQuart'
});

$('a[href="#management"]').click(function(evt){
  evt.stopPropagation();
  evt.preventDefault();
  $('#frame').cycle('pause').cycle(0);
});
$('a[href="#development"]').click(function(evt){
  evt.stopPropagation();
  evt.preventDefault();
  $('#frame').cycle('pause').cycle(1);
});
$('a[href="#culture"]').click(function(evt){
  evt.stopPropagation();
  evt.preventDefault();
  $('#frame').cycle('pause').cycle(2);
});
$('#slideshow-nav a').click(function(){
  $('#frame').cycle('pause');
})

$('#speakersList .speaker').not('.moreSpeakers')
  .hover(function(e) {
    $(this).find('.about').hoverFlow(e.type, { opacity: 1 }, 'fast');
  }, function(e) {
    $(this).find('.about').hoverFlow(e.type, { opacity: 0 }, 'fast');
  }).find('.about').css({'opacity': 0, 'display': 'block'});
$('#speakersList .speaker').not('.moreSpeakers').each(function(){
  var par = this;
  $(par).find('a').click(function(){
    $(par).find('.about').css({'opacity': 0});
  })
});

$('#venues li').hover(function(){
    $(this).find('.details').show();
  },function(){
    $(this).find('.details').hide();
  }).each(function(){
    var par = this;
    $(par).find('a').click(function(){
      $(par).find('.details').hide();
    })
  });

$('body.programme table.programme a[href^="#"]').click(function(evt){
  var content = $($(this).attr('href')),
                wrapper;
  if(content.length > 0 && content.is('h2'))
  {
    evt.stopPropagation();
    evt.preventDefault();
    wrapper = $('<div id="modal-content" />').append($('<a />', {'href': '#','class': 'close-modal','text': 'Close'}));
    content = (content.nextAll('h2').length > 0) ? content.nextUntil('h2').andSelf().clone(false) : content.nextAll().andSelf().clone(false);
    $.colorbox({
      transition: 'fade',
      speed: 'fast',
      width: '600px',
      close: 'none',
      html: wrapper.append(content).wrap('<div />').parent().html(),
      onOpen: function(){$('#cboxClose').remove();}
    })
  }
});
$('#modal-content .close-modal').live('click', function(evt){
  evt.stopPropagation();
  evt.preventDefault();
  $.colorbox.close();
});

$('#gallery').fancyPhotoset({
	apiKey: 'b57fe4857468863bd2098affd84450a6',
	photosetId: '72157628291982203',
	displayNum: 0 //use 0 for unlimited
});

/**
 * Form progressive enhancement 
 */
 
$(function() {
  
  // Generic toggle functions
  function show(set) {
    set.show('fast');
  }
  function hide(set) {
    set.hide('fast');
    // Wipe all the fields in the set
    set.find('input').each(function() {
      $(this).attr('checked', false);
    });
    set.find('textarea').each(function() {
      $(this).val('');
    });
    // Remove any validation errors in the set
    set.find('ul.errorlist').remove()
  }
  function make_toggle_function(trigger_function, set1, set2) {
    return function() {
      trigger = trigger_function()
      if(trigger) {
        show(set1);
        if(set2) {
          hide(set2);
        }
      }
      else {
        hide(set1);
        if(set2) {
          show(set2);
        }
      }
    }
  }
  
  var moreinfodeps = $('input#id_more_bursary_info').parent().nextAll()
    .wrapAll('<div/>').parent().css('display', 'none');
  
   
  var toggle_funding = make_toggle_function(
    function() {
      return $('input[name=funding_required]:checked').val() == 'Yes';}, 
    $('fieldset#fieldset-3'), 
    $('fieldset#fieldset-2'));
    
  
  
  var toggle_moreinfo = make_toggle_function(
    function() {return $('input#id_more_bursary_info:checked').length==1;},
    moreinfodeps);
    
  toggle_funding();
  $('input[name=funding_required]').click(function() {
    toggle_funding();
    toggle_moreinfo();
  });
  toggle_moreinfo();
  $('input#id_more_bursary_info').click(toggle_moreinfo);
  
  
  
});

$('input.submit')
  .mouseover(function() {
    $(this).addClass('submit-over');
  })
  .mouseout(function() {
    $(this).removeClass('submit-over');
  });



