var scrollTarget = null;
var docViewTop;
var homeBottomOffset;
var navBottomOffset;
  
var caseStudiesOffset;
var processOffset;
var differencesOffset;
var contactOffset;
  
var navCaseStudiesOffset;
var navProcessOffset;
var navDifferencesOffset;
var navContactOffset;

function checkOffsets() {
  docViewTop = $(window).scrollTop();
  homeBottomOffset = $('#home-bottom').offset().top;
  navBottomOffset = $('#nav-bottom').offset().top;
  
  caseStudiesOffset = $('#case-studies').offset().top;
  processOffset = $('#process').offset().top;
  differencesOffset = $('#differences').offset().top;
  contactOffset = $('#contact').offset().top;
  
  navCaseStudiesOffset = $('#nav-case-studies').offset().top;
  navProcessOffset = $('#nav-process').offset().top;
  navDifferencesOffset = $('#nav-differences').offset().top;
  navContactOffset = $('#nav-contact').offset().top;
}

function updateScrollInfo() {
  tick = 0;
  
  checkOffsets();
  
  if (navCaseStudiesOffset < caseStudiesOffset) {
    /*$('#navigation').animate({
      top: '116px'
    }, 300);*/
    if ($.browser.msie) {
      //IE cannot have opacity applied to transparent PNGs, so the workaround is to show/hide them instead
      //http://stackoverflow.com/questions/1251416/png-transparency-problems-in-ie8
      $('#logo-dp img').show();
      $('#tag-line').css('text-indent', '0');
    } else {
      $('#logo-dp, #tag-line').animate({
        opacity: '100%'
      }, 300);
    }
    if (debug) log('in home');
    
    $('#navigation a').removeClass('active');
    
  } else {
    
    if (debug) log('not home');
    
    closeNews();
    
    if (caseStudiesOffset < navCaseStudiesOffset && navCaseStudiesOffset < processOffset) {
      if (debug) log('case studies');
      $('#navigation a').not('#nav-case-studies').removeClass('active');
      $('#nav-case-studies').addClass('active');
    }
    if (processOffset < navProcessOffset && navProcessOffset < differencesOffset) {
      if (debug) log('process');
      $('#navigation a').not('#nav-process').removeClass('active');
      $('#nav-process').addClass('active');
    }
    if (differencesOffset < navDifferencesOffset && navDifferencesOffset < contactOffset) {
      if (debug) log('differences');
      $('#navigation a').not('#nav-differences').removeClass('active');
      $('#nav-differences').addClass('active');
    }
    if (contactOffset < navContactOffset) {
      if (debug) log('contact');
      $('#navigation a').not('#nav-contact').removeClass('active');
      $('#nav-contact').addClass('active');
      if ($.browser.msie) {
        $('#logo-dp img').show();
        $('#tag-line').css('text-indent', '0');
      } else {
        $('#logo-dp, #tag-line').animate({
          opacity: '100%'
        }, 300);
      }
    } else {
      if ($.browser.msie) {
        $('#logo-dp img').hide();
        $('#tag-line').css('text-indent', '-9999px');
      } else {
        $('#logo-dp, #tag-line').animate({
          opacity: 0
        }, 300);
      }
    }
  }
}

function updateSizes() {
  checkBrowserWidth();
  var windowHeight = window.innerHeight;
  var footerHeight = $('#footer').height();
  if (debug) log('window height: ' + windowHeight);
  $('#contact').css('min-height', (windowHeight-footerHeight-6) + 'px');
}

function closeNews() {
  if (newsVisible) {
    $('#news-window').slideUp();
    $('#news-bar').removeClass('open');
    $('#navigation').animate({
      top: '-=231'
    }, 400, function() {
      checkOffsets();
      if (scrollTarget != null) {
        var destination = $(scrollTarget).offset().top;
        scrollToPage(destination, null);
        scrollTarget = null;
      }
      recordLink('News closed');
    });
    newsVisible = false;
  }
}
function openNews() {
  if (!newsVisible) {
    $('#navigation').animate({
      top: '+=231'
    }, 400, function() {
      if (debug) log('open news is done');
      checkOffsets();
      recordLink('News opened');
    });
    $('#news-window').slideDown(400);
    $('#news-bar').addClass('open');
    newsVisible = true;
  }
}

