
    var map;
    var geocoder;
    var address;

    function initialize() {
     if (GBrowserIsCompatible()) {
      map =  new google.maps.Map(document.getElementById("map_canvas"));
   //   map.setCenter(new GLatLng(40.730885,-73.997383), 15);
     // map.setUIToDefault();
      GEvent.addListener(map, "click", getAddress);
      geocoder = new GClientGeocoder();
      }
    }
    
    function getAddress(overlay, latlng) {
      if (latlng != null) {
        address = latlng;
        geocoder.getLocations(latlng, showAddress);
      }
    }

    function showAddress(response) {
      map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("Status Code:" + response.Status.code);
      } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(
        		''
      //  '<b>orig latlng:</b>' + response.name + '<br/>'  
        +'(' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + ')<br>'
      //   +'<b>Status Code:</b>' + response.Status.code + '<br>' 
      //   +'<b>Status Request:</b>' + response.Status.request + '<br>' 
        + '<b>Address:</b>' + place.address + '<br>' 
      // + '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>'  
      // +  '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode
        );
      }
    }

function lookupAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
            }
          }
        );
      }
    }
   
function adjustIPhone(){
	var c=map.getCenter();
	var theLat=c.lat();
	var theLon=c.lng();
	var root=document.location.href;
	var p1=root.indexOf("//");
	var p2=root.indexOf("/",p1+3);
	   
	var url=root.substring(0,p2)+"/demo/d1.html?lat="+theLat+"&lon="+theLon+"&sim=all";
    document.getElementById('safari_window').src = url ;
	document.getElementById('safari_location').value = url;
}

function adjustIPhoneMusic(){
	var c=map.getCenter();
	var theLat=c.lat();
	var theLon=c.lng();
	var root=document.location.href;
	var p1=root.indexOf("//");
	var p2=root.indexOf("/",p1+3);
	   
	var url=root.substring(0,p2)+"/d4.html?lat="+theLat+"&lon="+theLon+"&sim=music";
    document.getElementById('safari_window').src = url ;
	document.getElementById('safari_location').value = url;
}

