	//<![CDATA[
	
	$(document).ready(function() {
	
		$('#storeSelect').change(function() {
			focusStore($(this).val());	
		});
	
	});
		
		
	var map;
	var geocoder;
	
	// home icon
    var iconHome = new GIcon(G_DEFAULT_ICON);
    iconHome.image = sl_map_home_icon;
    if (sl_map_home_icon.indexOf('flag') != '-1') {
        iconHome.shadow = add_base + "/icons/flag_shadow.png";
    }
    else 
        if (sl_map_home_icon.indexOf('arrow') != '-1') {
            iconHome.shadow = add_base + "/icons/arrow_shadow.png";
        }
        else {
            iconHome.shadow = add_base + "/icons/shadow.png";
        }
    iconHome.iconSize = new GSize(40, 40);
    iconHome.iconAnchor = new GPoint(28, 28);
    iconHome.infoWindowAnchor = new GPoint(21, 8);
	iconHome.imageMap = [6,3,33,3,33,39,6,39];
	
	
	// coffee icon
	var iconCoffee = new GIcon(G_DEFAULT_ICON);
	iconCoffee.image = sl_map_end_icon;
	if (sl_map_end_icon.indexOf('flag') != '-1') {
	    iconCoffee.shadow = add_base + "/icons/flag_shadow.png";
	}
	else if (sl_map_end_icon.indexOf('arrow') != '-1') {
	    iconCoffee.shadow = add_base + "/icons/arrow_shadow.png";
	}
	else if (sl_map_end_icon.indexOf('bubble') != '-1') {
	    iconCoffee.shadow = add_base + "/icons/bubble_shadow.png";
	}
	else if (sl_map_end_icon.indexOf('marker') != '-1') {
	    iconCoffee.shadow = add_base + "/icons/marker_shadow.png";
	}
	else {
	    iconCoffee.shadow = add_base + "/icons/shadow.png";
	}
				
	iconCoffee.iconSize = new GSize(40, 40);
	iconCoffee.iconAnchor = new GPoint(28, 28);
	iconCoffee.infoWindowAnchor = new GPoint(21, 8);	
	iconCoffee.imageMap = [0,0,39,0,39,39,0,39];
	
	
	// instore icon
	var iconDoughnut = new GIcon(G_DEFAULT_ICON);
	iconDoughnut.image = add_base + "/icons/donuticon.png";
	//iconDoughnut.shadow = add_base + "/icons/shadow.png";
	iconDoughnut.iconSize = new GSize(40, 40);
	iconDoughnut.iconAnchor = new GPoint(28, 28);
	iconDoughnut.infoWindowAnchor = new GPoint(21, 8);
	iconDoughnut.imageMap = [3,5,39,5,39,36,3,36];
	
	
	// hotlight icon
	var iconHot = new GIcon(G_DEFAULT_ICON);
	iconHot.image = add_base + "/icons/hoticon.png";
	//iconHot.shadow = add_base + "/icons/shadow.png";
	iconHot.iconSize = new GSize(39, 40);
	iconHot.iconAnchor = new GPoint(28, 28);
	iconHot.infoWindowAnchor = new GPoint(21, 8);
	iconHot.imageMap = [2,5,39,5,39,36,2,36];
	

	function focusStore(storeid) {
		
		if (storeid != '' && storeid != '-Choose Store-') {
	        var searchUrl = add_base + '/store-xml.php?storeid=' + storeid;
			GDownloadUrl(searchUrl, function(data){
				var xml = GXml.parse(data);
				var markers = xml.documentElement.getElementsByTagName('marker');
				 for (var i = 0; i < markers.length; i++) {
					address = markers[i].getAttribute('postcode');
				 }
				
				geocoder.getLatLng(address, function(latlng){
					if (!latlng) {
						alert(address + ' not found');
					}
					else {
						searchLocationsNear(latlng, address, storeid);
					}
				});
	        });
	    }
	}	
	

	function focusStoreFromName(storename) {
		
		if (storename != '' && storename != '-Choose Store-') {
	        var searchUrl = add_base + '/store-xml.php?storename=' + storename;
			GDownloadUrl(searchUrl, function(data){
				var xml = GXml.parse(data);
				var markers = xml.documentElement.getElementsByTagName('marker');
				 for (var i = 0; i < markers.length; i++) {
					storeid = markers[i].getAttribute('id');
					address = markers[i].getAttribute('postcode');
				 }
				
				geocoder.getLatLng(address, function(latlng){
					if (!latlng) {
						//alert(address + ' not found');
					}
					else {
						searchLocationsNear(latlng, address, storeid);
					}
				});
	        });
	    }
	}	
	
	function load(){
	    if (GBrowserIsCompatible()) {
	        geocoder = new GClientGeocoder();
	        map = new GMap2(document.getElementById('map'));
	        map.addControl(new GSmallMapControl());
	        //map.addControl(new GSmallZoomControl());
	        //map.addControl(new GOverviewMapControl());
	        //map.addControl(new GLargeMapControl); //11/29/08 1:19am Moyo
	        //map.addControl(new GMapTypeControl());
	        map.addControl(new GMapTypeControl());
	        map.addMapType(G_PHYSICAL_MAP);
	        geocoder.getLatLng(sl_google_map_country, function(latlng){
	            map.setCenter(latlng, sl_zoom_level, sl_map_type);
	        });
			GEvent.addListener(map, "load", searchLocations);
	    }
	    
//	    searchLocations();
	    
	}
	
	function searchLocations(){
	    var address = document.getElementById('addressInput').value + ", UK";
	    if (address != '' && address != 'Enter your postcode') {
	        geocoder.getLatLng(address, function(latlng){
	            if (!latlng) {
	                alert(address + ' not found');
	            }
	            else {
	                searchLocationsNear(latlng, address, '');
	            }
	        });
	    }
		if ($('#passedval').val() != ''){
			focusStoreFromName($('#passedval').val());	
			$('#passedval').val("");
		}
	}
	
	function searchLocationsNear(center, homeAddress, storeid){ // homeAddress param added by Moyo 5/23/08
	    var radius = document.getElementById('radiusSelect').value;
	    var searchUrl = add_base + '/generate-xml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius + '&zip=' + homeAddress;
	    GDownloadUrl(searchUrl, function(data){
	        var xml = GXml.parse(data);
	        var markers = xml.documentElement.getElementsByTagName('marker');
	        map.clearOverlays();
	        
	        var bounds = new GLatLngBounds(); //added here 1/25/09 by Moyo to handle extending bounds to show searched location
	        markerOpts = {
	            icon: iconHome
	        };
	        point = new GLatLng(center.lat(), center.lng());
	        bounds.extend(point); //added 1/25/09 to handle showing searched location within bounds everytime
	        var homeMarker = new GMarker(point, markerOpts);
	        var html = '<b>Your Location:</b> <br/>' + homeAddress;
	        GEvent.addListener(homeMarker, 'click', function(){
	            homeMarker.openInfoWindowHtml(html);
	        });
	        
			if (storeid != 'undefined' && storeid != ''){
				// no home icon for store specific searches
			} else {
				map.addOverlay(homeMarker);
			}
	        //end marker for searched location
	        
	        //var sidebar = document.getElementById('map_sidebar');
	        var sidebar = $('#map_sidebar');
	        //sidebar.innerHTML = '';
	        sidebar.text('');
	        if (markers.length == 0) {
	            sidebar.text('No results found.');
	            geocoder = new GClientGeocoder();
	            geocoder.getLatLng(sl_google_map_country, function(latlng){
	                map.setCenter(point, sl_zoom_level);
	            });
	            return;
	        }
			
	        var set = '0';
	        var myElement = '';
			var rides = new Array();
	        //var bounds = new GLatLngBounds(); //removed from here 1/25/09 to handle showing searched location with bounds at all times
	        for (var i = 0; i < markers.length; i++) {
				
				var currentpostcode = "  "+$("#storeSelect").val();
				var state_zip = markers[i].getAttribute('address').split(',')[1];
				if (markers[i].getAttribute('address').split(',')[2] != '' && markers[i].getAttribute('address').split(',')[2]){
					var state_zip = markers[i].getAttribute('address').split(',')[2];
				}
				
				if (markers[i].getAttribute('address').split(',')[3] != '' && markers[i].getAttribute('address').split(',')[3]){
					var state_zip = markers[i].getAttribute('address').split(',')[3];
				}
				//alert("|"+state_zip+"="+currentpostcode+"|");
				if (state_zip == currentpostcode || parseFloat(markers[i].getAttribute('distance')) == '0'){
				} else {
					rides[i] = new GDirections();
		        	rides[i].my_i = i;
			        GEvent.addListener(rides[i], "load", function (e){
			            var route = this.getRoute(0);
						var routeDistance = Math.round(((route.getDistance().meters)/1000)*0.6214*10)/10;
						//alert(".routeDistance"+this.my_i+" "+routeDistance+" miles");
						$(".routeDistance"+this.my_i).text(routeDistance);
						}
			        ); 
					dirCommand = "from: Home @" + center.lat() + ", " + center.lng() + " to: location @" + markers[i].getAttribute('lat') + ", " + markers[i].getAttribute('lng');
					rides[i].load(dirCommand, {getPolyline:true, getSteps:true});	
				}

	            var change = '0';
	            var name = markers[i].getAttribute('name');
	            var address = markers[i].getAttribute('address');
	            var distance = parseFloat(markers[i].getAttribute('distance'));
	            var roaddistance = parseFloat(markers[i].getAttribute('distance'));
	            var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')), parseFloat(markers[i].getAttribute('lng')));
	            var description = markers[i].getAttribute('description');
	            var url = markers[i].getAttribute('url');
	            var hours = markers[i].getAttribute('hours');
	            var phone = markers[i].getAttribute('phone');
	            var image = markers[i].getAttribute('image');
	            var instore = markers[i].getAttribute('instore');
	            var hotlight = markers[i].getAttribute('hotlight');
	            var postcode = markers[i].getAttribute('postcode');
	            if (set != '1') {
	                if (instore == '1') {
	                    change = '1';
	                    set = '1';
	                }
	            }
	            var divadd = '';
	            var marker = createMarker(point, name, address, homeAddress, description, url, hours, phone, image, instore, hotlight, postcode); 
	            map.addOverlay(marker);
	            
				if (storeid != 'undefined' && storeid != ''){
					if (markers[i].getAttribute('id') == storeid){
						GEvent.trigger(marker, 'click');
					}
				} 
				/*else if (markers[i].getAttribute('postcode') == homeAddress) {
		   	
		   			// show the info window
					GEvent.trigger(marker, 'click');
					
				}*/
				
	            if (i == '0') {
	                //myElement = document.createElement('div');
	            	myElement = $('<div class="bottom_first"></div><div id="store-legend-wrapper"><div class="store-legend-header">Legend</div><div class="store-legend-content"><ul><li class="first"><img src="/wp-content/themes/thesis/images/hotlight-header.gif" alt="Hotlight Stores" title="Hotlight Stores" /><h3 class="hotlight">Hotlight Stores</h3>Our hotlight icon signifies stores where we are making doughnuts fresh daily.</li><li class="mid"><img src="/wp-content/themes/thesis/images/coffee-header.gif" alt="Coffee Bars &amp; Kiosks" title="Coffee Bars &amp; Kiosks" /><h3 class="coffee">Coffee Bars &amp; Kiosks</h3>Our coffee bars and kiosks are located in central, commuter areas and shopping centres and all receive daily deliveries of fresh doughnuts from our hotlight stores.</li><li class="last"><img src="/wp-content/themes/thesis/images/donut-header.gif" alt="Tesco In-Store Cabinets" title="Tesco In-Store Cabinets" /><h3 class="donut">Tesco In-Store Cabinets</h3>We deliver fresh doughnuts daily from our Hotlight stores to our In-Store Cabinets at Tesco and Moto</li></ul></div></div><br>');
	                sidebar.append(myElement);
	            }
	            
	            var sidebarEntry = createSidebarEntry(marker, name, address, distance, homeAddress, url, instore, hotlight,i); // homeAddress param added by Moyo 5/23/08
	            //var contenttest = "<div>test</div>";
				//var contentobject = {item1:contenttest};
				sidebar.append(sidebarEntry);
				//contentobject.appendTo(sidebar);
	            bounds.extend(point);
	        }
	        map.setCenter(bounds.getCenter(), (map.getBoundsZoomLevel(bounds))); //8/28/08: -1 to zoom out one step // ignored due to wanting to be closer to the map
	        

	        // set wrapper over divs
	        $(".store").wrapAll("<div id='nearest-stores'></div>");
	        $(".instore").wrapAll("<div id='nearest-cabs'></div>");
	    });

	}
	
	function createMarker(point, name, address, homeAddress, description, url, hours, phone, image, instore, hotlight, postcode){ // homeAddress param added by Moyo 5/23/08
	    
		markerOpts = {
	        icon: iconCoffee
	    };
	    if (instore == '1') {
	        markerOpts = {
	            icon: iconDoughnut
	        };
	    }
	    if (hotlight == '1') {
	        markerOpts = {
	            icon: iconHot
	        };
	    }
	    
	    var marker = new GMarker(point, markerOpts);
	    
	    var more_html = "";
	    if (url.indexOf("http://") != -1 && url.indexOf(".") != -1) {
	        more_html += "| <a href='" + url + "' target='_blank' class='storelocatorlink'><nobr>" + sl_website_label + "</nobr></a>"
	    } else {url = "" }
	    if (image.indexOf(".") != -1) {
	        more_html += "<br/><img src='" + image + "' style='width:250px; max-height:200px border:none' >"
	    } else { image = "" }
	    if (description != "") {
	        more_html += "<br/>" + description + "";
	    } else { description = "" }
	    if (hours != "") {
	        more_html += "<br/><b>Hours:</b> " + hours;
	    } else { hours = "" }
	    if (phone != "") {
	        more_html += "<br/><b>Phone:</b> " + phone;
	    } else { phone = ""}
	    var street = address.split(',')[0];
	    if (street.split(' ').join('') != "") {
	        street += '<br/>';
	    }else {street = "";}
	    var city = address.split(',')[1];
	    if (city.split(' ').join('') != "") {
	        city += ', ';
	    }else {city = "";}
	    var state_zip = address.split(',')[2];
	    //address=street + city + state_zip;
	    
	    if (homeAddress.split(" ").join("") != "") {
	        var html = '<div id="sl_info_bubble"><!--tr><td--><strong>' + name + '</strong><br>' + street + city + state_zip + '<br/>' + postcode + '<br/> <a href="http://' + sl_google_map_domain + '/maps?q=' + homeAddress + ' to ' + address + '" target="_blank" class="storelocatorlink">Directions</a> ' + more_html + '<br/><!--/td></tr--></div>'; // Get Directions link added by Moyo 5/23/08
	    }
	    else {
	        var html = '<div id="sl_info_bubble"><!--tr><td--><strong>' + name + '</strong><br>' + street + city + state_zip + '<br/>' + postcode + '<br/> <a href="http://' + sl_google_map_domain + '/maps?q=' + address + '" target="_blank" class="storelocatorlink">Map</a> ' + more_html + '<!--/td></tr--></div>';
	    }
	    GEvent.addListener(marker, 'click', function(){
	        marker.openInfoWindowHtml(html);
	        //t=GMap2.getInfoWindow();
	        //t.reset(size:400);
	    });
	    return marker;
	}
	
	var resultsDisplayed = 0;
	var bgcol = "white";
	
	function createSidebarEntry(marker, name, address, distance, homeAddress, url, instore, hotlight, i){ // homeAddress param added by Moyo 5/23/08
	    document.getElementById('map_sidebar_td').style.display = 'block';
	    image_left = add_base + "/icons/storefinder_icon_factory.gif";
	    if (instore == '1') {
	        image_left = add_base + "/icons/instore_icon_factory.gif";
	    }
	    if (hotlight == '1') {
	        image_left = add_base + "/icons/storefinder_icon_fresh_shop.gif";
	    }
	    
	    //var div = document.createElement('div');
	    var div = $("<div>");
	    var a_class = '';
	    if (instore == '1') {
	        //div.setAttribute("class", "instore");
	       divhtml = "instore";
	    }
	    else {
	        //div.setAttribute("class", "store");
	        divhtml = "store";
	    }
	    var html = '';
	    //alert( name+ '/' + address);
	    var street = address.split(',')[0];
	    var city = address.split(',')[1];
	    if (city.split(' ').join('') != "") {
	        city += ', ';
	    }
	    else {
	        city = "";
	    }
	    var state_zip = address.split(',')[2];
		if (address.split(',')[3]){
	    	var state_zip = address.split(',')[3];
			city += address.split(',')[2];
		}
	    //var more = address.split(',')[3];
	    if (url.indexOf("http://") != -1 && url.indexOf(".") != -1) {
	        link = "<a href='" + url + "' target='_blank' class='storelocatorlink'><nobr>" + sl_website_label + "</nobr></a>&nbsp;|&nbsp;";
	    }
	    else {
	        url = "";
	        link = "";
	    }
	    
	    if (instore != '1') {
	        if (hotlight == '1') {
	            a_class = 'class="fresh"';
	        }
	        /* else {
	         a_class = 'class="coffee-store"';
	         }*/
	    }
		var currentpostcode = "  "+$("#storeSelect").val();
		//alert("|"+state_zip+"="+currentpostcode+"|");
		if (state_zip == currentpostcode){
			var distancetofixed = "0.00";
			//alert("match");
		} else {
			var distancetofixed = distance.toFixed(1);
		}
	    html += '<div class="'+divhtml+'" id="id'+i+'"><div class="details"><a ' + a_class + ' href="#mapheader"><span class="add_first"><strong>' + name + '</strong><span class="routeDistance' + i + '">0.0</span> ' + sl_distance_unit + '</span><span class="add">' + street + '<br/>' + city + state_zip + ' </span>' + link + '</a><a href="http://' + sl_google_map_domain + '/maps?q=' + homeAddress + ' to ' + address + '" target="_blank" class="storelocatorlink"><img src="/wp-content/themes/thesis/images/directions.png" width="90" height="23" alt="Directions" /></a></div></div>';
	    div.innerHTML = html;
	    
		$("#id"+i).live("click", function(){
			GEvent.trigger(marker, 'click');
		});
	    resultsDisplayed++;
	    GEvent.addDomListener(div, 'click', function(){
	        GEvent.trigger(marker, 'click');
	    });
	    GEvent.addDomListener(div, 'mouseout', function(){
	        div.style.backgroundColor = '#fff';
	    });
	    if (bgcol == "white") {
	        bgcol = "#ffffff";
	    }
	    else {
	        bgcol = "white";
	    }
	    //return div;
	    return html;
	}