var map = '';
var stores = '';
var mapProcessing = false;
var homeIcon = '/assets/img/googleMap/Normal-Home-icon.png';
var home = '';
var showSearchResultLabel = true;

function panToStore(i) {
	for(x=0; x < stores.length; x++) stores[x].marker.infoWindow.close();
	var latlng = new google.maps.LatLng(stores[i].latitude_float, stores[i].longitude_float);
	map.panTo(latlng);
	stores[i].marker.infoWindow.open(map,stores[i].marker);
}

function setUpLocatorResults(params) {
	var i, index = 0, items = [], itemsTemp, prev, next, prevClone, nextClone;
	
	function update() {
		var i, s;
		for(i = 0; i < items.length; i++) {
			s = items[i].style;
			if ((i >= index) && (i < index + params.visible)) {
				s.position = 'relative';
				s.top = '0';
				s.left = '0';
				s.visibility = 'visible';
			} else {
				s.position = 'absolute';
				s.top = '-1000';
				s.left = '-1000';
				s.visibility = 'hidden';
			}
		}

		if (index === 0) {
			prev.style.visibility = 'hidden';
			prevClone.style.visibility = 'hidden';
		} else {
			prev.style.visibility = 'visible';
			prevClone.style.visibility = 'visible';
		}
		
		if (index + params.visible >= items.length) {
			next.style.visibility = 'hidden';
			nextClone.style.visibility = 'hidden';
		} else {
			next.style.visibility = 'visible';
			nextClone.style.visibility = 'visible';
		}
	}
	
	itemsTemp = document.getElementById(params.container).getElementsByTagName('li');
	for (i=0; i < itemsTemp.length; i++) items[i] = itemsTemp[i];
	
	prev = document.getElementById(params.prev);
	next = document.getElementById(params.next);
	prevClone = prev.cloneNode(true);
	nextClone = next.cloneNode(true);
	
	prev.getElementsByTagName('a')[0].onclick = prevClone.getElementsByTagName('a')[0].onclick = function() {
		index -= params.visible;
		update();
	};

	next.getElementsByTagName('a')[0].onclick = nextClone.getElementsByTagName('a')[0].onclick = function() {
		index += params.visible;
		update();
	};

	clone = document.getElementById(params.clone);
	clone.appendChild(prevClone);
	clone.appendChild(nextClone);

	update();
}

function initGoogleMap() {
	if(typeof(google) == 'undefined') return;
	var latlng = new google.maps.LatLng(37.0625,-95.677068); // default to center of US
	var zoom = 3; // default the zoom as well

	if(typeof(StoreLocator) == 'object') { var latlng = new google.maps.LatLng(StoreLocator.Latitude,StoreLocator.Longitude); var zoom = 12; }
	var myOptions = { zoom:zoom, center:latlng, mapTypeId:google.maps.MapTypeId.ROADMAP };
	map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
}
$(document).ready(function(){ initGoogleMap(); });

function locateStores_request(obj) {
	if(Broadway.processing) return;
	Broadway.setProcessing(this);

	if(mapProcessing) return;
	mapProcessing = true;

	var stores = 0;
	
	while((obj.tagName != 'FORM') && (obj.tagName != 'BODY')) obj = obj.parentNode;
	if((typeof(obj) == 'undefined') || (obj.tagName != 'FORM')) return(false);

	$('storeList').innerHTML = '';
	$('#store-locator-results-pager-top').html('');
	$('#store-locator-results-pager-bottom').html('');

//	if(home[0]) home[0].marker.setMap(null);
	for(var i=0; i < stores.length; i++) stores[i].marker.setMap(null);

	obj.zipcode.value = obj.zipcode.value.trim();
	obj.city.value = obj.city.value.trim();
	obj.state.value = obj.state.value.trim();
	if(!(obj.zipcode.value.length + obj.city.value.length)) {
		alert('Please choose a search type to continue...');
		mapProcessing = false;
		return;
	}
	if(obj.city.value.length && (obj.zipcode.value.length != 5)) obj.zipcode.value = '';
	if(obj.zipcode.value.length && (obj.zipcode.value.length != 5)) {
		alert('zipcode must be 5 digits...');
		mapProcessing = false;
		return;
	}
	
	var msg = '... searching ...<br /> <br />';
	msg += (obj.zipcode.value.length ? ('zipcode: ' + obj.zipcode.value) : (obj.city.value + ', ' + obj.state.value));
	msg += '<br />Radius: ' + obj.radius.value + 'mi.';
	$('#storeList ol').html(msg);

	http('POST','/com/Ajax/FormsAjax.cfc?method=getStoreLocation', locateStores_response, obj);
}

