	
$(document).ready(function(){
	
	// HEADER MAP TOGGLE
	var markersLoaded = false;
	$("#map-tab").toggle(
	  function () {
	  	
		if (!markersLoaded) {
			$("#map-markers").load("/cgi-bin/header_map.cgi", function(){
				markersLoaded = 1;
			});
		}
		
		$("#map-area").slideDown('slow');
		var theImg = $(this).find('img:first');
		theImg.attr( "src", "/images/header/maptab_arrowup.png" );
		
		_gaq.push(['_trackEvent','Interact with Map','Open']);
		
	  },
	  function () {
		$("#map-area").slideUp('slow');
		var theImg = $(this).find('img:first');
		theImg.attr( "src", "/images/header/maptab_arrowdown.png" );
	  }
	);
	
	// HEADER MAP MARKERS
	$("#header-map-container .marker").live('click', function(e){
		e.preventDefault;
		
		$(".map-tooltip").hide();
		
		$this = $(this);
		var $tip = $(this).next();

		$tip.css({
			left : $this.position().left - (($tip.width() / 2) - ($this.width() / 2))  + "px",
			top : $this.position().top + $this.height() + "px"
		})
		$tip.show();
		return false;
	});
	
	$(".map-tooltip").live('mouseleave', function(){
		$(this).hide();
	});
	
	
	// BACKGROUND RESIZE
	$('#bg').jmc_resizr({
		cropType : 'fill_outer',
		binding : {
			vertical : 'top',
			horizontal : 'center'
		}
	},
		function() {
			$('#bg').fadeIn('slow');
	});
	
	
	// JUMP SCROLL
	$('a[rel=jump]').bind('click', function(){
	
		$('html,body').animate({
				scrollTop: $( $(this).attr('href') ).offset().top
			},'slow');
		return false;
	});
	
	
	// PLACEHOLDERS
	$('[placeholder]').placeholderLabel();
	
	// turn off caching
	$.ajaxSetup( {cache : false} );
	
	
	// SAVE TO GOLF BAG
	$(".add-golf-bag").click(function(e){
		//e.preventDefault;
		
		$this = $(this);
		var url = $this.attr('href');
		
		$.get(
			url, 
			'',
		   	function(data){
				if (data < 0) {
					alert("There was an error saving to your golf bag.");
				} else {
					$this.replaceWith('<span class="listing-added">Added to <a href="/golf_bag/">Golf Bag</a></span>');
					$('#golf-bag-count').html(data);
				}

				// track it
				//var name = $this.parents('.listing').find(".listingName").html() + " (" +  listingId + ")";
				//trackEvent('Interact With Businesses', 'Add to Suitcase', name, null);
				
			});
		return false;
	});
	
	
	// REMOVE FROM GOLF BAG
    $(".remove-golf-bag").click(function(e){
		e.preventDefault();
		$this = $(this);
		var url = $this.attr('href');
				
		$.get(
			url, 
			'',
		   	function(data){
				if (data < 0) {
					alert("There was an error deleting from your golf bag");
				} else {

					var $listing = $this.closest(".golf-bag-listing");
					var $listingGroup = $listing.closest(".listing-group");
					$listing.slideUp('fast', function(){
						$listing.remove();
						if ($listingGroup.find(".golf-bag-listing").length < 1) {
							$listingGroup.slideUp('fast');
							$listingGroup.remove();
						}
						
						if ($(".listing-group").length < 1) {
							$("#golf-bag-listings").html('<h2>Your Golf Bag is Empty.</h2>');
						}
						
					});
										
					$('#golf-bag-count').html(data);
				}
							
			});
	});	
	
	
	// Add a tooltip for users who haven't used the golf bag yet.
	if (!readCookie('golf_bag_tooltip')) {
		$(".add-golf-bag:first").wrap('<span class="golfBagTooltipWrapper" />').after('<img src="/images/golf_bag_tooltip.png" width="240" height="92" alt="" class="golfBagTooltip">');
	
		$(".golfBagTooltip, .add-golf-bag").click(function(){
			$(".golfBagTooltip").remove();
			setTooltipCookie();
		});
	
	}
	
	
});





function setTooltipCookie() {
	var date = new Date();
	var days = 1000;
	date.setTime(date.getTime()+(days*24*60*60*1000));
	document.cookie =  'golf_bag_tooltip=hide; expires=' + date.toGMTString() + '; path=/';
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
