layer_working = null;
LAYER_FADE_DURATION = 200; // ms

function showCallLayer($layer, center, with_shade)
{
  center = typeof(center) != 'undefined' ? center : true;
  with_shade = typeof(with_shade) != 'undefined' ? with_shade : true;
  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);
  if (layerTop < 0) layerTop = 0;
  var offs = $('body').width() - $('#content').width();
  var layerLeft = $(window).scrollLeft() + ($(window).width() / 2 - $layer.width() / 2);
  if (offs > 0) layerLeft -= offs;
  if (layerLeft < 0) layerLeft = 0;
 
  $('#shade').css('height', $(document).height() + 'px');
  if (with_shade) $('#shade').fadeIn(LAYER_FADE_DURATION, function(){
  });

  
  $('.hide_when_layer_shown').fadeOut(LAYER_FADE_DURATION);
  layer_working = true;


  $layer.fadeIn(LAYER_FADE_DURATION, function() {
    layer_working = false;
 
  });
  if (center)
  {
    $layer.css('top', layerTop + 'px');
//    $layer.css('left', layerLeft + 'px');
    $('.back-layer').show();
  }
}


function hideLayer($layer)
{
  layer_working = true;
  $('#shade').fadeOut(LAYER_FADE_DURATION);
  $('.hide_when_layer_shown').fadeIn(LAYER_FADE_DURATION);
  
  if ($layer)   
    $layer.fadeOut(LAYER_FADE_DURATION, function() { 
    $layer.css('filter', 'none');
    layer_working = false; 
    
    
    });
  else
    $('.layer, .layer_mini').fadeOut(LAYER_FADE_DURATION, function() { 
     layer_working = false; });
}

$(document).ready(function() {

  $('.callback_link').click(function() {
    $('#call-form-container').load($('#call-submit-url').text(), function() {
    	showCallLayer($('#call-layer'));
      bindCallForm();
    });
    return false;
  });

  bindCallForm = function() {
//    bindCaptchaItems();
//    updateFaqSubject();
//    $("#faq_subject_choice").change(updateFaqSubject);

    $('#call-form').submit(function() {
      $('#call-loader').show();
      $.post($(this).attr('action'), $(this).serialize(), function(data) {
        $('#call-form-container').html(data);
        $('#call-loader').hide();
        bindCallForm();
      });
      return false;
    });
  };

});

