
var gKey=0;    // переменная показывает что введены цифры
var lad =0;    // указывает включать нужно AJAX или нет
var cache = 0; // кешировать ли запросы

$(document).ready(function() {


setMn();	// включение меню реплик

//   проверяет включенность AJAX
//   Loaded();

$(function(){$('a[id=addfoto]').click(function() {

	if($(this).parent("div").children('div').length>=4)
	{alert('Можно добавить не более 5 картинок');}else{
	$(this).parent("div").append("<div id=ff1><a onclick='RemoFoto(this)' class=cl title='удалить поле'>[-] </a><input style='width:230' class='text' type=file name='me_foto[]' onchange='FileRemm(this,0,230)'><br><span></span></div>");
	}

    });  
 }); 


$(function(){$("select[name=s_me_usecity]").change(function()
 {
  if($(this).val()=="1"){$('#dosh').click();}
  if($(this).val()!=1 && $(this).val()!=0){
	$("input[name=me_usecoord]").val($(this).val());
	o = document.getElementById('scity');
	if(o){
	o.text   = $(this).get(0).options[$(this).get(0).selectedIndex].text;
	o.value  = $(this).get(0).options[$(this).get(0).selectedIndex].value;
        o.selected=true; }
	}
 });  
 }); 

  // установка флага что js включена
  // иначе "please click continue once more..."
  $(':hidden[name=lang]')
    .after('<input type="hidden" value="true" name="js"/>');

$('tr[id=detail]').css("display","none");

});


$(function(){$('div[id=dot]').mouseover(function() {

	$(this).attr("id","i1");
	var pos  = getElementPositions('i1');
	$(this).attr("id","dot");
	if($(this).attr("title")){$(this).attr("title1",$(this).attr("title"));
	$(this).removeAttr('title');}

	if($(this).attr("title1").length){
        var o = $("#remm");
        o.html($(this).attr("title1"));
	o.css("width","250");
	o.css('top',pos.top);
	o.css('left',pos.left+270);
	o.css("display","block"); }
    });  
 }); 

$(function(){$('div[id=dot]').mouseout(function() {
	var o = $("#remm");
	o.css("display","none");

    });  
 }); 



	// авто раскрытие поиска
function smenu(target){
$(target).click();
}


	// загрузка поиска
function clickFind(target,pref){

/*
if(gKey==0){
      $.get('/inc/validkey.php?EnterKey='+$(':text[name=EnterKey]').val(), 
	function(data) {
	gKey=data;
	if(gKey==0)          // !!! тут может быть косяк
	{alert(" Неверно повторены цифры с картинки! ");
	 $(':text[name=EnterKey]').focus();return;}else{
        clickFind2(target,pref);
        // сменить цвет полей широты и долготы
	HiddFild(pref,1);
	}
      });

}else{	}
*/

	clickFind2(target,pref);
 
}


function  clickFind2(target,pref){

$('#'+pref+'subfind').css({display: "none"});

var fo  = new Array("открыть поиск","open");
var fc  = new Array("скрыть поиск", "close");
var fl  = $(target).attr('title');
var obj = '#'+pref+'find';

if(fl==fc[1]){
   $(obj).css({display: "none"});
   $(target).text(fo[0]);
   $(target).attr('title',fo[1]);
	HiddFild(pref,0);
 return;
}else if(fl==fo[1]){
   $(obj).css({display: "block"});
   $(target).text(fc[0]);
   $(target).attr('title',fc[1]);
	HiddFild(pref,1);
 return;
}


    var rand = cache?Math.round(1+(Math.random()*10000000)):0;
    var flag = $(':hidden[name=flag]').val();
      $("#dwn").css({display: "block"});
      $.get('/search/index.php?m='+rand+'&pr='+pref+'&flag='+flag, 
	function(data) {
	  $(obj).empty();
          $("#dwn").css({display: "none"});
          $(obj).css({display: "block"});
	  $(obj).append(data);
	});

        $(target).attr('title', fc[1]);
        $(target).text(fc[0]);
        $(target).text(fc[0]);
        $("#usedv").click();
}


$("form[name=Ffoto]").submit( function() {
	return VForm();
});

function VForm(){
   var frm = document.forms.Ffoto;
   var fl  = true;
   var rem = "";
   var as  = $("input:text", frm);
   var sa  = "";
 for(i=0;i<as.length;i++){

   sa  = $(":text[name="+as[i].name+"]", frm);
   sb  = $("select[name=s_"+as[i].name+"]", frm).val();

   if(sa.parent("div").children("sup").length<1)continue;
   sa.css({'border':"solid 1 #a80f1a",'background':"#fdeaeb"});
   if(as[i].value.length<1 && (!sb || sb<=1 || sb==900)){
   sa.css({'border':"solid 1 red",'background':"#E3FAC5"});
   sa.focus();
   rem += sa.parent("div").children("span").text()+"\n";
   fl = false;
   }
 }

  sa2  = $("textarea[name=me_descr]", frm).val();
  if(sa2.length<1) {rem += "\nВведите текст объявления!\n";
		    fl = false;}

 if(!fl)alert("Ошибка! Не заполнены следующие поля: \n\n"+rem);

return fl;
}


	// проверка включенности .XMLHttpRequest

