﻿var currentmap = null;

var infowindow = null;
var currentContent = null;
var currentmcanvas = null;
var currentlocations = null;
var maps = new Array();
var lookUpMarkers = new Array();
var OriginalDivHeight=0;
var OriginalDivWidth=0;

function initializeMaps(map_canvas, Lat, Long, locations, level, iwindow) {
    initialize(map_canvas, Lat, Long, locations, level, iwindow, true);
}

function initialize(map_canvas, Lat, Long, locations, level, iwindow, useLookup) {


    if (locations.length > 0) {
        if (level == null)
            level = 10;
        var latlng = new google.maps.LatLng(Lat, Long);
        var myOptions = {
            zoom: level,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        infowindow = new google.maps.InfoWindow();
        var map = new google.maps.Map(document.getElementById(map_canvas), myOptions);

        setMarkers(map, locations, map_canvas, iwindow, useLookup);

        map.setCenter(latlng, level);
        currentmap = map;





    }
}


function setMarkers(map, locations, map_canvas, iwindow, useLookup) {
    for (var i = 0; i < locations.length; i++) {
        var name = locations[i];
        var myLatLng = new google.maps.LatLng(name[2], name[3]);
        marker = new google.maps.Marker({
            position: myLatLng,
            map: map
        });
        currentmarker = marker;
        if (iwindow != null && iwindow != "undefined") {
            contentStringSB = iwindow;
        } else {
            var contentStringSB = BuildInfoWindowContent(map, name, map_canvas);
        }

        if (useLookup) {
            lookUpMarkers.push([locations[i], map_canvas, map, locations, marker, contentStringSB]);
        }
        attachMarker(map, marker, contentStringSB, locations);
        // currentContent = contentStringSB;
    }

}

function attachMarker(map, marker, infoContent, locations, map_canvas) {


       google.maps.event.addListener(marker, 'click', function() {

        infowindow.setContent(infoContent);
        currentContent = infoContent;
        infowindow.open(map, marker);
        currentmarker = marker;
        currentmap = map;
        currentmcanvas = map_canvas;
        currentlocations = locations
 
    });
      google.maps.event.addListener(marker, 'mouseover', function() {

        infowindow.setContent(infoContent);
        currentContent = infoContent;
        infowindow.open(map, marker);
        currentmarker = marker;
        currentmap = map;
        currentmcanvas = map_canvas;
        currentlocations = locations

    });



}


function BuildInfoWindowContent(map, name, map_canvas) {


    name[0] = name[0].replace("'", "*");
    name[1] = name[1].replace("'", "*");


    var SendToMobile = "<br/> <a href='#Maintain' onclick='Javascript:OpenWindow(\"" + name + "\"); return false;'>Send to Mobile Phone</a><br/>";
    var directions = "<a href='#Maintain' onclick=" + "toggleDiv('tohere') return false" + ">To here</a>  -  <a href='#Maintain' onclick=" + "toggleDiv('fromhere') return false" + ">From here</a>";

   var nearbycontent = "&nbsp;<a href='#Maintain' onclick=" + "toggleDiv('nearby') return false" + ">Search nearby</a>";
    //does not work in firefox
    //var nearbycontent = "&nbsp;<a href='#' onclick='Javascript:toggleDiv('nearby'); return false'>Search nearby</a>";

    var zoomhere = "<br/><a href='#Maintain' onclick='Javascript:ZoomHere(\"" + name + "\"); return false'> Zoom Here </a>";
    var zoomReset = "<br/><a href='#Maintain' onclick=" + "ZoomReset() return false" +"> Reset Zoom</a>";

    name[0] = name[0].replace("*", "'");
    name[1] = name[1].replace("*", "'");
    var content = "<div id='maindiv' class='content contentLink divsize'>"
    + "<b>" + name[0] + "</b>" + name[1]

   + "<br> Get Directions:"

   + directions
   + zoomhere

   + nearbycontent
   + SendToMobile
   + "<hr>"
   + PopulateSubDiv(name)
   + " </div>";
 

    return content;
}

function ZoomReset() {
    initializeMap();

}
function ZoomHere(strname) {
    var name = strname.replace("*", "'");
    name = name.split(",");

    Locate(name);

    return false;
}
function Locate(name) {
    infowindow.close();
    var map_canvas;
    var locations;
    var map;

    var content;
    for (i = 0; i < lookUpMarkers.length; i++) {
         var item = lookUpMarkers[i];
        var id = item[0][4];


        if (name[5]==item[0][5])
       {
            newmap_canvas = item[1];
            currentmap = item[2];
            currentlocations = item[3];
            newmarker = item[4];
            newcontent = item[5];
           currentmarker= newmarker ;

            initialize(newmap_canvas, name[2], name[3], currentlocations, 16, "undefined", false);
            infowindow.close();
            infowindow.setContent(newcontent);
            infowindow.open(currentmap, newmarker);

           // attachMarker(currentmap, newmarker, newcontent, currentlocations);
            break;
        }
    }


}

function LocateHere(strname) {

    ZoomHere(strname);

    return false;
}


function OpenWindow(strname) {
    name = strname.replace("*", "'");
    name = name.replace(/<br>/g, "!~");
  
    var elements = name.split(",");
    var address = elements[0] + elements[1];
    var elements1 = address.split("!~");
    var id = elements1[0] + "," + elements1[1] + "," + elements1[2] + "," + elements1[4];
    id = id.replace(/</g, "");
    id = id.replace(/>/g, "");



    //  var oWnd = window.open('/locations/SendSMS.aspx?id=' + id, '', 'width=400,height=350');
    var oWnd = window.radopen('SendSMS.aspx?id=' + id, 'rwPopup')
    oWnd.SetSize(330, 400);
    oWnd.Center();
    oWnd.SetTitle("Text Address");

}


function PopulateSubDiv(name) {
    var subContents = ToHere(name);
    subContents += FromHere(name);
    subContents += SearchNearBy(name);
    return subContents;
}




function SearchNearBy(names) {

    //    var nearbycontent = "<br/><a href='' onclick='javascript:toggleDiv(nearby); return false;'> Search nearby: e.g. DMC</a>"
    var nearbycontent = "<div id='nearby' style='display:none;height:80px;'>"
   + "<form id='frmnearby' name='frmnearby' action='http://maps.google.com/maps' target='_blank' method='get'>Example: DMC "
   + "<input type='text' SIZE=30 MAXLENGTH=70 name='q' id='q' value=''/>"
   + "<div style='float:left;padding-right:10px'><a href='#Maintain' onclick=" + "toggleDiv('nearby') return false" + ">Back</a></div><INPUT value='Search nearby' TYPE='SUBMIT'/>"
   + "<input type='hidden' name='near' value='" + name + "'" + "@" + names[2] + "," + names[3] + "'/>"

   + "</form></div>";
    return nearbycontent;
}
function ToHere(names) {
    //var directions = "<a href='' onclick='javascript:toggleDiv(tohere); return false;'>To here</a>  -  <a href='' onclick='javascript:toggleDiv(fromhere); return false;'>From here</a>"
    var tohere = "<div id='tohere' style='display:none;height:80px;'>"
        + "<form action='http://maps.google.com/maps' id='frmTohere' name='frmTohere' method='get' target='_blank'>Start address:" +
           "<input type='text' SIZE=30 MAXLENGTH=40 name='saddr' id='saddr' value='' />" +
           "<div style='float:left;padding-right:10px'><a href='#Maintain' onclick=" + "toggleDiv('tohere') return false" + ">Back</a></div><INPUT value='Get Directions' TYPE='SUBMIT'/>" +
           "<input type='hidden' name='daddr' value='" + names[2] + "," + names[3] + "'/>"
             + "<input type='hidden' name='hl' value='en' />"
     + " </form></div>";


    return tohere;
}

function FromHere(names) {


    var fromHere = "<div id='fromhere' style='display:none;height:80px;'>"
          + "<form action='http://maps.google.com/maps' id='frmFromhere' name='frmFromhere' method='get' target='_blank'>End address:" +
           "<input type='text' SIZE=30 MAXLENGTH=40 name='daddr' id='daddr' value='' />" +
           "<div style='float:left;padding-right:10px'><a href='#Maintain' onclick=" + "toggleDiv('fromhere') return false" + ">Back</a></div> <INPUT value='Get Directions' TYPE='SUBMIT'/>" +
           "<input type='hidden' name='saddr' value='" + names[2] + "," + names[3] + "'/>"
            + "<input type='hidden' name='hl' value='en' />"

+ "</form></div>";

    return fromHere;
}




function toggleDiv(subdiv) {

    var infodiv= document.getElementById("maindiv");
   
    var varonid = subdiv;
    var infoSubdiv = document.getElementById(varonid);
    if (varonid != null && varonid == 'nearby') {
        document.getElementById('tohere').style.display = 'none';
        document.getElementById('fromhere').style.display = 'none';


    }
    if (varonid != null && varonid == 'tohere') {

        document.getElementById('fromhere').style.display = 'none';
        document.getElementById('nearby').style.display = 'none';


    }
    if (varonid != null && varonid == 'fromhere') {

        document.getElementById('tohere').style.display = 'none';
        document.getElementById('nearby').style.display = 'none';


    }
 	if(OriginalDivHeight==0)
 	{
	OriginalDivHeight= infodiv.scrollHeight;
	OriginalDivWidth = infodiv.scrollWidth;
	
	}
	
// alert(OriginalDivHeight);

     if (infoSubdiv != null) {
        if (infoSubdiv.style.display == 'none') {
            infoSubdiv.style.display = 'block';
             height = (OriginalDivHeight+ infoSubdiv.scrollHeight) + "px";
             width = OriginalDivWidth + "px";
              // infodiv.style.width = width;
               infodiv.style.height = height;
               
             }

        else {
           
                infodiv.style.height =OriginalDivHeight + "px";

	            infodiv.style.width =OriginalDivWidth + "px";
		 
            infoSubdiv.style.display = 'none';
            infoSubdiv.style.width = "0px" ;
             infoSubdiv.style.height ="0px" ;        
			 
        }
    }
  
   

  

    infowindow.close();
    infowindow = new google.maps.InfoWindow();     
    infowindow.setContent(infodiv);
   //infowindow.open(currentmap, currentmarker);
   
   infowindow.setPosition(currentmarker.getPosition()); 
  
   infowindow.open(currentmap);    
 return false;

}

function codeAddress(address) {
    initializeMap();
    infowindow.close();
    geocoder = new google.maps.Geocoder();
    if (geocoder) {
        geocoder.geocode({ 'address': address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {

                for (i = 0; i < lookUpMarkers.length; i++) {
                    var item = lookUpMarkers[i];
                    if (item[1] == 'map_canvasAlllocation') {
                        newmap_canvas = item[1];
                        currentmap = item[2];
                        currentlocations = item[3];
                        newmarker = item[4];



                        var marker = new google.maps.Marker({
                            map: item[2],
                            position: results[0].geometry.location,
                            icon: '/images/marker1.png'

                        });

                        infowindow.setContent(address);
                        infowindow.open(item[2], marker);

                        google.maps.event.addListener(marker, 'click', function() {
                            infowindow.setContent(address);
                            infowindow.open(item[2], marker);
                            item[2].setCenter(results[0].geometry.location);


                        });
                        item[2].setCenter(results[0].geometry.location);


                        break;
                    }


                }

            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
}





function handleAddressSubmit() {
    // setupInitalMarkers(); // call this again to blank out any submitted pins
    var addr = $("#Address").val() + ", " + $("#City").val() + ", " + $("#State").val() + " " + $("#Zip").val();
    codeAddress(addr);
}

function ClearGoogleMap() {
    if (infowindow != null) {
        infowindow.close();
    }

    currentmap = null;
     infowindow = null;
     currentContent = null;
     currentmcanvas = null;
     currentlocations = null;
     maps = null;
    lookUpMarkers = null;
}

