
////////////////////////////////////////////////////////////////////////////////////////
//
//
// function createMarkerClickHandler( marker, title, code, text) {
function createMarkerClickHandler(  place, eventText, firstTab ) {
    return function() {
        var gTabs = []; 
//         for (var name in place.tabbedInfo)
        var tabNames = [];

        if (firstTab ) tabNames.push( firstTab );
        for (var name in allPlaceTypes) {
            if (name != firstTab ) {
                tabNames.push( name );
            }
        }

        for (var i = 0; i<tabNames.length; i++)
        {
            name = tabNames[i];
            var text = '';
            if ( place.tabbedInfo[name] ) {  
                text = place.tabbedInfo[ name ] ;
            } /* else  {
                text = 'No ' + allPlaceTypes[name].prettyName + ' at this location.';
            } */
            if (name == 'food' ) {
                text += '<br/><a href="javascript:showNearbyPlaces(' 
                    + place.lat + ',' + place.lng 
                    + ', maxNearbyDist, \'' + name + '\');">'
                    + 'Show nearby ' + allPlaceTypes[name].prettyName + '</a>';
            }
            if (text && text != "" ) 
                gTabs.push( new GInfoWindowTab( allPlaceTypes[name].tabname, text ) );
        }
        
        // add the event info (if any) as a tab on the info window
        if (eventText && eventText != "" )
        {
            gTabs.push( new GInfoWindowTab( 
                            "Event Info", 
                            '<div class="eventInfoTab" >'
                            + eventText 
                            + '</div>' 
                            ) 
                        );
        }

        place.marker.openInfoWindowTabsHtml( gTabs );
//                     '<h3>' + title + '</h3>' +
//             genTabsForInfo(
//                 [ 
//                     { 
//                         name:"building", prettyName: "Building Info", 
//                         tabBody:text 
//                     },
//                     { 
//                         name:"food", prettyName:"food", 
//                         tabBody:"There is no food yet." 
//                     }
//                 ],
//                 code
//             ) 
//             + '<p class="mapPopup">' + text + '</p>' 
//         );
        return false;
    };
}

function genTabsForInfo( tabText, placeCode )
{

    // I'm probably going to bad-programmer-land for this, but it seems to work :-(
    // jQuery might've made it easier, but not to write ... 
//     var infoTabs = document.createElement("div");
//     $(infoTabs).addClass("flora");
//     $(infoTabs).attr("id",placeCode + "tabDiv" );

    var htmlString = '<div class="flora" id="' + placeCode + 'tabDiv">'
    htmlString += '<ul>';

    for (var i=0; i<tabText.length; i++)
    {
        htmlString += '<li><a href="#' + placeCode + tabText[i].name + '">';
        htmlString += '<span>' + tabText[i].prettyName + '</span>';
        htmlString += '</a></li>';
    }
    htmlString += '</ul>';

    for (var i=0; i<tabText.length; i++)
    {
        htmlString += '<div id="' + placeCode + tabText[i].name + '">';
        htmlString += tabText[i].tabBody;
        htmlString += '</div>';
    }
    htmlString += '</div>';

//     alert($(infoTabs).html());
//     $(infoTabs).append( $( htmlString ) );
//     $(infoTabs).append($('<script> $("#' + placeCode + 'tabDiv").tabs();</script>'));
    htmlString += '<script> $("#' + placeCode + 'tabDiv > ul").tabs();</script>';
//     alert($(infoTabs).html());
//     $(infoTabs).tabs();
//     alert($(infoTabs).html());
    
//     return $(infoTabs).html();
//     alert(htmlString);
    return htmlString;

}

////////////////////////////////////////////////////////////////////////////////////////
//
//
function addMarkerGroupFromList( list, groupName )
{
    for (var i=1; i < list.length; i++ )
    {
        if ( ! list[i].marker )
        {
            
            list[i].marker = new GMarker( new GLatLng( list[i].lat, list[i].lng ) );
        }
        map.addOverlay( list[i].marker );
    }
}

