if (navigator.userAgent.match(/MSIE\ 6\.0/i) || is_mobile()) {
  if (document.location.href == 'http://www.stargroup1.com/' || document.location.href == 'http://www.stargroup1.com' || document.location.href == 'http://www.stargroup1.com/home') {

    window.location = 'http://www.stargroup1.com/welcome';
  }
}

// Use latest version if jquery
$jq = jQuery.noConflict();

// Declare our global vars - commonly used
var windowWidth, windowHeight, slideWidth, gutterWidth, currNav;

$jq(document).ready(function () {
  // Immediately calculate dimensions and make some adjustments
  windowResized();

  // Figure out what frame the user is in and update the ticker
  adjustMenuLocation();

  // Fix the line spacing and whatnot for browsers
  headerFixes();

  // Checks ipad orientation to fix the bio page... what a hack...
  updateOrientation();


  // New brands page
  if ($jq('.brand-logos-grid').length > 0) {

    var $grid = $jq('.brand-logos-grid');

    // When a logo is hovered over, we want to fade away all other logos
    // and fade in the description for this logo's brand.
    $jq('.brand-logo img',$grid).mouseover(function () {
      // fade all other logos
      $jq('.brand-logo img',$grid).not(this).stop(true).fadeTo(500, 0.15);
      // hide all other descriptions
      $jq('.brand-logo-desc',$grid).hide();

      // Get some position/dimension values needed to determine where the description
      // div should go
      var thisWidth = $jq(this).width();
      var thisHeight = $jq(this).height();
      var thisLeft = $jq(this).position().left;
      var thisTop = $jq(this).position().top;
      var containerWidth = $grid.width();
      var viewportHeight = $jq(window).height() - $jq('#footer').height();

      var $desc = $jq(this).parent().find('.brand-logo-desc');
      var descHeight = $desc.height();
      var descWidth = $desc.width();
      var newDescLeft = thisLeft + (thisWidth / 2) - (descWidth / 2);
      var newDescTop = (thisTop + thisHeight) - 10;

      // Make sure the description stays within the bounds of the container
      if (newDescLeft < 0) {
        newDescLeft = 0;
      }
      if ((newDescLeft + descWidth) > containerWidth) {
        newDescLeft = containerWidth - descWidth;
      }

      // Determine if we should put the description on the top or bottom of the logo
      // (depends on what will be more visibile)
      if ((newDescTop + descHeight + $grid.offset().top) > (viewportHeight + $jq(window).scrollTop())) {
        newDescTop = thisTop - descHeight + 10;
      }

      // Adjust the description to it's new position, and then fade it in
      $desc.css({
          left: newDescLeft,
          top: newDescTop
      }).stop(true).fadeTo(500,1);

    });

    // When hovering off a logo, we can fade in all the other logos
    // and hide the descriptions quickly
    $jq('.brand-logo img',$grid).mouseout(function (event) {
      // show all the other logos
      $jq('.brand-logo img',$grid).not(this).fadeTo(500, 1);

      // Hide all descriptions
      $jq(this).parent().parent().find('.brand-logo-desc').hide(200);
    });

    // Stop all fade out animations on a desc div if someone hovers on it
    // We also fade it back in case the user left it (causing a fade out),
    // but then went back into it
    $jq('.brand-logo-desc',$grid).mouseover(function () {
      // ensure this description is shown fully
      $jq(this).stop(true).fadeTo(500, 1);

      // make sure all the logos (except the one this desc belongs to) are
      // remained faded out
      $jq('.brand-logo img',$grid).not($jq(this).parent().find('img')).stop(true).fadeTo(500, 0.15);
    });

    // Hovered away from a description, fade in the other logos and hide
    // this description (basically same code for when user hovers out of a logo)
    $jq('.brand-logo-desc',$grid).mouseout(function () {
      // show all the other logos
      $jq('.brand-logo img',$grid).not(this).fadeTo(500, 1);

      // Hide this description
      $jq(this).hide(200);

    });
  }


  // Hide the nav bar on everything but the homepage, for non-iOS
  if (!is_mobile() && $jq('body.front').length == 0) {
    $jq('#footer-slidenav').hide();
  } else {
    $jq('#footer-slidenav').show();
  }

  // Did the user click a nav element to slide to that area?
  $jq('#footer-slidenav li a').click(function (e) {

    if ($jq('body.front').length > 0 && !is_mobile()) {
      e.preventDefault();

      $clicked = $jq(this);
      var curr = $jq(this).parent().index();
      var snapTo = (windowWidth * curr) - (gutterWidth * curr);
      // Slowly animate the user to that area
      $jq('html,body').stop().animate({
        scrollLeft: snapTo
      }, 1000, 'swing', function () {
        $clicked.parent().parent().children().removeClass('active');
        $clicked.parent().addClass('active');

        if ((curr + 1) == $jq('#footer-slidenav-nav li').length) {
          $jq('#footer-slidenav #scrollright').hide();
        } else {
          $jq('#footer-slidenav #scrollright').show();
        }
      });

      return false;
    }
  });

  // Slide the homepage if the arrow is clicked
  $jq('#footer-slidenav #scrollright').click(function () {
    var activeSlideIndex = $jq('#footer-slidenav-nav li.active').index() + 1;
    if (activeSlideIndex < $jq('#footer-slidenav-nav li').length) {

      var snapTo = (windowWidth * activeSlideIndex) - (gutterWidth * activeSlideIndex);

      $jq('html,body').stop().animate({
        scrollLeft: snapTo
      }, 1000, 'swing', function () {
        $jq('#footer-slidenav-nav li').removeClass('active');
        $jq('#footer-slidenav-nav li').eq(activeSlideIndex).addClass('active');

        // remove the slide button
        if ((activeSlideIndex + 1) == $jq('#footer-slidenav-nav li').length) {
          $jq('#footer-slidenav #scrollright').hide();
        }
      });
    }

  });


  // Key player hovers
  $jq('#node-11 .keyplayer-box').mouseover(function () {
    var width = $jq(this).width();
    var posLeft = $jq(this).position().left;
    var nodeID = $jq(this).attr('id').substring(14);

    // show desc
    $jq('#node-11 .keyplayers-descriptions').find('.keyplayer-desc').hide().end().find('.keyplayer-desc-' + nodeID).show();

    // show indicator
    $jq('#node-11 .keyplayers-linkboxes').find('.keyplayer-indicator').hide().end().find('#keyplayer-indicator-' + nodeID).show();
  });

  iOSfix();

  windowResized();



});

