/* Road Trip */

var magIcon = new GIcon();
magIcon.image = "../images/magglass.png";
magIcon.iconSize = new GSize(24,50);
magIcon.iconAnchor = new GPoint(12,12);

var radius = 10;
var mile = 1609.344;

jmmap.roadtrip = {
	searchRadius: mile*(radius*1.5),
	routeLine: null,
	routePoints: [],
	searchAni: null,
	dirListener: null,
	show: function(from,to) {
		if (from != '' && to != '') {
			// set loading icon
			jmmap.showMessage('Calculating Route... <button onclick="stopSearch=true;">Stop</button>','loading');
			// clear list
			$('#locations-list').children().remove();
			// clear map
			jmmap.map.clearOverlays();
			// reset marker list
			jmmap.markers = [];
			jmmap.roadtrip.routePoints = [];
			// reset bounds
			jmmap.bounds = new GLatLngBounds();
			// turn off auto-refresh
			jmmap.autoRefresh = false;
			// load directions
			this.dirListener = GEvent.addListener(jmmap.directions.obj, 'load', function() {
				jmmap.roadtrip.routeLine = jmmap.directions.obj.getPolyline();
				jmmap.roadtrip.searchAni = new GMarker(jmmap.roadtrip.routeLine.GetPointAtDistance(0), magIcon);
				jmmap.map.addOverlay(jmmap.roadtrip.searchAni);
			
				stopSearch = false;
				jmmap.showMessage('Scanning your route... <button onclick="stopSearch=true;">Stop</button>','loading');
				setTimeout("jmmap.roadtrip.getPoints(0)",0);
			});
			// insert directions
			jmmap.directions.obj.load("from: " + from + " to: " + to);
		}
	},
	getPoints: function(i) {
		if (i<jmmap.roadtrip.routeLine.Distance()) {
			if (stopSearch == false) {
				lp = jmmap.roadtrip.routeLine.GetPointAtDistance(i);
				jmmap.roadtrip.searchAni.setPoint(lp);
				jmmap.roadtrip.routePoints.push(lp);
				i += jmmap.roadtrip.searchRadius;
				setTimeout("jmmap.roadtrip.getPoints(" + i + ")", + 0);
 				GEvent.removeListener(this.dirListener);
			} else {
				jmmap.map.removeOverlay(jmmap.roadtrip.searchAni);
				// unset loading icon
				jmmap.showMessage("Your search was stopped.",'done');
			}
		} else {
			jmmap.showMessage('Getting Stores...');
			jmmap.map.removeOverlay(jmmap.roadtrip.searchAni);
			$.ajax({
				url: '/locations/road-trip-store-finder.php',
				type: 'get',
				data: 'points=' + jmmap.roadtrip.routePoints,
				dataType: 'json',
				success: function(storelist){
					GEvent.removeListener(jmmap.roadtrip.dirListener);
					// loop thorugh stores
					for (var i=0;i<storelist.length;i++) {
						jmmap.createMarker(storelist[i],null,i);
					}
					// update store list
					jmmap.storeListFromBounds();
		     	}
		     });
		}
	}
};

if (typeof(is_mobile) == 'undefined') {
	$(document).ready(function () {
		tabber.setup();
		// detect gmaps compatible browser
		if (GBrowserIsCompatible()) {
			$('#content').addClass('active');
			jmmap.setup('map');
			jmmap.directions.setup('directions');
			if (!window.location.search) jmmap.showByState();
			// city state search
			$('form.city-state').submit(function(){
				if (this.state.value == '' && this.city.value == '') {
					jmmap.showMessage('Please enter a city, state or both.','alert');
				} else {
					jmmap.directions.obj.clear();
					jmmap.showStoresInCityState(this.city.value,this.state.value);
				}
				return false;
			});
			// remove radius
			$('#radius').hide();
			$('label[for=radius]').hide();
			// ajax zip lookup
			$('form.zip').submit(function(){
				jmmap.directions.obj.clear();
				if (this.zip.value.length == 5) {
					if (!jmmap.map) {
						jmmap.setup('map'); // (div)
					}
					jmmap.showStoresInZip(this.zip.value,true); // zip code, pickClosest
				} else {
					jmmap.showMessage('Please enter a 5 digit Zip Code','alert');
				}
				return false;
			});
			// road trip
			$('form.road-trip').submit(function(){
				if (this.start.value == '' || this.end.value == '') {
					jmmap.showMessage('Please enter a starting and ending address.','alert');
				} else {
					jmmap.directions.obj.clear();
					jmmap.roadtrip.show(this.start.value,this.end.value);
				}
				return false;
			});
			// handle querystring
			var q = window.location.search.substring(1);
			if (q) {
				jmmap.showStoresFromList();
			}
			if (q.indexOf('start') > -1) {
				tabber.tabs.eq(2).trigger('click');
				$('form.road-trip').submit();
			} else if (q.indexOf('city') > -1 || q.indexOf('state') > -1) {
				tabber.tabs.eq(1).trigger('click');
			} else if (q.indexOf('zip') > -1) {
				tabber.tabs.eq(0).trigger('click');
				// get zip lat lng for search radius
				zip = numOnly($('#zip').val());
				$.ajax({
					url: '/locations/zip-finder.php',
					type: 'get',
					data: 'zip='+zip,
					dataType: 'json',
					success: function(statelist) {
						if (statelist.length == 0) {
							// can't find zip
							jmmap.showMessage("Couldn't find that zip code, please enter a valid zip code.",'alert');
						} else {
							point = new GLatLng(statelist[0].lat, statelist[0].lng);
							jmmap.searchZip = {
								'code': zip,
								'point': point
							};
						}
					}
				})
/*
				jmmap.geocoder.getLatLng(zip,function(point) {
					if (point) {
						jmmap.searchZip = {
							'code': zip,
							'point': point
						};
					}
				});
*/
			} else if (q.indexOf('coop') >-1) {
				// turn off auto-refresh
				jmmap.autoRefresh = false;
			}
			window.onunload = GUnload;
		}
	});
}
