try {
  if (INTRANET_VALID === undefined)
  {
    var INTRANET_VALID = false;
  }
  else
  {
    var INTRANET_VALID = true;
  }
} catch(exception) {
  var INTRANET_VALID = false;
}

$(function(){
   
  var tool_text_default = $('#tool_text').html();
  var tool_mouseout = false;
  var view_site = $.cookie('view_site');  
  $.trackPage('UA-512472-5');
  $("a").track();
  $('#tool_tipper a').click(function(){
    return false;
  });
  var hoverIntent = null;
  $('#tool_tipper a').mouseover(function(){
    var text = $($(this).attr('href')).html();
    $(text).wrap('<div></div>').attr({id:'tool_layout'})
    tool_mouseout = false;
    clearTimeout(hoverIntent);
    hoverIntent = setTimeout(function () {
      $('#tool_text').stop().fadeIn().html(text);
    }, 300);    
  });
  $('#tool_tipper a').mouseout(function(){
    clearTimeout(hoverIntent);
    tool_mouseout = true;
    hide_tool_text();
  });
  
  var tool_text_defaultb = $('#tool_textb').html();
  var tool_mouseoutb = false;
  $('#tool_tipperb a').click(function(){
    return false;
  });
  
  var hoverIntentb = null;
  $('#tool_tipperb a').mouseover(function(){
    var text = $($(this).attr('href')).html();
    tool_mouseoutb = false;
    clearTimeout(hoverIntentb);
    hoverIntentb = setTimeout(function () {
      $('#tool_textb').stop().fadeIn().html(text);
    }, 300);    
  });
  $('#tool_tipperb a').mouseout(function(){
    clearTimeout(hoverIntentb);
    tool_mouseoutb = true;
    hide_tool_textb();
  });
  
  $('.pensions_content').hide();
  $('a.pension_options').click(function(){
    var id = '#' + $(this).attr('href');
    $('.pensions_content').hide().filter(id).show();  
    $('#tool_tipper').css({opacity:0.0});
    $.scrollTo( id, 800 );
    setTimeout(function(){
      $('#tool_tipper').animate({opacity:1.0},600);
    },400)
    return false;
  });
  
  function hide_tool_text(){
    setTimeout(function(){
      if(tool_mouseout) $('#tool_text').stop().html(tool_text_default);
    },6000);
  }
  
  function hide_tool_textb(){
    setTimeout(function(){
      if(tool_mouseout) $('#tool_textb').stop().html(tool_text_defaultb);
    },6000);
  }
  
  function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=735,height=436,left = 352.5,top = 200.5');");
  }
  
  function popUpSmall(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=735,height=236,left = 352.5,top = 200.5');");
  }
  
  $('a').click(function(){$(this).blur();});
  
  $("a.pop").click(function(){
    var url = $(this).attr('href');
    var popupwin = popUp(url);
    return false;
  });
  $("a.pop_small").click(function(){
    var url = $(this).attr('href');
    var popupwin = popUpSmall(url);
    return false;
  });
  $("a[rel*='external']").click(function(){
    this.target = "_blank";
  });
  $("a[rel*='intranet']").click(function(){
    if(INTRANET_VALID == false)
    {
      $(this).attr('href','intranet_only.html');
    }
  });
  
  $('#tabs').tabs();
  
  $('h3.with_buttons').each(function(){
    if($(this).parent().find('div.collapsable .togglable').length >= 1) {
      $(this)
        .append('<img class="plus" src="images/btn-collapsable-yellow-plus.png" />')
        .append('<img class="minus" src="images/btn-collapsable-yellow-minus.png" />');
    }
  });

  $('h3.with_buttons img.plus').click(function(){
    $(this).parent().parent().find('div.collapsable .togglable').stop().fadeIn();
  });
  $('h3.with_buttons img.minus').click(function(){
    $(this).parent().parent().find('div.collapsable .togglable').stop().fadeOut();
  });
  // for the more buttons
  $('a.toggle').click(function(){
    if($(this).parent().parent().find('div.togglable').is(':visible')){
      $(this).parent().parent().find('div.togglable').fadeOut();
      return false;
    } else {
      $(this).parent().parent().find('div.togglable').fadeIn();
      return false;
    }
  });
  
  // to fix ie which loses the margins in the tabbed areas don't know why
  $('.ui-tabs-panel p').css('margin-right','15px');
  
  $('#enrol_now').click(function(){
    offset = $(this).offset();
    text = "<p>You can't enrol till June the first but come back then...</p>";
    $('body').append('<div class="tooltip"></div>').find('div.tooltip')
              .css({ 
                top: offset.top, 
                left: offset.top, 
                'background': '#fff', 
                'padding': '10px'
              }).html(text);
    return false;
  });
  
  $('.fade').innerfade({
    containerheight: '165px',
    speed: 'slow',
    timeout: 5000,
    type: 'random'
  }).show();
  
  setTimeout(function() {
    $('.adBlock .bd').css({
      'background' : 'url(/images/adblock/animation1.gif) no-repeat center 8px',
      'opacity' : 0.0,
      'visibility' : 'visible'
    }).animate({'opacity' : 1.0},600)
  }, 100);
  
  if (jQuery().marquee) {
    $('.scroller marquee').marquee().mouseover(function () {
      $(this).trigger('stop');
    }).mouseout(function () {
      $(this).trigger('start');
    }).mousemove(function (event) {
      if ($(this).data('drag') == true) {
        this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
      }
    }).mousedown(function (event) {
      $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
    }).mouseup(function () {
      $(this).data('drag', false);
    });
  }   
});

function fade_banners(){
  $('#mh').html(html).find('#home_fade').innerfade({
    containerheight: '187px',
    speed: 'slow',
    timeout: 5000,
    type: 'sequence'
  });
  // var more_button = document.createElement('a');
  // $(more_button).attr({'id':'more_button','href':'#asdf'}).css({
  //     postion: 'absolute',
  //     top: 0,
  //     right: 0,
  //     display: 'block',
  //     zIndex: '200',
  //     width: '875px'
  //   }).text(' ');
  //   $('#mh').append(more_button);
}

var html = '<ul id="home_fade"><li class="home_banner" id="hm_1"></li><li class="home_banner" id="hm_2"></li><li class="home_banner" id="hm_3"></li><li class="home_banner" id="hm_4"></li><li class="home_banner" id="hm_5"></li></ul>';

$(window).load(function(){  
  if ($('#mh').length) {
    if ($.browser.msie && $.browser.version >= 7.0) {
      fade_banners();
    } else {
      var banners = ['/images/banners/home1.jpg', '/images/banners/home2.jpg', '/images/banners/home3.jpg', '/images/banners/home4.jpg', '/images/banners/home5.jpg'];
      var loaded = 0;
      
      // Preload pics for the banner after the rest of the page has loaded so the page doesn't hang for them
      $.each(banners,function(i,n){
        var img = new Image();
        $(img).attr('src',this).load(function(){
          loaded++
          if(loaded == banners.length) {
            fade_banners();
          }
        });        
      });
    }
  }
});