// Add resized function binder
$jq(window).resize(windowResized);

// When the user scrolls right, we want to change the active slide if necessary
$jq(window).scroll(adjustMenuLocation);



function adjustMenuLocation() {
  // no need to update this on iOS
  if (!is_mobile()) {

    var currLeft = $jq(this).scrollLeft();

    // cycle through each slide threshold to see which one the user is in
    $jq('#footer-slidenav li').each(function (index, element) {
      var currFrameStart = (slideWidth * index) - 100;
      var currFrameEnd = (slideWidth * (index + 1)) - 100;

      if ((currLeft >= currFrameStart && currLeft <= currFrameEnd)) {

        $jq('#footer-slidenav li').removeClass('active').eq(index).addClass('active');
        $f("*").each(function () {
          var $video = $jq(this.getParent());

          var pastRight = ($video.offset().left + $video.width() - parseInt($jq(".view-Homepage").css("margin-left"))) < $jq(window).scrollLeft();
          var pastLeft = ($jq(window).scrollLeft() + $jq(".slide").width()) < ($video.offset().left - parseInt($jq(".view-Homepage").css("margin-left")));
          if (pastRight || pastLeft) {
            this.pause();
          }
        });
        currNav = $jq('#footer-slidenav li').eq(index).text();
        return false; // found the slide we are in, no need to keep checking
      }
    });
  }

  adjustHeadshot();
}

// Window has been resized, re-evaluate the dimensions

