var LAYER_FADE_DURATION = 200; // ms

function getfp(idx) {
  if(navigator.appName.indexOf("Microsoft") != -1) { 
    return window['nke_homepage'+idx]; 
  } else { 
    return window.document['nke_homepage'+idx];
  }
}

function showLayer($layer, center, with_shade, offset)
{
  center = typeof(center) != 'undefined' ? center : true;
  with_shade = typeof(with_shade) != 'undefined' ? with_shade : true;
  offset = typeof(offset) != 'undefined' ? offset : 0;
  if ($('.layer:visible').length) $('.layer:visible').hide();
  if ($('.layer_mini:visible').length) $('.layer_mini:visible').hide();
  var layerTop = /*$(window).scrollTop() +*/ ($(window).height() / 2 - $layer.height() / 2) + offset;
  if (layerTop < 0) layerTop = 0;
  var layerLeft = $(window).scrollLeft() + ($(window).width() / 2 - $layer.width() / 2);
  if (layerLeft < 0) layerLeft = 0;
  $('#shade').css('height', $(document).height() + 'px');
  if (with_shade) $('#shade').fadeIn(LAYER_FADE_DURATION);
  $('.hide_when_layer_shown').fadeOut(LAYER_FADE_DURATION);
  $layer.fadeIn(LAYER_FADE_DURATION, function() {
    $layer.css('filter', '');
  });
  if (center)
  {
    $layer.css('top', layerTop + 'px');
    $layer.css('left', layerLeft + 'px');
  }
}


function hideLayer($layer)
{
  var movies = $('#video-layer .box div');
  var lngth = movies.length;
  var idx = 0;
  if (lngth > 0) {
    for (var i = 0;i < lngth;i++) {
      if ($(movies[i]).css('top') != '-1000px') {
        idx = i;
        break;
      }
    }
  }
  
  $('#shade').fadeOut(LAYER_FADE_DURATION);
  $('.hide_when_layer_shown').fadeIn(LAYER_FADE_DURATION);
  if ($layer)
    $layer.fadeOut(LAYER_FADE_DURATION);
  else
    $('.layer, .layer_mini').fadeOut(LAYER_FADE_DURATION);

  if (lngth > 0) {
    var fp = getfp(idx);
    fp.stopVideo();
  }
}

$(document).keydown(function(e) {
  var key = e.keyCode || e.which;
  if (key == 27) hideLayer();
});

$(document).ready(function() {

  mailContentSubmit = function() {
    $('#mail-form-loader').show();
    $.post($('#mail-url').val(), $(this).serialize(), function(data) {
      $('#mail-content').html(data);
      $('#mail-content form').submit(mailContentSubmit);
      bindCaptchaItems();
    });
    return false;
  };

  $('.feedback-link').click(function() {
    $('#mail-content').hide();
    $('#mail-loader').show();
    showLayer($('#mail'), true, false, -200);
    $.get($('#mail-url').val(), '', function(data) {
      $('#mail-loader').hide();
      $('#mail-content').html(data);
      $('#mail-content form').submit(mailContentSubmit);
      $('#mail-content').show();
    });
    return false;
  });

});    