function scrollToPage(destination, elementClicked) {
  $('html,body').animate({
    scrollTop: (destination)
  }, 1500, 'easeInOutCubic', function() {
    if (debug) log('Got to ' + elementClicked);
    updateScrollInfo();
  //$(this).addClass('active');
  });
}

$(function() {
  
  updateSizes();
  $(window).resize(updateSizes);
  
  //NEWS BAR START
  $('#news-button').click(function() {
    if (newsVisible) {
      closeNews();
    } else {
      openNews();
    }
    return false;
  });
  
  $('.news .story').hover(function() {
    $(this).addClass('story-active');
  }, function(){
    $(this).removeClass('story-active');
  });
  
  //news story scroller
  $('#news-bar .news').cycle({
    fx: 'scrollVert'
    ,
    slideExpr: '.row'
    ,
    prev: '#news-prev'
    ,
    next: '#news-next'
    ,
    pager: '#news-nav .pager'
    ,
    pause: true
    ,
    timeout: 0//disable auto-scroll
  });
  
  //news will only scroll when the news bar is open
  //$('#news-bar .news').cycle('pause');
  //NEWS BAR END
  
  //CASE STUDY LOGO SCROLLER START
  var autoScrollStep = 1; 
  var autoScrollInterval = 50;
  $('#logo-scroller').smoothDivScroll({
    scrollingHotSpotLeft: '#fake-left'
    ,
    scrollingHotSpotRight: '#fake-right'
    ,
    autoScroll: 'onstart'
    ,
    autoScrollDirection: 'endlessloopright'
    ,
    autoScrollStep: autoScrollStep
    ,
    autoScrollInterval: autoScrollInterval
    ,
    visibleHotSpots: ''//'always'
  });
  $('#logo-scroller .scroll-left').hover(function() {
    $('#logo-scroller').smoothDivScroll('option', 'autoScrollDirection', 'endlessloopright');
    recordLink('logo-scroll', 'left');
  });
  $('#logo-scroller .scroll-right').hover(function() {
    $('#logo-scroller').smoothDivScroll('option', 'autoScrollDirection', 'endlessloopleft');
    recordLink('logo-scroll', 'right');
  });
  $('#logo-scroller .scroll-middle').hover(function() {
    $('#logo-scroller').smoothDivScroll('stopAutoScroll');
    recordLink('logo-scroll', 'middle');
  }, function() {
    $('#logo-scroller').smoothDivScroll('startAutoScroll');
  });
  
  $('.speed-1').hover(function(){
    $('#logo-scroller').smoothDivScroll('option', 'autoScrollStep', autoScrollStep*2);
  }, function() {
    $('#logo-scroller').smoothDivScroll('option', 'autoScrollStep', autoScrollStep);
  });
  
  $('.speed-2').hover(function(){
    $('#logo-scroller').smoothDivScroll('option', 'autoScrollStep', autoScrollStep*4);
  }, function() {
    $('#logo-scroller').smoothDivScroll('option', 'autoScrollStep', autoScrollStep);
  });
  $('.speed-3').hover(function(){
    $('#logo-scroller').smoothDivScroll('option', 'autoScrollStep', autoScrollStep*6);
  }, function() {
    $('#logo-scroller').smoothDivScroll('option', 'autoScrollStep', autoScrollStep);
  });
  
  //CASE STUDY LOGO SCROLLER END
  
  //CASE STUDY LIGHTBOX START
  $('#case-studies-wrapper a').hover(function() {
    $(this).addClass('cs-hover');
  }, function() {
    $(this).removeClass('cs-hover');
  });
  $('#case-studies-wrapper a').colorbox({
    iframe: true
    ,
    speed: 500
    ,
    fastIframe: true
    ,
    innerWidth: 931
    ,
    innerHeight: 835
    ,
    initialWidth: 973
    ,
    initialHeight: 920
    ,
    current: ''
    ,
    close: 'or press esc to close'
    ,
    opacity: 0.85
  //,fixed: true
  });
  //CASE STUDY LIGHTBOX END
  
  //PROCESS PAGE ACCORDION START
  $('#p-accordion').easyAccordion({
    slideNum: false
  });
  
  $('#p-accordion .arrow').not('.grow .arrow').click(function() {
    $(this).parents('dd').next().addClass('active').activateSlide();
    return false;
  });
  $('#p-accordion dd.grow .arrow').click(function() {
    if (debug) log('previous');
    $('dt.strategy').addClass('active').activateSlide();
    //$(this).parents('dd').prev('dt').removeClass('active').prev('dt').addClass('active').activateSlide();
    return false;
  });
  
  var servicesOpen = false;
  $('#view-services').click(function() {
    $('#services .column').slideToggle();
    //$(this).toggleClass('active');
    servicesOpen = !servicesOpen;
    if (servicesOpen) {
      recordLink('List Services', 'open', null, null);
    } else {
      recordLink('List Services', 'close', null, null);
    }
    return false;
  });
  
  //http://benalman.com/projects/jquery-dotimeout-plugin/
  $.doTimeout('growcheck', 500, function() {
    if ($('dt.grow').hasClass('active')) {
      $('#view-services').addClass('active');
      return false;
    } 
    return true;
  });
  //PROCESS PAGE ACCORDION END
  
  //DIFFERENCES PAGE START
  $('a.d-link').click(function() {
    //$(this).next('.sub')
    return false;
  });
  
  //$('#differences-wrapper .slice').addClass('bg1');
  
  var lastIndex = -1;
  var loaded = 0;
  var direction = 'right';
  var hovering = false;
  for (var i = 1; i < 6; i++) {
    $('<img />').load(function() {
      ++loaded;
      if (debug) log('Loaded ' + loaded);
      if (loaded == 5) {
        $('#differences-wrapper .d-link').hover(function(e) {
          hovering = true;
          var index = $('#differences-wrapper .d-link').index(this);
          /* if we hover the current one, then don't do anything */
          if(index == lastIndex) {
            //log('same: ' + lastIndex);
            return;
          }
    
          /* item is bg1, bg2, bg3, bg4, bg5, depending where we are hovering */
          var item = e.target.id;
          
          if (index < lastIndex || (lastIndex == 0 && item == 'bg5')) {
            //log('left-to-right ' + item);
            //if we hover on the first slice or if we come from the last one, then the images should move left-to-right
            $('#differences-wrapper .slice').animate({
              backgroundPosition: '-700px 0'
            }, 0).removeClass('bg0 bg1 bg2 bg3 bg4 bg5').addClass(item);
            move(1, item);
          } else {
            //log('right-to-left ' + item);
            $('#differences-wrapper .slice').animate({
              backgroundPosition: '700px 0'
            }, 0).removeClass('bg0 bg1 bg2 bg3 bg4 bg5').addClass(item);
            move(0, item);
          }
    
          /* change the lastIndex element */
          lastIndex = index;
          
        }, function() {
          hovering = false;
        });
      }
    }).attr('src', '/images/bg' + i + '.jpg');
  }
  
  $('#differences-wrapper').hover(function() {
    log('on');
  }, function() {
    log('off');
    lastIndex = -1;
    /*if (!$('#differences-wrapper .slice').hasClass('bg0')) {
      $('#differences-wrapper .slice').animate({backgroundPosition: '-700px 0'}, 0).removeClass('bg0 bg1 bg2 bg3 bg4 bg5').addClass('bg0');
      move(1, 'bg0');
    }*/
  });
  
  //dir: 0 = right-to-left
  //dir: 1 = left-to-rigt
  function move(dir, item) {
    if (1 == dir) {
      //log('move left');
      $('#slice-1').stop().animate({backgroundPosition:"0 0"}, 200);
      $('#slice-2').stop().animate({backgroundPosition:'-140px 0px'}, 300);
      $('#slice-3').stop().animate({backgroundPosition:'-280px 0px'}, 400);
      $('#slice-4').stop().animate({backgroundPosition:'-420px 0px'}, 500);
      $('#slice-5').stop().animate({backgroundPosition:'-560px 0px'}, 600, function() {
        $('#differences-wrapper').removeClass('bg0 bg1 bg2 bg3 bg4 bg5').addClass(item);
      });
    } else {
      $('#slice-1').stop().animate({backgroundPosition:"0 0"}, 600, function() {
        $('#differences-wrapper').removeClass('bg0 bg1 bg2 bg3 bg4 bg5').addClass(item);
      });
      $('#slice-2').stop().animate({backgroundPosition:'-140px 0px'}, 500);
      $('#slice-3').stop().animate({backgroundPosition:'-280px 0px'}, 400);
      $('#slice-4').stop().animate({backgroundPosition:'-420px 0px'}, 300);
      $('#slice-5').stop().animate({backgroundPosition:'-560px 0px'}, 200);
    }
  }
  
  
  $('#diff-menu .level-1').hover(function() {
    var slice = 'bg-' + $(this).children('a.d-link').attr('rel');
    if (debug) log(slice);
    //$('#differences-wrapper').removeAttr('class').addClass(slice);
    $(this).addClass('hover');
    $(this).find('.sub').animate({
      opacity: 1
    }, 300);
  }, function() {
    //$('#differences-wrapper').removeAttr('class').addClass('differences-off');
    $(this).removeClass('hover');
    $(this).find('.sub').animate({
      opacity: 0
    }, 300);
  });
  
  $('#diff-menu .d-find').click(function() {
    $(this).parent().next('.story').toggle();
    return false;
  });
  //DIFFERENCES PAGE END
  
  //CONTACT PAGE START
  //contact page highlighting
  $('.people a').click(function() {
    $('.people a').removeClass('active');
    $(this).addClass('active');
    var dale = $(this).hasClass('dale');
    var rel = (dale) ? '#dale-nav' : '#brennan-nav';
    var href = $(this).attr('href');
    $('.page-contact .tabs .nav li').removeClass('active');
    $(rel).addClass('active');
    $('.page-contact .tab').not(href).removeClass('tab-active');
    $(href).addClass('tab-active');
    
    return false;
  });
  $('.page-contact .nav li').hover(function() {
    $('.page-contact .nav li').not(this).removeClass('active');
    $(this).addClass('active');
    var dale = $(this).hasClass('dale');
    var rel = (dale) ? '#dale-tab' : '#brennan-tab';
    var pic = (dale) ? '#dale-pic' : '#brennan-pic';
    $('.page-contact .tab').removeClass('tab-active');
    $('.page-contact ' + rel).addClass('tab-active');
    
    $('.people a').removeClass('active');
    if (debug) log(rel);
    $(pic).addClass('active');
  }, function() {});
  //CONTACT PAGE END
  
  
  //http://flesler.blogspot.com/2007/10/jqueryscrollto.html
  //sample: http://www.bountybev.com/assets/js/navigation.js
  $('#logo, #logo-dp, #navigation a').click(function() {
    //$('#navigation a').not(this).removeClass('active');
    
    var elementClicked = $(this).attr('href');
    var destination = $(elementClicked).offset().top;
    if (debug) log('going to ' + elementClicked + ', vertical offset = ' + destination);
    if (newsVisible) {
      log('news visible');
      scrollTarget = elementClicked;
      closeNews();
    } else {
      scrollToPage(destination, elementClicked);
    }
    
    return false;
  });
  
  //scroll position actions
  scrollStop = setTimeout(updateScrollInfo, 250);
  
  $(window).scroll(function(){ 
    tick++;
    clearTimeout(scrollStop);
    scrollStop = setTimeout(updateScrollInfo, 250);
    if (tick > 10) {
      updateScrollInfo();
    }
  });
  
});
