﻿function hidemap() {
    todo = { action: 'clear' };
    $j('#map1').gmap3(todo);
    $j("#map").show();
    $j("#map1").hide();
    $j("#map2").hide();
    $j("#closebutton").css("z-index", "0");
}
function region(rid, rnameurl) {
    $j.ajax({
        type: "POST",
        url: "Default.aspx/GetRegion",
        data: JSON.stringify({ "regionurl": rnameurl }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (result) { alert(result.toSource()); },
        success: function (data) {
            var rt = data.d;
            if (rt != "") {
                var destList = rt.split('*');
                var myJSON = new Array();
                for (var i = 0; i <= destList.length - 2; i++) {
                    if (destList[i].split('~')[0] != "" && destList[i].split('~')[1] != "") {
                        var obj = new Marker(parseFloat(destList[i].split('~')[0]), parseFloat(destList[i].split('~')[1]), destList[i].split('~')[2]);
                        myJSON.push(obj);
                    }

                }
                var reglat = rt.split('$')[1];
                var reglong = rt.split('$')[2].split('#')[0];
                var regzoom = parseInt(rt.split('#')[1]);
                $j("#map").hide();
                $j("#map1").show();
                $j("#closebutton").css("z-index", "99999999");
                $j("#map1").gmap3({ action: 'init',
                    options: {
                        // center: [reglat, reglong],
                        zoom: regzoom
                    }
                },
                                        { action: 'addMarkers',
                                            markers: myJSON, marker: {
                                                options: {
                                                    draggable: false
                                                },
                                                events: {
                                                    mouseover: function (marker, event, data) {
                                                        var map = $j(this).gmap3('get'),
                      infowindow = $j(this).gmap3({ action: 'get', name: 'infowindow' });
                                                        if (infowindow) {
                                                            infowindow.open(map, marker);
                                                            infowindow.setContent(data);
                                                        } else {
                                                            $j(this).gmap3({ action: 'addinfowindow', anchor: marker, options: { content: data} });
                                                        }
                                                    },
                                                    mouseout: function () {
                                                        var infowindow = $j(this).gmap3({ action: 'get', name: 'infowindow' });
                                                        if (infowindow) {
                                                            infowindow.close();
                                                        }
                                                    },
                                                    click: function (marker, event, data) {
                                                        data = data.replaceAll(' ', '-');
                                                        data = data.replaceAll("'", '');
                                                        data = data.replaceAll("/", '-');
                                                        data = data.replaceAll("---", '-');
                                                        data = data.replaceAll(".", '');
                                                        window.location.href = "search-in-" + rnameurl + "/" + data.replaceAll(' ', '-') + "/page=1";

                                                    }
                                                }
                                            }
                                        });
                var myLatlng = new google.maps.LatLng(reglat, reglong);
                $j('#map1').gmap3({ action: 'setCenter', args: [myLatlng] });
                $j('#map1').gmap3({ action: "setZoom", args: [regzoom] });
            }
            else {
                $j("#map").hide();
                $j("#map2").show();
                $j("#closebutton").css("z-index", "99999999");
                $j('#map2').html('<h3>Sorry , no properties available at present  in this region</h3>');
                
            }
        }

    });


}
function Marker(lat, lng, data) {
    this.lat = lat;
    this.lng = lng;
    this.data = data;
}
String.prototype.replaceAll = function (str1, str2, ignore) {
    return this.replace(new RegExp(str1.replace(/([\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, function (c) { return "\\" + c; }), "g" + (ignore ? "i" : "")), str2);
};
