div_to_load = 'content';
after_load  = '';
last_req    = '';
var rooms   = 0;

function ContentLoad(link, div_name, after_func)
 {
  div_to_load = div_name || 'content';
  after_load  = after_func || '';

  last_req    = link;

  $("#"+div_to_load).css("-moz-opacity", 0.5);
  $("#"+div_to_load).css("opacity", 0.5);
  $("#"+div_to_load).css("filter", "alpha(opacity=50)");

  var main_div = $("#"+div_to_load);
  var height   = $("#"+div_to_load).height();
  var width    = $("#"+div_to_load).width();
  var ofset    = $("#"+div_to_load).offset();

  $("<div id='content_loading'></div>")
  .html("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;çàãðóçêà...")
  .css("position","absolute")
  .css("height",  height / 2)
  .css("width",   width*2 / 3)
  .css("top",     ofset.top+height/2)
  .css("left",    ofset.left+width/3)
  .prependTo(main_div);

  var reg = new RegExp("^.+[&?]+.*$", "i");
  var match = reg.exec(link);
  if (match !==null) link += '&';
  else               link += '/?';
  link += 'ajax=1';

  $.get(link, {},onAjaxSuccess);
 }

function onAjaxSuccess(data)
 {
  $("#content_loading").remove();
  $("#"+div_to_load).css("-moz-opacity", 1);
  $("#"+div_to_load).css("opacity", 1);
  $("#"+div_to_load).css("filter", "alpha(opacity=100)");
  $("#"+div_to_load).attr("innerHTML", data);

  var room_obj = $("#rooms");
  if (room_obj && rooms) room_obj.attr("value", rooms)

  if (after_load != '') eval(after_load);
 }

function DivHide(id, sid, nid)
 {
  $("#"+id).animate(
  {
    height: "hide",
    opacity: "hide"
  },
  300);
  $("#"+sid).attr("class", "plus");
 }

function CatalogDivSwap(id, cat, to_change, has_prods, only_swap)
 {
  only_swap = only_swap || 0;
  var cid = 'cat_'+id+'_'+cat;
  var sid = 'sign_'+id+'_'+cat;
  var nid = 'name_'+id+'_'+cat;
  if ($("#"+cid).css("display") == 'none' || has_prods)
   {
    $("div").each(
    function()
     {
      var reg = new RegExp("^cat_"+id+"_(\\w+)$", "i");
      var match = reg.exec(this.id);
      if (match !==null && match.length && match[1] != cat && $("#cat_"+id+"_"+match[1]).css("display") == 'block')
       {
        DivHide('cat_'+id+'_'+match[1], 'sign_'+id+'_'+match[1], 'name_'+id+'_'+match[1]);
       }

      $("#"+this.id+" a").each(
      function()
       {
        var reg = new RegExp("^(name_.+)$", "i");
        var match = reg.exec(this.id);
        if (match !==null && match.length && match[1] != nid)
         {
          $("#"+match[1]).attr("class", "pasive");
         }
        }
       );

     }
    );

    $("#"+cid).animate(
    {
      height: "show",
      opacity: "show"
    },
    300);

    $("#"+sid).attr("class", "minus");
    if (!only_swap) $("#"+nid).attr("class", "active");
   }
  else
   {
    if (to_change)
     {
      DivHide(cid, sid, nid);
      if (!only_swap) $("#"+nid).attr("class", "active");
     }
   }
 }

function GoTo(link)
 {
  var part = '';
  if (document.main_form.cat_main.value > 0) part = 'cat_id='+document.main_form.cat_main.value;
  if ($("#r1").attr("checked"))
   {
    if (part) part += '&';
    part += 'type=0';
   }
  if ($("#r2").attr("checked"))
   {
    if (part) part +='&';
    part += 'type=1';
   }

  if (part) link += '/?'+part
  document.location = link;
 }

function CatsLoad(link, id, div_name, act, focus_cat)
 {
  focus_cat = focus_cat || 0;
  $("#validationmsg").attr("innerHTML", "");
  if (id === null)
   {
    id = document.main_form.cat_main.value;
   }

  if (id > 0)
   {
    link += '/'+id;
    var is_check = false;
    if ($("#r1").attr("checked")) { link += '/1';  is_check = true;}
    if ($("#r2").attr("checked")) {link += '/2'; is_check = true;}
    if (is_check)
     {
      after_func = '';
      if (focus_cat && !act) after_func = "ContentLoad('/ru/catalog/search_fields/"+id+"/', 'search_fields');";
      ContentLoad(link, div_name, after_func);
     }
    else if (focus_cat && !act) ContentLoad('/ru/catalog/search_fields/'+id, 'search_fields');
   }
 }

function SubCatsLoad(link, id, div_name)
 {
  $("#validationmsg").attr("innerHTML", "");
  if (id == 10 || id == 63) rooms = 1;
  if (id == 13 || id == 64) rooms = 2;
  if (id == 14 || id == 65) rooms = 3;
  if (id == 18 || id == 66) rooms = 4;

  if (id > 0) ContentLoad(link+'/'+id, div_name);
 }

function AgentIdSet(id, link)
 {
  go_to = last_req || link;

  var reg = new RegExp("^(.+)((\\/\\?)|&)a=\\d+(.*)$", "i");
  var match = reg.exec(go_to);
  if (match !==null && match.length)
   {
    go_to = match[1]+match[4];
   }


  var reg = new RegExp("^.+[&?]+.*$", "i");
  var match = reg.exec(go_to);
  if (match !==null) go_to += '&';
  else               go_to += '/?';
  go_to += 'a='+id;

  ContentLoad(go_to);
 }

function SearchGo(link)
 {
  var res = jQuery.validate({messageheader: 'Ââåäåííûå Âàìè äàíûå íåêîððåêòíû'});
  if (!res) return;
  var queryString = $('#main_form').formSerialize();
//  alert(queryString);
  ContentLoad(link+'/?'+queryString);
 }

function SearchByIdGo()
 {
  var queryString = $('#sub_form').formSerialize();
  ContentLoad('/ru/catalog/search_by_id/?'+queryString);
 }

function SubscribeGo(link)
 {
  var queryString = $('#subscribe_form').formSerialize();
  ContentLoad(link+'/?'+queryString, 'subscribe');
 }

function NewWin(link)
 {
  window.open(link+'/?ajax=1',"s","width=670,height=600,scrollbars=yes");
 }

var counter = 0;
function TextAdd(text)
 {
  var Obj = $('#text'+counter);
  counter += 1;
  Obj.attr("innerHTML", '<br />'+text+'<span id=\"text'+counter+'\"></span>');
 }

