﻿var glat;
var glon;
var map;
var geocoder;
var TitleText="";
var batch=[];
var options,a;
var count;
var urlMain;
var oldMarkers = [];


Init = function () { if (window.XMLHttpRequest) { _req = new XMLHttpRequest() } else if (window.ActiveXObject) { _req = new ActiveXObject("Microsoft.XMLHTTP") } }
processStateChange = function () { if (_req.readyState == 4) { if (_req.status == 200) { if (_req.responseText == "") { return false } else { BlogsOnMap(_req.responseText) } } } }
processStateChangeMap = function () { if (_req.readyState == 4) { if (_req.status == 200) { if (_req.responseText == "") { return false } else { eval(_req.responseText); PlaceBlogs(blogsParam); } } } }
processStateCountryMap = function () { if (_req.readyState == 4) { if (_req.status == 200) { if (_req.responseText == "") { return false } else { eval(_req.responseText); PlaceCountryMarker(blogsParam); } } } }


/* old code for google map verison 2 */
function Initialize(totalblog, siteUrl) { Init(); var url = siteUrl + 'getLatLng.aspx'; if (_req != null) { _req.onreadystatechange = processStateChange; _req.open("GET", url, true); try { _req.send(null) } catch (e) { } } count = totalblog }
function initializeleft(remotelat, remotelng) { var lat = remotelat; var lng = remotelng; map = new GMap2(document.getElementById("mapcanvas")); map.setCenter(new GLatLng(lat, lng), 6); map.addControl(new GLargeMapControl()); var markers = []; for (var i = 0; i < count; ++i) { var latlng = new GLatLng(lat, lng); var value = count + 'blogs'; var marker = new GMarker(latlng, { title: value }); markers.push(marker) } var markerCluster = new MarkerClusterer(map, markers); markers = null }
function KeyDownHandler(e, btn) { var eventInstance = window.event ? event : e; if (window.event) { if (eventInstance.keyCode == 13) { eventInstance.returnValue = false; eventInstance.cancel = true; var obj = document.getElementById(btn); obj.click(); return false } } else if (eventInstance.which) { if (eventInstance.which == 13) { eventInstance.returnValue = false; eventInstance.preventDefault(); eventInstance.cancelBubble = true; var obj = document.getElementById(btn); obj.click(); return false } } }
function SetFieldTitle(id) { var fld = document.getElementById(id); if (fld && fld.title != "") { Trimmer(fld); if (fld.value == "") { fld.value = fld.title; fld.className = "search" } fld.onfocus = function () { if (this.value.toLowerCase() == this.title.toLowerCase()) { this.value = ''; this.className = "search" } }; fld.onblur = function () { Trimmer(this); if (this.value == '') { this.value = this.title; this.className = "search" } } } }
function Trimmer(controlName) { pVal = controlName.value; TRs = 0; for (i = 0; i < pVal.length; i++) { if (pVal.substr(i, 1) == " " || pVal.charCodeAt(i) == 13 || pVal.charCodeAt(i) == 10) { TRs++ } else { break } } TRe = pVal.length - 1; for (i = TRe; i > TRs - 1; i--) { if (pVal.substr(i, 1) == " " || pVal.charCodeAt(i) == 13 || pVal.charCodeAt(i) == 10) { TRe-- } else { break } } controlName.value = pVal.substr(TRs, TRe - TRs + 1) }

function InitalizeMap(siteUrl) {
    Init();
    urlMain = siteUrl;
    var url = siteUrl + 'getLatLng.aspx'; 
    if (_req != null) { _req.onreadystatechange = processStateChange; _req.open("GET", url, true); try { _req.send(null) } catch (e) { } }
    
}

function BlogsOnMap(address) {
    var siteLatLng = new google.maps.LatLng(28.635308, 77.22496); // 28.635308 ,77.22496 , Jaipur  37.4192, 122.057 // NORTH KOREA
    var myOptions = {
        center: siteLatLng,
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl: false,
        streetViewControl: false,
        navigationControlOptions: { style: google.maps.NavigationControlStyle.ZOOM_PAN }
    }
    map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions);
    
    if (!geocoder)
        geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        'address': address
    },
    function (result, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (result.length == 0) {
                //alert("Location not found");
            }
            else {
                map.setCenter(result[0].geometry.location);
                return false;
            }
        } //End if Status
    });
    google.maps.event.addListenerOnce(map, 'tilesloaded', function () {
        SearchBlogs();
    });

    google.maps.event.addListener(map, 'dragend', function () {
        SearchBlogs();
    });

    google.maps.event.addListener(map, 'zoom_changed', function () {
        //Putting settimeout becoz map takes some time for loading then give correct bounds
        window.setTimeout(SearchBlogs, 100);
    });
}


