/* External Links Sitewide */ 
function setupExternalLinks(){
  $('a[href^="http://"]').click( function() {
      var attr = $(this).attr('href');
      if( attr.search('whit.verbsite.com') < 0 && attr.search('whit-ny.com') < 0 ) {
        window.open( $(this).attr('href') );
        return false;
      } else {
        return true;
      }
    });
}

/* Homepage Cycle */
function setupHomeCycle(){
  $(".lookBookSlide li:nth-child(12n)").addClass('turnHere'); // class to specify when to advance icon nav

  var iconGroup = $('.iconNavSlide ul.icons');
  // Check for more than one set of icons, insert controls and initialize
  if (iconGroup.length > 1) {
    $('.iconNavSlide').after('<div class="iconNavControls"><a href="#" class="prev">prev</a> <a href="#" class="next">next</a></div>');
    $('.iconNavSlide').cycle({ 
      fx:     'scrollHorz', 
      speed:  'fast',
      cleartype: false,
      timeout: 0,
      prev: 'a.prev',
      next: 'a.next'
    });
  }
    // Callbacks and options for cycle version ONE
  function onBefore(currSlide, nextSlide) {
    $(nextSlide).animate({'opacity': 0});
  }
  function onAfter(startingSlide, curr, opts) {
    $(startingSlide).animate({'opacity': 1});

    var index = opts.currSlide;
    var advanceNav = $(this).hasClass('turnHere');
    if (index = advanceNav) {
      $('.iconNavSlide').cycle('next');
    }
  }
  var cycleOpts1 = {
    fx:     'scrollHorz', 
    speed: 1100,
    cssFirst: { opacity: 0 },
    timeout: 0,
    delay: -2000,
    pager:  'ul.icons',
    before: onBefore,
    after: onAfter,
    autostop: 1,
    nowrap: 1,
    pagerAnchorBuilder: function(idx, slide) { 
      return 'ul.icons li:eq(' + idx + ') a';
    }
  }
  // Callbacks and options for cycle version TWO
  function onAfter2(curr, opts) {
    var index = opts.currSlide;
    var advanceNav = $(this).hasClass('turnHere');
    if (index = advanceNav) {
      $('.iconNavSlide').cycle('next');
    }
  }
  var cycleOpts2 = {
    fx:     'fade', 
    speed: 900,
    timeout: 2500,
    pager:  'ul.icons',
    next: '.frameEdges',
    pagerAnchorBuilder: function(idx, slide) { 
      return 'ul.icons li:eq(' + idx + ') a';
    },
    after: onAfter2,
    autostop: 1,
    nowrap: 1
  };

  $.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
    $(pager).find('li').animate({'opacity' : 0.55}, 0).filter('li:eq('+currSlideIndex+')').animate({'opacity' : 1}, 'fast');
    $(pager).find('li').removeClass('activeSlide').filter('li:eq('+currSlideIndex+')').addClass('activeSlide');
  }; 
           
   // Initialize both cycles
  $('.scrollHorz .lookBookSlide').cycle(cycleOpts1);
  $('.fade .lookBookSlide').cycle(cycleOpts2);
  
  // Initially Pause Cycle Version 1
  $('.scrollHorz .lookBookSlide').cycle('pause');
  $('.lookBookSlide').cycle('pause');

   // Play & Pause Controls 
    $('#play').click(function () {
        $(this).addClass('hide').siblings('a#pause').removeClass('hide');
    $('.lookBookSlide').cycle('resume');
    });
    $('#pause').click(function () {
        $(this).addClass('hide').siblings('a#play').removeClass('hide');
    $('.lookBookSlide').cycle('pause');
    });
}

/* Homepage Accordion */
function setupHomeArchiveAccordian() {
  $('#lookBookArchive h2 a').click(function(){
    $('#lookBookArchive ul').slideToggle('fast');
  });
}

/* Contact Pages */
function initializeOverlabelValidate(){
  if(jQuery.fn.overlabel) {
    $("#contactForm label").overlabel();
  }
  if(jQuery.fn.validate) {
    $('#page form').validate();
  }
}

/* Inspiration ToolTip Version 2 */
function setupToolTip2(){
  if(jQuery.fn.tooltip_bubble) {
    $(".bt1 a[title]").tooltip_bubble({ 
          track: true, 
          delay: 500, 
          showURL: false, 
          opacity: 1, 
          fixPNG: true,
          showBody: " - ", 
          extraClass: "bubble", 
          top: -250, 
          left: -220,
          fade: 200
    });
  }
}

/* Lazy Load for Inspiration Pages */
function setupLazyLoad(){
  if(jQuery.fn.lazyload) {
    $("#inspirationPg #page div div img").lazyload({ 
      threshold : 0,
      placeholder: "lib/css/images/clear.gif",
      effect : "fadeIn"
    });
  }
}

/* Force iphone to show lazyload images */
function iphoneFix() {
  var agent = navigator.userAgent.toLowerCase();
  var is_iphone = (agent.indexOf('iphone')!=-1);
  if (is_iphone) { 
    $('#inspirationPg #page div div img').trigger("appear");
   }
}

/* Setup the Press link behavior */
function setupPressLinks() {
  $('.pressItems li:not(.active) a.external').click(function() {
    var $li = $(this).parent();
    
    window.location.href = $li.find('a:first').attr('href');
    
  });
}

/* Document Ready */
$(document).ready(function(){

  // Add class to last stockist column
  $("#stockistsPg #page div:nth-child(6)").addClass('last');

  Cufon.replace('.bodoni', { fontFamily: 'Bodoni BE', hover: true });
  Cufon.replace('.tradeGothic', { fontFamily: 'ITC Franklin Gothic', hover: true });
  
  setupExternalLinks();
  
  setupHomeCycle();
  
  setupHomeArchiveAccordian();
  setupLazyLoad();
  initializeOverlabelValidate();  
  setupToolTip2();
  iphoneFix();
  setupPressLinks();
  
  $('a[rel*=nogo]').click(function(){
    return false;   
  }); 
  
  if(jQuery.fn.pngFix) {
    $('.iconNavSlide ul.icons, .prev a, .next a').pngFix();
  }
  
  // Add class to lookBookArchive so that it can be altered based on layout
  $('.layout2').siblings('div#lookBookArchive').addClass('LBarchiveLayout2');
  $('.layout3').siblings('div#lookBookArchive').addClass('LBarchiveLayout3');

  // Option select on press page  
  $('#pressSelect').change(function() {
    window.location = $(this).val();
  });
  
  
});