function windowResized() {

  // re-evaluate commonly used dimensions
  windowWidth = $jq(window).width();
  windowHeight = $jq(window).height();
  slideWidth = $jq('.homepage-slider .slide').width();
  gutterWidth = windowWidth - slideWidth;

  if (!is_mobile()) {
    if ($jq('body').hasClass('front')) {

      // non commonly used
      var totalSlides = $jq('.homepage-slider .slide').length;
      var viewportHeight = windowHeight - $jq('#footer-slidenav').height() - $jq('#footer').height();

      // Adjust the left margin of the homepage slider to keep it centered
      var leftMargin = (windowWidth - slideWidth) / 2;
      $jq('#content-area .homepage-slider').css({
        width: (slideWidth * totalSlides) + leftMargin,
        marginLeft: leftMargin
      });

      if (viewportHeight < 650) {
        setMinHeight = 650;
      } else {
        setMinHeight = viewportHeight;
      }
      $jq('#content-area .homepage-slider .slide .node').css({
        minHeight: setMinHeight
      });

      // Adjust gutters
      $jq('#content-gutter-left').css({
        width: gutterWidth / 2,
        left: 0
      });
      $jq('#content-gutter-right').css({
        width: (gutterWidth / 2) + 30,
        left: slideWidth + (gutterWidth / 2)
      });
    }

    var viewportHeight = windowHeight - $jq('#footer').height();
    if (viewportHeight < $jq('#headshot img').height()) {
      $jq('#headshot img').height(viewportHeight);
    } else {
      if (viewportHeight <= 600) {
        $jq('#headshot img').height(viewportHeight);
      }
    }
  }
}

function iOSfix() {
  if (is_mobile() || navigator.userAgent.match(/MSIE\ 6\.0/i)) {
    $jq('#footer').css({
      position: 'static'
    });
    $jq('#footer-slidenav').css({
      position: 'static'
    }).show();
    $jq('#footer-slidenav a').css({
      color: '#EF3E42'
    });
    $jq('#content-area .homepage-slider').css({
      width: '100%',
      marginLeft: 0
    });
    $jq('#content-area .homepage-slider .slide').css({
      float: 'none',
      width: '100%'
    });
    $jq('#content-gutter-right, #content-gutter-left').hide();
    $jq('#scrollright').hide();
    $jq('#ipad-homeicon').show();
    $jq('#homeicon-container').hide();

    $jq('#headshot').css({
      height: $jq('body').height(),
      bottom: 0,
      position: 'absolute'
    }).find('img').css({
      bottom: 0,
      position: 'absolute'
    });

  }
}

function is_mobile() {
  var uagent = navigator.userAgent.toLowerCase();
  if (navigator && navigator.platform && (navigator.platform.match(/^(iPad|iPod|iPhone)$/) || uagent.search('android') > -1)) {
    return true;
  } else {
    return false;
  }
}

function headerFixes() {
  if (navigator && navigator.userAgent) {

    var headerHeight, headerLineHeight;
    if (navigator.userAgent.match(/Windows/i) && navigator.userAgent.match(/Chrome/i)) {
      headerHeight = '54px';
      headerLineHeight = '54px';
      $jq('#node-11 .keyplayers-descriptions .keyplayer-title').css('paddingTop', '10px');
    }

    if (navigator.userAgent.match(/Windows/i) && navigator.userAgent.match(/Firefox/i)) {
      headerHeight = '54px';
      headerLineHeight = '54px';
      $jq('#node-11 .keyplayers-descriptions .keyplayer-title').css('paddingTop', '10px');
    }

    $jq('h1.title, h2.title, div.newsheader').not('#header-nmajh').css({
      lineHeight: headerLineHeight,
      height: headerHeight
    });

  }

}

function updateOrientation() {
  adjustHeadshot();

  if (window.orientation == 90 || window.orientation == -90) {
    $jq('.node-type-biopage #content-inner').css('paddingTop', '30px');
  } else {
    $jq('.node-type-biopage #content-inner').css('paddingTop', '130px');
  }
}

function adjustHeadshot() {
  // adjust the position of the header images
  if (is_mobile()) {

    var windowArea = windowHeight;

    $jq('#headshot, #headshot img').height(500);
    var topSpacing = windowArea - 500 + $jq('#footer-slidenav').height();

    var scrollTop = $jq(window).scrollTop();

    var newTop = topSpacing + scrollTop;

    $jq('#headshot img').css({
      position: 'absolute',
      top: 0,
      bottom: 'auto'
    });
    $jq('#headshot').css('top', newTop);
  }
}
