$(document).ready(function() {
	
	Cufon.replace('h1, h2, h3');
	
	//Auto Clear and Replase if blank.
	$('input:text, textarea').each(function() {
		$(this).focus(function() { cur = $(this).attr('value');$(this).val(''); });
		$(this).blur(function() {
			if ( $.trim($(this).val()) == '' ) { $(this).attr('value', cur); }
		})
	});
	
	
	$('#findLocations').submit(function() {
		if( is_numeric($('#zip').val()) == false ){
			alert('you must enter a zip code!');			
		} else {
			_gaq.push(['_trackEvent', 'Shop Locator', 'Clicked Go', $('#zip').val()]);

			$("#popover").data('overlay').load();
			$("#popoverContent").html("Loading...");
			$.get('locate.php?zip='+$('#zip').val(), function(data) {			
				if( (data * 1) == 0) { false_data(); }
				else { showPopover(data); }
			});
		}
		return false;
	});

	
	$("#popover").overlay({
		// custom top position
		top: 260,
		// some mask tweaks suitable for facebox-looking dialogs
		mask: {
			// you might also consider a "transparent" color for the mask
			color: '#000',
			// load mask a little faster
			loadSpeed: 200,
			// very transparent
			opacity: 0.5
		},
		// disable this for modal dialog-type of overlays
		closeOnClick: false
	});

	
	
	$('a[href^=http://]:not([href^=http://' + location.host + '])').live(
	   'click',
	   function() {
	       window.open($(this).attr('href'), $(this).text());
	       return false;
	   }
	);
	
});


function locationURL(url, title){
	_gaq.push(['_trackEvent', 'Shop Locator', 'Clicked Location', title]);
	window.open(url,'_blank');
}

function clickedMap(url, title){
	_gaq.push(['_trackEvent', 'Shop Locator', 'Clicked Map Link', title]);
	window.open(url,'_blank');
}

function closePopover(){
	$("#popover").data("overlay").close();

}

function showPopover(html){
	$("#popover").html(html);
}


function is_numeric(input) {
   return (input - 0) == input && input.length > 0;
}

function false_data() {
	var data = '<p>Your Search produced no results!</p>';
	fade_in(data)
}


