/*-------------------------------------------------
   SITEWIDE
-------------------------------------------------*/
function setupExternalLinks(){
  // External Links in New Window
  $('a[rel*=external]').click(function(){
   window.open(this.href);
   return false;
  });
}

/*-------------------------------------------------
   HOMEPAGE BANNER CYCLE
-------------------------------------------------*/
function initHomeBannerCycle() {
  // Define cycle options
  var cycleOpts = {
    speed : '600',
    timeout : '3200',
    pager: '.thumbNav',
    pagerAnchorBuilder: buildThumbs
  }
  // Identify children images
  var homeChildImgs = $('.homeBanner ul:first').children('li');
  // Quantity of children images
  var tNavQuantity = homeChildImgs.length;
  // Get width of thumbNav (# of units by unit width)
  var tNavWidth = tNavQuantity*24;

  // Builds thumbnails from slide images
  function buildThumbs(idx, slide) {
      return '<li><a href="#">'+idx+'</a></li>' 
  }
  // Setup Current Slide Class
  $.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
    $(pager).find('li').removeClass('activeSlide').filter('li:eq('+currSlideIndex+')').addClass('activeSlide');
  };
  // Check for minimum of two images, inject pager and initialize cycle
  if(tNavQuantity > 1){
    $('.homeBanner ul').after('<div class="thumbBox"><ul class="thumbNav">').cycle(cycleOpts);
    // Assign width for centering purposes
    $('div.thumbBox, ul.thumbNav').css({'width':''+tNavWidth+'px'})
  }

}

/*-------------------------------------------------
   PORTAL NAVIGATION
-------------------------------------------------*/
// Set classes on hover
function setupPortalNav(){
    // $('#portalNav li > a').click(function(){
    $('#portalNav li > a:not(.current)').click(function(){
      if ($(this).hasClass('open')) {
        $(this).removeClass('open').parents('li').find('div.wrap').slideUp(200);
      } else {
        $(this).parents('li')
        .siblings('li').find('a.open')
        .removeClass('open')
        .parents('li').find('div.wrap').slideUp(200);
        $(this).addClass('open').siblings('div.wrap').slideDown(400);
      return false;
      }
    }); 
    $('a.close').click(function(){
      $(this).parents('li').find('div.wrap').slideUp(200).siblings('a.open').removeClass();
    });
}


/*-------------------------------------------------
   PORTFOLIO GALLERY - Setup Cycle and Carousel
-------------------------------------------------*/
function initPortfolioGallery() {
    /*  MAIN IMAGE CYCLE
    -------------------------------------------------*/
    // Define cycle options
    var cycleOptsLessThan = {
      speed : '400',
      timeout : 0,
      pager: '.thumbNav',
      slideExpr: '> img',
      pagerAnchorBuilder: buildThumbs
    }
    var cycleOptsMoreThan = {
      speed: '400',
      timeout: 0,
      pager: '.thumbBox',
      slideExpr: '> img',
      pagerAnchorBuilder: buildThumbs
    }
    // Identify Children Images
    var childImgs = $('.photoBox').children('img');

    // Builds thumbnails from slide images
    function buildThumbs(idx, slide) {
        return '<li><a href="#"><img src="' + slide.src + '" width="105" height="78" /></a></li>' 
    }

    // Inject pager and initialize cycle
    if((childImgs.length > 1) && (childImgs.length <= 4)){
        $('.photoBox').before('<div class="thumbBox"><ul class="thumbNav">').cycle(cycleOptsLessThan);
        $('.projectDescription').removeClass('stayLeft');
      } 
    else if(childImgs.length > 4){
        $('.photoBox').before('<div class="thumbBox">').cycle(cycleOptsMoreThan);
        $('.projectDescription').removeClass('stayLeft');
      }
    
    /*  THUMB NAV CYCLE
    -------------------------------------------------*/
    //  Paginate Thumbs Function
    function pagez(e, w, n, c) {
      var $group = $(e+'>*'),
          pageCount = Math.ceil($group.length / n);
      for(var i = 0; i < $group.length; i+=n) {
        $group.slice(i, i+n).wrapAll('<'+w+' class="'+c+'" />');
      }
    }

    // Initialize Pagination
    function initPaginateSetOfItems(){
      /* (where we find the set of items, what we wrap them with, how many we're grabbing, class of wrapper)*/
      pagez('#projectsPg div.thumbBox','ul',4,'thumbNav');
    }

    //  Setup Prev/Next States
    function disablePrev() { $('.prev a').addClass('disable');}
    function disableNext() { $('.next a').addClass('disable').animate({'opacity':'.3'});}
    function enablePrev() { $('.prev a').removeClass('disable').animate({'opacity':'1'});}
    function enableNext() { $('.next a').removeClass('disable').animate({'opacity':'1'});}
    
    function onAfter(curr, next, opts) {    
        var index = opts.currSlide;    
        $('.prev')[index == 0 ? disablePrev() : enablePrev()];    
        $('.next')[index == opts.slideCount - 1 ? disableNext() : enableNext()];
    }

    //  Check for length, trigger pagination and init cycle for Thumbnail Nav
    if(childImgs.length > 4) {

      initPaginateSetOfItems();

      $('#projectsPg .thumbBox').cycle({
        after: onAfter,
        timeout: 0,
        speed: 300,
        prev: ".galleryControls span.prev a",
        next: ".galleryControls span.next a",
        nowrap: true,
        slideExpr: 'ul'
      });
    } else {
      $('.photoBox .galleryControls').hide();
    }
}



/*-------------------------------------------------
   DOCUMENT READY
-------------------------------------------------*/
$(document).ready(function(){ 

  // Footer Nav items styling
  $('ul.footerNav li ul').parent('li').addClass('hasSub');
  $('ul.footerNav > li:first').addClass('first');

  // Add class to last item in sidenav for bottom border
  $('.sideNav li:last').addClass('last');
  $('.sideNav li:first').addClass('first');

  // Add styles to Contact Page table 
  $('#contactPg td:nth-child(2n), #contactPg th:nth-child(2n)').css({'text-align':'right'});
  $('#contactPg table tr:nth-child(2n)').children('td').css({'background-color':'#fff'});
  $('.locationBox:first').css({'margin-top':'0'})
  
  // Projects Landing Page
  $('.projects li:nth-child(2n)').addClass('last');

  // Contact Page
  // $('.locationBox:first').addClass('pullUp');
  // $('#contactPg #primaryContent h2:first').addClass('firstH2');

  setupExternalLinks()
  initHomeBannerCycle();
  setupPortalNav();
  initPortfolioGallery();
});
