/******************************************************************************/
/**
 * Create a new TourMap object.
 * @param xmlurl - the URL used to fetch map markers
 */

function TourMap(div,xmlurl)
{
    if (div)
    {
        this.div = document.getElementById(div);
        this.xmlurl = xmlurl;

        if (!GBrowserIsCompatible())
        {
            alert('Your browser does not support mapping');
            return;
        }

        this.places = new Array();

        // Default map icon
        this.icon = new GIcon();
        this.icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        this.icon.iconSize = new GSize(12, 20);
        this.icon.shadowSize = new GSize(22, 20);
        this.icon.iconAnchor = new GPoint(6, 20);
        this.icon.infoWindowAnchor = new GPoint(5, 1);
        this.icon.infoShadowAnchor = new GPoint(10, 12);

        // Load XML data
        this.request = GXmlHttp.create();
        this.request.open("GET", this.xmlurl, true);
        var tm = this;
        this.request.onreadystatechange = function() { tm.LoadXML() };
        
        this.request.send(null);
    }
}

new TourMap();  // Must have this to be able to define prototype items

// Defaults
//TourMap.prototype.lat = '';
//TourMap.prototype.lng = '';

/******************************************************************************/
/**
 * Create map and trigger marker loading
 * @param lat, lng - lat/lng used to center the map
 */
function TourMap_DrawMap(lat, lng, zoom)
{
    if (GBrowserIsCompatible())
    {
        this.map = new GMap2(this.div);

        if (this.div.offsetWidth >= 500)
        {
            this.map.addControl(new GLargeMapControl());
            this.map.addControl(new GLargeMapControl());
            this.map.addControl(new GScaleControl(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(10,50)));
            this.map.addControl(new GMapTypeControl());
            //this.map.addControl(new GHierarchicalMapTypeControl());
            this.map.addControl(new GOverviewMapControl());
            this.map.enableDoubleClickZoom();
            this.map.enableGoogleBar();
        }
        else
        {
            this.map.addControl(new GSmallZoomControl());
            this.map.enableDoubleClickZoom();
        }
        this.map.setCenter(new GLatLng(lat, lng), zoom);
    }
}
TourMap.prototype.DrawMap = TourMap_DrawMap;

/******************************************************************************/
/**
 * Load map markers - callback routine from loading the marker XML data
 */
function TourMap_LoadXML()
{
    if (this.request.readyState == 4)
    {
        var xmlDoc = this.request.responseXML;
        var elements = xmlDoc.documentElement.getElementsByTagName("place");

        for (var i = 0; i < elements.length; i++)
        {
            this.places[this.places.length] = new TourMapPlace(this, elements[i]);
        }

        this.XMLLoadComplete();
    }
}
TourMap.prototype.LoadXML = TourMap_LoadXML;

/**
 * LoadFunction called when XML load is complete
 */
function TourMap_XMLLoadComplete()
{
    // If we have a map, make map markers
    if (this.map)
    {
        for (var i = 0; i < this.places.length; i++)
        {
            this.map.addOverlay(this.places[i].CreateMapMarker());
        }
    }
}
TourMap.prototype.XMLLoadComplete = TourMap_XMLLoadComplete;

/******************************************************************************/
/**
 * Create a new TourMapPlace object.
 * @param xmldata - data about the place
 */

function TourMapPlace(tm, xmldata)
{
    if (tm)
    {
        this.tm = tm;

        this.name = unescape(xmldata.firstChild.nodeValue);

        for (var i = 0; i < this.xml_element_names.length; i++)
        {
            this[this.xml_element_names[i]] = unescape(xmldata.getAttribute(this.xml_element_names[i]));
        }
    }
}

new TourMapPlace();  // Must have this to be able to define prototype items

TourMapPlace.prototype.xml_element_names = new Array('place_id', 'address', 'city', 'state', 'zip', 'phone', 'lat', 'lng',
                                                     'website', 'type', 'ba_place_id', 'rb_place_id', 'bm_place_id');

/******************************************************************************/
/**
 * Create map marker
 */
