$(document).ready(function() {

  $("a[rel='wordpressLightbox']").colorbox({transition:"none", width:"75%", height:"75%"});


  if ($('#tweet').length) {
    //use the tweet jquery plugin
    $('#tweet').tweet({
      username: twitterAccount,
      count: 6,
      loading_text: "Loading tweets...",
      teaser_div: "tweet"
    });
  }


  // Validate forms
  if ($('form').length) {
    $('form').each(function() {
      var form = $(this);
      $('input[type="submit"]', form).click(function() {
        $(form).validate();
      })
    });
  }

  // handle the focus stage on the small newsletter form
  if ($('#newsletterSmall').length) {
    var newsletterForm = $('#newsletterSmall'),
        $emailInput = $('input.email', newsletterForm);

    $emailInput.focus(
        function() {

          if ($emailInput.val().toUpperCase() == 'YOUR EMAIL') $emailInput.val('');
        }).focusout(function() {
          if ($emailInput.val() == '') $emailInput.val('Your email');
        });
  }

  // scroll to top
  // fade in #back-top
  if ($('#topLink').length) {
    var $topLink = $('#topLink');
    $(window).scroll(function () {
      if ($(this).scrollTop() > 400) {
        $topLink.fadeIn();
      } else {
        $topLink.fadeOut();
      }
    });

    // scroll body to 0px on click
    $topLink.click(function () {
      $('html, body').animate({scrollTop:0}, 800, 'easeOutQuad');

      return false;
    });
  }


  if ($('#stall-finder-map').length) {

    $('.navTabs ul li').eq(0).addClass('active');
    $('.innerContentWrapper').eq(0).css({"opacity":"1","z-index":"9999","left":"9px","top":"9px"});

    swfobject.embedSWF("swf/store-finder.swf", "mapsStores", "960", "1065", "9.0.0");


    $('.navTabs ul li a').click(function(e) {
      e.preventDefault();
      if ($(this).parent('li').hasClass('active')) {

      } else {
        $('.navTabs ul li.active').removeClass('active');
        $(this).parent('li').addClass('active');
        $('.innerContentWrapper').css({"left":"9999px","opacity":"0","z-index":"1"}).hide();
        if ($(this).hasClass('stores')) {
          $('.mapContent .stores').show()
              .css({"left":"9px","top":"9px","z-index":"9999"})
              .animate({"opacity":"1"}, 400);
        } else if ($(this).hasClass('parking')) {
          $('.mapContent .parking').show()
              .css({"left":"9px","top":"9px","z-index":"9999"})
              .animate({"opacity":"1"}, 400);
        } else {
          $('.mapContent .getting-here').show()
              .css({"left":"9px","top":"9px","z-index":"9999"})
              .animate({"opacity":"1"}, 400);
        }
      }
    });

  }
});

function rollOverStore(storeId) {
  var $rollover = $('#mapsMain a:contains("' + storeId + '")');
}


function header_image_resize(hiddenImages) {
  var window_w = $(window).width(),
      grid_w = $('.headerMiddleInner').width();

  if (grid_w > window_w) {
    var newRight = Math.ceil((grid_w - window_w) / 2) - 207;
    $('.headerMiddleInner').css({"right":newRight + "px"});
  } else {
    $('.headerMiddleInner').css({"right":"0"});
  }
  if (hiddenImages) $('.headerMiddleInner img').css({'opacity': 0}).show().animate({opacity: 1}, 500, 'easeOutQuad');
}

$(window).load(
    function() {
      var accum_width = 0;
      $('.headerMiddleInner img').each(function() {
        accum_width += $(this).width();
      });
      $('.headerMiddleInner').width(accum_width);
      header_image_resize(true);
      $(window).resize(function() {
        header_image_resize(false)
      });
    }
);



