var t = 'Поиск по сайту...';

$(document).ready(function() {
  var window_width = $(window).width();
  var window_height = $(window).height();

  //vertical and horizontal centering of modal window(s)
  $('.search_outwin').each(function() {
    //get the height and width of the modal
    var modal_height = $(this).outerHeight();
    var modal_width = $(this).outerWidth();
  
    //calculate top and left offset needed for centering
    var top = (window_height-modal_height)/2;
    var left = (window_width-modal_width)/2;
  
    //apply new top and left css values
    $(this).css({'top' : top , 'left' : left});
  });  

  $('.activate_modal').click(function() {
    //get the id of the modal window stored in the name of the activating element
    var modal_id = $(this).attr('name');

    //use the function to show it
    show_modal(modal_id);
  });

  $('.close_modal').click(function(){
    //use the function to close it
    close_modal();
  });
});

//THE FUNCTIONS

function close_modal() {
  //hide the mask
  $('#mask').fadeOut(500);

  //hide modal window(s)
  $('.search_outwin').fadeOut(500);
}

function show_modal(modal_id) {
  //set display to block and opacity to 0 so we can use fadeTo
  $('#mask').css({ 'display' : 'block', opacity : 0});

  //fade in the mask to opacity 0.8
  $('#mask').fadeTo(500,0.8);

  //show the modal window
  $('#'+modal_id).fadeIn(500);
}

$(document).ready(function() {
  $("#form-text, #form-text2").attr("value", t);
  $("#form-text, #form-text2").focus(function() {
    //$("#search-box").show("fast");
    $(this).removeClass("inactive"); 
    if ($(this).attr("value") == t) $(this).attr("value", "");
  });
  $("#form-text, #form-text2").blur(function() {
    $(this).addClass("inactive"); 
    if ($(this).attr("value") == "") $(this).attr("value", t);
  });
});

$(document).ready(function() {
  $("#search-form, #search-form2").bind("submit", function(event) {
    var search_word = $("#form-text").val();
    if (search_word == t || search_word == '') return false;
    if ($(this).attr('id') == 'search-form') {
      show_modal('search_outwin');
    } else {
      search_word = $("#form-text2").val();
    }
    var dataString = 'q='+ search_word;

    if(search_word != '') {
      $.ajax({
        async: true,
        type: "GET",
        url: "cgi-bin/search.php",
        data: dataString,
        cache: false,
        scriptCharset: "windows-1251",
        beforeSend: function(html) {
          document.getElementById("search_result").innerHTML = ''; 
          $("#flash").show();
          $("#searchword").show();
          $("#flash").html('<img src="/ajax-loader.gif" />');
        },
        success: function(html) {
          $("#search_result").show();
          $("#search_result").append(html);
          $("#flash").hide();
        }
      });
    }
    event.preventDefault();
  });
});

