﻿
$(document).ready(function() {
	// where to buy search form
	$('form#aspnetForm').submit(function() {
		$zipInput = $('.zipSearch input[name="zip"]');
		var zip = $zipInput.val();
		if ( $zipInput.length > 0 && zip ) {
			$error = $('.zipSearch .error');
			$error.hide();
			if ( zip.match(/^\d{5}$/) ) {
				window.location.href = '/wheretobuy/Pages/Results.aspx?zip=' + zip;
			}
			else {
				$error.html('Please enter a five-digit zip code.');
				$error.fadeIn();
			}
			return false;
		}
	});
	
	// results map
	$map = $('#map');
	if ( $map.length > 0 ) {
		var map = new google.maps.Map2($map[0]);
		map.addControl(new GSmallMapControl());
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		
		var bounds = new GLatLngBounds();
		for ( i in markers ) {
			var m = markers[i];
			map.addOverlay(markers[i].marker);
			m.marker._offset = i;
			m.infoHtml = '<div style="color: #000; font-size: 14px;"><h2>' + m.name + '</h2>';
			m.infoHtml += '<div class="address">';
			m.infoHtml += m.address1 + '<br/>' + (m.address2 ? m.address2 + '<br/>' : '') + m.city + ', ' + m.state + ' ' + m.zip + '<br/>';
			m.infoHtml += '</div>';
			if ( m.url ) {
				m.infoHtml += '<div class="website"><a href="' + m.url + '" class="cta" target="_blank">Website</a></div>';
			}
			m.infoHtml += '</div>';
			GEvent.addListener(m.marker, 'click', function () {
				this.openInfoWindowHtml(markers[this._offset].infoHtml);
			});
			
			bounds.extend(m.marker.getPoint());
		}
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
		
		var offset = 0;		
		$('.searchResultsWindow a').each(function() {
			$(this).data('markerOffset', offset);
			$(this).attr('href', 'javascript:void(0);');
			$(this).click(function() {
				var m = markers[$(this).data('markerOffset')];
				if ( m ) {
					m.marker.openInfoWindowHtml(m.infoHtml);
				}
				return false;
			});
			++offset;
		});
	}
	
	/**
	 * Extra GA tracking
	 */
	$('.col1 .blackBox a').click(function() {
		try {
			pageTracker._trackPageview('/outbound/retailer/' + escape($(this).text()));
		}
		catch (err) {}
	});
});
