$(function(){
  //CUFON
  //Cufon.replace('h2.pageHeadline');
  //SORT HP ATTORNEY PROFILE THUMBNAILS
  if($('ul.staff').length > 0) {
    $("ul.staff").randomize("li");
  }
  //HP ATTORNEY AJAX CALLS
  $('ul.staff li a').unbind('click').click(function(e){
    e.preventDefault();
    var $toLoad =  $(this).attr('href');
    hpNameTag = '.member-title h3'
    hpPosTag = '.member-title span',
    hpDescTag = '.member-text',
    hpMemPic = '.member-picture img',
    mpNameTag = '.pnlNameAndTitle h2',
    mpPosTag = '.pnlNameAndTitle span',
    mpDescTag = '#pnlBio #sb',
    mpMemPic = '.pnlBioOverview img'

    //MANAGE BUTTONS
    $('ul.staff li a').each(function(e) {
      $(this).removeClass('active');
    });
    $(this).addClass('active');
    $.ajax({
      type: "GET",
      url: $toLoad,
      context: document.body,
      dataType: "html",
      success: function (data) {
        var title = $(data).find(mpNameTag).html(),
        position = $(data).find(mpPosTag).html(),
        description = $(data).find(mpDescTag).html(),
        picture = $(data).find(mpMemPic).attr('src');
        $('.member').fadeOut(200, function() {
          $(hpNameTag).html(title);
          $(hpPosTag).html(position);
          $(hpDescTag).html('<p>'+description+'</p><p><a href="/our-people-practices/?tb='+$toLoad+'">View Profile</a></p>');
          $(hpMemPic).attr('src', picture);
          $(this).fadeIn(200);
        })
      }
    });
  });
  //ACTIVATE JQ FEATURES ON PUBLICATION TABS
  if($('ul.right-side-tabs').length > 0) {
    $('.right-side-tabs li a').click(function(e) {
      $('.right-side-tabs li').each(function(e) {
        $(this).removeClass('active-tab');
        $('#ulPublicationSort li').each(function(e) {
          $(this).css('display','none');
        });
      });
      e.preventDefault();
      $(this).parent().parent().addClass('active-tab');
      var sortTo = $(this).attr('data-val');
      $('#ulPublicationSort li').each(function(e) {
        if($(this).attr('data-id') != sortTo && sortTo != 'All') {
          $(this).css('display','none');
        } else {
          $(this).css('display','block');
        }
      });
    });
  }
  //TRUNCATE NEWS PAGE DESCRIPTIONS
  //$('#ulNewsDescription li p').jTruncate({length: 150, moreText: ""});
  $('#ulNewsDescription li').css('min-height', '150px');
  //ADD THIS
  $('#share').addClass('addthis_toolbox addthis_32x32_style');
  $('#share').html('<a class="addthis_button_facebook"><img src="/images/elem/f-icon.png" alt="Facebook" /></a><a class="addthis_button_twitter"><img src="/images/elem/t-icon.png" alt="Twitter" /></a><a class="addthis_button_email"><img src="/images/elem/email-icon.png" alt="Email" /></a><a class="addthis_button_print"><img src="/images/elem/print-icon.png" alt="Print" /></a>');
  addthis.toolbox('#share');
  //STYLE SELECT ELEMENTS
  $(".applying, .choose-subject, .dropdownlist select").selectbox();
  //NAV LOGO ROLLOVER
  $('.nav-wrap').append('<div id="logoRollover"><img src="/media/9003/logorollover.jpg" alt="Metz, Lewis, Bordman, Must, O\'Keefe" /></div>');
  $('#logoRollover').css('display', 'none');
  $('.logo a').hover(function(){
    $('#logoRollover').slideDown();
  }, function(){
    $('#logoRollover').slideUp();
  });
  //SORT THE ATTORNEY PROFILES
  if($('#ulAttorneyListing').length > 0) {
    $('#ulAttorneyListing').isotope({
      itemSelector : '.xElement'
    });
    $('.top-nav li a').click(function(){
      var selector = $(this).attr('data-filter');
      $('ul#ulAttorneyListing').isotope({ filter: selector });
      var $toLoad =  $(this).attr('href');
        subGroup = '#subGroupNav'
        
        $.ajax({
            type: "GET",
            url: $toLoad,
            context: document.body,
            dataType: "html",
            success: function (data) {
                var title = $(data).find(subGroup).html();
                //$('#subGroupNav').slideUp(400, function() {
                    $(subGroup).html(title);
                    $('#subGroupNav div.tabs').tabs();
                    //$(this).slideDown(1200);
                //})
            }
        });
      return false;
    });
  }
  //BIOS thickbox AND TABS
  $('.bioListing').append('<div id="ajaxAttorneyDetail"></div>');
  $('#ulAttorneyListing a').each(function(){
    var $getThis = $(this).attr('href').substr(0, $(this).attr('href').length-1)
    $(this).click(function(e){
      e.preventDefault();
      $('#ajaxAttorneyDetail').empty();
      executeTB($getThis);
    });
  });
  $('#ulAttorneyListing img').each(function(){
    $(this).css('cursor', 'pointer');
    var $getThis = $(this).next().children().attr('href').substr(0, $(this).next().children().attr('href').length-1)
     $(this).click(function(e){
       e.preventDefault();
       $('#ajaxAttorneyDetail').empty();
       executeTB($getThis);
     });
  });
    
  //CAREERS ACCORDION
  $('#pnlCareerListings').accordion({ collapsible: true, active: false });
 
  //handle default HP bio link
  if($(".member-text a").length > 0) {
    src = $(".member-text a").attr('href');
    newsrc = "/our-people-practices/?tb="+src;
    $(".member-text a").attr('href', newsrc);
  }
  
  //handle linked to TB
  if(getUrlVars()["tb"]) {
    executeTB(getUrlVars()["tb"]);
  }
  //HOMEPAGE SCROLLER
//click through images
  $('.thumbs li').css('display', 'none');
  $('.thumbs li:first').css('display', 'block').addClass('current');
  
  $('#lnkNext').click(function(){
    var current = $('.thumbs li.current');
    var total = $('.thumbs li').length -1;
    if($(current).index() < total) {
      $(current).fadeOut(500);
      $(current).next().delay(600).fadeIn(500);
      $(current).removeClass('current').next().addClass('current');
    } else {
      $(current).fadeOut(500);
      $('.thumbs li:first').delay(600).fadeIn(500);
      $(current).removeClass('current');
      $('.thumbs li:first').addClass('current');
    }
    return false;
  });
  $('#lnkPrev').click(function(){
    var current = $('.thumbs li.current');
    var total = $('.thumbs li').length -1;
    if($(current).index() > 0) {
      $(current).fadeOut(500);
      $(current).prev().delay(600).fadeIn(500);
      $(current).removeClass('current').prev().addClass('current');
    } else {
      $(current).fadeOut(500);
      $('.thumbs li:last').delay(600).fadeIn(500);
      $(current).removeClass('current');
      $('.thumbs li:last').addClass('current');
    }    
    return false;
  });
  //automatically scroll images
  function fadeFeature(){
    setTimeout(function() {
      var current = $('.thumbs li.current');
      var total = $('.thumbs li').length -1;
      if($(current).index() < total) {
        $(current).fadeOut(500);
        $(current).next().delay(500).fadeIn(500);
        $(current).removeClass('current').next().addClass('current');
      } else {
        $(current).fadeOut(500);
        $('.thumbs li:first').delay(500).fadeIn(500);
        $(current).removeClass('current');
        $('.thumbs li:first').addClass('current');
      } 
      fadeFeature();
    }, 8000);
  }; 
  fadeFeature();
  $('#pnlFooter .nav li:last').css('border-right', 'none');
  //LEGAL TRENDS BACK BUTTON
  $('.trend .beck-button').click(function(){
      history.go(-1);
    return false;
  });
  
  //REPLACE UPLOAD FIELDS IN CAREER PORTAL
  $('#ContentPlaceHolderDefault_placeholderBodyContent_RenderForm_3_rpFieldsets_rpFields_0_87cde803_c9ee_4753_b3a2_4241fe34fb6f_12').append('<img src="/images/btn/resume-btn.png" alt="Submit Your Resume" />');
  $('#ContentPlaceHolderDefault_placeholderBodyContent_RenderForm_3_rpFieldsets_rpFields_0_5c022fc0_6d01_4a25_990b_c4f694ffaf67_13').append('<img src="/images/btn/cv-btn.png" alt="Submit Your CV" />');
  $('#pnlScribdContent').prepend('<a href="/publications" class="beck-button">Back</a>');
  if ($.browser.webkit) {
    $('body').addClass('webkit');
  }
  $('#ulNewsListing.archive').jPaginate({items: 10, cookies: false});
  //DOWNLOAD FIX FOR SCRIBD
  //Elements 
  //// #uiDownload
  //// #pnlScribdContent (data-filename)
  //// .expandable_button download
  /*
  if($('#uiDownload').length > 0) {
    var fPath = $('#pnlScribdContent').attr('data-filename');
    $('#uiDownload').attr('href', fPath);
  }
  */
});

function executeTB(path) {
    $.ajax({
        url: path,
        success: function(data){
          var detail = $(data).find('.content-wrap').html()
          $('#ajaxAttorneyDetail').html(detail);
          $('.bioTabs').tabs();
          tb_show('','#TB_inline?height=600&width=980&inlineId=ajaxAttorneyDetail','')    
        }
      });
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

