<repo_name>richmengsix/Tweather<file_sep>/js/GoogleAPI.js
/**
 * @author SHarmony
 */
    var geocoder; 
     var map;
     var mc;
     var mcOptions;
      function initialize() {
      	geocoder = new google.maps.Geocoder(); 
      	
      	
        var mapOptions = {
          center: new google.maps.LatLng(40.807979,-73.963137),
          zoom: 18,
          mapTypeId: google.maps.MapTypeId.SATELLITE
        };
        map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
        var markers=new Array();    
        //Adding points

		markers[0]=pining(40.806979,-73.964137,"Columbia University","<h4>Columbia University</h4><h5>- Columbia University</h4><a id='stata-get' href='#' onclick=''>Get Inside Direction</a>",'http://maps.google.com/mapfiles/ms/icons/green-dot.png');
		markers[1]=pining(40.907979,-73.863137,"Columbia University","<h4>Columbia University</h4><h5>- Columbia University</h4><a id='stata-get' href='#' onclick=''>Get Inside Direction</a>",'http://maps.google.com/mapfiles/ms/icons/green-dot.png');
		markers[2]=pining(40.927979,-73.893137,"Columbia University","<h4>Columbia University</h4><h5>- Columbia University</h4><a id='stata-get' href='#' onclick=''>Get Inside Direction</a>",'http://maps.google.com/mapfiles/ms/icons/green-dot.png');
		markers[3]=pining(40.807989,-73.963137,"Columbia University","<h4>Columbia University</h4><h5>- Columbia University</h4><a id='stata-get' href='#' onclick=''>Get Inside Direction</a>",'http://maps.google.com/mapfiles/ms/icons/green-dot.png');
		markers[4]=pining(40.907999,-73.863137,"Columbia University","<h4>Columbia University</h4><h5>- Columbia University</h4><a id='stata-get' href='#' onclick=''>Get Inside Direction</a>",'http://maps.google.com/mapfiles/ms/icons/green-dot.png');
		markers[5]=pining(40.927969,-73.893137,"Columbia University","<h4>Columbia University</h4><h5>- Columbia University</h4><a id='stata-get' href='#' onclick=''>Get Inside Direction</a>",'http://maps.google.com/mapfiles/ms/icons/green-dot.png');
		
		//set style options for marker clusters (these are the default styles)
mcOptions = {gridSize: 30, maxZoom: 15,
	styles: [{
		height: 64,
		url: "img/sunny64.png",
		width: 64
	},
	{
		height: 64,
		url: "img/sunny64.png",
		width: 64
	},
	{
		height: 64,
		url: "img/cloudy64.png",
		width: 64
	},
	{
		height: 64,
		url: "img/snowy64.png",
		width: 64
	},
	{
		height: 64,
		url: "img/rainy64.png",
		width: 64
		}]};
		mc = new MarkerClusterer(map,markers,mcOptions);
      }
      
      
	  function codeAddress() {
	    var address = document.getElementById("Google-address").value;
	    geocoder.geocode( { 'address': address}, function(results, status) {
	      if (status == google.maps.GeocoderStatus.OK) {
	        map.setCenter(results[0].geometry.location);
	        var marker = new google.maps.Marker({
	            map: map,
	            position: results[0].geometry.location
	        });
//	        alert('(lng,lat): '+ [results[0].geometry.location.lng(),results[0].geometry.location.lat()]);
	        getNowCoordinate(address,[results[0].geometry.location.lng(),results[0].geometry.location.lat()]);
	        $("#current-addr").html("Current Address: "+address);
	      } else {
	        alert("Geocode was not successful for the following reason: " + status);
	      }
	    });
	  }
  
  function refresh(pinary)
  {
  	var iconPath;
  	var iconSmallPath;
  	var content;
  	var CurrentMarkers = new Array();
  	for(var i=0;i<pinary.length;i++){
  		iconPath=getWeatherPic(pinary[i].weather);
  		iconSmallPath=getWeatherSmallPic(pinary[i].weather);
		content="<div class='mapitem'>"+
							"<div class='mapitem-left'>"+
								"<p class='mapitem-title'>"+pinary[i].name+"</p>"+
								"<img class='mapitem-line' src='img/line22.png'/>"+
								"<p class='mapitem-tweet'>"+pinary[i].tweet+"</p>"+
								"<p class='mapitem-time'>"+pinary[i].nowtime+"</p>"+
							"</div>"+
							"<div class='mapitem-right'>"+
								"<img class='mapitem-img' src='"+iconPath+"' />"+
							"</div>"+
						"</div>";
  		CurrentMarkers[i]=pining(pinary[i].lat,pinary[i].lng,pinary[i].description,content,iconSmallPath);

  	}
 	mc.clearMarkers();
	mc.addMarkers(CurrentMarkers,false);
  }
  function getWeatherPic(weather){
  	var sunny="img/sunny.png";
  	var cloudy="img/cloudy.png";
  	var rainy="img/rainy.png";
  	var snowy="img/snow.png";
  	var defaut="img/clearmoon.png";
  	
  	if(weather=="sunny") return sunny;
  	else if (weather=="cloudy") return cloudy;
  	else if (weather=="rainy") return rainy;
  	else if (weather=="snowy") return snowy;
  	else return defaut;
  }
  function getWeatherSmallPic(weather){
  	var sunny="img/sunny64.png";
  	var cloudy="img/cloudy64.png";
  	var rainy="img/rainy64.png";
  	var snowy="img/snow64.png";
  	var defaut="img/sunny_night64.png";
  	
  	if(weather=="sunny") return sunny;
  	else if (weather=="cloudy") return cloudy;
  	else if (weather=="rainy") return rainy;
  	else if (weather=="snowy") return snowy;
  	else return defaut;
  }  
  function pining(lat,lng,description,contentString,iconPath)
  {
  	var myLatlng = new google.maps.LatLng(lat,lng);
/*	contentS="<div class='mapitem'>"+
							"<div class='mapitem-left'>"+
								"<p class='mapitem-title'>Columbia University</p>"+
								"<img class='mapitem-line' src='img/line22.png'/>"+
								"<p class='mapitem-tweet'>Wow! It's sunny outside... Finally!</p>"+
								"<p class='mapitem-time'>-30 minutes ago</p>"+
							"</div>"+
							"<div class='mapitem-right'>"+
								"<img class='mapitem-img' src='img/sunny.png' />"+
							"</div>"+
						"</div>";
						*/
	var infowindow = new google.maps.InfoWindow({
    	content: contentString
		});

  	var marker = new google.maps.Marker({ 
        position: myLatlng, 
        map: map, 
        flat: true, 
        title: description, 
     });
		iconFile = iconPath; 
		marker.setIcon(iconFile);

    // The function after click
	google.maps.event.addListener(marker, 'click', function() {
  		infowindow.open(map,marker);
		});
		return marker;
  	}<file_sep>/js/weather.js
