// Cufon
////////////////////////////////////////////////////////
var bodoni = [
    'h2.headline',
    '#blogPage #secondaryContent h3'
    ],
    din = [
    '#header .blurb h3',
    'h3.subhead',
    '.standard.map #primaryContent h2',
    '#homePage #se h2',
    '#homePage #se h3',
    '#homePage #ne h2',
    '#secondaryNavigation a',
    '#secondaryNavigation h3',
    '#processSidebar h4',
    '.grid h3',
    '.single h3',
    '.post h3',
    '.post h1',
    '.vcard h3',
    '#footer h3',
    '#footer h4',
    '#footer h5' 
    ];
Cufon.replace(bodoni, { fontFamily: 'Bodoni' }); // Bodoni
Cufon.replace(din, { fontFamily: 'DIN' }); // DIN

function requestBio(url, e) {
  if ($(e).hasClass('current')) {
    return false;
  } else {
    $.getJSON(url+'?json_encode=1', function(data){
      updateBio(data, e);
    });
  }
}

function updateBio(data, e) {
  var $thumb = $('#bioThumb'),
      $head = $('#bioHead'),
      $text = $('#bioText');
      
  $('#bioList a').unbind('click').click(function(){return false;});
  $(e).addClass('current').parents('#bioList').find('a.current').not(e).removeClass('current');
  $thumb.empty();
  $head.empty();
  $text.empty();
  
  if(data.image) {
    $thumb.show().append(data.image).find('img').hide().bind('load', function(){
      $(this).fadeIn(500);
      $('#bioList a').bind('click', function(){
        var url = $(this).attr('href');

        requestBio(url, this);

        return false;
      });
    });
  } else {
    $thumb.hide();
    $('#bioList a').bind('click', function(){
      var url = $(this).attr('href');

      requestBio(url, this);

      return false;
    });
  }
  
  
  $head.append('<h3>'+data.name+'</h3>');
  $head.append('<span class="title">'+data.title+'</span>');
  $head.append('<span class="branch">'+data.branch+'</span>');
  
  $text.append(data.text);
  
} 
function validateContact(ajaxsubmit) {
  $('#contactForm form').validate({
    rules: {
      department: {
        required: true
      }
    }, 
    messages: {
      department: "Please Select a Department"
    }
  });
  $('#contactForm #department').change(function(){
    var val = $(this).val(),
        $option = $(this).find('option[value='+ val+']');
    if($option.hasClass('file')) {
      $('#formMessage').after('<fieldset id="formAttachments"><p class="clear"><label for="attachment">Attach Resume</label><input type="file" name="attachment"  /></p></fieldset>');
    } else {
      $('#formAttachments').remove();
    }
  });
  $('#contactForm #reset').live('click', function(){
    if(ajax == true) {
      $.fancybox('close');
    }
    return false;
  });

}

// Document
////////////////////////////////////////////////////////
$(document).ready(function(){
  
  // Open External Links in New Window
  $('a[rel*=external]').click(function(){
     window.open(this.href);
     return false;
    });
  
  // Accordian Navigation
  $('#secondaryNavigation li:has(a.current) ul').css('display', 'block'); // Force Current To Show on Pag Load
  $('#secondaryNavigation li:has(ul)>a').click(function(){
    var $parent = $(this).parent(),
        $subnav = $parent.find('ul'),
        duration = 250;
    if ($parent.find('a').hasClass('current')) {
      $parent.find('a').removeClass('current');
      $subnav.slideUp(duration);
      Cufon.replace(din, { fontFamily: 'DIN' });
    } else {
      $parent.find('>a').addClass('current').end().siblings('li').find('a.current').removeClass('current').siblings('ul').slideUp(duration);
      setTimeout(function(){
        $subnav.slideDown(duration);
        Cufon.replace(din, { fontFamily: 'DIN' });
      }, duration);
      
    }
    return false; 
  });
  
  // Lazyload View All
  if (jQuery.fn.lazyload) {
    $("ul.grid img").lazyload({ placeholder : "/assets/holder.jpg", "effect" : "fadeIn", threshold : -50 });
  }
  
  // Slideshow
  $('.slideshowLink').fancybox({
    padding: 26, 
    autoDimensions: false,
    autoScale: false,
    height: 615,
    width: 880,
    scrolling: "no",
    onComplete: function(){
      $('#slides').cycle({
        timeout: 0,
        next: '.next',
        prev: '.prev',
        pager: '#thumbs ul',
        pagerAnchorBuilder: function(idx){
          return '#thumbs ul li:eq(' + idx + ') a';
        }
      });
    }
  });
  
  // Bio List
  $('#bioList a').click(function(){
    var url = $(this).attr('href');

    requestBio(url, this);

    return false;
  });
  
  // Footer Locations
  $('#locations a').hover(function(){
    var $this = $(this),
        id = $this.attr('id').split('-').slice(-1);
    $('li#locInfo-'+id[0]).addClass('show').siblings().removeClass('show');
  });
  
  // Non Lightboxed Contact Form Validation
  if($('#contactForm').length){
    validateContact(false);
  }
  // Lightboxed Contact Form
  $('#contactBu').click(function(){
    var url = $(this).attr('href');
    $.fancybox({
      href: url+'?lightbox=1',
      padding: 0,
      background: '#EEE',
      onComplete:  function(){validateContact(true);}
    });
    return false;
  });
  
  
  
});
// Window
////////////////////////////////////////////////////////
