﻿// JScript File

//<![CDATA[

//globals
var _mHybridMode = "Both";
var _mHybridModeShort = "Both";
window.onUnload=function(){window.onunload;GUnload();};

// create the map
var map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl(1));
map.addControl(new GScaleControl(300));
map.enableContinuousZoom();
map.enableDoubleClickZoom();
// create geocoder
var geocoder = new GClientGeocoder();
//geocoder.getLatLng("7575 Commerce Ct, Sarasota, FL 34243", function(point){});

//overview
var ovSize=new GSize(200, 150)
var ovMap=new GOverviewMapControl(ovSize);
map.addControl(ovMap);
var mini=ovMap.getOverviewMap();
ovMap.hide();

///mouse movement
map.hideControls();
GEvent.addListener(map, "mouseover", function(){
map.showControls();
});
GEvent.addListener(map, "mouseout", function(){
map.hideControls(); 
//map.closeInfoWindow();
});

// show marker
var side_bar_html = "";
var gmarkers = [];
var htmls = [];
var i = 0;
var bounds = new GLatLngBounds();
var attract;
var hot = "http://labs.google.com/ridefinder/images/mm_20_red.png";
var cold = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
var tiny = new GIcon();
tiny.image = cold
tiny.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
tiny.iconSize = new GSize(12, 20);
tiny.shadowSize = new GSize(22, 20);
tiny.iconAnchor = new GPoint(6, 20);
tiny.infoWindowAnchor = new GPoint(5, 1);
var hotIcon = new GIcon(G_DEFAULT_ICON);

function heat(num){
    attract = new GMarker(gmarkers[num].getPoint(),hotIcon);
    map.addOverlay(attract);
}
function cool(num){
    if(attract) map.removeOverlay(attract);
}
function clearMarkers() {
    if(attract) map.removeOverlay(attract);
    if(gmarkers) for(var k=0;k<gmarkers.length;k++) map.removeOverlay(gmarkers[k]);
    i=0;gmarkers=[];htmls=[];side_bar_html="";
    bounds = new GLatLngBounds();
}
function createMarker(point,name,html) {
    var marker = new GMarker(point,{icon:tiny,title:name});
    // The info window version with the "to here" form open
    direction = '<br/>Directions: <b>'+
            '<a href="http://maps.google.com/maps?saddr=&daddr=' 
                + point.lat() + ',' + point.lng() + 
            '">To here</a></b> - <b>'+
            '<a href="http://maps.google.com/maps?daddr=&saddr=' 
                + point.lat() + ',' + point.lng() + 
            '">From here</a></b>';
    html = "<b>"+name+"</b><br/>"+html+direction;
    gmarkers[i]=marker;
    htmls[i]=html;
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html);
    });
    // add a line to the side_bar html
    side_bar_html += '<a href="javascript:showAddress(' + i + ')" onmouseout="cool('+i+')" onmouseover="heat('+i+')">' + name + '</a><br/>';
    i++;
    return marker;
}
function showAddress(i) {
    gmarkers[i].openInfoWindowHtml(htmls[i]);
}
function fit(){
    //bounds.extend(pointer.getPoint());
    //if(bounds
    var center = bounds.getCenter();
    map.closeInfoWindow();
    if(i>0){map.setZoom(map.getBoundsZoomLevel(bounds));}
    map.setCenter(center);
    //map.panDirection(0,0.2);
}
// process returns
process_it = function(doc) {
    clearMarkers();
    lines = doc.split("\n");
    for (var i=0; i<lines.length; i++) {
        if (lines[i].length > 1) {
            parts = lines[i].split("|");
            var lat = parseFloat(parts[0]);
            var lng = parseFloat(parts[1]);
            var label = parts[2];
            var html = parts[3];
            var phone = parts[4];
            html += "<br/>Phone: "+phone;
            var point = new GLatLng(lat,lng);
            bounds.extend(point);
            // create the marker
            var marker = createMarker(point,label,html);
            map.addOverlay(marker);
        }
    }
    if(lines.length>=20) side_bar_html+="More...";
    if(lines.length>0) fit();
    if(side_bar_html=="") side_bar_html="Your search yielded no results. Please select a greater radius and search again."
    // put the assembled side_bar_html contents into the side_bar div
    document.getElementById("result").innerHTML = side_bar_html;
}
// process returns
process_it_nofit = function(doc) {
    clearMarkers();
    lines = doc.split("\n");
    for (var i=0; i<lines.length; i++) {
        if (lines[i].length > 1) {
            parts = lines[i].split("|");
            var lat = parseFloat(parts[0]);
            var lng = parseFloat(parts[1]);
            var label = parts[2];
            var html = parts[3];
            var phone = parts[4];
            html += "<br/>Phone: "+phone;
            var point = new GLatLng(lat,lng);
            bounds.extend(point);
            // create the marker
            var marker = createMarker(point,label,html);
            map.addOverlay(marker);
        }
    }
    if(lines.length>=20) side_bar_html+="More...";
    if(side_bar_html=="") side_bar_html="Your search yielded no results. Please select a greater radius and search again."
    // put the assembled side_bar_html contents into the side_bar div
    document.getElementById("result").innerHTML = side_bar_html;
}
function getlist_nofit() {
    point = map.getCenter();
    var rad = document.getElementById("radius");
    var radius=rad.options[rad.selectedIndex].value;
    GDownloadUrl("../../aspx/find/getdrlist.aspx?c="+point.lat()+','+point.lng()+'&r='+radius, process_it_nofit);
}
function getlist() {
    point = map.getCenter();
    var rad = document.getElementById("radius");
    var radius=rad.options[rad.selectedIndex].value;
    GDownloadUrl("../../aspx/find/getdrlist.aspx?c="+point.lat()+','+point.lng()+'&r='+radius, process_it);
}
function move2HVC() {    
    var hvc = '<div style="width:240px"><a href="http://www.extreme-h2o.com">Hydrogel Vision Corp.</a><br/>7575 Commerce Ct<br/>Sarasota, FL 34233<br/>877.336.2482</div>';

    if (GBrowserIsCompatible()) {
      var home=new GLatLng(27.405963,-82.55213);
      map.setCenter(home, 14);    // move to HVC main office
      var marker = new GMarker(home);
      map.addOverlay(marker);
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(hvc);
      });
      setTimeout(function(){marker.openInfoWindowHtml(hvc);},1000);
    }
}
function move2MyAddress(addobj) {
    if(geocoder) {
        var address = document.getElementById(addobj).value;
        geocoder.getLatLng(
            address,
            function(point) {
                if (!point) {
                    alert(address + " not found");
                    document.getElementById("latlong").innerHTML = "";
                    move2HVC();
                } else {
                    document.getElementById("latlong").innerHTML = "latitude:"+point.lat()+", longitude:"+point.lng();
                    map.setCenter(point);
                    getlist();
                    var marker = new GMarker(point);
                    map.addOverlay(marker);
                    GEvent.addListener(marker, "click", function() {
                        marker.openInfoWindowHtml(address);
                    });
                    marker.openInfoWindowHtml(address);
                }
            }
        );
    }
}
//getlist();

//]]>


