$(document).ready(function(){//
//$('ins').click(function(){
//	alert($('#mplace_block').html());
//});

$('#cart').mouseover(function() {
	$('#cartother').show();
});
$('#cart').mouseout(function() {
	if(t > 190){			$('#cartother').hide();}
		});

});


function tgl(id){
	if(document.getElementById(id).style.display=='none'){
		document.getElementById('cartother').style.display='';
	}else{
		document.getElementById('cartother').style.display='none';
	}
}



function ci(id){
//$('#ciphoto').attr('src','/design/img/loading.gif');
$('#ciphoto').attr('src','');
$('#ciphoto').attr('src','/i/bc/'+id+'.jpg');

$('#cilink').attr('href','/'+lang+'/search/?mplace_form='+id+'|&form_of_search=by_ctg');}




function cinext(id){
var a=document.getElementById('cisel');
var b=(a.selectedIndex<(a.options.length-1))?a.options[a.selectedIndex+1].value:0;
	if(b)
	{		a.options[a.selectedIndex+1].selected=true;
	 	ci(b);	}
}


function ciprev(id){
var a=document.getElementById('cisel');
var b=(a.selectedIndex>0)?a.options[a.selectedIndex-1].value:-1;
	if(b!=-1)
	{
		a.options[a.selectedIndex-1].selected=true;
	 	ci(b);
	}
}

function createBLD(t){
$.getJSON('/act/json.php?bld='+t, function(data) {
  var items = [];
  $.each(data, function(key, val) {  if(key==0){ci(val['id']);}
  items.push('<option value="' + val['id'] + '">' + val['name'] + '</option>');
});

   $('#cisel').html(items.join(''));
});

}



function swh(id){ $('#inf').removeClass('hsel');
 $('#ord').removeClass('hsel');if(id=='inf'){ $('#inf').addClass('hsel');
 $('#ord').addClass('h_switch');
 $('#details').show();
$('#back_form').hide();

 }else{ $('#ord').addClass('hsel');
 $('#inf').addClass('h_switch');
$('#back_form').show();
$('#details').hide(); }}






function GetChar (event,id){

            var chCode = ('charCode' in event) ? event.charCode : event.keyCode;
            if(event.keyCode==13)
            {            	if(id !='id_str')
            	{
            		SignIn();	            }
	            else
	            {             	id_str()
             	}            }

        }



function priceFor(id){	_pricef=id;
setObjV(0,lim[id],id);
priceReset('',' ',pref[id]);
 $('#cost').val(id);
swClear(Array('by_ob','by_1m'));
tbClear(Array('by_ob','by_1m'));
swOn(id);
tbShow(id);
}

function swOn(id){
 $('#'+id).addClass('on');
}

function tbClear(ar){
for(i in ar){
 $('#tb_'+ar[i]).hide();
 }
}

function tbShow(id){
 $('#tb_'+id).show();
}



function with_photo(id){var s=0;if(id){s=1;}$('#with_photo').val(s);get();}
function with_renovation(id){var s=0;if(id){s=1;}$('#with_renovation').val(s);get();}

function swClear(ar){
	var i=0;
for(i in ar){
 $('#'+ar[i]).removeClass('on');
 }
}


function delfromcart(id){
  $('#obj'+id).hide(150);
  listing(id);
}



function listing2(){
$.get('/act/listing.php', { action: 'all', id: '0' } , function(data) {   $('#lnum').html(0);
   testc();
   });}

function listing(id)
{
  $('#listing'+id).html('<img src="/design/img/smallloading.gif"/>');
  var act='save';
  if($('#listing'+id).hasClass('selected'))
  	{
     act='remove';
	}
$.get('/act/listing.php', { action: act, id: id } , function(data) {
  if(act=='save')
  	{
	  	$('#listing'+id).removeClass('unselected');
       $('#listing'+id).addClass('selected');
         $('#listing'+id).html('<img src="/design/img/checked.png"/>');
         lnum=lnum+1;
          $('#lnum').html(lnum);
	}
	else
	{
	  	$('#listing'+id).removeClass('selected');
       $('#listing'+id).addClass('unselected');
         $('#listing'+id).html('<img src="/design/img/unchecked.png"/>');
          lnum=lnum-1;
      $('#lnum').html(lnum);

	}

//  alert('Load was performed.'+data);
});


 }



