
//***************************************************************************************************************
//This File Has Modified Code so if you're copying and pasting to use the locator in a different site, please talk to Brian Munz first
//***************************************************************************************************************
window.onunload = function(){
	GUnload()
};

var myGlobalHandlers = {
	onCreate: function(){
		Loader.on()
	},

	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			Loader.off()
		}
	},
	
	onError: function(e,f){
		storeLocator.msg({dataString:f})
		Loader.off()
	},
	
	onException: function(e,f){
		storeLocator.msg({dataString:f})
		Loader.off()	
	}
};

Ajax.Responders.register(myGlobalHandlers);

var Loader = {
	on: function(){
		$('sl_loader').style.display = 'block'
	},
	
	off: function(){
		$('sl_loader').style.display = 'none'
	},
	
	follow: function(e){
		var loader = $('sl_loader')
		loader.style.left = Event.pointerX(e)  + 13 + 'px'
		loader.style.top = Event.pointerY(e)  + 13 + 'px'
	}
};


var storeLocator = {
	
	//page scope variables
	vars: {
		storeHeight: 140,
		storesPerPage: 3,
		pageMultiplier: 50,
		zoom: 8,
		minZoomLevel: 3,
		maxZoomLevel: 17,
		mapControler:false,
		
		map: false,
		marker: true,
		currentPageVar: false,
		stores: [],
		totalPages: false,
		address: false,
		miles: true,
		slider: null,
		sliderAry:[],
		initialLoad: true
	},
	
	services: {
		store: '/storeLocServ'
	},
	
	//base parameters
	pars: {
		operation:'coSearch',
		lat:44,
		lon:-90,
		numResults:100,
		mnlt:false,
		mxlt:false,
		mnln:false,
		mxln:false,
		token:'CJB',
		heavy: true
	},
	
	//recent request caches
	cache: {
		geo: {count:50,recs:[{call:false,response:{lat:false,lon:false}}]},
		store: {count:10,recs:[{call:false,response:false}]}
	},
	
	stopReturn: function(event){
		if(Event.keyCode(event) == Event.KEY_RETURN){
			storeLocator.updateMap()
			return false
		}
	},
	
	resetPage: function(obj){
		//clear points
		storeLocator.map.clearMarkers(storeLocator.vars.map,storeLocator.vars.stores)
		
		storeLocator.vars.currentPageVar = 1
		
		$('ResultHeader').innerHTML = '';
		$('ResultList').innerHTML = '';
		$('ResultFooter').innerHTML = '';
		$('errorBox').innerHTML = '';
		$('errorBox').style.display = 'none';
		
	},
	
	//message handling
	msg: function(para){
		$('errorBox').style.display = 'block'
		$('errorBox').innerHTML = para.dataString
	},
	
	buildAddress: function(){
		var string = ''
		
		var address = $('fldAddress').value.trim()
		var city = $('fldCity').value.trim()
		var state = $('fldState').value.trim()
		var zip = $('fldZip').value.trim()
		
		if((city && state)|| zip){
			if(address)string += address + ' '
			if(city)string += city + ', '
			if(state)string += state + '. '
			if(zip){
				string += zip
			}
			
			//validation
			if(!string){
				storeLocator.resetPage()
				//storeLocator.msg({dataString:'Please enter an address that our system can search for.'})
				$('ResultList').innerHTML = '<div class="paddMsg"><p class="fB">Please enter an address that our system can search for.</div>';				
			}
			
			if(string.match(/0/g)){
				if(string.length == string.match(/0/g).length){
					string=''
					storeLocator.resetPage()
					//storeLocator.msg({dataString:'Search Not Recognized'})
					$('ResultList').innerHTML = '<div class="paddMsg"><p class="fB">Search not recognized.</div>';
					
				}	
			}
			
		}else{
			storeLocator.resetPage()
			//storeLocator.msg({dataString:'Please enter a City and State OR a Zip Code.'})
			$('ResultList').innerHTML = '<div class="paddMsg"><p class="fB">Please enter a City and State OR a Zip Code.</div>';
			
		}
		return string
	},
	
	//START OF MAP UPDATE CASCADE
	updateMap: function(){
		if(GBrowserIsCompatible()) {
			
			//clear bubble
			storeLocator.map.bubble.hide()
			
			//populate global pars object
			storeLocator.vars.address = storeLocator.buildAddress()
			if(!storeLocator.vars.address){
				return	
			}
			
			//scan cache
			var cached = storeLocator.cache.geo.recs.detect(function(v,i){
				if(storeLocator.vars.address == v.call){
					//update lat and lng
					storeLocator.pars.lat = v.response.lat
					storeLocator.pars.lon = v.response.lon
					return true
				}
			});
			
			if(cached){
				storeLocator.setLocation()
			}else{
				storeLocator.getLocation()
			}
		}
	},
	
	getLocation: function(){
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(storeLocator.vars.address,function(point){
			
			//validation
			if(point == null){
				storeLocator.resetPage()
				//storeLocator.msg({dataString:'Search Not Recognized'})
				$('ResultList').innerHTML = '<div class="paddMsg"><p class="fB">Sorry, no locations found in this area.</p><p>While we have a vast network of stores, no stores were found using your current selection.</p><ul>Please refine your search by:<li>Altering your search parameters </li><li>Expanding your zoom level</li></ul></div>';
				
				return false
			}else{
				//load results in cache
				storeLocator.cache.geo.recs.unshift({call:storeLocator.vars.address,response:{lat:point.lat(),lon:point.lng()}})
				//check size of cache and remove if nessessary
				if(storeLocator.cache.geo.recs.length>storeLocator.cache.geo.count)storeLocator.cache.geo.recs.pop()
				
				//set new points
				storeLocator.pars.lat = point.lat()
				storeLocator.pars.lon = point.lng()
				
				//set new location on map
				storeLocator.setLocation()
			}
		});
	},
	
	setLocation: function(point){
		if(!point){
			var point = new GLatLng(storeLocator.pars.lat,storeLocator.pars.lon)
		}
		
		GEvent.clearInstanceListeners(storeLocator.vars.map)
		
		//pan to center of address
		storeLocator.vars.map.setCenter(point)
		
		setTimeout(function(){storeLocator.map.addEvents(storeLocator.vars.map)},100)
		
		storeLocator.vars.miles = true
		
		if(storeLocator.vars.initialLoad == true){
			storeLocator.map.cachedSetup(storeLocator.vars.map,false)
			storeLocator.vars.initialLoad = false
			storeLocator.vars.map.savePosition()
		}
		
		
		//set map bounds for stores
		storeLocator.setBounds()
	},
	
	getFromHere: function(){
		var point = storeLocator.vars.map.getCenter();
		
		//set new points
		storeLocator.pars.lat = point.lat()
		storeLocator.pars.lon = point.lng()
		
		//set map bounds for stores
		storeLocator.setBounds()
	},
	
	setBounds: function(){
		var bounds = storeLocator.vars.map.getBounds()
		storeLocator.pars.mnlt = Math.min(bounds.getSouthWest().lat(),bounds.getNorthEast().lat())
		storeLocator.pars.mxlt = Math.max(bounds.getSouthWest().lat(),bounds.getNorthEast().lat())
		storeLocator.pars.mnln = Math.min(bounds.getSouthWest().lng(),bounds.getNorthEast().lng())
		storeLocator.pars.mxln = Math.max(bounds.getSouthWest().lng(),bounds.getNorthEast().lng())
		
		//set cookie
		storeLocator.setCookie()
	},
	
	setCookie: function(){
		//store the new points
		var today = new Date();
		var expire = new Date();
		expire.setTime(today.getTime() + 3600000*2);
		document.cookie = "stLocLat="+escape(storeLocator.pars.lat) + ";expires="+expire.toGMTString();
		document.cookie = "stLocLon="+escape(storeLocator.pars.lon) + ";expires="+expire.toGMTString();
		document.cookie = "stLocZoom="+escape(storeLocator.vars.zoom) + ";expires="+expire.toGMTString();
	
		//go get the stores
		storeLocator.getStores()
	},
	
	getStores: function(){	
		//clear bubble
		storeLocator.map.bubble.hide()
		
		var paraObj = {}
		Object.extend(paraObj,storeLocator.pars)
		
		var pars = Transform.toQueryString(paraObj);
		
		//scan cache
		var cached = storeLocator.cache.store.recs.detect(function(v,i){
			if(pars == v.call){
				return v
			}
		});
		
		if(cached){
			storeLocator.showList(cached.response)
		}else{
			storeLocator.locatorService(storeLocator.services.store,pars,storeLocator.showList)
		}
	},
	
	locatorService: function(url,pars,callBack){
		
		//$('status').innerHTML = encodeURI(pars)
		var myAjax = new Ajax.Request(url, { 
			method: 'get', 
			parameters: encodeURI(pars), 
			onComplete: callBack
		});
	},
	
	showList: function (originalRequest){
		$('ResultHeader').style.backgroundColor = "#e3ddcd";
		$('ResultFooter').style.backgroundColor = "#e3ddcd";
		//alert('redraw')
		//$('status').innerHTML = $('status').innerHTML +(originalRequest.responseText)
		
		//is the data from the service or the cache
		if(originalRequest.responseText){
			try{
				var obj = originalRequest.responseText.exe()
			}catch(e){
				//alert('error in service')
				var obj = originalRequest.responseText.replace(/":}/g,'":""}')
				obj = obj.exe()
			}
			
			//load results in cache
			storeLocator.cache.store.recs.unshift({call:this.parameters,response:obj})
			//check size of cache and remove if nessessary
			if(storeLocator.cache.store.recs.length>storeLocator.cache.store.count)storeLocator.cache.store.recs.pop()
		}else{
			var obj = originalRequest
		}
		
		//reset current page vars
		storeLocator.resetPage(obj)
		
		//if no stores
		if(obj.COUNT==0){
			$('ResultList').innerHTML = '<div class="paddMsg"><p class="fB">Sorry, no locations were found in this area.</p><p>While we have a vast network of stores, no stores were found using your current selection.</p><ul>Please refine your search by:<li>Altering your search parameters </li><li>Expanding your zoom level</li></ul></div>';
			return
		}
		
		//if There are to many stores
		if(obj.COUNT>storeLocator.pars.numResults){
			$('ResultList').innerHTML = '<div class="paddMsg"><p class="fB">Please narrow your search.</p><p>With our vast network of stores, the area you have selected includes more than '+ storeLocator.pars.numResults +' stores.</p><ul>Please narrow your search by:<li>Altering your search parameters </li><li> Narrowing your zoom level </li></ul></div>';
			return
		
		}
		
		//just right amount of stores
		storeLocator.vars.totalPages = Math.ceil(parseInt(obj.COUNT)/storeLocator.vars.storesPerPage)
	
		//number header
		storeLocator.list.updateStoreNumber(obj)
	
		//record set
		var recSlider = document.createElement('div')
		recSlider.id = 'recSlider'
		recSlider.style.top = '0px'
		
		//for each store
		obj.RESULTS.each(function(v,i){
			if(v.store){
				
				//each store
				var rec = document.createElement('div')
				rec.id = 'storeList'+i
				rec.className = 'rec'
				if(i==0)rec.className = 'rec first'
				
				//BEGIN map stuff
				//the stores location icon
				var icon = storeLocator.map.icon.store()
				if(v.store.shipToStore == 'Y'){
					icon = storeLocator.map.icon.shipToStore()
				}
				
				//create and add store to map
				var point = new GLatLng(v.store.latitude,v.store.longitude)
				
				var marker = new GMarker(point,icon)
				storeLocator.vars.map.addOverlay(marker);
				
				//add store to stores array
				storeLocator.vars.stores.push(marker)
				
				//add bubble to marker
				GEvent.addListener(marker, "click", function() {
					storeLocator.map.bubble.show(i,v,rec.id,marker,true)
				});
				//END map stuff
				
				//store Name
				var a = $E('div',rec,{className:"storeName",innerHTML:v.store.locationName})
				//map link
				a.index = i
				a.value = v
				a.rec = rec.id
				a.marker = marker
				a.onclick = function(){
					//map link
					storeLocator.map.bubble.show(this.index,this.value,this.rec,this.marker,false)
				}
				
				//location code
//				$E('div',rec,{className:"locationCode",innerHTML:'#'+v.store.locationCode})
				//address
				//$E('div',rec,{className:"address",innerHTML:v.store.address1})
				if(v.store.address2){
					$E('div',rec,{className:"address",innerHTML:v.store.address2})
				}
				$E('div',rec,{className:"address",innerHTML:v.store.city+', '+v.store.stateCode+ ' ' +v.store.postalCode})
				
				//phone number
				if(v.store.phoneNumber){
					$E('div',rec,{className:"phoneNumber",innerHTML:storeLocator.val.phone(v.store.phoneNumber)})
				}
				//miles
				if(storeLocator.vars.miles){
					$E('span',rec,{className:"miles",innerHTML:v['distance'].round(1) + ' Miles |'})
				}
				//more info link 
				var a = $E('span',rec,{className:"info",innerHTML:'Details'})
				//map link
				a.index = i
				a.value = v
				a.rec = rec.id
				a.marker = marker
				a.onclick = function(){
					//map link
					storeLocator.map.bubble.show(this.index,this.value,this.rec,this.marker)
				}
				
				//add to rec
				recSlider.appendChild(rec)
			}
		});
		$('ResultList').appendChild(recSlider)
		
		//record navigation
		var nav = document.createElement('div')
		nav.className = "nav"
		
		//back button
		$E('div',nav,{id:'backBtn',className:"off",innerHTML:'<< Previous',onclick:function(){
			storeLocator.list.pan(storeLocator.vars.currentPageVar - 2)
		}});
		
		//next button
		var nextBtn = document.createElement('div')
		nextBtn.id = 'nextBtn'
		nextBtn.innerHTML = 'Next >>'
		if(obj.COUNT<4)nextBtn.className = 'off'
		nextBtn.onclick = function(){
			storeLocator.list.pan(storeLocator.vars.currentPageVar)
		}
		nav.appendChild(nextBtn)
		$('ResultFooter').appendChild(nav)
		
		//status bar
		var status = document.createElement('div')
		status.className = "status"
		status.appendChild(document.createTextNode('Page '))
		
		//current page
		$E('span',status,{id:"currentPage",innerHTML:storeLocator.vars.currentPageVar})
		status.appendChild(document.createTextNode(' of '))
		
		//total pages
		$E('span',status,{className:"drivingLink",innerHTML:storeLocator.vars.totalPages})
		$('ResultFooter').appendChild(status)
	}
	
	//END OF MAP UPDATE CASCADE
	
}

