
// add function to mark li on hover if the browser doesn't support li:hover

$(function() {
  fixExternalLinks();
  fixEmailLinks();
  setupLightfootFiles();
  setupFeaturedNews();
});

var finddomain = new RegExp('http://(.*?)/');

var fn_min = 0, fn_max, fn_current = 0, fn_delay = 9, fn_interval = null;

function setupFeaturedNews() {
  var $fnc = $('.featured-news-container');
  if($fnc.length != 1) return;
  fn_max = $('.featured-news').length;
  $('.indicator .marker').click(function() { var index = parseInt($(this).attr('rel')); fn_goto(index, true); })
  $('.fn-0').addClass('current_feature');
  // next line is a fix to help the marker click work properly
  for(var i = 0; i < fn_max; i++) { fn_goto(i, false); } fn_goto(0, false);
  fn_interval = window.setInterval(function() { fn_next(true); }, fn_delay*1000);
}

function fn_goto(index, animate)
{
  window.clearInterval(fn_interval); fn_interval = null;
  fn_current = index;
  $('.indicator .current').removeClass('current');
  $('.m-fn-'+fn_current).addClass('current');
  if(animate) $('.current_feature').fadeOut(200)
  else $('.current_feature').hide();
  $('.current_feature').removeClass('current_feature');
  if(animate) $('.fn-'+fn_current).fadeIn(200);
  else $('.fn-'+fn_current).show();
  $('.fn-'+fn_current).addClass('current_feature');
  // fn_interval = window.setInterval(function() { fn_next(true); }, fn_delay*1000);
}

function fn_next(animate) {
  var fn_old = fn_current;
  fn_current++;
  if(fn_current < 0) fn_current = 0;
  if(fn_current >= fn_max) fn_current = 0;
  if(animate) $('.current_feature').fadeOut(200);
  else $('.current_feature').hide();
  $('.current_feature').removeClass('current_feature');
  if(animate) $('.fn-'+fn_current).fadeIn(200);
  else $('.fn-'+fn_current).show();
  $('.fn-'+fn_current).addClass('current_feature');
  $('.indicator .current').removeClass('current');
  $('.m-fn-'+fn_current).addClass('current');
}

var lf_min = 0, lf_max, lf_current = 1, lf_offset;

function lf_next() {
  lf_goto(lf_current+1);
}

function lf_previous() {
  lf_goto(lf_current-1);
}

function lf_goto(index) {
  lf_current = index;
  if(lf_current < 0) lf_current = lf_max;
  if(lf_current > lf_max) lf_current = 0;
  var pos = (lf_current) * lf_offset; // * -1;
  $('.lf_features_scroller').animate({right:pos});
  var current_thumbnail = $('.lf_thumb:eq('+lf_current+')');
  var pos_i = $(current_thumbnail).position().left + 10;
  $('.lf_thumb_indicator').animate({left:pos_i});
}

function setupLightfootFiles() {
  if( $('.lf_container').length != 1 ) return;
  $('a.modal').click( function() {  
    var offset_y = $('body').scrollTop();
    var offset_x = $('body').scrollLeft();
    $('body').css('overflow','hidden');
    $('body').append('<div class="modal_bg" style="position:absolute;left:0;top:0;background:#000;opacity:0.8;filter:alpha(opacity=80);width:200%;height:200%;z-index:1999;">&nbsp;</div>');
    
    var viewportWidth = $(window).width(), 
        viewportHeight = $(window).height(), 
        $el = $($(this).attr('href')),
        elWidth = $el.width(),
        elHeight = $el.height(),
        elOffset = $el.offset();
    
    //$(window).scrollTop(elOffset.top + (elHeight/2) - (viewportHeight/2))
    //  .scrollLeft(elOffset.left + (elWidth/2) - (viewportWidth/2));
    
    var x = ($('body').outerWidth()/2) - 403 + offset_x;
    var y = ($('body').outerHeight()/2) - 300 + offset_y;
    $el.css('z-index',2000).fadeIn(100).css('top',y).css('left',x).fadeIn(200); 
    return false;
  });
  $('.lf_close').click(function() {
    $('.lf_container, .modal_bg').fadeOut(200, function() { $('.modal_bg').remove(); });
    $('body').css('overflow','auto');
    return false;
  });
  lf_max = $('.lf_feature').length-1;
  lf_offset = $('.lf_feature').width()+40;
  $('.lf_next').click( lf_next );
  $('.lf_previous').click( lf_previous );
  $('.lf_thumb').click( function() { 
    var i = parseInt($(this).attr('rel'));
    lf_goto( i ); 
  });
}
  
function fixExternalLinks()
{
  var ls = document.getElementsByTagName('a');
  var cd = getCurrentDomain();

  $('a[rel=ext],a.rel,a.ext,a.external').each(function() { 
      $(this).target = '_blank';
  });
}

function getCurrentDomain()
{

  var domain = window.location.href;
  var x = domain.match(finddomain);
  return x[1];
}

function fixEmailLinks()
{
  
  $('a[href^=mailto]').click( function() {
    return confirm('We would be pleased to communicate with you concerning legal matters, but we cannot do so until we first know that doing so would not create a conflict of interest.\n\nIf you are interested in having Lightfoot, Franklin & White, L.L.C. represent you in a legal matter, the best way to initiate possible representation is to communicate with one of the contact attorneys listed in the appropriate practice area section of the site. That attorney will arrange to have you go through our conflict of interest procedures and, following that, will put you directly in touch with the attorney best suited to handle your matter.\n\nPlease do not send us any confidential information until after you have received from us a written statement that we represent you in your matter (an "engagement letter").\n\nIf you communicate with us through this site, by e-mail or otherwise, concerning a legal matter for which we do not already represent you, your communication may not be treated as privileged or confidential and will not be deemed to create an attorney-client relationship.');
  });

}