function BlogsByCountry() {
    // group by country
    var cp = map.getCenter();
    var bounds = map.getBounds();
    var sw = bounds.getSouthWest();
    var ne = bounds.getNorthEast();
    var south = sw.lat();
    var west = sw.lng();
    var north = ne.lat();
    var east = ne.lng();
    if (oldMarkers.length > 0) {
        deleteOverlays();
    }
    var url = urlMain + "getAjaxCountry.aspx?south=" + south + "&north=" + north + "&east=" + east + "&west=" + west;
    if (_req != null) {
        _req.onreadystatechange = processStateCountryMap;
        _req.open("GET", url, true);
        try { _req.send(null) } catch (e) { };
    }
}



function SearchBlogs() {
    if (map.zoom <= 7) {
        BlogsByCountry();
        return;
    }
    var cp = map.getCenter();
    var bounds = map.getBounds();
    var sw = bounds.getSouthWest();
    var ne = bounds.getNorthEast();
    var south = sw.lat();
    var west = sw.lng();
    var north = ne.lat();
    var east = ne.lng();
    //alert(south + ' -- ' + west + ' -- ' + north + ' -- ' + east);
    if (oldMarkers.length > 0) {
        deleteOverlays();
    }
    var url = urlMain + "getAjaxBlogs.aspx?south=" + south + "&north=" + north + "&east=" + east + "&west=" + west;
    if (_req != null) {
        _req.onreadystatechange = processStateChangeMap;
        _req.open("GET", url, true);
        try { _req.send(null) } catch (e) { };
    }
}

function PlaceCountryMarker(markers) {
    for (var i = 0; i < markers.length; i++) {
        var text = '';
        var blogstext = '';
        var sites = markers[i];
        //alert(sites[0]+'--'+ sites[2]+ ' -- '+ sites[3]);
        var siteLatLng = new google.maps.LatLng(sites[2], sites[3]);
        if (parseInt(sites[1]) >1)
        {
            text = "are";
            blogstext = "blogs";
        }
        else{
            text = "is";
            blogstext = "blog";
        }
        var marker = new google.maps.Marker({
            position: siteLatLng,
            map: map,
            title: sites[0],
            html: "<span class=formatText>There "+ text +" "+  sites[1] + " "+ blogstext + " in " + sites[0] +"<span>"
            //icon: image
        });
        oldMarkers.push(marker);
        var infowindow = new google.maps.InfoWindow(
            { content: sites[0],
                size: new google.maps.Size(70, 70)
            });
        google.maps.event.addListener(marker, "click", function () {
            infowindow.setContent(this.html);
            infowindow.open(map, this);
        });

    }
}

function PlaceBlogs(markers) {
    //alert(marker.length);
    for (var i = 0; i < markers.length; i++) {
        var sites = markers[i];
        //alert(sites[0]+'--'+ sites[2]+ ' -- '+ sites[3]);
        var siteLatLng = new google.maps.LatLng(sites[2], sites[3]);
        var marker = new google.maps.Marker({
            position: siteLatLng,
            map: map,
            title: sites[0],
            html: "<span class=formatText><a href='" + sites[1] + "'>" + sites[0] + "</a><span><br/><span class=formatText>" + sites[5] + "</span><br/><span class=formatText>" + sites[4] + "</span>"
            //icon: image
        });
        oldMarkers.push(marker);
        var infowindow = new google.maps.InfoWindow(
            { content: sites[0],
                size: new google.maps.Size(70, 70)
            });
        google.maps.event.addListener(marker, "click", function () {
            infowindow.setContent(this.html);
            infowindow.open(map, this);
        });

    }
}

function deleteOverlays() {
    for (var i = 0; i < oldMarkers.length; i++) {
        oldMarkers[i].setMap(null);
    }
    this.oldMarkers = new Array();
    this.blogsParam = new Array();
}