////////////////////////////////////////////////////////////////////////////////////////
//
//
function addMarkerGroup( groupname )
{
    for (var i=0; i<allPlaces.length; i++)
    {
        if ((allPlaces[i].type == groupname ) || ( allPlaces[i].hasFeature( groupname ) ) )
        {
            // if we've never created a marker for this Place before, try to do that now
            if ( ! allPlaces[i].marker )
            {
//                     allPoints[i].marker = place.addMeToGMap( map, "" );
                    // this will create a GMarker, add it to the map, and save it in the Place object 
                allPlaces[i].addMeToGMap( map, "", groupname );
//                 allPoints[i].marker = new GMarker( new GLatLng( allPoints[i].lat, allPoints[i].lng ) );
//                 markerListing.push( { "marker":allPoints[i].marker, "group":groupName } );
//                 var handler = createMarkerClickHandler( 
//                         allPoints[i].marker , 
//                         allPoints[i].name + "<br/>" + allPoints[i].desc ? allPoints[i].desc : "" ,
//                         "http://www.utexas.edu/maps/main/buildings/" + allPoints[i].code +".html"
//                     );
//                 GEvent.addListener(allPoints[i].marker, "click", handler );
//                 GEvent.addListener(allPoints[i].marker, "mouseover", handler );
            }

            // if there's a marker attached to this object (either an old one, or we just created one)
            //  add it to the map now
            //  this might not run if we were unsuccessful in creating a new marker in the conditional block above 
            //  (i.e., if we have no coordinates in the Place object)
            if ( allPlaces[i].marker )
            {
                map.addOverlay( allPlaces[i].marker );
            }
        } else {
            softAlert(" didn't add " + allPlaces[i].name + " - type: " + allPlaces[i].type );
        }
    }
}

////////////////////////////////////////////////////////////////////////////////////////
//
//
function addBuildingMarker( )
{
    // want to extract the  marker and handler creation code from the function
    // "addMarkerGroup" and stick it here, so we can add a single building
    // marker at a time and still have the pop up text display correctly
    // 
    // maybe we need a separate addEventMarker as well ... 
    // which would call addBuildingMarker with extra text?
    // 
    // also be nice to have some way to add text to an existing marker .... hrmmmm
    // would need to know which markers were out there (either baesd on latlon
    // or based on the building name)
}


////////////////////////////////////////////////////////////////////////////////////////
//
//
function toggleMarkerGroup( checkBox )
{
//     alert ("checkbox checked is " + checkBox.checked ) ;
    if (checkBox.checked ) 
    {
        addMarkerGroup( checkBox.name );
    }
    else
    {
        removeMarkerGroup( checkBox.name );
    }

    return false;
    
}

////////////////////////////////////////////////////////////////////////////////////////
//
//
function clearAllMarkers()
{
    for (var i=0; i<allPlaces.length; i++)
    {
        if (allPlaces[i].marker)
        {
            map.removeOverlay( allPlaces[i].marker );
        }
    }
}
////////////////////////////////////////////////////////////////////////////////////////
//
//
function removeMarkerGroup( groupname )
{
    for (var i=0; i<allPlaces.length; i++)
    {
        if (allPlaces[i].type == groupname )
        {
            if ( allPlaces[i].marker )
            {
                map.removeOverlay( allPlaces[i].marker );
            }
        }
    }
//     for (var i=0; i<markerListing.length; i++)
//     {
//         if ( groupName == markerListing[i].group) {
//             map.removeOverlay( markerListing[i].marker );
//         }
//     }
}

// hides or shows the image overlay of the UT Campus map
function toggleCampusOverlay()
{
    if (campusMap.isHidden()  )
    {
        campusMap.show();
        $("#toggleOverlayLink").text("Hide campus map");
    } else {
        campusMap.hide(); 
        $("#toggleOverlayLink").text("Show campus map");
    }
    return false;
}
