Cufon.replace('.fslola', { fontFamily: 'FS Lola Medium' });
Cufon.replace('.fslola-bold', { fontFamily: 'FS Lola Bold' });


$(function(){
  Cufon.now();

  /** topnav search **/
  $('input#field-search').focus(function(){
    if( $(this).val()=='Search' ) $(this).val('');
  }).blur(function(){
    if( $(this).val()=='' ) $(this).val('Search');
  });


  /* login box */
  $('div#header a.members-area#login').click(function(e){
    $('form#login-form').slideDown('fast');
    return false;
  });
  $('div#header span#close-login').click(function(e){
    $('form#login-form').slideUp('fast');
    return false;
  });

  $('form#login-form input#field-username').focus(function(){
    if( $(this).val()=='Username' ) $(this).val('');
  }).blur(function(){
    if( $(this).val()=='' ) $(this).val('Username');
  });
  $('form#login-form input#field-plain_password').focus(function(){
    $(this).hide();
    $('form#login-form input#field-password').show().focus();
  });
  $('form#login-form input#field-password').blur(function(){
    if( $(this).val()=='' ){
      $(this).hide();
      $('form#login-form input#field-plain_password').show()
    }
  });


  /** homepage news tabs**/
  $('.news-tab').click(function(e){
    var _id = $(this).attr('id');
    $('.news-tab').removeClass('sel');
    $('#'+_id).addClass('sel');
    $('#news-panel .content').hide();
    $('#news-panel #'+_id+'-content').show();
  });


  /** homepage statistics **/
  if( $('div#statistics-slides').length )
  {
    $('div#statistics-slides').cycle({
      after:   onAfter,
      fx:      'fade',
      speed:   300,
      timeout: 5000
    });
    $('ul#statistics-paging li').click(function(e){
      var id = $(this).attr('id');
      id = id.substr( id.indexOf('-') + 1, id.length );
      $('div#statistics-slides').cycle( parseInt(id) );
      return false;
    });
  }

  /** expand/close panels **/
  $('a.expand').click(function(e){
    $('div#'+$(this).attr('id')+'-panel').slideToggle('fast', function(){
      if( $(this).is(":hidden") ){
        $(this).parent().find('span').hide();
      } else {
        $(this).parent().find('span').show();
      }
      equalise('div.eq-subpageframe');
    });
    return false;
  });


  if( $.fancybox ) {
    $("a.fancybox").fancybox({
  		overlayColor: '#000',
  		overlayOpacity: 0.5,
  		titleShow: false
  	});
  }


  /** google map **/
  if( $('div.map_canvas').length ){
    var counter = 1;
    $('div.map_canvas').each(function(){
      var item = $(this).html().split('|');
      var c = item[0].replace(/[( )]/g, "").split(',');
      var latlng = new google.maps.LatLng( c[0], c[1] );
      var config = {
        zoom: 16,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(this, config);
      var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: item[1]
      });
      
      
      var sAddress = $(this).parent().find('p#address-'+counter).html();
      
      
      var oWindow = new google.maps.InfoWindow({
        // content: '<p style="color: #000000; margin-bottom: 0;padding-bottom: 0;"><strong>10th Floor,</strong><br />George\'s Quay Plaza,<br />Georges Quay<br />Dublin 2</p>',
        content: sAddress,
        anchor: marker
      });
      
      sAddress = '';
      counter++;
      
    });

  }


  /** funds map **/
  $('map#m_map area').click(function(e){
    var area = $(this).attr('href');
    $('div.map-item').hide();
    if( area.indexOf('#') == 0 )
    {
      // work out the bounding box of the polygon
      var i, x=[], y=[];
      var c = $(this).attr('coords').split(',');
      for (i=0; i < c.length; i++){
        x.push( c[i++] );
        y.push( c[i] );
      }
      var t = y.sort( function(a, b){ return (a-b); } )[0]; // ascending sort
      var l = x.sort( function(a, b){ return (a-b); } )[0];
      var b = y.sort( function(a, b){ return (b-a); } )[0]; // descending sort
      var r = x.sort( function(a, b){ return (b-a); } )[0];
      // position the popup
      var popup = $('div#map-'+area.substr(1));
      var top = t - popup.height() - parseInt((t-b)/2);
      var left = l - (popup.width()/2) + parseInt((r-l)/2);
      popup.css({
        'position': 'absolute',
        'left': left+'px',
        'top':  top+'px'
      }).fadeIn('fast').click(function(){
        $(this).fadeOut('fast');
      });
    }
    else
    {
      return;
    }
  });


  /** decorative **/
  if( $("select.uniform, input.uniform").length )
  {
    $("select.uniform, input.uniform").uniform();
  }

  equalise('div.eq-subpageframe');
  $('div#left ul li a.sel').parent().prev().find('span').css({
    'border-bottom': 'none'  /** nav tweak **/
  });

  //keepAlive();

});



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


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


/** keep login session alive
function keepAlive()
{
  $.get(
    '/keepalive.php',
    {},
    function(oData){
      console.log('KEEP ALIVE');
    },
    'json'
  );
  setTimeout( 'keepAlive()', 1000*60*15 ); // 15 mins
} **/