/**
 * @author SHarmony
 */
//Node object
$(document).ready(function(){
	getJASON();
	$('#refresh-button').click(function(){
		refresh(WeatherNodes);
	});
});


function getJASON(){
		
		$.getJSON("temp.json", function(json) {		
	    	console.log(json); // this will show the info it in firebug console
			$.each(json.results, function(i, item) {

				var created_at = item.created_at;
				var user = item.from_user;
				var lat = item.geo.coordinates[0];
				var lng = item.geo.coordinates[1];
				var tweet = item.text;
				var weather = item.weather;
																			
				addWeatherNode(user,lat,lng,user,tweet,created_at,weather);
				
				$('<p>').text(tweet).appendTo('body');
		});
	});
}

function WeatherNode(name,lat,lng,description,tweet,nowtime,weather){
	this.name=name;
	this.lat=lat;
	this.lng=lng;
	this.description=description;
	this.tweet=tweet;
	this.nowtime=nowtime;
	this.weather=weather;
}
var WeatherNodes=new Array();
WeatherNodes[0]=new WeatherNode("Columbia University",40.807979,-73.963137,"Columbia University","Wow! It's sunny outside... Finally!",getTimeAgo("Sat, 09 Feb 2013 03:55:38 +0000"),"sunny");
WeatherNodes[1]=new WeatherNode("Time Square",40.756035,-73.986676,"Time Square","Cloudy!! It's so bad!",getTimeAgo("Sat, 09 Feb 2013 04:55:38 +0000"),"snowy");

function addWeatherNode(name,lat,lng,description,tweet,nowtime,weather){
	var length = WeatherNodes.length;
	WeatherNodes[length] = new WeatherNode(name,lat,lng,name,tweet,getTimeAgo(nowtime),weather);
	alert(WeatherNodes.length);
}

function getTimeAgo(timestatement){
	var date = new Date();
	var second = date.getSeconds();
	var hour = date.getHours();
	var minute = date.getMinutes();
	//Sat, 09 Feb 2013 03:55:38 +0000
	var TweetHour = timestatement.split(" ")[4].split(":")[0];
	var TweetMinute = timestatement.split(" ")[4].split(":")[1];
	var TweetSecond = timestatement.split(" ")[4].split(":")[2];
	var OffsetHour = hour-(TweetHour-5);
	var OffsetMinute = minute-TweetMinute;
	//modify cross day
	if (OffsetHour<0) OffsetHour=OffsetHour+24;
	//modify cross hour
	if (OffsetHour>0 && OffsetMinute<0){
		OffsetHour--;
		OffsetMinute=OffsetMinute+60;
	}
	//Output
	if (OffsetHour==0) return " - "+OffsetMinute+" min ago";
	else return " - "+OffsetHour+" hr and "+OffsetMinute+" min ago"; 
}

function loadScript() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.google.com/maps/api/js?key=<KEY>&sensor=false&callback=initialize";
  document.body.appendChild(script);
}
 
window.onload = loadScript;