function TourMapPlace_CreateMapMarker()
{
    var icon = new GIcon(this.tm.icon);
    var color = 'black';
    if (this.type == 't')
    {
        color = 'brown';
    }
    else if (this.type == 'm')
    {
        color = 'red';
    }
    else if (this.type == 'b')
    {
        color = 'yellow';
    }
    else if (this.type == 'p')
    {
        color = 'blue';
    }
    else if (this.type == 's')
    {
        color = 'green';
    }
    else if (this.type == 'l')
    {
        color = 'gray';
    }
    icon.image = "/image/mm_20_" + color + ".png";

    this.map_marker = new GMarker(new GLatLng(this.lat, this.lng), { title: this.name, icon: icon });

    var place = this;

    GEvent.addListener(this.map_marker, "click", function() { place.ShowInfoWindow(); });
    //GEvent.addListener(this.map_marker, "mouseover", function() { place.ShowTooltip(); });
    //GEvent.addListener(this.map_marker, "mouseout", function() { place.HideTooltip(); });

    return this.map_marker;
}
TourMapPlace.prototype.CreateMapMarker = TourMapPlace_CreateMapMarker;

///******************************************************************************/
///**
// * Show marker tooltip - callback routine from marker mouseover
// */
//function TourMapPlace_ShowTooltip()
//{
//    var map = this.tm.map;
//    var marker = this.map_marker;
//
//    var tooltip = document.getElementById("tooltip");
//
//    tooltip.innerHTML = "<table border=1 bgcolor=white cellpading=0 cellspacing=0><tr><td>"+this.name+"</td></tr></table>";
//
//    var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
//    var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
//    var anchor=marker.getIcon().iconAnchor;
//    var width=marker.getIcon().iconSize.width;
//    var height=tooltip.clientHeight;
//    var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height));
//    pos.apply(tooltip);
//
//    tooltip.style.visibility="visible";
//}
//TourMapPlace.prototype.ShowTooltip = TourMapPlace_ShowTooltip;
//
///**
// * Hide marker tooltip - callback routine from marker mouseover
// */
//function TourMapPlace_HideTooltip()
//{
//    var tooltip = document.getElementById("tooltip");
//    tooltip.style.visibility="hidden";
//}
//TourMapPlace.prototype.HideTooltip = TourMapPlace_HideTooltip;


/******************************************************************************/
/**
 * Information popup - shown when map marker is clicked
 */
function TourMapPlace_ShowInfoWindow()
{
    if (!this.info_windox_text)
    {
        this.info_window_text = '<b><a target=_blank href="/pages/travel/place.php?placeID=' + this.place_id + '">'+this.name+"</a></b>";
        this.info_window_text += '<br>' + this.address;
        this.info_window_text += '<br>' + this.city+' '+this.state+'  '+this.zip;
        this.info_window_text += '<br>' + this.phone;
/*
        if (this.website)
        {
            this.info_window_text += '<br><a target=_blank href="' + this.website + '">Website</a>';
        }

        if (this.ba_place_id > 0)
        {
            this.info_window_text += '<br><a target=_blank href="http://beeradvocate.com/beer/profile/' + this.ba_place_id + '">BeerAdvocate</a>';
        }

        if (this.rb_place_id > 0)
        {
            this.info_window_text += '<br><a target=_blank href="http://www.ratebeer.com/Places/ShowPlace.asp?PlaceID=' + this.rb_place_id + '">RateBeer</a>';
        }

        if (this.bm_place_id > 0)
        {
            this.info_window_text += '<br><a target=_blank href="http://BeerMapping.com/maps/reviews/reviews.php?locid=' + this.bm_place_id + '">BeerMapping</a>';
        }

        this.info_window_text += '<br><a target=_blank href="http://maps.google.com/maps?q=hotels&near=' + this.lat + ',' + this.lng + escape(' (' + this.name + ')') + '">Nearby Hotels</a>';

        this.info_window_text += '<br>' + this.place_id;
*/
    }

    this.map_marker.openInfoWindowHtml(this.info_window_text);
}
TourMapPlace.prototype.ShowInfoWindow = TourMapPlace_ShowInfoWindow;