function Loaded(){
if(lad){return;}
var flag='true',day=0;
  if(window.XMLHttpRequest){
    http=new XMLHttpRequest();
    if(http.overrideMimeType){
      http.overrideMimeType('text/xml');
    }
  }else if(window.ActiveXObject){
    try{
      http=new ActiveXObject('Msxml2.XMLHTTP');
    }catch(e){
      try{
        http=new ActiveXObject('Microsoft.XMLHTTP');
      }catch(e){
        flag='false';
      }
    }
  }

  if(flag=='false'){
    document.write('<hr>');
    document.write('<font size=\"+1\" color=red>');
    document.write('Для работы необходимо включить поддержку JavaScript, AJAX, ActiveXObject!<br>');
    document.write('Either XMLHttpRequest, Msxml2.XMLHTTP or Microsoft.XMLHTTP AJAX service is needed.<hr>');
    document.write('</font>');
  }

}

function FileRemm(o,n,w){

//alert(111+" "+o.offsetParent.childNodes[0].tagName); // tagName
// var e = o.offsetParent.getElementsByTagName('SPAN');
// alert(e.length);
//[n].innerHTML ='2222222222222';

  o.parentElement.getElementsByTagName('SPAN')[n].innerHTML = "<input class=irem "+(w?"style='width:"+w+";'":"")+" type=text name='r_"+o.getAttribute('name')+"' maxlength='150' value='введите название' onclick='delAttr(this)'>";
}

function delAttr(o){
o.value    = "";
o.onclick='';
}

function RemoFoto(o){
$(o).parent("div[id=ff1]").remove();
}


// появление меню 
function setMn(){
   if(document.getElementById('tcat')){
   var as  = document.getElementById('tcat').getElementsByTagName('TR');
   if(as.length<1)return;
   for(i=0;i<as.length;i++){
   if(!as[i].getAttribute("pd"))continue;
   sa  = $("tr[pd="+as[i].pd+"]");  
   var par = as[i].getAttribute("pd").split(":");
    if (navigator.appName == "Microsoft Internet Explorer"){
     sa.html("<td class=aa style='border-right:0;' onclick='preDo(1,0)'> &nbsp; <a href='/replik.php?oc=4&oid="+par[0]+"' title='оставить комментарии' class=author onMouseOver='preDo1(\""+par[0]+":4\",this)'> комментарии </a> ("+(par[1]>0?"<a href='/post.php?id="+par[0]+"' title='посмотреть комментарии' class=txt>"+par[1]+"</a>":"0")+") | просмотров ("+par[2]+") </td><td colspan=2 class=aa align=right style='border-left:0;' onclick='preDo(1,0)'> &nbsp; <a href='/replik.php?oc=2&oid="+par[0]+"' title='написать пользователю' class=author onMouseOver='preDo1(\""+par[0]+":2\",this)'> написать</a> | <a href='/replik.php?oc=3&oid="+par[0]+"' title='получить телефон пользователя' class=author onMouseOver='preDo1(\""+par[0]+":3\",this)'> получить телефон</a> | <a href='/replik.php?oc=1&oid="+par[0]+"' title='пригласить пользователя на сайт' class=author onMouseOver='preDo1(\""+par[0]+":1\",this)'> пригласить друга</a> </td>");
    }else{ as[i].innerHTML = "<td class=aa style='border-right:0;' onclick='preDo(1,0)'> &nbsp; <a href='/replik.php?oc=4&oid="+par[0]+"' title='оставить комментарии' class=author onMouseOver='preDo1(\""+par[0]+":4\",this)'> комментарии </a> ("+(par[1]>0?"<a href='/post.php?id="+par[0]+"' title='посмотреть комментарии' class=txt>"+par[1]+"</a>":"0")+") | просмотров ("+par[2]+") </td><td colspan=2 class=aa align=right style='border-left:0;' onclick='preDo(1,0)'> &nbsp; <a href='/replik.php?oc=2&oid="+par[0]+"' title='написать пользователю' class=author onMouseOver='preDo1(\""+par[0]+":2\",this)'> написать</a> | <a href='/replik.php?oc=3&oid="+par[0]+"' title='получить телефон пользователя' class=author onMouseOver='preDo1(\""+par[0]+":3\",this)'> получить телефон</a> | <a href='/replik.php?oc=1&oid="+par[0]+"' title='пригласить пользователя на сайт' class=author onMouseOver='preDo1(\""+par[0]+":1\",this)'> пригласить друга</a> </td>";}
   }}
}


function goMarker(){
var marker1 = getCookie("marker");
if(marker1){
vis = marker1.split(":");
  for(i=0;i<vis.length;i++){
   if(document.getElementById('u'+vis[i])){
     ids = document.getElementById('u'+vis[i]);
     ids.className = 'nbag';
     ids.title = 'объявление помечено. снять маркер';
   }
  }
}
var hist1 = getCookie("hist");
if(hist1){
vis = hist1.split(":");
  for(i=0;i<vis.length;i++){
   if(document.getElementById('u'+vis[i])){
     ids = document.getElementById('u'+vis[i]);
     ids.id = 'hist';
   }
  }
}}




