var oldHash = window.location.hash;
var newHash = "";

var map;
var mgr;
var gdir;

$(document).ready(function() {
    $("#statesListing a").click( function() {
        setTimeout("checkHash();",10);
    });
     try{
        setStateActive(location.hash.substring(1));
    }
    catch(err){}

    directionsInit();

    if(location.hash.substring(1).length > 0)
    {
        setTimeout("setStateActive(location.hash.substring(1));",1500);
    }
   
   // $("#map").click(function() {
      //  directionsInit();
        //setTimeout("directionsInit();",1000);
    //});
});

function checkHash()
{
    newHash = window.location.hash;
    if (oldHash != newHash)
    {
        setStateActive(location.hash.substring(1));
        oldHash = window.location.hash;
    }
}

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    gdir  = new GDirections(map, document.getElementById("directions"));
     var centerUS = new GLatLng(38,-97);

    map.setCenter(centerUS, 3);
    map.setUIToDefault();
    addMarkers();
  }
}

function addMarker( lat, lng, html )
{
    var point = new GLatLng( lat, lng );
    var baseIcon = new GIcon();
    baseIcon.image = "/~kangiser/Templates/shared/images/gPoint-small.png";
    baseIcon.iconSize = new GSize(8, 8);
    baseIcon.iconAnchor = new GPoint(8, 8);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);

    var marker = new GMarker(point, baseIcon);

    map.addOverlay(marker);

    GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(html);
            setTimeout("directionsInit();",500);
        });

}

function addMarkerPin( lat, lng, html )
{
    var point = new GLatLng( lat, lng );
    var pinMarker = new GIcon(G_DEFAULT_ICON);
    var marker = new GMarker(point, pinMarker);

    map.addOverlay(marker);

    GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(html);
            setTimeout("directionsInit();",500);
        });
}

function getDirections( address )
{
    gdir.load( address );
}

function setStateActive( stateID )
{
    map.clearOverlays();
    var reCenterObject = new GLatLng( reCenter[stateID]['lat'], reCenter[stateID]['lng'] );
    map.setCenter( reCenterObject, reCenter[stateID]['zoom'] );

	$("#statesListing a").removeClass("active");
	$("#statesListing ."+stateID.toLowerCase()).addClass("active");
	$(".community").css("display","none");
	$("#community_"+stateID.toLowerCase()).css("display","block");

    $.ajax({
       type: "POST",
       url: "/~kangiser/Templates/Communities/ajaxCommunities.php",
       data: "a=getState&state="+stateID,
       success: function(msg){
         eval(msg);
         directionsInit();
       }
     });

}

function directionsInit()
{
     $(".closedDirections a").click( function() {
                    $(this).parent().css("display","none");
                    $(this).parent().next().css("display","block");
                });
     $(".submitDirections").click( function() {
        submitDirections(this);
     });

      $('.openDirections input').keyup(function(e) {
        if(e.keyCode == 13) {
            submitDirections(this);
        }
      });

}

function submitDirections( element )
{
    var toAddress = $(element).parent().find(".toAddress").val();
    var fromAddress = $(element).parent().find(".fromAddress").val();
    // From and to address got switched, deal with it.
    getDirections( "from:"+toAddress+""+fromAddress);
}