function adjustIPhoneBooks(){
	var c=map.getCenter();
	var theLat=c.lat();
	var theLon=c.lng();
	var root=document.location.href;
	var p1=root.indexOf("//");
	var p2=root.indexOf("/",p1+3);
	   
	var url=root.substring(0,p2)+"/b2.html?lat="+theLat+"&lon="+theLon+"&sim=music";
    document.getElementById('safari_window').src = url ;
	document.getElementById('safari_location').value = url;
}


   function findDeals(){
      var c=map.getCenter();
      var theLat=c.lat();
      var theLon=c.lng();
      $("#deal_canvas").load("y1.jsp", {lat: theLat, lon:theLon}, function(){
   				//alert("The last 25 entries in the feed have been loaded");
      });
   } 
   function findYelpDealsDebug(){
      var c=map.getCenter();
      var theLat=c.lat();
      var theLon=c.lng();
      var theUrl="y2.jsp?lat="+theLat+"&lon="+theLon;
      $.getJSON(theUrl,plotYelpDebugData);
      $("#queryList").html("");
      $("#dealList").html("");
      $("#businessList").html("");
      $("#explainList").html("");
   } 
   
    function findOutsideDealsDebug(){
      var c=map.getCenter();
      var theLat=c.lat();
      var theLon=c.lng();
      var theUrl="o2.jsp?lat="+theLat+"&lon="+theLon;
      $.getJSON(theUrl,plotOutsideDebugData);
      $("#queryList").html("");
      $("#dealList").html("");
      $("#businessList").html("");
      $("#explainList").html("");
   } 
   
   function findTwitterDealsDebug(){
      var c=map.getCenter();
      var theLat=c.lat();
      var theLon=c.lng();
      var theUrl="t2.jsp?lat="+theLat+"&lon="+theLon;
      $.getJSON(theUrl,plotTwitterDebugData);
      $("#queryList").html("");
      $("#dealList").html("");
      $("#businessList").html("");
      $("#explainList").html("");
   } 
   
   function findBlockDealsDebug(){
      var c=map.getCenter();
      var theLat=c.lat();
      var theLon=c.lng();
      var theUrl="b2.jsp?lat="+theLat+"&lon="+theLon;
      $.getJSON(theUrl,plotBlockDebugData);
      $("#queryList").html("");
      $("#dealList").html("");
      $("#businessList").html("");
      $("#explainList").html("");
   } 
   function findFwixDealsDebug(){
      var c=map.getCenter();
      var theLat=c.lat();
      var theLon=c.lng();
      var theUrl="f2.jsp?lat="+theLat+"&lon="+theLon;
      $.getJSON(theUrl,plotFwixDebugData);
      $("#queryList").html("");
      $("#dealList").html("");
      $("#businessList").html("");
      $("#explainList").html("");
   } 
   
   function findMusicDealsDebug(){
	      var c=map.getCenter();
	      var theLat=c.lat();
	      var theLon=c.lng();
	      var theUrl="tm2.jsp?bug=true&lat="+theLat+"&lon="+theLon;
	      $.getJSON(theUrl,plotTwitterMusicDebugData);
	      $("#queryList").html("");
	      $("#dealList").html("");
	      $("#businessList").html("");
	      $("#explainList").html("");
	   } 
	   
   
   
   function addMarker(lat,lon,nm){
    var latlng = new GLatLng(lat,lon);
    map.addOverlay(new GMarker(latlng, { title: nm}));
   }
   
   function explainThings(deal,exp){
   var dl=$("#explainList");
     for (var i=0;i<deal.length;i++){
        var d=deal[i];
        var e=exp[i];
        var why=" <br><pre>("+e["description"]+"--"+e["str"]+")</pre>";
        dl.append("<li> <a href='"+d["clickurl"]+"'>"+d["offerdescription"]+"</a> from "+d["advertisername"]+why+"</li>");
     }
   }
   
   function explainMusicThings(deal,exp){
	   var dl=$("#explainList");
	     for (var i=0;i<deal.length;i++){
	        var d=deal[i];
	        var e=exp[i];
	        var why=" <br><pre>("+e["description"]+"--"+e["str"]+")</pre>";
	        dl.append("<li> <a href='"+d["itURL"]+"'>"+d["albumname"]+"</a> from "+d["artistname"]+why+"</li>");
	     }
	   }
   
   function plotGenericExplanations(d){
      var ql=$("#queryList");
      $.each(d.query,function(i,o){
        ql.append("<li>"+o+"</li>");
      });
      var dl=$("#dealList");
    
      $.each(d.deals,function(i,o){
        dl.append("<li> <a href='"+o["clickurl"]+"'>"+o["offerdescription"]+"</a> from "+o["advertisername"]+"</li>");
      });
      
     var iPhone=$('#safari_window').contents().find('#body');
     if (iPhone){
    	 iPhone.append("<ul id='iPhoneList'></ul>");
    	 var iPhoneList=$('#safari_window').contents().find('#iPhoneList');
    	 $.each(d.deals,function(i,o){
    		 iPhoneList.append("<li> <a href='"+o["clickurl"]+"'>"+o["offerdescription"]+"</a> from "+o["advertisername"]+"</li>");
    	      });
     }
   }
   
   
   
   function plotMusicGenericExplanations(d){
	      var ql=$("#queryList");
	      $.each(d.query,function(i,o){
	        ql.append("<li>"+o+"</li>");
	      });
	      var dl=$("#dealList");
	    
	      $.each(d.deals,function(i,o){
	    	var im=o["coverJPG"];
	    	var tx="<li> <a href='"+o["itURL"]+"'>"+o["albumname"]+"</a> from "+o["artistname"];
	    	if (im && im.length>0){
	    		tx+="<img width='40px' src='"+im+"'>"
	    	}
	    	tx+="</li>";
	        dl.append(tx);
	      });
	      
	     var iPhone=$('#safari_window').contents().find('#body');
	     if (iPhone){
	    	 iPhone.append("<ul id='iPhoneList'></ul>");
	    	 var iPhoneList=$('#safari_window').contents().find('#iPhoneList');
	    	 $.each(d.deals,function(i,o){
	    		 var im=o["coverJPG"];
	 	    	 var tx="<li> <a href='"+o["itURL"]+"'>"+o["albumname"]+"</a> from "+o["artistname"];
	 	    	 if (im && im.length>0){
	 	    		tx+="<img width='40px' src='"+im+"'>"
	 	    	 }
	 	         tx+="</li>";
	    		 iPhoneList.append(tx);
	    	      });
	     }
	   }
   
   function plotYelpDebugData(d){
      plotGenericExplanations(d);
   
   
     $.each(d.businesses,function(i,o){
         var x="<li><b>"+o["name"]+"</b>";
         x+=o["distance"]+" mi ("+o["latitude"]+","+o["longitude"]+")";
         addMarker(o["latitude"],o["longitude"],o["name"]);
         $.each(o["categories"], function(ii,oo){
            x+=" "+oo["name"];
         });
         
         x+="<ul>";
         $.each(o['reviews'],function(ii,oo){
            x+="<li>"+oo["text_excerpt"]+"</li>";
         });
         x+="</ul></li>";
       
       $("#businessList").append(x);
     });
     explainThings(d.deals,d.explanations);
   }
   
   function plotBlockDebugData(d){
      plotGenericExplanations(d);
   
   
     $.each(d.response,function(i,o){
         var x="<li><b>"+o["contents"]+"</b>";
         var lat=o.coordinates["lat"];
         var lon=o.coordinates["long"];
         x+=o["distance"]+"  ("+lat+","+lon+")";
         addMarker(lat,lon,"test");
         x+="</li>";
         $("#businessList").append(x);
     });
     explainThings(d.deals,d.explanations);
   }
   
    function plotFwixDebugData(d){
      plotGenericExplanations(d);
   
   
     $.each(d.result,function(i,o){
         var x="<li><b>"+o["title"]+"</b>";
         var lat=o["latitude"];
         var lon=o["longitude"];
         x+=o["summary"]+"  ("+lat+","+lon+")";
         addMarker(lat,lon,"test");
         x+="</li>";
         $("#businessList").append(x);
     });
     explainThings(d.deals,d.explanations);
   }
   
   
    function plotOutsideDebugData(d){
      plotGenericExplanations(d);
      $.each(d.response,function(i,o){
        var x="<li> "+o["title"]+" </li>";
        
         x+="<ul>";
         $.each(o['places'],function(ii,oo){
            x+="<li><a href='"+oo["url"]+"'>"+oo["name"]+"</a>";
            var pt=oo["georss:point"];
            pts=pt.split(' ');
            x+="("+pts[0]+","+pts[1]+")";
            x+="</li>";
            addMarker(pts[0],pts[1],oo["name"]);
         });
          x+="</ul></li>";
       $("#businessList").append(x);
     });
     explainThings(d.deals,d.explanations);
   }
   
   
    function plotTwitterDebugData(d){
      plotGenericExplanations(d);
   
   
     $.each(d.results,function(i,o){
        var  x="<li>"+o["text"]+" <i>("+o["from_user"]+" "+o["created_at"]+")</i>";
        var pt=o["location"];
        var pt1=pt.split(' ');
        if (pt1.length>1){  // not always there?!
          var pts=pt1[1].split(',');
         }  else {
          var pts=pt.split(',');
         }
          x+="("+pts[0]+","+pts[1]+")";
        
          addMarker(pts[0],pts[1],o["text"]);
         
          x+="</li>";
       $("#businessList").append(x);
     });
     explainThings(d.deals,d.explanations);
   }
   
    function plotTwitterMusicDebugData(d){
        plotMusicGenericExplanations(d);
     
     
       $.each(d.results,function(i,o){
          var  x="<li>"+o["text"]+" <i>("+o["from_user"]+" "+o["created_at"]+")</i>";
          var pt=o["location"];
          var pt1=pt.split(' ');
          if (pt1.length>1){  // not always there?!
            var pts=pt1[1].split(',');
           }  else {
            var pts=pt.split(',');
           }
            x+="("+pts[0]+","+pts[1]+")";
          
            addMarker(pts[0],pts[1],o["text"]);
           
            x+="</li>";
         $("#businessList").append(x);
       });
       explainMusicThings(d.deals,d.explanations);
     }
     
     
   

