$(document).ready(function() {

	// Get rid of the border in the right-most link. Listen,
	// it's just easier than doing it programatically.
	$("#proAdLinks ul li:last").css({"border-right": "0"});
	$("#proAdAreasServed ul li:last").css({"border-right": "0"});

	// Floats
	//
	// When a header link is clicked:
	// 	- Close other open floats.
	// 	- Open the float that has a class that is the same as the id
	// 	  of the link.
	// 	- If the float for the link is already open then close it.
	$("#proAdLinks a, #proAdHeader .headerBox.map img").each(function() {
		$(this).click(function(e) {
			handleFloat(e, $(this).attr("id"));
		});
	});
	
	$(".listingReadReviews").click(function(e) {
		handleFloat(e, "proAdLinkReviews");
	});

	// Close Floats
	$(".proAdFloatContent").prepend('<div class="close"><a href="#close0">Close</a></div>');
	$(".proAdFloatContent .close").click(function(e) {
		$(this).parent().slideUp(500);
	});
	
	// Open a float by default if a user came to this page with the
	// appropriate url.
	var anchorNav = anchorNavigation();
	if ( anchorNav.navLink ) {
		handleFloat(undefined, anchorNav.navLink);
		//location.href = "#" + anchorNav.navLink;
	}


	// Additional Locations
	//
	// Open the Directions float and change the iframe src to that
	// of the link.
	$(".additionalLocation").click(function(e) {
		e.preventDefault();
		setDirections($(this).attr("href"), $(this).attr("name"));
		$(".proAdLinkMapDirections").slideDown(500);
	});
	
	setDirections($(".additionalLocation:first").attr("href"), $(".additionalLocation:first").attr("name"));
	
	// Click To Call
	$(".clickToCall a").each(function() {
		$(this).click(function(e) {
			e.preventDefault();
			window.open(
				$(this).attr("href"),
				"clickToCallWindow",
				"status=1, width=375, height=580"
			);
		});
	});
	
	// Print Directions
	$("#printDirections").click(function(e) {
		e.preventDefault();
		//$("#directionsIFrame").get(0).print();
		$("#directionsIFrame").focus();
		window.directionsIFrame.print();
	});
	
	// This keeps ie7 from doing funny things with the floats.
	// I know it's weird.
	$(".fixIE7Floats").each(function() {
		$(this).css({height: "1px"});
		$(this).show(
			1,
			function() {
				$(this).hide();
			}
		);
	});

});

$(window).load(function() {

	// Resize logo
	var $logo = $("#listingInformation > .logo > img");
	var maxWidth = 250;
	var maxHeight = 100;

	var logoWidthOverRatio = $logo.width() / maxWidth;
	var logoHeightOverRatio = $logo.height() / maxHeight;

	if (logoWidthOverRatio > 1 || logoHeightOverRatio > 1) {
		if (logoWidthOverRatio > logoHeightOverRatio) {
			$logo.width(maxWidth);
		} else {
			$logo.height(maxHeight);
		}
	}
});

function setDirections(href, identifiedBy) {
	$(".proAdLinkMapDirections iframe").attr("src", href);
	$(".proAdDirectionsListing").find(".mapMarker img").hide();
	$(".proAdDirectionsListing").removeClass("selected");
	
	var directionsLink = $(".proAdDirectionsListing a[name=" + identifiedBy+ "]");
	if (directionsLink.length > 0) {
		directionsLink.parent().parent().addClass("selected");
		directionsLink.parent().parent().find(".mapMarker img").show();
	}
}

function handleFloat(e, floatClass) {
	//e.preventDefault();

	if ( $(".proAdFloatContent").is(":visible") ) {
		if ( $(".proAdFloatContent." + floatClass).is(":visible") ) {
			$(".proAdFloatContent." + floatClass).slideUp(500);
		} else {
			$(".proAdFloatContent").slideUp(500);
			$(".proAdFloatContent." + floatClass).slideDown(500);
		}
	} else {
		$(".proAdFloatContent." + floatClass).slideDown(500);
	}
}