storeLocator.map = {
	
	setup:function(map) {
		map = storeLocator.vars.map = new GMap2($(map));
		
		//get rid of bubble shadow
		map.getPane(G_MAP_FLOAT_SHADOW_PANE).style.display = 'none'
		
		if(storeLocator.vars.initialLoad == true){
			storeLocator.map.initialSetup(map)
		}else{
			storeLocator.map.cachedSetup(map,true)
		}
	},
	
	initialSetup: function(map){
		map.setCenter(new GLatLng(storeLocator.pars.lat, storeLocator.pars.lon), 3);
		
		$('ResultList').innerHTML = '';
	
	},
	
	cachedSetup: function(map,init) {
		
		map.setCenter(new GLatLng(storeLocator.pars.lat, storeLocator.pars.lon), storeLocator.vars.zoom);
		
		//get map container
		var obj = map.getContainer()
		if(storeLocator.vars.mapControler){
			obj = $(storeLocator.vars.mapControler)
		}
		$('headRight').style.display = "none";
		// add control labels
		$E('div',obj,{className:'panLabel', innerHTML:'Pan Controls'});
		$E('div',obj,{className:'centerLabel', innerHTML:'Center'});
		$E('div',obj,{className:'zoomLabel', innerHTML:'Zoom Control'});
		
		//add pan control
		//up
		$E('div',obj,{className:'control panUp',onclick:function(){
			map.panDirection(0,+1)
			storeLocator.vars.miles = false
		}, alt:'Move North', title:'Move North'});
		//down
		$E('div',obj,{className:'control panDown',onclick:function(){
			map.panDirection(0,-1)
			storeLocator.vars.miles = false
		}, alt:'Move South', title:'Move South'});
		//left
		$E('div',obj,{className:'control panLeft',onclick:function(){
			map.panDirection(+1,0)
			storeLocator.vars.miles = false
		}, alt:'Move West', title:'Move West'});
		//right
		$E('div',obj,{className:'control panRight',onclick:function(){
			map.panDirection(-1,0)
			storeLocator.vars.miles = false
		}, alt:'Move East', title:'Move East'});
		
		//recenter
		var recenter = $E('div',obj,{className:'control recenter',onclick:function(){
			storeLocator.vars.miles = true
			map.returnToSavedPosition()
		}, alt:'Re-center the Map', title:'Re-center the Map'});
		
		//add zoom control
		//in
		$E('div',obj,{id:'zoomIn',className:'control zoomIn',onclick:function(){
			storeLocator.map.zoom(map,+1)
		}, alt:'Zoom In', title:'Zoom In'});
		//out
		$E('div',obj,{id:'zoomOut',className:'control zoomOut',onclick:function(){
			storeLocator.map.zoom(map,-1)
		}, alt:'Zoom Out', title:'Zoom Out'});
		
		//build slider
		for(var i=storeLocator.vars.maxZoomLevel; i>=storeLocator.vars.minZoomLevel; i--){
			storeLocator.vars.sliderAry.push(i)
		}
		var slider = $E('div',obj,{className:'slider'})
		storeLocator.slider = new Slider(slider,{onStop:function(){
			var newZoomLevel = storeLocator.slider.getCurrentValue()
			map.setZoom(parseInt(newZoomLevel))
			storeLocator.map.adjustZoomButtons(map)
		},
		values:storeLocator.vars.sliderAry});
		storeLocator.slider.setCurrentValue(storeLocator.map.index(storeLocator.vars.sliderAry,storeLocator.vars.zoom))	
		
		storeLocator.map.adjustZoomButtons(map)
		
		//add map type
		//map
		$E('div',obj,{innerHTML:'Map',id:'cmap',className:'control cmap',onclick:function(){
			storeLocator.map.mapType(map,0)
		}});
		//sat
		$E('div',obj,{innerHTML:'Satellite',id:'csat',className:'control csat',onclick:function(){
			storeLocator.map.mapType(map,1)
		}});
		//hybrid
		$E('div',obj,{innerHTML:'Hybrid',id:'chybrid',className:'control chybrid',onclick:function(){
			storeLocator.map.mapType(map,2)
		}});
		
		//adjust map type
		storeLocator.map.adjustMapButtons(storeLocator.vars.map)
		
		if(init){
			storeLocator.map.addEvents(map)
			var point = new GLatLng(storeLocator.pars.lat,storeLocator.pars.lon)
			storeLocator.setBounds(point)
		}
		
	},
	
	addEvents: function(map){
		GEvent.addListener(map, "dragstart", function() {
			storeLocator.vars.miles = false
		});
		
		GEvent.addListener(map, "moveend", function() {
			storeLocator.getFromHere()
		});
		
		GEvent.addListener(map, "movestart", function() {
			//clear bubble
			storeLocator.map.bubble.hide()
		});
	},
	
	clearMarkers: function(map,stores){
		if(stores.length>0)while(stores.length>0)map.removeOverlay(stores.shift());
	},
	
	zoom: function(map,dir){
		if(dir>0){
			if(storeLocator.vars.maxZoomLevel>(parseInt(map.getZoom())))map.zoomIn()
		}else if(dir<0){
			if(storeLocator.vars.minZoomLevel<(parseInt(map.getZoom())))map.zoomOut()
		}
		storeLocator.map.adjustZoomButtons(map)
	},
	
	adjustZoomButtons: function(map){
		Element.removeClassName('zoomOut','zoomOutOff')
		Element.removeClassName('zoomIn','zoomInOff')
		storeLocator.vars.zoom = map.getZoom()
		
		if(storeLocator.vars.minZoomLevel>(storeLocator.vars.zoom-1)){
			Element.addClassName('zoomOut','zoomOutOff')
		}
		else if(storeLocator.vars.maxZoomLevel<(storeLocator.vars.zoom+1)){
			Element.addClassName('zoomIn','zoomInOff')
		}
		storeLocator.slider.setCurrentValue(storeLocator.map.index(storeLocator.vars.sliderAry,storeLocator.vars.zoom))
	},
	
	mapType: function(map,i){
		map.setMapType(G_DEFAULT_MAP_TYPES[i])
		storeLocator.map.adjustMapButtons(map)
		
	},
	
	adjustMapButtons: function(map){
		Element.removeClassName('cmap','cmapOn')
		Element.removeClassName('csat','csatOn')
		Element.removeClassName('chybrid','chybridOn')
		
		var type = map.getCurrentMapType().getName()
		if(type == 'Map'){
			Element.addClassName('cmap','cmapOn')
		}else if(type == 'Satellite'){
			Element.addClassName('csat','csatOn')
		}else if(type == 'Hybrid'){
			Element.addClassName('chybrid','chybridOn')
		}
	},
	
	markerToPixel: function(marker){
		var par = false
		loopster:
		for(p in marker){
			try{
				if(marker[p].tagName == 'IMG'){
					par = p
					break loopster
				}
			}catch(e){
				
			}
		}
		if(!par){
			loopster2:
			for(p in marker){
				try{
					if(marker[p].tagName == 'DIV'){
						par = p
						break loopster2
					}
				}catch(e){
					
				}
			}
		}
		
		if(par){
			var pixels = Position.cumulativeOffset(marker[par])
			var pos = Position.cumulativeOffset($('locatorMain'))
			var x = parseInt(pixels[0]) - parseInt(pos[0])
			var y = parseInt(pixels[1]) - parseInt(pos[1])
		}else{
			var x = 0
			var y = 0
		}
		return {x:x,y:y}
	},
	
	index: function(ary,value){
		loopster:
		for(var i=0; i<ary.length; i++){
			if(ary[i] == value){
				return i
				break loopster
			}
		}
		return false	
	}
}

