// fah.js

var map = null;
var geocoder = null;
//var marker = null;

var baseIcon = new GIcon();
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

var gmarkers = [];
var texts = [];
var to_texts = [];
var from_texts = [];
var i=0;
var showopen = 1;
var showclosed = 0;


function markMap(map, point, name, short_name, id, type, feature_level, count) {
    var myicon = new GIcon(G_DEFAULT_ICON);
    if (feature_level <= 1) {
        //        icon.image = "http://www.findahaunt.com/markerreg.png";
        myicon.image = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=H|6666FF|000000";
    } else {
        //        icon.image = "http://www.findahaunt.com/markerfeature.png";
        myicon.image = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=H|FF0000|000000";
    }

    var marker = new GMarker(point, {icon:myicon, title:name});

    //    var label = new ELabel(point, short_name, "maplabel");
    
    GEvent.addListener(marker, "click", function() {
            //            marker.closeInfoWindow();
            //            marker.openInfoWindowHtml("this is the onclick window");
            showsingle(id);
        });
    
    //    GEvent.addListener(marker, "mouseover", function() {
    //        marker.openInfoWindowHtml("<span class=\"iwind\">" + name + "</span>");
    //    });
    
    //    GEvent.addListener(marker, "mouseout", function() {
    //            marker.closeInfoWindow();
    //        });
    
    map.addOverlay(marker);

    //    map.addOverlay(label);

    return marker;
}

function createMarker(point, name, text, tag, feature_level, count) {
    var icon = new GIcon(G_DEFAULT_ICON);
    if (feature_level <= 1) {
        //        icon.image = "http://www.findahaunt.com/markerreg.png";
        icon.image = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=H|6666FF|000000";
    } else {
        //        icon.image = "http://www.findahaunt.com/markerfeature.png";
        icon.image = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=H|FF0000|000000";
    }

    var marker = new GMarker(point, icon);

    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(name);
    });

    return marker;
}

function createMarkerOrig(point, name, text, tag, feature_level, count) {
    var icon = new GIcon(G_DEFAULT_ICON);
    if (feature_level <= 1) {
        //        icon.image = "http://www.findahaunt.com/markerreg.png";
        icon.image = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=H|6666FF|000000";
    } else {
        //        icon.image = "http://www.findahaunt.com/markerfeature.png";
        icon.image = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=H|FF0000|000000";
    }

    var marker = new GMarker(point, icon);

    to_texts[count] = "<span class=\"iwind\">" + text +
        '<hr>Directions: <b>To here</b> - <a href="javascript:fromhere('
        + count + ')">From here</a>' +
        '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
        '<input type="text" size=40 maxlength=40 name="saddr" id="saddr" value="" />' +
        '<input value="Get Directions" type="submit">' +
        '<input type="hidden" name="daddr" value="' +
        point.y + ',' + point.x + "(" + name + ")" +
        '"/></form><br>Be careful - These directions are only approximate</span>';
    // The info window version with the "to here" form open
    from_texts[count] = "<span class=\"iwind\">" + text +
        '<hr>Directions: <a href="javascript:tohere(' + count +
        ')">To here</a> - <b>From here</b>' +
        '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
        '<input type="text" size=40 maxlength=40 name="daddr" id="daddr" value="" />' +
        '<input value="Get Directions" type="submit">' +
        '<input type="hidden" name="saddr" value="' +
        point.y + ',' + point.x + "(" + name + ")" +
        '"/></form><br>Be careful - These directions are only approximate</span>';
    // The inactive version of the direction info
    text = "<span class=\"iwind\">" + text +
        '<hr>Directions: <a href="javascript:tohere(' + count +
        ')">To here</a> - <a href="javascript:fromhere(' + count +
        ')">From here</a>' + "</span>";

    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(text);
    });
    gmarkers[count] = marker;
    texts[count] = text;
    //    i++;
    return marker;
}

function zoomhere(y, x){
    map.setcenter(new GLatLng(y, x), 14);
    closeInfoWindow();
}

