﻿var loading = '<div id="formBuilderWrapperLoading"><img src="/assets/images/loading.gif"></div>';

$(document).ready(function(){
	$("#StateCode").change(function() {
		citySelector();
	});
	$("#City").change(function() {
		submitLocation='/components/storelocator/index.cfm?step=storeList';
		formLabelsAndValues=$("form#SelectByCity").serialize();
		$("#StoreList").html(loading);
		$.post(submitLocation,formLabelsAndValues, function(response) {
			storeList(response);
		});
	});	
	$("form#SelectByZipCode").submit(function(){
		var submitLocation='/components/storelocator/index.cfm?step=storeList';
		formLabelsAndValues=$("form#SelectByZipCode").serialize();
		$("#StoreList").html(loading);
		$.post(submitLocation,formLabelsAndValues, function(response) {
			storeList(response);
		});
		return false;
	});
});

function citySelector() {
	//ShowRentalTypes//
	
	$.getJSON("/components/storelocator/index.cfm?step=citySelector",{stateCode: $("#StateCode").val()}, function(j){
		var options = '';
		for (var i = 0; i < j.length; i++) {
			options += '<option value="' + j[i].optionValue + '" ' + j[i].optionSelected + ' >' + j[i].optionDisplay + '</option>';
		}
		$("#City").html(options);
	});
}


function storeList(response) {
	if($("status",response).text() == "2") return;
	storeresults=$(response).find(".lisquery");
	$("#StoreList").html(storeresults);
	GUnload();
	Json=eval($(response).find("#MapJson").html());
	initializeMap(Json[0].lat,Json[0].long,Json);	
}

function initializeMap(lat,long,mapPoints) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_canvas"));
		if (lat == 0) {
			map.setCenter(new GLatLng(lat,long), 1);
			map.setUIToDefault();
			map.addControl(new GLargeMapControl());
		} else {
			map.setCenter(new GLatLng(lat,long), 12);
			map.setUIToDefault();
			map.addControl(new GLargeMapControl());
			for (var i = 0; i < mapPoints.length; i++) {
				point = new GLatLng(mapPoints[i].lat,mapPoints[i].long);
				html=mapPoints[i].html;
				map.addOverlay(createMarker(point, html));
			}
		}
	}
}

function createMarker(point, htmlString) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(htmlString);
	});
	return marker;
}