﻿$(document).ready(function () {


    var dlg = $('#pBesichtigungSuche').dialog({
        autoOpen: false,
        draggable: false,
        resizable: false,
        closeOnEscape: true,
        modal: true,
        width: 723,
        open: function (event, ui) {
            var el = document.getElementById('pBesichtigungSuche');
            var elP = el.parentNode;
            if (!elP.id) {
                //dynamically add the id, and then the scroll follow code
                elP.setAttribute('id', 'dialogOuterSuche');
                $('#dialogOuterSuche').scrollFollow(
                                                {
                                                    speed: 1000,
                                                    offset: 0
                                                }
                        );
            }
        }
    });

    dlg.parent().appendTo($("form:first"));


    $('.dialogClick').each(function () {
        $(this).click(function () {
            dlg.dialog('open');
        });
    });


    var searchtxt = $("input#ctl00_ctl00_cm_cr_SucheAendern_tbSuche");
    var result = $("div#result")

    searchtxt.keyup(function (e) {
        var Service = new LocationService(
                      function (data) {
                          result.empty();
                          if (data != null) {
                              if (data.length != 0) {
                                  var HtmlElement = "<div class='searchclose'><span>x</span></div>";
                                  for (var i = 0; i < data.length; i++) {
                                      var Head = "<span class='betont'>" + data[i].Stadt + "</span>"
                                      HtmlElement = HtmlElement + "<div class='searchresult'>" + Head + "<br /><span>" + data[i].PLZ + "</span></div>";
                                  }
                                  result.append(HtmlElement);
                                  result.show("slow");
                                  $(".searchresult").each(function (k) {
                                      $(this).click(function () {
                                          //On click of a result div, the fullname will be written into the seachfield
                                          var FullText = data[k].Stadt; //+ ", " + data[k].PLZ;
                                          //selectPLZ = data[i].PLZ;
                                          searchtxt.val(FullText);
                                          //Hide autocomplete suggestions
                                          result.hide();
                                      });
                                  });
                                  $(".searchclose").click(function () {
                                      result.hide();
                                  });
                              } else {
                                  result.hide();
                              }
                          } else {
                              result.hide();
                          }
                      },
                      OnPageError)

        if (searchtxt.val().length > 3) {
            Service.GetOrt(searchtxt.val());
        } else {
            result.empty();
        }
    });

    function OnPageError(xhr, errorMsg, thrown) { }

    $("#divMehr").hide("fast");
    $("#asucheMehr").click(function () {
        $(this).hide("fast");
        $("#divMehr").show("slow");
    });

    $("#divStadtteile").hide("fast");
    $("#aStadtteile").click(function () {
        $(this).removeClass("MouseHand");
        $("#divStadtteile").show("slow");
    });


});



//function rawMap(arr) {
//    map = new jGoogleMaps($('#myMap')[0]);
//    jQuery.each(arr, function() {
//        var html = '<div><div style="float:left;width:133px;"><img src="' + this.bild + '" />"</div><div style="float:left;margin-left:10px;" class="text"><span class="betont">' + this.name + '<br/><br/></span><span>' + this.strasse + '<br/>' + this.plz + ' ' + this.ort + '<br/><a href=' + this.url + ' target="_blank">Details anzeigen</a></span></div>';
//        if (this.lat == 0.0 || this.lon == 0.0) {
//            map.AddMakerByAddress(this.strasse + ' ' + this.plz + ' ' + this.ort + ' germany', html);
//        }
//        else {
//            map.AddMarkerByLatLan(this.lat, this.lon, html);
//        }
//    });
//};




