
  $(document).ready(function(){

$(".wtooltip").tooltip({
    track: true,
    delay: 0,
    showURL: false,
    showBody: " - ",
    fade: 150
  });



   $(".mainTable a").tooltip({
    track: true,
    delay: 0,
    showURL: false,
    showBody: " - ",
    fade: 150
  });

    $(".regionS").click(function () {
        $(".regionList").toggle("normal");
    });

   $(".regionList, BODY").click(function () {
        $(".regionList").css("display","none");
    });

    $("[id^='s_']").mouseover(function () {
        var id = $(this).attr("id");
        var src = $("." + id).attr("src");

        $("." + id).attr("src", src.replace(/images\//g,'images/over_'));
    });

    $("[id^='s_']").mouseout(function () {
        var id = $(this).attr("id");
        var src = $("." + id).attr("src");

        $("." + id).attr("src", src.replace(/over_/g,''));
    });

    //отправка формы подачи объявления(разделы)
    $("#addButton").click(function () {
      var f = document.forms.addForma;
      if(f.section_id.value != 0) {
        var url = "/forma/"+f.section_id.value;
      }

      if(f.sub_id != null && f.sub_id.value != 0) {
        var url = "/forma/"+f.sub_id.value;
      }

      if(f.sub_id2 != null && f.sub_id2.value != 0) {
        var url = "/forma/"+f.sub_id2.value;
      }

      if(url) {
        document.location = url;
      }else{
        return false;
      }

      return false;
    });

    //Подача объявления
    $("#formSubmit").click(function () {
      //$("#ads").submit();

      var str = $("#ads").serialize();
      $.post("/lib/_addAds.php", str,
         function(json){
            if(json.status == 'ok') {
              $("#subRes").html('').css("color", "#000").append(json.message);
              if(json.location != '') {
                var loc = json.location.replace(/amp;/g,'');
                document.location = loc;
              }
            }else{
              $("#subRes").html('');

              $.each(json.message, function() {
                  $("#subRes").css("color", "#F00").append(this + "<br />");
                });

              scroll(0,0);
            }
         }, "json");
      return false;

    });


	// Подача-предпросмотр

    try
    {
		$('a#formSubmitPreviewTrigger').fancybox({
	                	'hideOnContentClick': false,
	                	'overlayShow':	true,
	                	'frameWidth': 700,
	    	            'frameHeight': 400
	                });
	}
	catch(e){};




    $("#formSubmitPreview").click(function () {
      //$("#ads").submit();

      var str = $("#ads").serialize();
      $.post("/lib/_addAds.php?preview=1", str,
         function(json){
            if(json.status == 'ok') {
              $("#subRes").html('').css("color", "#000").empty();
              if(json.previewId != '') {              	eval(json.js);                //oWin(json.previewId, 1);
              }
            }else{
              $("#subRes").html('');

              $.each(json.message, function() {
                  $("#subRes").css("color", "#F00").append(this + "<br />");
                });

              scroll(0,0);
            }
         }, "json");
      return false;

    });




    //подгрузка подкатегорий
    $("#catsel").change(function () {
      var eto = $(this).val();
      if(parseFloat(eto) == eto) {
        $.get("/lib/_subCats.php", { section_id: $(this).val() },
         function(data){
           $('#catsec').html(data);
         });
      }else{
        document.location = eto;
      }
    });

    //подгрузка городов
    $("#regionsel").change(function () {
      $.get("/lib/_subRegions.php", { section_id: $(this).val() },
       function(data){
         $('#regionsec').html(data);
       });
    });

    //дополнительные фотографии
    $("#moreFhotos").click(function() {
      $("#moreFiles").show("normal");
      $(this).css("display", "none");
    });

    //загрузка фоток
    $(".f").change(function () {
      $("#file").submit();
      resetForm("file");
      $("#show").html("<img src='/css/loading.gif'>");
    });

    //Переключение валют
    $(".currencyes").click(function() {
      document.location = $(this).val();
    });

    //обновление валют
      $("#updateSubmit").click(function () {
      //$("#ads").submit();

      var str = $("#ads").serialize();
      $.post("/lib/_updateAds.php", str,
         function(json){
            if(json.status == 'ok') {
              $("#subRes").html('').css("color", "#000").append(json.message);
              if(json.location != '') {
                var loc = json.location.replace(/amp;/g,'');
                document.location = loc;
              }
            }else{
              $("#subRes").html('');

              $.each(json.message, function() {
                  $("#subRes").css("color", "#F00").append(this + "<br />");
                });

              scroll(0,0);
            }
         }, "json");
      return false;

    });


      $(".ee").click(function() {
        var art = $(this).attr('art');
        var cat = $(this).attr('cat');
        if($(this).attr('checked'))
          var action = 'add';
        else
          var action = 'del';
  			$.get("/lib/_addComparab.php", { art_id: art, cat_id: cat, act: action },
         function(data){
           $('#compar').html(data);
         });
		});

      $(".e2").click(function() {
        var art = $(this).attr('art');
        var cat = $(this).attr('cat');
  			$.get("/lib/_addComparab.php", { art_id: art, cat_id: cat, act: 'del' },
  			  function(data){
           window.location.reload();
         });
		});

      $("#getCredit").click(function() {
        var cid = $(this).attr('cID');
  			$.get("/lib/_creditCount.php", { cid: cid },
  			  function(){});
		});

  });

  function handleResponse(mes, id) {
    $("#show").html("");
    $("#show"+id).html(mes);
  }

  function resetForm(id) {
  	$('#'+id).each(function(){
  	        this.reset();
  	});
  }

  function subSect(id) {
    $.get("/lib/_subSections.php", { section_id: id },
     function(data){
        $('#scForm').html(data);
     });
  }

  function subSectSearch(id) {
    document.location = '/searchform/'+id;
    /*
    $.get("/lib/_subSectionsSearch.php", { section_id: id },
     function(data){
        $('#scForm').html(data);
     });
    */
  }

      //Удаление фотографий
    function imageDel(name, id) {
      $.get("/lib/_photo_delete.php", { id: name },
       function(data){
         $("#show"+id).html(data);
       });
    }

    function oWin(id) {		mywindow = window.open ("/ob.php?id="+id, "mywindow"+id,"location=1,status=0,menubar=0,resizable=1,scrollbars=1, width=800,height=650");
      mywindow.moveTo(150,0);
      return false;
    }

    function kWin(id) {
      mywindow = window.open ("/ob.php?id="+id+"&section=contacts", "mywindow"+id,"location=1,status=0,menubar=0,resizable=1,scrollbars=1, width=800,height=650");
      mywindow.moveTo(150,0);
      return false;
    }

    function cWin(id) {
      mywindow = window.open ("/compar.php?id="+id, "window"+id,"location=1,status=0,menubar=0,resizable=1,scrollbars=1, width=800,height=650");
      mywindow.moveTo(150,0);
      return false;
    }

    function confirmDelete() {
      return confirm("Вы подтверждаете удаление?");
    }

    function number_format( number, decimals, dec_point, thousands_sep ) {
      // http://kevin.vanzonneveld.net
      // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
      // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
      // +     bugfix by: Michael White (http://crestidg.com)
      // +     bugfix by: Benjamin Lupton
      // +     bugfix by: Allan Jensen (http://www.winternet.no)
      // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
      // *     example 1: number_format(1234.5678, 2, '.', '');
      // *     returns 1: 1234.57

      var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
      var d = dec_point == undefined ? "," : dec_point;
      var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
      var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

      return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");

    }
	
	var curSelected = new Array();
	
	function makeBold(group, id) {
		//alert(curSelected);
		
		if (curSelected[group] && document.getElementById(curSelected[group])) {
			document.getElementById(curSelected[group]).style.fontWeight = 'normal';
		}
		document.getElementById(id).style.fontWeight = 'bold';
		curSelected[group] = id;
		//alert(curSelected[group]);
	}