//var openinginfowindow;
var reportinfowindow = new google.maps.InfoWindow();
var map;
var geocoder = new google.maps.Geocoder();
  
function parseDate(dateToParse) {
	var year = parseInt(dateToParse.substring(0,4), 10);
	var month = parseInt(dateToParse.substring(5,7), 10);
	var day = parseInt(dateToParse.substring(8,10), 10);
	var hour = parseInt(dateToParse.substring(11,13), 10);
	var min = parseInt(dateToParse.substring(14,16), 10);

	var date = new Date();
	date.setFullYear(year, month-1, day);
	date.setHours(hour, min);
	
	return date.format("dddd, mmm dd, yyyy hh:MM TT");

}

function createMarker(reportlatlng, reportid, state, message, createts, mediaurl, thumbnail, map){
	var markerimage;
	var markerstate;
	if(state == "heavy") {
		 markerimage = "../images/heavy.png";
		 markerstate ="HEAVY";
	} else if(state == "mod"){
		 markerimage = "../images/mod.png";
		 markerstate ="MODERATE";						 
	} else{
		 markerimage = "../images/light.png";
		 markerstate ="LIGHT";						 
	}
	
	var markericonimage = new google.maps.MarkerImage(markerimage,
	// This marker is 26 pixels wide by 26 pixels tall.
	new google.maps.Size(15, 26),
	// The origin for this image is 10,10.
	new google.maps.Point(0,0),
	// The anchor for this image is the base of the flagpole at 13,12.
	new google.maps.Point(8, 26));
	
	var reportmarker = new google.maps.Marker({
		position: reportlatlng,
		icon: markericonimage
	});
	
	var str = '<div class="m_bubble"><div class="m_iconmessage">';
	
	//check if there is a photo
	if (mediaurl == '' || mediaurl == null || mediaurl == '(null)'){
		if(state == "heavy") {
			str += '<div class="m_heavy"></div>';
		} else if(state == "mod"){
			str += '<div class="m_mod"></div>';
		}
		else{
			str += '<div class="m_light"></div>';
		};
	} else{
			str += '<div class="m_wphoto"><a href="'+mediaurl+'" target="_blank"><img src="'+thumbnail+'" width=50 height=50 border=0></img></a></div>';
	}

	str += '<div class="m_message"><b>' + markerstate + ' traffic</b> is reported in this area; ' + message + '<div class="m_timestamp"><br>' + parseDate(createts) + '</div></div></div></div>';	


	google.maps.event.addListener(reportmarker, 'click', function() {
		//openinginfowindow.close();
		reportinfowindow.setOptions({
			content: str,
			maxWidth: 300,
			position: reportlatlng			
		});																		 
		reportinfowindow.open(map,reportmarker);		
	});							

	google.maps.event.addListener(map, 'click', function(){
		reportinfowindow.close();		
	});
	
	return reportmarker;
}
		
function initialize(plat, plon, pzoom, paddress) {
	var latlng = new google.maps.LatLng(14.591007471703454, 121.06434074096678);
	var markerlatlng = new google.maps.LatLng(14.5480, 121.0525);
	
	if (plat == 0 || plon == 0 || pzoom == 0){
	  map = drawmap(latlng, markerlatlng, 12);	
		if (paddress != '' && paddress != null){
			codeAddress(paddress, map);
		}	  
	  displayReports(map);
	}
	else{
	  latlng = new google.maps.LatLng(plat,plon);
	  var latgeo = plat - .0020;
	  var lnggeo = plon - .0008;
	  markerlatlng = new google.maps.LatLng(latgeo, lnggeo);
	  map = drawmap(latlng, markerlatlng, 16);		
	  displayReports(map); 
	}	
}

function drawmap(latlng, markerlatlng, thiszoom){
	var myOptions = {
	  zoom: thiszoom,
	  center: latlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP,
	  mapTypeControl: true
	};
	map = new google.maps.Map(document.getElementById("m_map"), myOptions);
	return map;
}

function displayReports(map){
	
	/*
	// opening initial marker overlay
	var contentStr = '<div style="width:200px;height:110px;text-align:left;font-size:12px;">Beat the traffic, be StreetSmart. <br><br>Click on the traffic pins to view report details. If you have an Android phone, <a href="http://web.portal.globe.com.ph/globe/pmsdata.html?target=globeweb/main/infoDld_comnt.html&catg=AppZone&name=FA000000156">download StreetSmart now</a> so you can start submitting reports, too!</div>';
	openinginfowindow = new google.maps.InfoWindow({
		content: contentStr
	});
	
	//var initImage = '../images/shim.gif';
	var marker = new google.maps.Marker({
	  position: markerlatlng,
	  title:"StreetSmart"
	  //icon: initImage
	});

	openinginfowindow.open(map,marker);

	google.maps.event.addListener(map, 'click', function(){
		openinginfowindow.close();
	});		*/	
	
	
	var url = "../ajax/m_getreports.php";
	  $.getJSON(url, function (data) {
		if (data.results && data.results.length > 0) {
		  var results = data.results;
		  //populate the map
			for (var i=0; i < results.length; i++) {
				var result = results[i];
				  if (result.message == null){
					  var thismessage = "";
				  }
				  else{
					  var thismessage = result.message;					
				  }				
				var reportlatlng = new google.maps.LatLng(result.geolat, result.geolon);
				var reportmarker = createMarker(reportlatlng, result.reportid, result.state, thismessage, result.createts, result.mediaurl, result.thumbnail, map);										
				reportmarker.setMap(map);
			}			   
		}
	  });		
}		

function searchMap(){
	var address = document.getElementById('searchfield').value;
	
	var latlng = new google.maps.LatLng(14.591007471703454, 121.06434074096678);
	var markerlatlng = new google.maps.LatLng(14.5480, 121.0525);
	map = drawmap(latlng, markerlatlng, 14);
	codeAddress(address, map);
}

function codeAddress(address) {
	geocoder.geocode( { 'address': address, 'region': 'PH'}, function(results, status) {
	  if (status == google.maps.GeocoderStatus.OK) {
		map.setCenter(results[0].geometry.location);		
	  } else {
		alert("Ooopps! Sorry, we can't find that location on the map!");
	  }
	});
}


