var map;
var marcadorIcon = { path: google.maps.SymbolPath.CIRCLE, fillColor: "#56ABF6", fillOpacity: 1, scale: 5, strokeWeight: 0 }
var infowindowOpen = false;

function logomapa(logomapaDiv) {
	var logomapa = document.createElement('div');
	logomapa.style.marginTop = '10px';
	logomapa.style.marginLeft = '10px';
	logomapa.innerHTML = '<a href="index"><img src="http://tfg-sharenews.rhcloud.com/static/media/logo.png" class="logomap" height="40px"/></a>';
	logomapaDiv.appendChild(logomapa);
}

var currentlatitude = false;
var currentlongitude = false;
var currentlocalitzacio = '';

function geopointer(){
	if (currentlatitude == false || currentlongitude == false) {
		document.getElementById('geopointer').className = 'fa fa-spinner fa-spin geopointer';
		navigator.geolocation.getCurrentPosition(function(position) {
			currentlatitude = position.coords.latitude;
			currentlongitude = position.coords.longitude;
			document.getElementById('geopointer').className = 'geopointerNOT';
			document.getElementById('geopointerNOT').className = 'fa fa-map-marker geopointer geopointer-on';
			document.getElementById('geolatitud').value = currentlatitude;
			document.getElementById('geolongitud').value = currentlongitude;
			geocity(41.375162914009984, 2.0996290024678856);
		});
	} else {
		document.getElementById('geopointer').className = 'geopointerNOT';
		document.getElementById('geopointerNOT').className = 'fa fa-map-marker geopointer geopointer-on';
		document.getElementById('geolatitud').value = currentlatitude;
		document.getElementById('geolongitud').value = currentlongitude;
		document.getElementById('localitzacio').value = currentlocalitzacio;
		document.getElementById('news-data-localitzacio').innerHTML = '<i class="fa fa-map-marker" title="Localització"></i><b>'+currentlocalitzacio+'</b>';
	}
}

function geopointerNOT(){
	document.getElementById('geopointerNOT').className = 'geopointerNOT';
	document.getElementById('geopointer').className = 'fa fa-map-marker geopointer';
	document.getElementById('geolatitud').value = '';
	document.getElementById('geolongitud').value = '';
	document.getElementById('localitzacio').value = '';
	document.getElementById('news-data-localitzacio').innerHTML = '';

}

function geocity(lat, lng) {
	$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+lng, function(data) {
		for(var i = 0; i < data['results']['0']['address_components'].length; i++){
			if(data['results']['0']['address_components'][i]['types'][0] === "locality"){
				currentlocalitzacio = data['results']['0']['address_components'][i]['long_name'];
				document.getElementById('localitzacio').value = currentlocalitzacio;
			}
			if(data['results']['0']['address_components'][i]['types'][0] === "administrative_area_level_1"){
				if (currentlocalitzacio != '') {
					currentlocalitzacio = currentlocalitzacio+', '+data['results']['0']['address_components'][i]['long_name'];
				} else {
					currentlocalitzacio = data['results']['0']['address_components'][i]['long_name'];
				}
				document.getElementById('localitzacio').value = currentlocalitzacio;
			}
			if(data['results']['0']['address_components'][i]['types'][0] === "country"){
				if (currentlocalitzacio != '') {
					currentlocalitzacio = currentlocalitzacio+' ('+data['results']['0']['address_components'][i]['long_name']+')';
				} else {
					currentlocalitzacio = data['results']['0']['address_components'][i]['long_name'];
				}
				document.getElementById('localitzacio').value = currentlocalitzacio;
			}
		}
		document.getElementById('news-data-localitzacio').innerHTML = '<i class="fa fa-map-marker" title="Localització"></i><b>'+currentlocalitzacio+'</b>';
	});
}

function limitsmapa() {
	var latNorth = map.getBounds().getNorthEast().lat();
	var latSouth = map.getBounds().getSouthWest().lat();
	var newLat;

	if(latNorth>85) {
		newLat =  map.getCenter().lat() - (latNorth-85);
	}
	if(latSouth<-85) {
		newLat =  map.getCenter().lat() - (latSouth+85);
	}
	if(newLat) {
		var newCenter= new google.maps.LatLng(newLat, map.getCenter().lng());
		map.setCenter(newCenter);
	}
}

function initialize(){
	var estil = [
		{ featureType: "all", elementType: "all", stylers: [{visibility: "off"}] },
		{ featureType: "water", elementType: "all", stylers: [{visibility: "on"}, {color: "#344a5f"}] },
		{ featureType: "landscape", elementType: "all", stylers: [{visibility: "on"}, {color: "#009966"}] },
	];

	var opcions = {
		center: new google.maps.LatLng(40,0),
		zoom: 2, minZoom: 2,
		mapTypeControl: false,
		panControl: false,
		zoomControl: true, zoomControlOptions: { style: google.maps.ZoomControlStyle.LARGE, position: google.maps.ControlPosition.TOP_RIGHT },
		streetViewControl: false,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		styles: estil,
	};

	map = new google.maps.Map(document.getElementById("map_canvas"), opcions);

	var logomapaDiv = document.createElement('div');
	new logomapa(logomapaDiv);
	logomapaDiv.index = 1;
	map.controls[google.maps.ControlPosition.TOP_LEFT].push(logomapaDiv);

	google.maps.event.addListener(map, 'center_changed', function() { limitsmapa(); });

	newsToMap();

	new DayNightOverlay({
		map: map,
		fillColor: 'rgba(0,0,0,0.3)',
	});
}