﻿$(document).ready(function() {
	//Accordions
	$("#accordion").accordion({ event: 'click', header: 'h3', autoHeight: false, active: false, collapsible: true });

	// Navigation highlights

	// Using the current path (e.g. /Section/Page)
	var path = location.pathname;

	// Handle links to the current path
	if (path) {
		// $('a[href$="' + path + '"]').addClass('active').parents().filter('a').addClass('active').end().parents().filter('ul').show().end().find("ul").show();
		$('a[href$="' + path + '"]').addClass('active').parents().filter('li').find('ul').show().end().find('a:first').addClass('active').parents().filter('ul').show();
	}

	if (typeof initLocations == 'function') {
		initLocations();
	}

	$(".location", "#locations").click(function() {
		$(".location", "#locations").removeClass("active");
		$(this).addClass("active");
	});

	if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
		// do nothing
	}
	else {
		$("ul li a[href^='http://www.docnet.']", "#content").each(function() {
			$(this).attr("rel", "doc-gallery");
			$(this).colorbox({
				iframe: true,
				href: $(this).attr("href"),
				title: $(this).text(),
				initialWidth: 100,
				initialHeight: 100,
				innerWidth: "790",
				height: "580",
				opacity: 0.5,
				current: "Physician {current} of {total}.",
				onCleanup: function() {
					// This just kills the contents before the box closes for a smoother appearance
					$("#cboxLoadedContent").html("");
				}
			});
		});

		$("ul li a[href*='/Team/']", "#content").each(function() {
			$(this).attr("rel", "doc-gallery");
			$(this).colorbox({
				href: $(this).attr("href") + " .profile",
				title: $(this).text(),
				initialWidth: 100,
				initialHeight: 100,
				innerWidth: "660",
				height: "580",
				opacity: 0.5,
				current: "Physician {current} of {total}.",
				onCleanup: function() {
					// This just kills the contents before the box closes for a smoother appearance
					$("#cboxLoadedContent").html("");
				}
			});
		});

		$("a[href*='youtube.']", "#content").each(function() {
			// Grab video ID from the url and the title from the H3 tag
			var videoID = $(this).attr('href').match(/watch\?v=(.+)+/)[1];
			var videoTitle = $(this).siblings("h3").text();

			// Move the link to before the title to it can float to the left properly
			$(this).siblings("h3").before($(this));

			// Add instructions
			$(this).siblings("p").after("<p class='watch'>Click to watch video</p>");

			// Add the thumbnail
			$(this).html("<img src='http://img.youtube.com/vi/" + videoID + "/default.jpg' />");

			// Set up ColorBox
			$(this).parents("li:first").colorbox({
				iframe: true,
				href: "http://www.youtube.com/v/" + videoID + "?autoplay=1&rel=0",
				title: videoTitle,
				initialWidth: 100,
				initialHeight: 100,
				width: "775",
				height: "580",
				opacity: 0.5,
				onCleanup: function() {
					// This just kills the contents before the box closes for a smoother appearance
					$("#cboxLoadedContent").html("");
				}
			});
		});

		$("a", "ul#stories").each(function() {
			$(this).hide().after("<p class='watch'>Click to read story</p>");
			
			var $li = $(this).parents("li:first");

			// Set up ColorBox
			$li.colorbox({
				href: $(this).attr("href") + " #copy",
				title: "",
				initialWidth: 100,
				initialHeight: 100,
				width: "775",
				height: "550",
				opacity: 0.9,
				onCleanup: function() {
					// This just kills the contents before the box closes for a smoother appearance
					$("#cboxLoadedContent").html("");
				}
			});
		});
	}
});

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 hte location points so the info window is displayed
        $("ul.locations li.location").each(function() {
            $(this).click(function() {
                var marker = hPoints.get($(this).attr("id").replace("loc_", "point_"));
                map.setCenter(marker.getLatLng(), 14);
                marker.openInfoWindowHtml($(this).find("div.popup").html());
            }).find("div.popup").hide();
        });
    }
}

// Creates a marker 
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(30, 30);
        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() {
        marker.openInfoWindowHtml(htmlContent);
    });
    
    return marker;
}


