

	function storeSort(a, b) {
		if (! a['yelp']['avg_rating'] ) {
			return 1;
		}
		if (! b['yelp']['avg_rating']) {
			return -1;
		}
		if (a['yelp']['avg_rating'] == b['yelp']['avg_rating']) {
			return secondaryStoreSort(a, b);
		}
		return ((a['yelp']['avg_rating'] < b['yelp']['avg_rating']) ? 1 : -1);
	}

	function secondaryStoreSort(a, b) {
		if (! a['yelp']['review_count'] ) {
			return 1;
		}
		if (! b['yelp']['review_count']) {
			return -1;
		}
		return ((a['yelp']['review_count'] < b['yelp']['review_count']) ? 1 : -1);
	}

	// TODO  should really do this somewhere more central
	//stores.sort(storeSort);
	
	function getStoresVars() {
		var listmax = 15,
		    numstores = stores.length,
		    loopmax = Math.min(listmax, numstores);
		return {
			listmax: listmax,
			numstores: numstores,
			loopmax: loopmax
	//		shownInList : 0,
	//		shownInMap : 0
		};
	}
	storesvars = getStoresVars();
		

   	google.load("maps", "2")  ;

     // Call this function when the page has been loaded
	function initialize() {
		var mapOptions = {
			googleBarOptions : {
				style : "new",
				adsOptions: {
					client: "pub-0077572760990687",
					channel: "9879103914",
					adsafe: "high",
					language: "en"
				}
			}
		}

		var map = new google.maps.Map2(document.getElementById("map"), mapOptions);
//		map.setCenter(new google.maps.LatLng(40.749988,-74.000988), 12);
		map.setCenter(new google.maps.LatLng(40.743376,-73.953877), 12);
		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
		map.addControl(new GLargeMapControl());
		map.enableGoogleBar();

	

		function addStore(i, disp) {
			var store = stores[i];
			var coor = new GLatLng(store["latitude"], store["longitude"]);
			var marker = new GMarker(coor, { hide: ! disp });
			var storeExitStr = store["name"];
			storeExitStr = storeExitStr.replace(/ /g, '_');
			storeExitStr = storeExitStr.replace(/'/g, '');
			
			var html = "<b>" + store["name"] + "</b><br/>" + store["address"] + "<br/> " + store["phone"] + "<br/><a href='" + store["url"] + "' onclick='javascript:pageTracker._trackPageview(\"/outbound/links/storeslist/store/" + storeExitStr + "\");'>" + store["url"] + "</a>";
			if(store["url2"]) {
				html += "<br/><a href='" + store["url2"] + "' onclick='javascript:pageTracker._trackPageview(\"/outbound/links/storeslist/store/" + storeExitStr + "2\");'>" + store["url2_text"] + "</a>";
			}
			
			marker.bindInfoWindowHtml(html)
			store['marker'] = marker; 
			//GEvent.addListener(marker, "click", function() {
			//});
			return marker;
		}

		for(var i=0; i < storesvars.numstores; i++) {
			var mrkr = addStore(i, (i < storesvars.numstores));
			map.addOverlay(mrkr);
		}	


		// add this listener after addStore() has been called for each store (creates marker for each)
		GEvent.addListener(map, "moveend", function() {
			bounds = this.getBounds();
			// hiding all stores that are not in map bounds
			// have to run through all of them, hiding any out of bounds and all remaining once we have hit the list max
			var numFound = 0;
			storesvars.shownInMap = 0;
			storesvars.shownInList = 0;
			for(i=0; i < storesvars.numstores; i++) {
				var store = stores[i];
				var coor = new GLatLng(store["latitude"], store["longitude"]);
				var storeListItem = document.getElementById("store" + i + "ListItem");
				if(bounds.containsLatLng(coor))
					storesvars.shownInMap++;
				if(bounds.containsLatLng(coor) && numFound < storesvars.listmax) {
					storeListItem.style.display = 'block';
					storesvars.shownInList++;
					//store['marker'].show();
					numFound++;
				}
				else {
					storeListItem.style.display = 'none';
					//store['marker'].hide();
				}
			}

			var $j = jQuery.noConflict();
                        var el = $j("#storeslistshowing");
                        el[0].innerHTML = storesvars.shownInList;
                        el = $j("#storeslisttotal");
                        el[0].innerHTML = storesvars.shownInMap;	
		});
		
		GEvent.trigger(map, "moveend") 

	}	   
	
	google.setOnLoadCallback(initialize);

		var $j = jQuery.noConflict();
		var list = $j("#maplist");

		

		function addLink(i, disp) {
			var store = stores[i];
			var $j = jQuery.noConflict();
			var list = $j("#maplist");
			var storeExitStr = store["name"];
			storeExitStr.replace(' ', '_');
			
			var storeLine = "<a href='javascript:void(0);' class='store_name'>" + store['name'] + "</a>"
			if (store['yelp']['url'] && store['yelp']['review_count']) 
				storeLine += '<img src="' + store['yelp']['rating_img_url'] + '"/><a href="' + store['yelp']['url'] + '" onclick="javascript:pageTracker._trackPageview(\'/outbound/links/storeslist/yelp_review/" + storeExitStr + "\');">' + store['yelp']['review_count'] + ' reviews</a><span class="att"> on Yelp</span>';
			var display = (disp) ? '' : 'style="display:none;"';
			list.append("<div id=\"store" + i + "ListItem\" class='store' " + display + ">" + storeLine + "</div>");
			var lnk = $j("#maplist > div:last");
			lnk.click(function () { 
				GEvent.trigger(store['marker'], "click")
			});	
		}
		
		$j(document).ready(function(){
			for(var i=0; i < storesvars.numstores; i++) {
				addLink(i, i < storesvars.loopmax);
			}		   
			var map = new google.maps.Map2(document.getElementById("map"));
		});
		