storeLocator.map.icon = {
	shipToStore: function(){
		var icon = new GIcon();
		icon.image = "../images/icon.png";
		icon.shadow = "../images/icon_shadow.png";
		icon.iconSize = new GSize(65, 55);
		icon.shadowSize = new GSize(65, 52);
		icon.iconAnchor = new GPoint(20, 55);
		icon.infoWindowAnchor = new GPoint(0, 0);
		return icon
	},
	
	store: function(){
		var icon = new GIcon();
		icon.image = "../images/icon.png";
		icon.shadow = "../images/icon_shadow.png";
		icon.iconSize = new GSize(65, 55);
		icon.shadowSize = new GSize(65, 52);
		icon.iconAnchor = new GPoint(20, 55);
		icon.infoWindowAnchor = new GPoint(0, 0);
		return icon
	}
}

storeLocator.map.bubble = {
	
	store: function(i,v){
		var bubble = document.createElement('div')
		bubble.className = 'mapBubble'
		
		//tab 1
		var tab1 = $E('div',bubble,{className:"tab1"})
		
		//tab 2
		var tab2 = $E('div',bubble,{className:"tab2"})
		
		//tab 1 hit area
		var tab1HitArea = $E('div',bubble,{className:"hitArea1"})
		tab1HitArea.onclick = function(){
			tab1HitArea.style.display = 'none'
			tab2HitArea.style.display = 'block'
			tab2.style.display = 'none'
			tab1.style.display = 'block'
		}
		
		if(v.store.storeHours){
			//tab 2 hit area
			var tab2HitArea = $E('div',bubble,{className:"hitArea2"})
			tab2HitArea.onclick = function(){
				tab2HitArea.style.display = 'none'
				tab1HitArea.style.display = 'block'
				tab1.style.display = 'none'
				tab2.style.display = 'block'
			}
			
			//tab 2 content
			var content2 = $E('div',tab2,{className:"bubbleContent"})
			//store Name
			$E('span',content2,{className:"storeName",innerHTML:v.store.locationName})
			$E('div',content2,{className:"fB",innerHTML:'* Store Hours'})
			//store hours
			$E('div',content2,{className:"storeHours",innerHTML:v.store.storeHours})
			$E('div',content2,{className:"space",innerHTML:''})
			$E('div',content2,{className:"discl",innerHTML:'*Hours may vary slightly, particularly during the holidays. Please call the store for exact hours.'})
			
		}else{
			//tab 2 no hit area
			var tab2HitArea = $E('div',bubble,{className:"hitArea2Inactive"})
		}
		
		//tab 1 content
		var content = $E('div',tab1,{className:"bubbleContent"})
		
		//store Name
		$E('span',content,{className:"storeName",innerHTML:v.store.locationName})
		
		//address
		$E('div',content,{className:"address",innerHTML:v.store.address1})
		if(v.store.address2){
			$E('div',content,{className:"address",innerHTML:v.store.address2})
		}
		$E('div',content,{className:"address",innerHTML:v.store.city+', '+v.store.stateCode+ ' ' +v.store.postalCode})
		
		//phone number
		if(v.store.phoneNumber){
			$E('div',content,{className:"phoneNumber",innerHTML:storeLocator.val.phone(v.store.phoneNumber)})
		}
		//directions	
		var a = $E('a',content,{href:storeLocator.val.directions(v),className:"directions",innerHTML:'Get Directions'})
		a.target = "_blank"
		
		$E('div',content,{className:"cbk_logo",innerHTML:''})
		
		//close btn
		var closeIt = document.createElement('div')
		closeIt.className = "closeIt"
		closeIt.onclick = function(){
			storeLocator.map.bubble.hide()
			storeLocator.list.goDim()
		}
		bubble.appendChild(closeIt)
		
		var mapBubbleContainer = document.createElement('div')
		mapBubbleContainer.className = 'mapBubbleContainer'
		mapBubbleContainer.page = Math.floor(i/storeLocator.vars.storesPerPage)
		mapBubbleContainer.appendChild(bubble)
		
		return mapBubbleContainer;
	},
	
	hide: function(){
		var bubbles = $C('mapBubbleContainer',$('locatorMain'))
		for(var i=0; i<bubbles.length; i++){
			$('locatorMain').removeChild(bubbles[i])
		}
	},
	
	show: function(i,v,rec,marker,pan){
		
		//hide Bubbles
		storeLocator.map.bubble.hide()
		
		//get pixels
		var pixels = storeLocator.map.markerToPixel(marker)
		
		var left = pixels.x - 115
		var top = pixels.y - 175
		
		//show bubble
		var bubble = storeLocator.map.bubble.store(i,v);
		
		//Conditional added by Munz to avoid bubble from appearing to high on the page when the marker is barely visible and is clicked.
		if(top < -45){
			bubble.style.left = left + 'px'
			bubble.style.top = '-45px'
		}else{
			bubble.style.left = left + 'px'
			bubble.style.top = top + 'px'
		}
		
		$('locatorMain').appendChild(bubble)		
		
		if(pan){
			storeLocator.list.pan(bubble.page)
		}
		
		storeLocator.list.hightLight(rec)
	}
}