var reCenter = {
                                "AK": { "lat":61.3850, "lng":-152.2683, "zoom":5 },
                                "AL": { "lat":32.7990,"lng":-86.8073, "zoom":5 },
                                "AR": { "lat":34.9513,"lng":-92.3809, "zoom":6 },
                                "AS": { "lat":14.2417,"lng":-170.7197, "zoom":5 },
                                "AZ": { "lat":33.7712,"lng":-111.3877, "zoom":6 },
                                "CA": { "lat":36.1700,"lng":-119.7462, "zoom":5 },
                                "CO": { "lat":39.0646,"lng":-105.3272, "zoom":5 },
                                "CT": { "lat":41.5834,"lng":-72.7622, "zoom":8 },
                                "DC": { "lat":38.8964,"lng":-77.0262, "zoom":5 },
                                "DE": { "lat":39.3498,"lng":-75.5148, "zoom":5 },
                                "FL": { "lat":27.8333,"lng":-81.7170, "zoom":6 },
                                "GA": { "lat":32.9866,"lng":-83.6487, "zoom":5 },
                                "HI": { "lat":21.1098,"lng":-157.5311, "zoom":5 },
                                "IA": { "lat":42.0046,"lng":-93.2140, "zoom":6 },
                                "ID": { "lat":44.2394,"lng":-114.5103, "zoom":5 },
                                "IL": { "lat":40.3363,"lng":-89.0022, "zoom":6 },
                                "IN": { "lat":39.8647,"lng":-86.2604, "zoom":6 },
                                "KS": { "lat":38.5111,"lng":-96.8005, "zoom":6 },
                                "KY": { "lat":37.6690,"lng":-84.6514, "zoom":5 },
                                "LA": { "lat":31.1801,"lng":-91.8749, "zoom":7 },
                                "MA": { "lat":42.2373,"lng":-71.5314, "zoom":5 },
                                "MD": { "lat":39.0724,"lng":-76.7902, "zoom":5 },
                                "ME": { "lat":44.6074,"lng":-69.3977, "zoom":5 },
                                "MI": { "lat":43.3504,"lng":-84.5603, "zoom":5 },
                                "MN": { "lat":45.7326,"lng":-93.9196, "zoom":6 },
                                "MO": { "lat":38.4623,"lng":-92.3020, "zoom":6 },
                                "MP": { "lat":14.8058,"lng":145.5505, "zoom":5 },
                                "MS": { "lat":32.7673,"lng":-89.6812, "zoom":6 },
                                "MT": { "lat":46.9048,"lng":-110.3261, "zoom":5 },
                                "NC": { "lat":35.6411,"lng":-79.8431, "zoom":6 },
                                "ND": { "lat":47.5362,"lng":-99.7930, "zoom":5 },
                                "NE": { "lat":41.1289,"lng":-98.2883, "zoom":6 },
                                "NH": { "lat":43.4108,"lng":-71.5653, "zoom":5 },
                                "NJ": { "lat":40.3140,"lng":-74.5089, "zoom":6 },
                                "NM": { "lat":34.8375,"lng":-106.2371, "zoom":5 },
                                "NV": { "lat":38.4199,"lng":-117.1219, "zoom":5 },
                                "NY": { "lat":42.1497,"lng":-74.9384, "zoom":6 },
                                "OH": { "lat":40.3736,"lng":-82.7755, "zoom":6 },
                                "OK": { "lat":35.5376,"lng":-96.9247, "zoom":6 },
                                "OR": { "lat":44.5672,"lng":-122.1269, "zoom":5 },
                                "PA": { "lat":40.5773,"lng":-77.2640, "zoom":5 },
                                "PR": { "lat":18.2766,"lng":-66.3350, "zoom":5 },
                                "RI": { "lat":41.6772,"lng":-71.5101, "zoom":5 },
                                "SC": { "lat":33.8191,"lng":-80.9066, "zoom":7 },
                                "SD": { "lat":44.2853,"lng":-99.4632, "zoom":5 },
                                "TN": { "lat":35.7449,"lng":-86.7489, "zoom":5 },
                                "TX": { "lat":31.1060,"lng":-97.6475, "zoom":5 },
                                "UT": { "lat":40.1135,"lng":-111.8535, "zoom":5 },
                                "VA": { "lat":37.7680,"lng":-78.2057, "zoom":5 },
                                "VI": { "lat":18.0001,"lng":-64.8199, "zoom":5 },
                                "VT": { "lat":44.0407,"lng":-72.7093, "zoom":5 },
                                "WA": { "lat":47.3917,"lng":-121.5708, "zoom":5 },
                                "WI": { "lat":44.2563,"lng":-89.6385, "zoom":5 },
                                "WV": { "lat":38.4680,"lng":-80.9696, "zoom":5 },
                                "WY": { "lat":42.7475,"lng":-107.2085, "zoom":5 }
                            };
