$(document).ready(function(){
  
  /* locale and country switch */
  initialize_languages();
  
  initialize_disclaimers();
  //initialize_user_country();
  //initialize_user_disclaimer();
  //initialize_global_disclaimer();
  
});

function ie6_fixes(){
  ie6_min_height('#products ul.ranges', 456);
  ie6_min_height('#content_wrapper', 671);
}

function ie6_min_height(selector, height){
  if($(selector).length > 0){
    if($(selector).height() < height){
      $(selector).css({ height: height });
    }
  }
}

function initialize_disclaimers(){
  if($('#user_country').length > 0){
    $('#user_disclaimer').hide();
    $('#global_disclaimer').hide();
    show_user_country();
  }
  else if($('#current_audience').length > 0){
    $('#global_disclaimer').hide();
    
    if($('#current_audience').val() == '') show_user_disclaimer();
    else $('#user_disclaimer').hide();

    $('#user_switch').click(function(e){
      e.preventDefault();
      show_user_disclaimer();
    });
  }
  else {
    if($('#current_audience').val() != ''){
      show_global_disclaimer();
    }
    else {
      $('#global_disclaimer').hide();
    }
  }
}

function show_user_country(){
  var container = $('#user_country');

  container.before('<div id="user_country_wrapper"></div>');

  container.css({
    display: 'block',
    position: 'absolute',
    top: ($(document).scrollTop() + 200),
    bottom: "auto",
    width: 500,
    left: '50%',
    height: 'auto',
    'margin-left': -250
  });

  var wrapper = $('#user_country_wrapper');
  wrapper.css({ opacity: .6, height: $(document.body).outerHeight() });

}

function show_user_disclaimer(){
  var container = $('#user_disclaimer');

  container.before('<div id="user_disclaimer_wrapper"></div>');

  container.css({
    display: 'block',
    position: 'absolute',
    top: ($(document).scrollTop() + 200),
    bottom: "auto",
    width: 500,
    left: '50%',
    height: 'auto',
    'margin-left': -250
    });

  var wrapper = $('#user_disclaimer_wrapper');
  wrapper.css({ opacity: .6, height: $(document.body).outerHeight() });

  var path = $('#user_audience_path').val();
  var params = { back_path: $('#user_back_path').val() };

  $('#user_individual').click(function(e){
    e.preventDefault();

    params.audience = "individual";
    params.remember = $('#user_audience_remember').attr('checked');

    location.replace(path + "?" + $.param(params));
    });

  $('#user_professional').click(function(e){
    e.preventDefault();

    params.audience = "professional";
    params.remember = $('#user_audience_remember').attr('checked');

    location.replace(path + "?" + $.param(params));
    });
  
  $('#user_disclaimer_wrapper').click(function(e){
    //$('#user_disclaimer_wrapper').remove();
    //$('#user_disclaimer').hide();
    });
  }
  
function show_global_disclaimer(){
  var container = $('#global_disclaimer');

  container.before('<div id="user_disclaimer_wrapper"></div>');
  
  container.css({
    display: 'block',
    position: 'absolute',
    top: ($(document).scrollTop() + 200),
    bottom: "auto",
    width: 500,
    left: '50%',
    height: 'auto',
    'margin-left': -250,
    'z-index': 10000
    });

  var wrapper = $('#user_disclaimer_wrapper');
  wrapper.css({ opacity: .6, height: $(document.body).outerHeight() });
}

function initialize_languages(){
  $('header .switch ul').hide().removeClass('hidden');
  
  $("header .switch p strong").click(function(e){
    e.preventDefault();
    var list = $(e.target).parents('nav').find('ul');
    
    if(!list.is(':visible')){
      e.stopPropagation();
      close_all_language_switches();
      list.slideDown(250);
    }
    else list.slideUp(250);
  });
  
  $(document.body).click(function(e){
    close_all_language_switches();
  });
  
}

function close_all_language_switches(){
  $("header .switch ul").each(function(){
    if($(this).is(':visible')) $(this).slideUp(250);
  });
}
