$(function(){
	var loadingDiv = '<div><img src="/common_images/parks-ajax-loader.gif" alt="Please wait"/> Loading</div>'
	tooltip();
    $("#find_park_zip").autocomplete("/web/park_search/controller/ParkFacilitySearchAjax.php", {
        delay: 10,
        minChars: 1,
        matchSubset: 1,
        matchContains: 0,
        maxItemsToShow: 10,
        cacheLength: 10,
        autoFill: true,
        extraParams: {
            cmd: 'zip'
        }
    });
    
    
	
    function addFacilityIconHover(){
	  $('li:not(.icon_highlight) .facility_icon').hover(
	      function () {
	            $(this).animate({
	                borderTopColor: "#8E5823",
	                borderRightColor: "#8E5823",
	                borderBottomColor: "#8E5823",
	                borderLeftColor: "#8E5823"
	            }, 100)
	        }, 
	      function () {
	            $(this).animate({
	                borderTopColor: "#FFFFFF",
	                borderRightColor: "#FFFFFF",
	                borderBottomColor: "#FFFFFF",
	                borderLeftColor: "#FFFFFF"
	            }, 100)
	      }
    	);
	}
	
	addFacilityIconHover();

	if($('#find_park_name').val())
		$('.search_park_item h3').highlight($('#find_park_name').val());
   
    
    $('#find_park_zip').focus(function(){
        if ($(this).val() == 'Citywide') 
            $(this).val('');
    });
    $('#find_park_zip').blur(function(){
        if ($(this).val() == '') 
            $(this).val('Citywide');
    });
    
    if($('#find_park_name').val()||$('#find_park_zip').val()||$('#parksearch_bottom input:checked').length) {
		window.location = "#form_top";
	}
	$('.rounded_corners').corner();
	$('.rc_bottom').corner("bottom");
	$('.rc_top').corner("top");
    var citywide = 'Citywide';
	
    $('.facilities_check:checked').parent().css({backgroundColor: "#C3DBA1"});
	$('.facilities_check:checked').siblings("label").children("img").css({
        borderColor: "#FFFFFF"
    });
	$('.facilities_check').change(function(){
        if ($(this).attr("checked") == true) {
            $(this).parent().animate({
                backgroundColor: "#C3DBA1"
            }, 300)
			$(this).siblings("label").children("img").animate({
                borderTopColor: "#FFFFFF",
                borderRightColor: "#FFFFFF",
                borderBottomColor: "#FFFFFF",
                borderLeftColor: "#FFFFFF"
            }, 300)
        }
        else {
            $(this).parent().animate({
                backgroundColor: "#F5FBEF"
            }, 250)
			$(this).siblings("label").children("img").animate({
                borderTopColor: "#D0E3BF",
                borderRightColor: "#D0E3BF",
                borderBottomColor: "#D0E3BF",
                borderLeftColor: "#D0E3BF"
            }, 300)
        }
    });
	
	var t;
	function tooltip(){	
	$(".tooltipme").unbind();
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$(".tooltipme").hover(
	function(e){											  
		this.t = $(this).attr('title');
		$(this).removeAttr('title');									  
		$("body").append("<div id='tooltip'><div class='bubble'><div><p>"+ this.t +"</p></div></div></div>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		$(this).attr('title',this.t);	
		$("#tooltip").remove();
    });	
	
	$(".tooltipme").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};
});

/*

highlight v3

Highlights arbitrary terms.

<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>

MIT license.

Johann Burkard
<http://johannburkard.de>
<mailto:jb@eaio.com>

*/

jQuery.fn.highlight = function(pat) {
 function innerHighlight(node, pat) {
  var skip = 0;
  if (node.nodeType == 3) {
   var pos = node.data.toUpperCase().indexOf(pat);
   if (pos >= 0) {
    var spannode = document.createElement('span');
    spannode.className = 'highlight';
    var middlebit = node.splitText(pos);
    var endbit = middlebit.splitText(pat.length);
    var middleclone = middlebit.cloneNode(true);
    spannode.appendChild(middleclone);
    middlebit.parentNode.replaceChild(spannode, middlebit);
    skip = 1;
   }
  }
  else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
   for (var i = 0; i < node.childNodes.length; ++i) {
    i += innerHighlight(node.childNodes[i], pat);
   }
  }
  return skip;
 }
 return this.each(function() {
  innerHighlight(this, pat.toUpperCase());
 });
};

jQuery.fn.removeHighlight = function() {
 return this.find("span.highlight").each(function() {
  this.parentNode.firstChild.nodeName;
  with (this.parentNode) {
   replaceChild(this.firstChild, this);
   normalize();
  }
 }).end();
};