function pinfo(){	$('#pinfo').html('<img src="/design/img/loading.gif" />');$.get('/act/pinfo.php', { } , function(data) {
	var a=data;
	if(a != '0.00'){		$('#pinfo').removeClass('not');
		$('#pinfo').html(data);	}else{		$('#pinfo').addClass('not');
		$('#pinfo').html('Не достаточно данных');	}
});
}


function dload(){
var h=document.getElementById('cnt').offsetHeight;
document.getElementById('dload').style.height=h+'px';
document.getElementById('dload').style.display='';
}

function get(p,cl){
dload();
 if(!p){
   var buf=$('#curpage').val(0);
 }
 if(cl==true){ 	var buf=''; 	} 	else{
var buf=$('#search_form').serialize();
}
$('#num_obj').html('<img src="/design/img/loading.gif"/>');
$.get('/act/index.php?'+buf+'&clang='+lang, { } , function(data) {$('#cnt').html(data);
pinfo();

$.get('/act/num_obj.php', { } , function(data2) {
$('#num_obj').html(data2);
	});

try {
      window.history.pushState("object or string", "Title", "/"+lang+"/search/?"+buf);
          return;
        } catch(e) {
         document.location.hash = '#page=' + "/"+lang+"/search/?"+buf;
        }

	});


}

function stop(l,r){
	if(mD){
		get();
		mD=0;
	}
}

function setD(){
 mD=1;
}


function getLim(id){
var buf=$('#lim').val(id);

get();
}

function getSort(id){
var buf=$('#sort').val(id);
get();
}


function getPage(id){
var buf=$('#curpage').val(id);
get(1);
}



function getJSON(id, tp){

$.getJSON('/act/json.php?tp='+tp, function(data) {
  var items = [];

  $.each(data, function(key, val) {  items.push('<option value="' + val['id'] + '">' + val['name'] + '</option>');
});

  $('<select />', {    'class': 'st-list',
    'onchange': '',
    'style': 'display:none',

    'id': 'st-list-'+tp,
    html: items.join('')
  }).appendTo(id);
   $('#st-list-'+tp).change(function() {
  	Ins(this.form, this.value, this.selectedIndex);
   });
});

}

function get2JSON(id,a,b){
$.getJSON('/act/json.php?'+a+'='+b, function(data) {
  var items = [];
  items[1]=new Array();
  items[2]=new Array();
  items[3]=new Array();
  items[4]=new Array();
  items[5]=new Array();
  var buf = Math.floor(data.length/4);
  var cc=1;
  $.each(data, function(key, val) {
  if(val['id']==404){
  items[cc].push('<li id="' + val['id'] + '"><a id="robob' + val['id'] + '" onclick="pA('+val['id']+',\''+val['name']+'\')" style="color:#821744;">' + val['name'] + '</a></li>');
  }else{items[cc].push('<li id="' + val['id'] + '"><a id="robob' + val['id'] + '" onclick="pA('+val['id']+',\''+val['name']+'\')">' + val['name'] + '</a></li>');  }
  	if(q_st[val['id']]==0){  		       q_st[val['id']]=val['name'];  	}
  if((key==((buf)*cc) && cc <4 ) || key==(data.length-1)){
//  alert(data.length-1+'--'+key);
		 app(id,items[cc],cc);
		  cc++;  }
  });



     $('<input type="button"  value="  '+_ok_+' " onclick="TestAndGo()" />', {  }).appendTo('#win-r');
    _q_st();
});

}


function _q_st(){for(i in q_st){//         pA('+val['id']+',\''+val['name']+'\')
	if(q_st[i]!=0){		pA(i,q_st[i]);
	}
	}}

function  app(id,it, cc){
	$('<div/>', {
   'id': 'st-list-'+cc,
    html: ''
  }).appendTo(id);
  $('<ul/>', {
   'class': 'st-list',
    html: it.join('')
  }).appendTo('#st-list-'+cc);

}


function popup(t){$('#popup').show();
$('#win-'+t).show();}

function cspl2(){
   $('#scs').hide();
}


