var map     = null;
var IconA   = null;
var IconB   = null;
var latitude  = null;
var longitude = null;
var tooltip = null;
var zoom = 8;
var custom  = [];
var customs = [];
var markers = [];
var results = [];

function initialize() {

    map = new GMap2(document.getElementById("map-container"));

    if (latitude != null && longitude != null) {
        map.setCenter(new GLatLng(latitude, longitude), zoom);
    } else {
        map.setCenter(new GLatLng(35.193425, -79.465501), zoom);
    }

    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    if (map_type == 'normal'){
    	map.setMapType(G_NORMAL_MAP);
    } else if (map_type == 'satellite') {
    	map.setMapType(G_SATELLITE_MAP);
    } else {
    	map.setMapType(G_HYBRID_MAP);
    }
	    
    IconA = new GIcon(G_DEFAULT_ICON);
    IconA.iconSize   = new GSize(32, 28);
    IconA.shadowSize = new GSize(32, 28);
    IconA.iconAnchor = new GPoint(12, 27);
    IconA.image  = '/idx/img/map-icons/house-icon.png';
    IconA.shadow = '/idx/img/map-icons/shadow-icon.png';

    IconB = new GIcon(G_DEFAULT_ICON);
//    IconB.iconSize   = new GSize(36, 28);
//    IconB.shadowSize = new GSize(36, 28);
//    IconB.iconAnchor = new GPoint(12, 27);
//    IconB.image  = '/idx/img/map-icons/house-icon.png';
//    IconB.shadow = '/idx/img/map-icons/shadow-icon.png';

    tooltip = document.createElement("div");
    document.getElementById("map-container").appendChild(tooltip);
    tooltip.style.visibility="hidden";

    setTimeout("populate();", 500);
    setTimeout("custom_markers();", 500);
    //populate();

}

function populate () {

    //map.clearOverlays();
    if (results.length > 0) {

        for (x = 0; x < results.length; x++) {

            var point  = results[x];

            var i = point[2];

            markers[i] = new GMarker(point[0], { icon: IconA });
            markers[i].text = point[1];
            markers[i].mlsn = point[2];
            markers[i].ttip = '<div class="map-tooltip">' + point[3] + '</div>';

            GEvent.addListener(markers[i], "mouseover", function() {
    //            clicked = this;
                map.openInfoWindowHtml(this.getPoint(), this.text);
            });

//            GEvent.addListener(markers[i], "mouseover", function() {
//                //this.icon = IconB;
//                //this.setImage(IconB.image);
//                showTooltip(this);
//            });

            GEvent.addListener(markers[i], "mouseout", function() {
    //            if (clicked) {
    //                if (this.mlsn != clicked.mlsn) {
    //                    $('tr.listing-' + this.mlsn).removeClass('map-highlight');
    //                    this.setImage(IconA.image);
    //                }
    //            } else {
                    //this.icon = IconA;
                    this.setImage(IconA.image);
                    tooltip.style.visibility="hidden";
    //            }
            });

            map.addOverlay(markers[i]);

        }

    }

}


function custom_markers () {

    if (custom.length > 0) {

        for (x = 0; x < custom.length; x++) {

            var point  = custom[x];

            customs[x] = new GMarker(point[0], { icon: IconB });
            customs[x].text = point[1];

            GEvent.addListener(customs[x], "click", function() {
                map.openInfoWindowHtml(this.getPoint(), this.text);
            });

            map.addOverlay(customs[x]);

        }

    }

}

function showTooltip(marker) {
    tooltip.innerHTML = marker.ttip;
    var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
    var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
    var anchor=marker.getIcon().iconAnchor;
    var width=marker.getIcon().iconSize.width;
    var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y));
    pos.apply(tooltip);
    tooltip.style.visibility="visible";
}

//google.setOnLoadCallback(initialize)

$(document).ready(function () {

    initialize();
    //setTimeout("initialize();", 1000);

});

window.onunload = GUnload;