$(document).ready(function() {
  var controller = new Controller();
  controller.activateSlider();
});

function Slider() {
  var self = this;
  self.bars = $("#slideControl > li > div");
  self.slides = $("#slides > li");
  self.currentSlideIndex = 0;
  self.slideTimer;
  self.animFinished = true;
  
  self.slide = function(targetIndex) {
    return function() {
      if (targetIndex != self.currentSlideIndex) {
        if (targetIndex == 0 && self.currentSlideIndex == 2) {
          self.shiftRight(targetIndex + 1);
        }
        else if (targetIndex == 2 && self.currentSlideIndex == 0) {
          self.shiftLeft(targetIndex + 1);
        }
        else if (targetIndex > self.currentSlideIndex) {
          self.shiftRight(targetIndex + 1);
        }
        else if (targetIndex < self.currentSlideIndex) {
          self.shiftLeft(targetIndex + 1);
        }
      }
      self.currentSlideIndex = targetIndex;
    }
  };
  
  self.shiftRight = function(targetIndex) {
    self.stopSlideTimer();
    $("#slide_" + targetIndex ).css("left", "-950px");
    $("#slideBar_" + targetIndex).css("left", "-35px");
    self.slides.each(function() {
      $(this).animate({
        left: "+=950",
        queue: true
      }, 1000);
    });
    var pos = 0;
    self.bars.each(function() {
      $(this).animate({
        left: "+=35",
        queue: true
      }, 1000, function() {
        if (pos==2) {
          self.startSlideTimer();
        }
        pos++;
      });
    });
  };
  
  self.shiftLeft = function(targetIndex) {
    self.stopSlideTimer();
    $("#slide_" + targetIndex).css("left", "950px");
    $("#slideBar_" + targetIndex).css("left", "35px");
    self.slides.each(function() {
      $(this).animate({
        left: "-=950",
        queue: true
      }, 1000);
    });
    var pos = 0;
    self.bars.each(function() {
      $(this).animate({
        left: "-=35",
        queue: true
      }, 1000, function() {
        if (pos == 2) {
          self.startSlideTimer();
        }
      });
    });
  };
  
  self.startSlideTimer = function() {
    self.slideTimer = setTimeout(function() {
      self.shiftRight(((self.currentSlideIndex + 1) % 3) + 1);
      self.currentSlideIndex++;
    }, 5000);
  };
  
  self.stopSlideTimer = function() {
    clearTimeout(self.slideTimer);
  };
}

function Controller() {
  var self = this;
  self.slider = new Slider();
  
  self.activateSlider = function() {
    var slideControls = $("#slideControl > li");
    var currentControl = 0;
    slideControls.each(function() {
      $(this).click(self.slider.slide(currentControl));
      currentControl++;
    });
    self.slider.startSlideTimer();
  }
}
    
$(document).ready(function() {
  var pathname = window.location.pathname;
  var wholeUrl = document.location.href;
  
  if(pathname)
  {
    $('#topNav a[href$="' + pathname + '"]').attr('class', 'currentPage');
    
    $('#sideNav a[href$="' + pathname + '"]').attr('class', 'currentPageSubnav');
  }
  
  if(pathname == '/default.aspx' || wholeUrl == 'http://www.plugoutfitness.com/' || wholeUrl == 'http://www.plugoutfitness.com')
  {
    $("#content").css({"borderTop": "none", "paddingTop": "0"});
    $('#topNav a[href$="/default.aspx"]').attr('class', 'currentPage');
  }
  
  //$("#topNav a:last")
  
  //$("#topNav li:nth-child(8), #topNav li:nth-child(10)").hide();
  
  //$("#topNav li:nth-child(9) a").html("&nbsp;&nbsp&nbsp;&nbsp;My PlugOut").addClass("topNavLast");
  
  //$("#sitemap li:nth-child(9) a").html("myplugout");  
  
  var topLevelCategories = ["products", "about", "locations", "news", "blog", "contact", "My PlugOut"];
  
  for ( i = 0; i <= 7; i++)
      {
        if(pathname.indexOf(topLevelCategories[i]) > -1)
        {
            $("#topNav a[href$=" + topLevelCategories[i] + ".aspx]").addClass('currentPage');
        }
      }
  
  $('#links a').each(function() {
    if ( $(this).attr('href').indexOf('pdf') > -1 )
    {
        $(this).attr('target','_blank')
    }
  })
  
  /*if(pathname.indexOf('products') > -1)
  {
    $("#topNav a[href$='products.aspx']").addClass('currentPage')
  }
  else if(pathname.indexOf('about') > -1)
  {
      $("#topNav a[href$='about.aspx']").addClass('currentPage')
  }*/
});

