// pre-load countdown flash
var countdown = $.flash.create({
  swf: '/flash/countdown.swf',
  hasVersion: 6,
  width: 878,
  height: 149,
  params: {
    play: "true",
    loop: "false"
  }
});

jQuery(document).ready(function($) {

  // Handle external links
  $('a.external').click(function() {
    window.open($(this).attr('href'));
    return false;
  });

  // Allow 'hinting' in text fields with the title attribute
  $('input[type=text][title]:not(.nohint), textarea[title]:not(.nohint)').blur(function() {
    if($(this).val() == '') {
      $(this).addClass('hinting').val($(this).attr('title'));
      $(this).one('focus', function() {
        $(this).removeClass('hinting').val('');
      });
    }
  }).trigger('blur');

  // Stop search submitting if there's nothing to submit
  $('form:has(input[name=query]):first').submit(function() {
    var searchBox = $(this).find('input[name=query]');
    if(searchBox.is('.hinting') || searchBox.val() == '') {
      return false;
    }
  });

  // Hide header images
  $('ul.header-images').hide();

  // Insert countdown flash animation
  $('.header-grab-container').append('<div class="countdown-flash" style="position: absolute; z-index: 110;">' + countdown + '</div>');

  // Wait for countdown flash to finish
  var countdown_wait = setInterval(function() {
    if(!$('.countdown-flash :first').get(0).IsPlaying()) {
      clearInterval(countdown_wait);
      $('.countdown-flash').remove();

      // Cycle header images
      $('ul.header-images').show().each(function() {
        var list = this;
        $(list).css({position: 'relative'}).find('li').css({position: 'absolute'}).filter(':not(:first)').hide();
        setInterval(function() {
          var current = $(list).find('li:visible');
          var next = current.next();
          if(next.length == 0) next = current.siblings(':first');
          current.css({zIndex: 102}).fadeOut(1500, function(){ $(this).hide(); });
          next.css({zIndex: 101}).show().fadeIn(1);
        }, 13000);
      });
    }
  }, 1000);

  // Display a "oh noes - you r leavings?!?!" message before following the client login link
  $('a[href=https://access-web.accessrecordsmanagement.com/rswebnet/]').click(function() {
    if(!$.cookie('iknowiknowiknow')) {
      var $leaving = $('<div class="leaving"><a class="close" href="#">Close</a><p>You are about to navigate away from the Access Records Management website. Please click the Back arrow (top left of the screen) to re-enter the website.</p><p><label><input type="checkbox" name="yeah_i_know" /> Do not show this message again</label></p><a class="continue" href="https://access-web.accessrecordsmanagement.com/rswebnet/">Continue &gt;</a></div>');
      $leaving.appendTo($('body'));
      $('.leaving a.continue').click(function() {
        if($('.leaving input[name=yeah_i_know]').is(':checked')) {
          $.cookie('iknowiknowiknow', 'iknow', { expires: 9999, path: '/' });
        }
      });
      $('.leaving a.close').click(function() {
        $('.leaving').remove();
        return false;
      });
      return false;
    } else {
      return true;
    }
  });

  // Testimonials scroller
  $('.testimonials-long .testimonials-long-scroll').each(function() {
    var $scroller = $(this);
    $(this).bind('slide', function() {
      var scrollTo = $(this).height() + 100;
      var scrollDistance = scrollTo + parseInt($(this).css('top'), 10);
      var scrollTime = scrollDistance * 65;
      $(this).stop().animate({top: -scrollTo}, scrollTime, 'linear', function() {
        $(this).css({top: $(this).parent().height()});
        setTimeout(function() { $scroller.trigger('slide'); }, 50);
      });
    }).bind('stop', function() {
      $(this).stop();
    }).parent().mouseenter(function() {
      $scroller.trigger('stop');
    }).mouseleave(function() {
      $scroller.trigger('slide');
    }).end().trigger('slide');
  });

  // Fading sidebar links
  $('.cycle-container').show().cycle({
    fx: 'fade',
    speed: 650
  });
  
  // Fix 'boxes' menu if needed
  $('.boxes-container').each(function() {
    var boxes = $(this).find('.box').length;
    if(boxes > 5) {
      $(this).addClass('more-than-five');
    } else if(boxes > 4) {
      $(this).addClass('more-than-four');
    }
  });
  
});




/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options.expires=-1;} var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000));}else{date=options.expires;} expires='; expires='+date.toUTCString();} var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break;}}} return cookieValue;}};