storeLocator.list = {
	pan: function(page){
		var top = page*(storeLocator.vars.storeHeight*-storeLocator.vars.storesPerPage)
		var elm = $('recSlider')
		var current = parseInt(elm.style.top)
		var skip = parseInt(Math.abs(top-current)/(storeLocator.vars.storeHeight*storeLocator.vars.storesPerPage))
		var update = false
		if(top>current){
			if(page>=0){
				Move.Y($('recSlider'),{Y:top,skip:(storeLocator.vars.pageMultiplier*skip),pause:50,timerVar:'slider'})
				update = true
			}else{
				$('backBtn').className = 'off'
			}
		}else if(top<current){
			if(page<storeLocator.vars.totalPages){
				Move.Y($('recSlider'),{Y:top,skip:(storeLocator.vars.pageMultiplier*skip),pause:50,timerVar:'slider'})
				update = true
			}
		}
		if(update){
			storeLocator.vars.currentPageVar = page + 1
			$('currentPage').innerHTML = storeLocator.vars.currentPageVar
			$('nextBtn').className = ''
			$('backBtn').className = ''
			if(storeLocator.vars.currentPageVar==storeLocator.vars.totalPages){
				$('nextBtn').className = 'off'
			}
			else if(storeLocator.vars.currentPageVar==1){
				$('backBtn').className = 'off'
			}
		}
	},
	
	hightLight: function(obj){
		storeLocator.list.goDim()
		
		obj = $(obj)
		Element.addClassName(obj,'on')
	},
	
	goDim: function(){
		var ary = $C('rec','ResultList')
		ary.each(function(v,i){
			Element.removeClassName(v,'on')
		});
	},
	
	updateStoreNumber: function(obj){
		var count = document.createElement('div')
		var html = '<div id="resultHead">There is <strong>' + obj.COUNT + '</strong> Store in Your Area</div>'
		if(obj.COUNT > 1){
			html = '<div id="resultHead">There are <strong>' + obj.COUNT + '</strong> Stores in Your Area</div>'
		}
		$('ResultHeader').innerHTML = html
	}
}