// functions that open the directions forms
function tohere(i) {
    gmarkers[i].openInfoWindowHtml('<div style="white-space:nowrap;">'+ to_texts[i]+'</div>');
}

function fromhere(i) {
    gmarkers[i].openInfoWindowHtml('<div style="white-space:nowrap;">'+ from_texts[i]+'</div>');
}

function drawCircle(map, center, radius, numPoints, color, lineWidth) {
    // distanceFrom returns distance in meters
    var metersPerMile = 1609.344;
    var miPerDegLat =
        center.distanceFrom(new GLatLng(center.lat() + 1,
                                        center.lng())) / metersPerMile;
    var miPerDegLng =
        center.distanceFrom(new GLatLng(center.lat(),
                                        center.lng() + 1)) / metersPerMile;

    var pointArr = [];
    var increment = parseInt(360 / numPoints);
    for (var i = 0; i < 360; i += increment) {
        var pTmp =
            new GLatLng(center.lat() +
                        radius / miPerDegLat * Math.cos(i * Math.PI / 180),
                        center.lng() +
                        radius / miPerDegLng * Math.sin(i * Math.PI / 180));
        pointArr.push(pTmp);
    }
    pointArr.push(pointArr[0]);
    var poly = new GPolyline(pointArr, color, lineWidth);
    map.addOverlay(poly);

    pointArr = [];
    pTmp = new GLatLng(center.lat() + radius / miPerDegLat / 30, center.lng());
    pointArr.push(pTmp);
    pTmp = new GLatLng(center.lat() - radius / miPerDegLat / 30, center.lng());
    pointArr.push(pTmp);
    poly = new GPolyline(pointArr, color, lineWidth);
    map.addOverlay(poly);

    pointArr = [];
    pTmp = new GLatLng(center.lat(), center.lng() + radius / miPerDegLat / 25);
    pointArr.push(pTmp);
    pTmp = new GLatLng(center.lat(), center.lng() - radius / miPerDegLat / 25);
    pointArr.push(pTmp);
    poly = new GPolyline(pointArr, color, lineWidth);
    map.addOverlay(poly);
}

function geocodeInput(address) {
  if (geocoder) {
    geocoder.getLatLng(address, function(point) {
      if (!point) {
        alert("Address \"" + address + "\" was not found, please try again");
      } else {
        document.geoform.search_lat.value=point.y;
        document.geoform.search_lng.value=point.x;
        document.geoform.search_addr.value=address;
        document.geoform.search_rad.value=document.geoformhelper.rad.value;
        document.geoform.submit();
      }
    });
  }
}

function non_map_initialize() {
    if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
    }
}

function notblank(value, text)
{
    if (!value) {
        alert(text + " can not be blank");
        return 1;
    }
}

function validateaccountinput()
{
    var count = 0;
    count += notblank(document.account.email1.value, "Email address 1");
    count += notblank(document.account.email2.value, "Email address 2");
    count += notblank(document.account.fname.value, "First name");
    count += notblank(document.account.lname.value, "Last name");
    count += notblank(document.account.street1.value, "Street");
    count += notblank(document.account.city.value, "City");
    count += notblank(document.account.st.value, "State");
    count += notblank(document.account.zip.value, "ZIP Code");
    count += notblank(document.account.phone.value, "Phone");
    if (count) {
        return(false);
    }

    if (document.account.email1.value != document.account.email2.value) {
        alert("Email addresses did not match");
        return(false);
    }
    
    if (document.account.pw1.value != document.account.pw2.value) {
        alert("Passwords did not match");
        return(false);
    }

    return(true);
}

function validatelistinginput()
{
    var count = 0;
    count += notblank(document.listing.name.value, "Name");
    count += notblank(document.listing.short_name.value, "Short Name");
    count += notblank(document.listing.street1.value, "Street");
    count += notblank(document.listing.city.value, "City");
    count += notblank(document.listing.st.value, "State");
    count += notblank(document.listing.zip.value, "ZIP Code");
    count += notblank(document.listing.lat.value, "Latitude");
    count += notblank(document.listing.lng.value, "Longitude");

    if (count) {
        return(false);
    }
    return(true);
}

// EOF

