$(window).resize(function() {
  var min = 1000;
  if($('body').width() < min) {
    $('div#tag').css({
      'right': 'auto',
      'left': min - $('div#tag').width()
    });
  } else {
    $('div#tag').css({
      'left': 'auto',
      'right': 0
    });
  }
});

var kc_rotate_interval = 30000;
var kc_rotate_current = 'none';
function kc_rotate_bg() {
  if($('ul#backgrounds li').length) {
    if(kc_rotate_current == 'none') {
      kc_rotate_current = 0;
    } else {
      kc_rotate_current = (kc_rotate_current + 1) % $('ul#backgrounds li').length;
    }
    $('div#body_bg').css('background-image', $($('ul#backgrounds li').get(kc_rotate_current)).css('background-image'));
    
    if($('ul#backgrounds li').length > 1) {
      setTimeout(kc_rotate_bg, kc_rotate_interval);
    }
  }
}

var kc_nav_offset;

$(function() {
  $(window).resize();
  kc_rotate_bg();
  
  kc_nav_offset = {
    skew: parseInt($('div#page_nav').css('padding-top')),
    base: 362
  };
  
  $('div#page_nav ul.page_nav a.here').click(function() {
    var tgt = this.href;
    tgt = 'div.piece a.' + (tgt.substr(tgt.indexOf('#') + 1));
    
    // Scrolling window
    $.scrollTo(tgt, {
      duration: 2000,
      axis: 'y',
      offset: { top: -130 }
    });
    
    // Active
    $('div#page_nav ul.page_nav li').removeClass('active');
    $(this).parent().addClass('active');
    
    this.blur();
    return false;
  });
  
  $(window).scroll(function() {
    var top = $(window).scrollTop();
    $('div#page_nav').stop(true);
    if(top <= kc_nav_offset.base) {
      $('div#page_nav').css('padding-top', kc_nav_offset.skew);
    } else {
      $('div#page_nav').animate({ paddingTop: top - kc_nav_offset.base + kc_nav_offset.skew }, 1);
    }
  });
  
  if(location.hash != '') {
    $('div#page_nav ul.page_nav a[href='+(location.hash)+']').click();
    $(window).scroll();
  }
  
  $('a.signup_form').click(function() {
    $('form.signup_form').get(0).submit();
    return false;
  });
});