storeLocator.val = {
	hours: function(start,end){
		if(start && end){
			return start.toStdTime() + ' to ' + end.toStdTime()
		}else{
			return 'Closed'
		}	
	},
	
	phone: function(string){
		string = string.replace(/-/g,'')
		if(string && string.length == 10){
			return 'Phone: ('+string.substring(0,3)+') '+string.substring(3,6)+'-'+string.substring(6,10)
		}else{
			return 'Phone: '+string
		}
	},
	
	directions: function(v){
		// begin: replace characters that function encodeURI doesn't
		var cleanAddr1 =  v.store.address1;
		cleanAddr1 = cleanAddr1.replace('&','%26');
		cleanAddr1 = cleanAddr1.replace('(','%28');
		cleanAddr1 = cleanAddr1.replace(')','%29');
		// end: replace characters that function encodeURI doesn't
		
		if(storeLocator.vars.address != " "){
			return 'http://maps.google.com/maps?saddr='+storeLocator.pars.lat+','+storeLocator.pars.lon+'('+encodeURI(storeLocator.vars.address)+')&daddr='+v.store.latitude+','+v.store.longitude+'('+encodeURI(cleanAddr1+' '+v.store.city+', '+v.store.stateCode+' '+v.store.postalCode)+')'
		}else{
			return 'http://maps.google.com/maps?q='+v.store.latitude+','+v.store.longitude+'('+encodeURI(cleanAddr1+' '+v.store.city+', '+v.store.stateCode+' '+v.store.postalCode)+')'
		}
	}
}
