/**
 * Map class to show Events.
 */
function EventMap()
{
	this.map;
	this.start = null;
	this.polyline;
	this.points = [];
	this.places = [];
	
	this.markerCluster;
}

/** Google map */
EventMap.prototype.map;

EventMap.prototype.getMap = function()
{
	return this.map;
}

EventMap.prototype.getMarkers = function()
{
	var m = [];
	var len = this.places.length;
	for (var i = 0; i < len; i++)
	{
		m.push(this.places[i].getMarker());
	}
	
	return m;
}

/**
 * Initialise the Map. 
 */
EventMap.prototype.load = function()
{
	var latlng = new google.maps.LatLng(0, 0);
	var myOptions = {
		zoom: 1,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
        overviewMapControl: true
	};
	this.map = new google.maps.Map(document.getElementById("map"), myOptions);

	var me = this; // must use a var to pass this instance to the function.
	
	// initialise map click listener
	google.maps.event.addListener(this.map, "click", function(event) 
	{
		me.addPoint(event.latLng);
	});
	
	var chp = document.getElementById("search_boundingbox");
	if (chp.value)
	{
		var bb = createMarker(chp.value);
		var len = bb.length
		for (var i = 0; i < len; i++)
		{
			this.addPoint(null, bb[i]);
		}
	}
};

EventMap.prototype.addPoint = function(latLng)
{
	this.points.push(latLng);
	
	if (this.points.length == 1)
	{
		this.start = new google.maps.Marker({
			position: latLng,
			map : this.map,
			icon : redImg,
			shadow : redShw
		});
		
		var me = this;
		
		google.maps.event.addListener(this.start, "click",  function enclose()
		{
			me.closePoints();
		});
	}
	else
	{
		this.createPolyline();
	}
};

EventMap.prototype.createPolyline = function()
{
	this.removePolyline();
	
	this.polyline = new google.maps.Polyline({
		path : this.points, 
		map : this.map,
		strokeColor: "#ff0000",
		strokeWeight: 3,
		strokeOpacity: 0.5
	});
};

EventMap.prototype.removePolyline = function()
{
	if (this.polyline)
	{
		this.polyline.setMap(null);
		this.polyline = null;
	}
};

EventMap.prototype.closePoints = function()
{
	var first = this.points[0];
    var last = this.points[this.points.length-1];
    if (first != last)
    {
    	this.points.push(first);
	    var chp = document.getElementById("search_boundingbox");
	    chp.value = this.points;
	    
	    // update map
	    this.createPolyline();
    }
};

EventMap.prototype.reset = function()
{
	this.removePolyline();
	if (this.start)
	{
		this.start.setMap(null);
		this.start = null;
	}
	
  var len = this.places.length;
  for (var i = 0; i < len; i++)
  {
	  this.places[i].setMap(null);
  }

  this.places = [];
  this.points = [];
	
  var chp = document.getElementById("search_boundingbox");
  chp.value = "";
 
  document.getElementById("search_keywords").value = "";
 
  document.getElementById("search_fromDate_month").value = "";
  document.getElementById("search_fromDate_day").value = "";
  document.getElementById("search_fromDate_year").value = "";
  document.getElementById("search_fromDate_hour").value = "";
  document.getElementById("search_fromDate_minute").value = "";
 
  document.getElementById("search_toDate_month").value = "";
  document.getElementById("search_toDate_day").value = "";
  document.getElementById("search_toDate_year").value = "";
  document.getElementById("search_toDate_hour").value = "";
  document.getElementById("search_toDate_minute").value = "";
  
  var options = document.getElementsByName("search[option]");
  var len = options.length;
  for (var i=0; i<len; i++)
  {
	  options[i].checked = false;
  }
};

EventMap.prototype.loadEvent = function(eid, etitle, etype, ewhen, eurl, pid, pname, type, url, showurl, googlepoint)
{
	if (this.disabled)
		return;
	
	var len = this.places.length;
	var found = false;
	for (var i = 0; i < len; i++)
	{
		var ep = this.places[i];
		
		if (ep.getId() == pid)
		{
			found = true;
			ep.addEvent(eid, etitle, etype, ewhen, eurl);
			
			break;
		}
	}
	
	if (len == 0 || !found)
	{
		var eventPlace = new EventPlace(this.map);
		
		eventPlace.construct(pid, pname, type, url, showurl, googlepoint);
		eventPlace.addEvent(eid, etitle, etype, ewhen, eurl);
		
		this.places.push(eventPlace);
	}
};

EventMap.prototype.cluster = function()
{
	this.markerCluster = new MarkerClusterer(this.getMap(), this.getMarkers());
};

/**
 * Zoom the map fit the boundary.
 */
EventMap.prototype.zoom = function()
{
	var allgeom = [];
	var rlen = this.points.length;
	for (var i = 0; i < rlen; i++)
	{
		allgeom.push(this.points[i]);
	}

	reSizeAndReCentreMap(allgeom, this.map);
};
