$(document).ready(function() {
	// Add Markup for sub menu items
	$("li > ul", "#nav").wrap("<div class='sub-item'></div>");

	// Initialize the menu
	initMenu();

	$(".Calendar table.title", "#content").parents().filter("td").addClass("top-row");
	$(".Calendar .cal", "#content").attr("title", "");

	$(".Calendar td.day a", "#content").each(function() {
		$(this).attr("title", $(this).text()).qtip({
			content: $(this).attr("title"), show: { delay: 0 }, position: { corner: { tooltip: 'bottomMiddle', target: 'topMiddle' }, adjust: { x: 0, y: 0} }, style: { width: 240, border: { width: 6, radius: 5, color: "#4D7EA8" }, tip: { corner: 'bottomMiddle', color: '#4D7EA8'} }
		}).attr("title", "");
	});

	// $("a[href^='http://twitter.com']", "#content").twitterize("UDalumni");

	$(".accordion").accordion({ event: 'click', header: 'h3', autoheight: false });
});

// ****************************************************************************************
// Initializes the menu by hooking up mouseover/out evens and handling class name changes.
// ****************************************************************************************
function initMenu()
{
	var nav = document.getElementById("nav");
	if (nav)
	{
		var nodes = nav.getElementsByTagName("li");
		for (var i = 0; i < nodes.length; i++)
		{
			if (nodes[i].parentNode.id == "nav")
			{
				nodes[i].onmouseover = function () 
				{
					if (this.className.indexOf("hover") == -1)
					{
						this.className += " hover";
					}
				}
				nodes[i].onmouseout = function ()
				{
					this.className = this.className.replace("hover", "");
				}
			}
		}
	}
	var show = document.getElementById("show");
	if (show)
	{
		var items = show.getElementsByTagName("li");
		for (var i = 0; i < items.length; i++)
		{
			items[i].onmouseover = function () 
			{
				if (this.className.indexOf("hover") == -1)
				{
					this.className += " hover";
				}
			}
			items[i].onmouseout = function ()
			{
				this.className = this.className.replace("hover", "");
			}
		}
	}
}

// **************************************************************************************
// Adds points to the map
// **************************************************************************************
function addPoints() {
    if (GBrowserIsCompatible()) {
        var hPoints = new Hashtable();
        var bounds = new GLatLngBounds();

        // Add a marker for each point using the indicated lat/long/icon/etc...
        // Store the marker in a hashtable so we can get at it later
        $("ul.points li.point").hide().each(function() {
            var lat = $(this).find("ul li.lat").text();
            var lng = $(this).find("ul li.lng").text();
            var htmlContent = $(this).find("div.popup").html();
            var pinIcon = $(this).find("img.icon").attr("src");
            var point = new GLatLng(lat, lng);
            var marker = createMarker(point, pinIcon, htmlContent);
            hPoints.put($(this).attr("id"), marker);
            map.addOverlay(marker);
            bounds.extend(point);
        });

        map.setCenter(bounds.getCenter());
        map.setZoom(map.getBoundsZoomLevel(bounds));

        // Add a click event to each of the location points so the info window is displayed
        $("ul.pointslist li.location").each(function() {
            $(this).click(function() {
                var marker = hPoints.get($(this).attr("id").replace("loc_", "point_"));
                map.setCenter(marker.getLatLng(), 15);
                marker.openInfoWindowHtml($(this).find("div.popup").html());
            }).find("div.popup").hide();
        });
    }
}

// **************************************************************************************
// Creates a map marker object
// **************************************************************************************
function createMarker(point, pinIcon, htmlContent) {
    // Create a custom icon for this point using our icon class
    var customIcon = new GIcon(G_DEFAULT_ICON);
    if (pinIcon != null) {
        customIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        customIcon.image = pinIcon;
        customIcon.iconSize = new GSize(20, 34);
        //customIcon.shadowSize = new GSize(54, 30);
        //customIcon.iconAnchor = new GPoint(9, 34);
        //customIcon.infoWindowAnchor = new GPoint(16, 2)
        //customIcon.infoShadowAnchor = new GPoint(18, 25);
    }

    // Set up our GMarkerOptions object
    markerOptions = { icon: customIcon };
    var marker = new GMarker(point, markerOptions);

    // Hook up event for the info window
    GEvent.addListener(marker, "click", function() {
        try {
            // logAction("Click Map Icon", htmlContent.match(/<h4>(.+)<\/h4>/)[1], location.pathname, "Maps");
        }
        catch (err) {
        }
        marker.openInfoWindowHtml(htmlContent);
    });

    return marker;
}
