$(function(){

  if( $.facebox ) {
    $.facebox.settings.opacity = 0.7;
    $('a[rel*=facebox]').facebox();
  }


  $('h1#title').css({
    'background-position': $('h1#title span').width() +'px 0px'
  });

  if( $('div.cycle').length ){ // homepage only
    $('div.cycle').cycle({
      after:   onAfter,
      fx:      'fade',
      next:    'li#next',
      prev:    'li#prev',
      speed:   300,
      timeout: 8000
    });

    $('li.goto').click( function(){
      var id = $(this).attr('id');
      id = id.substr( id.indexOf('-') + 1, id.length );
      $('div.cycle').cycle( parseInt(id) );
      return false;
    });

    // center the controls
    var m = Math.floor( (350 - $('ul#slideshow-controls').width() ) / 2 );
    $('ul#slideshow-controls').css({
      'left': m + 'px'
    })

  }

  $('div.cycle-languages').cycle({
    fx:      'fade',
    speed:   300,
    timeout: 8000
  });

  $('input#field-q').click(function(){
    if( $(this).val()=='Search' ) $(this).val('');
  }).blur(function(){
    if( $(this).val()=='' ) $(this).val('Search');
  });


  $('a.question').click(function(){
    var id = $(this).attr('id');
    id = id.substr( id.indexOf('-'), id.length );
    $('#answer' + id).slideToggle();
    return false;
  });

  if( $('div#map_canvas').length ){
    if (GBrowserIsCompatible()) {
      var point = new GLatLng( 54.585605,-5.926126 );
      var marker = new GMarker( point );
      var map = new GMap2( document.getElementById("map_canvas") );
      map.disableDragging();
      GEvent.addListener(map, 'click', function() {
        window.open('http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=Simon+Community+(Northern+Ireland)&aq=&sll=54.585605,-5.926126&sspn=0.001127,0.002411&ie=UTF8&hq=Simon+Community&hnear=&z=18&iwloc=A');
      });
      map.setCenter( point, 16 );
      map.addOverlay( marker );
    }
  }

  window.setTimeout( function(){equalise('.equal')}, 500 );

});



function onAfter(curr, next, opts)
{
  $('li.goto').removeClass('sel');
  $('li#slide-'+opts.currSlide).addClass('sel');
}


function equalise(selector)
{
  var maxHeight = 0;
  $(selector).each(function(){
    maxHeight = Math.max( maxHeight, $(this).height() );
  });
  $(selector).css({ 'height': maxHeight+'px' });
}


