
$(document).ready(function() {

  // fade out the logo, fade in the carousel and scroll to the middle-most item when the page loads
    
    $("#spotco-large-type-logo").delay(2000).fadeOut(100, function() {
    
        $("#makeMeScrollable").addClass('visible').animate({ opacity: 1 });
        
        if ($.browser.msie) { 
            $(".scrollingHotSpotRight").css({ 'visibility': 'hidden' }); 
        }
        
    });

    $("#makeMeScrollable").smoothDivScroll({
        autoScroll: "onstart", 
        autoScrollSpeed: 1, 
        autoScrollDirection: "backandforth", 
        scrollingSpeed: 20, 
        pauseAutoScroll: "",
        visibleHotSpots: "always",
        hiddenOnStart: false,
        pauseAutoScroll: "mouseover"
    });

  $('#branding ul li ul').hide();
  $('#branding ul li.current_page_item ul, #branding ul li.current_page_ancestor ul').show();
  
  // hide child page
  $('#branding ul ul li.page_item ul').hide();
  
  // when navigate to a child page show all pages
  $('#branding ul ul li.current_page_item').parent("ul").show();
  
  // add active class on load
  $('#branding ul ul li.current_page_item, #branding ul ul li.current_page_parent').addClass('active');
  
  // show page list when toggle
  $('#branding ul ul li.active ul').show();
  
  // show current page's child page
  $('#branding ul ul li.current_page_item ul').show();
  $('#branding ul ul li span').click(function() { 
    
    $(this).parent().siblings().removeClass('active');
    $(this).parent().addClass("active");
    
    $(this).parent().parent().find('ul:visible').slideToggle();
    $(this).parent().children('ul').slideToggle();
    return ($(this).parent().children('ul'));
  });
  
  
  // Hide input labels when input is focused and re-show if input is empty on blur
  $("#s").focus(function() {
    var elementId = $(this).attr('id');
    $("label[for='" + elementId + "']").hide();
  });
  $("#s").blur(function() {
    var element = $(this);
    var elementId = element.attr('id');
    if (element.val().trim() == '') { $("label[for='" + elementId + "']").show(); }
  });
  
  // sets up portfolio image carousel
  $("#portfolio-image-carousel").jCarouselLite({ 
    visible: 1, 
    scroll: 1, 
    speed: 1500, 
    btnNext: "#next-item-arrow",
    btnPrev: "#previous-item-arrow",
    btnGo: [
      ".btn-1",
      ".btn-2",
      ".btn-3",
      ".btn-4",
      ".btn-5",
      ".btn-6",
      ".btn-7",
      ".btn-8",
      ".btn-9",
      ".btn-10"
    ]
  });
  
  // sets up fading carousel for interior pages like About Us and Partial Client List.
  if ($("#header-carousel li").length > 1) {
    $("#header-carousel").jCarouselLite({
      visible: 1,
      scroll: 1,
      speed: 0,
      auto: 4000, 
      circular: true,
      beforeStart: function(a) {
        $(a).parent().fadeTo(800, 0);
      },
      afterEnd: function(a) {
        $(a).parent().fadeTo(800, 1);
      }
    });
  }
  
  // adds support for highlighting the active image when clicked from the thumbnail navigation
  $("#carousel-navigation li a").click(function() {
    $(this).blur();
    $(this).parent().siblings().removeClass('active');
    $(this).parent().addClass('active');
  })
  
  // support for vertical centering of image inside of carousel
  var verticallyCenterElement = function(element) {
    var newHeight = ($(element).parent().height() - $($(element).parent().find('img')[0]).attr('height')) / 2;
    $(element).css({ paddingTop: newHeight + 'px' });
  }
  $("#portfolio-image-carousel img, .flash-video-wrapper").each(function() { verticallyCenterElement(this); });
  
  // highlights the portfolio item text when rolling over the nav, and vice versa
  $("#branding ul ul ul li a, #portfolio-items li a").hover(function() {
    var url = $(this).attr('href');
    $("a[href=" + url + "]").addClass('hovered');
  }, function() {
    var url = $(this).attr('href');
    $("a[href=" + url + "]").removeClass('hovered');
  })

  // Allows for switching between sections of case study content and highlighting nav
  $("#content-navigation a").click(function() {
    $("#content-sections li").hide();
    $("#content-navigation li a").removeClass('active');
    
    $($(this).addClass('active').attr('href')).show();
    // return false;
  })
  
  
  // handle flash video embedding as unobtrusively as possibleich
  $(".flash-video").each(function() {
    var flashVideo = $(this);
    
    var embedDiv = flashVideo.find('.flash-video-embed');
    embedDiv.attr('id', 'div-' + Math.random());
    
    swfobject.embedSWF(
      flashVideo.attr('data-url'),
      embedDiv.attr('id'),
      flashVideo.attr('data-width'),
      flashVideo.attr('data-height'),
      "9.0.0", 
      "expressInstall.swf",
      { 
        file: flashVideo.attr('data-file'), 
        image: flashVideo.attr('data-image')
      },
      {},
      {},
      function() { 
        verticallyCenterElement(flashVideo.find('.flash-wrapper'));
      }
    );
  });
  
  $(".flash-category-video").each(function() {
    swfobject.embedSWF(
      $(this).attr('data-url'),
      $(this).attr('id'),
      $(this).attr('data-width'),
      $(this).attr('data-height'),
      "9.00",
      "expressInstall.swf",
      {
        file: $(this).attr('data-file'),
        autostart: true,
        repeat: 'always',
        controlbar: 'none'
      },
      {},
      {}
    );
  });
  
  var controlsHeight = 26;
  $(".flash-audio img").each(function() {
    var paddingHeight = 2 * parseInt($(this).css('padding-top'));
    $(this).css({ 'top': -( $(this).height() + controlsHeight + paddingHeight) + 'px' });
  });
  
  // Archives dropdown for month listing
  $("#month-dropdown").change(function() {
    var currentValue = $(this).val();
    if (currentValue) {
      window.location.href = currentValue;
    }
  });
  
  // lower the lights
  $("#shadow").css("height", $(document).height() + 'px');
  $(".lightSwitcher").click(function(){
    if ($("#shadow").is(":hidden")) {
      $("#shadow").fadeIn();
      $(this).html("Turn on the lights"); 
    } else {
      $("#shadow").fadeOut();
      $(this).html("Turn off the lights");
    }
    return false;
  });
  
      // Font replacement
    Cufon.replace('#main-navigation>ul>li>a, #welcome-to-spotco h2, article[class="about"] h2', 
      { hover: true, fontFamily: "TradeGothicCondensed", fontWeight: 700 } );
    Cufon.replace('#welcome-to-spotco p, article>header>h1[class!="bordered"], article[class!="about"] h2, article blockquote',  
      { hover: true, fontFamily: "TradeGothicCondensed", fontWeight: 500 } );
    Cufon.replace('#latest-and-greatest h2', 
      { fontFamily: "TradeGothic", fontWeight: 700 } );

  
});
