$(document).ready(function() {

  document.createElement('header');
  document.createElement('footer');

  // Main Marquee
  $("#sliderContainer").scrollable({
    circular:true,
    touch: false
  }).navigator().autoscroll({
    autoplay: true,
    interval: 7000,
    autopause: true,
    touch: false
  }).navigator('#marqueeNavi');
  $("#athletesSliderContainer").scrollable({
    keyboard:false,
    circular:true,
    touch: false,
    prev:'.athletesPrev',
    next:'.athletesNext'
  }).navigator('#athletesNavi');
    
  $("div.product-showcase > ul").tabs("#tabs-featured-products > div");
  
  // Columns with margins fix
  $('div.product:nth-child(4n+4)').addClass('right');
  $('div.product:nth-child(5n+5)').addClass('left');
  $('#callouts a:nth-child(4n+4)').addClass('right');
  
  $("div.athelete").hover(
    function () {
      $(this).addClass("hover");
      $('div.blurb', this).fadeIn(160);
    },
    function () {
      $(this).removeClass("hover");
      $('div.blurb', this).fadeOut(160);
    }
  );
  
  // Form Swap Values
  $(function() {
  	swapValues = [];
  		$(".replaceValue").each(function(i){
  			swapValues[i] = $(this).val();
  			$(this).focus(function(){
  				if ($(this).val() == swapValues[i]) {
  					$(this).val("");
  				}
  			}).blur(function(){
  				if ($.trim($(this).val()) == "") {
  					$(this).val(swapValues[i]);
  			}
  		});
  	});
  });
  
  //Latest Tweet
  $(".tweet").tweet({
    username: "phiten",
    join_text: "auto",
    avatar_size: 0,
    count: 1,
    auto_join_text_default: "we said,", 
    auto_join_text_ed: "we",
    auto_join_text_ing: "we were",
    auto_join_text_reply: "we replied to",
    auto_join_text_url: "we were checking out",
    loading_text: "loading tweets..."
  });

  //Fancybox
  $("a.mlb").fancybox({
    padding: 0,
    margin: 0,
    onComplete: function() {
      $('#mlbDisclaimer').show();
      $('#interview').hide();
    },
    onClosed: function() {
	    $('#mlbDisclaimer').hide();
	    $('#interview').hide();
  	}
	});
	$("a.interview").fancybox({
    padding: 0,
    margin: 0,
    onComplete: function() {
      $('#mlbDisclaimer').hide();
      $('#interview').show();
    },
    onClosed: function() {
	    $('#interview').hide();
	    $('#mlbDisclaimer').hide();
  	}
	});
  $("a.notMlb").fancybox({
    padding: 0,
    margin: 0,
    onComplete: function() {
      $('#mlbDisclaimer').hide();
      $('#interview').hide();
    },
    onClosed: function() {
	    $('#mlbDisclaimer').hide();
	    $('#interview').hide();
  	}
	});
	    
  $('li.search a').click(function (e) {
      e.preventDefault();
      
      $('div.search .form').fadeToggle('fast', function () {
          $(this).find('#term').focus();
      });
  });
  
  // Hide Address Bar on Load
  $(window).bind("load",function() {
  	setTimeout(function(){
  		window.scrollTo(0, 1);
  	}, 0);
  });
  
  //Navigation
  function navHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show();
  }
  
  function navHoverOut(){
    $(this).find(".sub").stop().fadeTo('fast', 0, function() {
      $(this).hide();
    });
  }
  
  $("#navigation ul.main li a.mainLink").delay(150).hover(function() {
    $("#subnavOverlay").toggle();
  });
  
  $("#navigation ul.main li .sub").hover(function() {
    $("#subnavOverlay").toggle();
  });
  
  var config = {
    sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
    interval: 0, // number = milliseconds for onMouseOver polling interval
    over: navHoverOver, // function = onMouseOver callback (REQUIRED)
    timeout: 0, // number = milliseconds delay before onMouseOut
    out: navHoverOut // function = onMouseOut callback (REQUIRED)
  };

  $("#navigation ul.main .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
  $("#navigation ul.main li").hoverIntent(config); //Trigger Hover intent with custom configurations
  
  //Navigation Hover Image Switch
  $("#navigation ul.main li .sub li a").mouseenter(function() {
    $(this).next("img").stop(true, true).fadeIn(200);
    $("img.initial").stop(true, true).fadeOut(100);
  }).mouseleave(function() {
    $(this).next("img").stop(true, true).fadeOut(200);
    $("img.initial").stop(true, true).delay(400).fadeIn(200);
  });

});