function locateStores_response(obj) {
	Broadway.clearProcessing();

	initGoogleMap();
	stores = ((obj.stores == 'undefined') || (typeof(obj.stores) == 'undefined')) ? 0 : obj.stores;
	home = obj.home;

	var bounds = new google.maps.LatLngBounds();
	var links = '';

	links += '<div id="store-locator-results-prev"><a href="javascript:;">&larr; prev</a></div>';
	links += '<div id="store-locator-results-next"><a href="javascript:;">next &rarr;</a></div>';
	$('#store-locator-results-pager-bottom').append(links);
	if(showSearchResultLabel) {
		$('#storeList').prepend('<h2>Search Results</h2>');
		showSearchResultLabel = false;
	}
	$('#storeList ol').html('');
	$('#storeList h5').html('');

//	home[0].icon = homeIcon;
//	home[0].LatLng = new google.maps.LatLng(home[0].latitude, home[0].longitude);
//	home[0].marker = new google.maps.Marker({ position:home[0].LatLng, map:map, icon:home[0].icon, zIndex:-1 });
//	bounds.extend(home[0].LatLng);

	for(var i=0; i < stores.length; i++) {
		var store = '<li><div id="store' + i + '">';
			store += '<div class="store-locator-results-address">';
			store += '<h3><a href="javascript:panToStore(' + i + ');">' + stores[i].entity_name + '</a></h3>';
			store += stores[i].address + '<br />';
			store += stores[i].city + ', ' + stores[i].state + ' &nbsp; ' + stores[i].zipcode + '<br />';
			store += stores[i].store_phone1;
			store += '</div>';
			store += '<div class="store-locator-results-links">';
			store += '<a href="javascript:panToStore(' + i + ');">show on map</a><br />';
//			store += '<a href="javascript:panToStore(' + i + ');">show on map</a> | <a href="">get directions</a><br />';
			store += '<a href="http://www.oreckstore.com/' + stores[i].vpath.toLowerCase() + '/" target="_blank">view store website</a> ~' + stores[i].dist.toFixed(1) + ' mi. </div>';
			store += '</div></li>';
		$('#storeList ol').append(store);

		stores[i].LatLng = new google.maps.LatLng(stores[i].latitude_float, stores[i].longitude_float);
		bounds.extend(stores[i].LatLng);
    stores[i].marker = new google.maps.Marker({ position:stores[i].LatLng, map:map, zIndex:i });
		stores[i].marker.infoWindow = new google.maps.InfoWindow({ content:document.getElementById('store'+i).innerHTML });
		google.maps.event.addListener(stores[i].marker, 'click', function() { for(i=0; i < stores.length; i++) stores[i].marker.infoWindow.close(); this.infoWindow.open(this.map,this); });
	}
	if(!stores.length) $('#storeList h5').html('[ no results ]');

	if(stores.length > 1) {
		map.fitBounds(bounds);
	} else if(stores.length == 1) {
		var latlng = new google.maps.LatLng(home[0].latitude, home[0].longitude);
		map.setZoom(10);
		map.panTo(latlng);
	} else {
		var latlng = new google.maps.LatLng(37.0625,-95.677068); // default to center of US
		map.setZoom(3);
		map.panTo(latlng);
	}

	setUpLocatorResults({
		container:'store-locator-results',
		prev:'store-locator-results-prev',
		next:'store-locator-results-next',
		clone:'store-locator-results-pager-top',
		visible:3
	});

	Broadway.processing = false;
	mapProcessing = false;
}