function cspl(){   $('#popup').hide();
 	$('#win-r').hide();
 	$('#win-s').hide();}

function TestAndGo(t){ 	$('#popup').hide();
 	$('#win-r').hide();
 	$('#win-s').hide();
 	if(t=='s'){
         clearrn();

 	}else{
// 		$('#st-list').show();
		stclear();

 	}
 	setInpRob();
    createA($('#st_v_1').val(),$('#st_v_2').val(),$('#st_h_1').val(),$('#st_h_2').val());
 	get();}

function clearrn(){	$('.place').remove();
		$('#mplace_default').show();
 		$('#mplace_form').val(0);

		orob=Array();
		$('.st-list li a').css('background','#ffffff');}

function pA(id,name){
$('#krestik').hide();
if(id==404){clearrn();return 0;}var buf=0;
if(id!=''){
var test=testOrob(id);
	if(test[0]){		mRemove(id,'not');	}else{
		orob.push(Array(id,name));
		setRobob(id,'s');
		setMp(id,name)	}
}}



function testOrob(id){	var x=0;      var ind=0;
for (var val in orob) {	if(orob[val][0]==id){            x=1;
            ind=val;
	}	}
	return Array(x, ind);
}

function setSelectedRobob(){$.each(orob, function(key, val) {       setRobob(val[0]);	});}

function setRobob(id, t){
	if($('#robob'+id).css('background-color')=='#FFB8B8' || t=='n'){		$('#robob'+id).css('background-color','#ffffff');
//		$('#robob'+id).css('color','#000000');
	}else{//		$('#robob'+id).css('color','#ffffff');
		$('#robob'+id).css('background-color','#FFB8B8');	}
}

function setMp(id,name){
$('#mplace_default').hide();

var zn='';
	$('#mplace_block').html($('#mplace_block').html()+'<span class="place" id="place_'+id+'">'+name+' <ins onclick="mRemove('+id+')">x</ins> </span>');
//
}

function setMplace(){ if(robob.length>0){ }}

function mRemove(id,go){	var x=testOrob(id);	if(x[0]){       orob.splice(x[1],1);	}
	$('#place_'+id).remove();
	setRobob(id,'n');
	if(orob.length==0){		$('#mplace_default').show();	}
	if(go=='not'){	}else{
	setInpRob();		get();	}
}


function setInpRob(){	var zn='|';
	$('#mplace_form').val('');	for (var val in orob) {         if(val==(orob.lenght-1)){         	zn='';         }
		$('#mplace_form').val($('#mplace_form').val()+orob[val][0]+zn);
	}}



function St(n,t){
$('#scs').show();
$('select.st-list').hide();
$('#buftype').val(t);
$('#bufid').val(n);
 var ar=new Array();
 ar['v']='vertical';
 ar['h']='horizontal';
 var a=document.getElementById('st-list-'+ar[t]);
 var n=$('#st_'+t+'_'+n+'').val();
  setSelect(a,n);
$('#st-list-'+ar[t]).show();}


function setSelect(oListbox,n)
{
  for (var i=oListbox.options.length-1; i >= 0; i--)
  {
	if(n==oListbox.options[i].value)
	{		oListbox.options[i].selected=true;
	}
  }
};

function Ins(f,v,t){
var z=($('#buftype').val()=='h')?'horizontal':'vertical';
var elm =document.getElementById('st-list-'+z);
$('#buftext').val(elm.options[t].text);
$('#bufval').val(v);
}


function stclear(){$('#mplace_default').html();
$('#st_h_1').val(0);$('#st_h_2').val(0);$('#st_v_1').val(0);$('#st_v_2').val(0);
if(document.getElementById('st-list-horizontal')){
	document.getElementById('st-list-horizontal').options[0].selected=true;
}
if(document.getElementById('st-list-vertical')){
	document.getElementById('st-list-vertical').options[0].selected=true;
}
$('#h1').html(_any_2);
$('#h2').html(_any_2);
$('#v1').html(_any_3);
$('#v2').html(_any_3);

}

function stIns(){
$('#krestik').hide();
var n=$('#buftext').val();
var _t=$('#buftype').val();
var _id=$('#bufid').val();
var _bv=$('#bufval').val();
if(n=='0')
{
	n=document.getElementById('st-list-vertical').options[0].text;
}
if(_bv==0){vn[''+_t+_id+'']=0;	}else{
vn[''+_t+_id+'']=n;
}
var str='';

if($('#buftype').val()=='v')
{
	n=n.split('')
	for(i=0;i<n.length;i++)
	{		str=str+n[i]+'<br/>';	}
	n=str;
}
$('#'+_t+_id).html(n);
$('#st_'+_t+'_'+_id).val(_bv);
 $('#scs').hide();



 /////////////////// очитска

$('#buftext').val(0);
$('#buftype').val(0);
$('#bufid').val(0);
$('#bufval').val(0);

}


function initSt(){if(vn[0]!=0)
{
	$('#v1').val(vn[0]);
}
if(vn[1]!=0)
{
	$('#v2').val(vn[1]);
}
if(vn[2]!=0)
{
	$('#h1').val(vn[2]);
}
if(vn[3]!=0)
{
	$('#h2').val(vn[3]);
}

}


function createA(v1,v2,h1,h2){var buf=0;
var txt='';


if(v1!=0 && v1!=undefined){buf++;}
if(v2!=0 && v2!=undefined){buf++;}
if(h1!=0 && h1!=undefined){buf++;}
if(h2!=0 && h2!=undefined){buf++;}

var st='';
for(var t in vn)
{	if(vn[t]!=0)
	{		if(st!='')
		{			st=st+', '+vn[t];		}
		else
		{			st=vn[t];		}	}}

	if(buf==1)
	{		txt='<font color="#1A263C">'+_along_+':</font> <b>'+st+'</b>';
	}

	if(buf==2 && ((v1!=0 && v2!=0) || (h1!=0 && h2!=0)))
	{
		txt='<font color="#1A263C">'+_between_+':</font> <b>'+st+'</b>';
	}

	if(buf==2 && ((v1!=0 && h2!=0) || (v1!=0 && h1!=0) || (v2!=0 && h1!=0) || (v2!=0 && h2!=0)))
	{
		txt='<font color="#1A263C">'+_cross_+':</font> <b>'+st+'</b>';
	}

	if(buf>=3)
	{
		txt='<font color="#1A263C">'+_square_+':</font> <b>'+st+'</b>';
	}

 	$('#mplace_default').html(txt);
}


function rooms(frm,v)
{
	for(var i=0; i<5; i++)
	{
		if(frm.elements[i].checked==true )
		{			 	$('#rooms'+frm.elements[i].value).removeAttr('disabled');
		}
		else
		{
			 	$('#rooms'+frm.elements[i].value).attr('disabled', 'true');		}
	}
	get();}


            /*

function sdf_FTS(_number,_decimal,_separator)
{
// определяем, какой будет сепаратор [он же разделитель] между разрядами
var separator=(typeof(_separator)!='undefined')?_separator:'';

// преобразовываем входящий параметр к дробному числу, на всяк случай, если вдруг
// входящий параметр будет не корректным
var r=parseFloat(_number)
// разделяем разряды в больших числах, если это необходимо
// то есть, 1000 превращаем 1 000

b=r.replace(/(\d{1,3}(?=(\d{3})+(?:\.\d|\b)))/g,"\$1"+separator);


return b;// возвращаем результат
}             */






function SignIn(){
    var data=$('#loginform').serialize();
 $.ajax({
   type: 'POST',
   url: '/act/login.php',
   data: data,
   success: LoginRes
 });

 }



 function  LoginRes(msg){

	if(msg=='ok'){
		window.location.href=tpr;
	}else{
		$('#ler').show();
		$('#auth').addClass('xred');
	}

}


 function replace(subject){ 			var search= ["<![","CDATA[","]]>"];
 			var replace="";
           var ra = replace instanceof Array,
                   sa = subject instanceof Array,
                   l = (search = [].concat(search)).length,
                   replace = [].concat(replace),
                   i = (subject = [].concat(subject)).length;
           while(j = 0, i--)
                   while(subject[i] = subject[i].split(search[j]).join(ra ? replace[j] || "" : replace[0]), ++j < l);
           return sa ? subject : subject[0];
    }
