/*
 * tripedia.org
 * 
 * (c) Jan Poeschko
 * 
 */

var DestinationMap = Class.create(Map, {
	initialize: function($super, container, photos, options) {
		$super(container, options);

    var bounds = new GLatLngBounds(this.map.getCenter());  
		
		photos.each(function(photo) {
			latlng = new GLatLng(photo.lat, photo.lng);
    	bounds.extend(latlng);
		}.bind(this));
    
    var zoomLevel = this.map.getBoundsZoomLevel(bounds);
    if (zoomLevel > 15)
      zoomLevel = 15;
    this.map.setCenter(bounds.getMidpoint(), zoomLevel);
    
    this.map.setMapType(G_NORMAL_MAP);
    
    //this.map.addControl(new google.maps.LargeMapControl());
    this.addControl(new ExtLargeMapControl())
    this.addControl(new google.maps.OverviewMapControl());
    this.addControl(new google.maps.MapTypeControl());
		
		photos.each(function(photo) {
			this.displayPhoto(photo);
			//latlng = new GLatLng(photo.lat, photo.lng);
			//points.push(latlng);
		}.bind(this));
		
		/*poly = new GPolyline(points, '#ffff00', 5, 0.6);
		this.map.addOverlay(poly);
		
		var length = poly.getLength();
		if (length > 0)
			$('phototourlength').setText("Length of photo tour: " + commafy(Math.round(length)) + " m");
		*/
		
		this.tourPoly = null;
		this.refreshTour(photos);
		
		this.createMarkerBorders();
		//this.createMarkerBorders.bind(this).delay(0.5);
		//this.createMarkerBorders.bind(this).delay(1);
		//this.createMarkerBorders.bind(this).delay(2);
		
		/*this.allowDrag = this.options.allowDrag || false;
		
		var pos = new GLatLng(photo.lat, photo.lng);
		this.map.setCenter(pos, 13);
	  
	  this.map.setMapType(G_HYBRID_MAP);

	  this.addControl(new GSmallZoomControl());
	  
	  this.marker = new GMarker(pos, {
	  	draggable: this.allowDrag,
	  	clickable: false
	  });
	  this.map.addOverlay(this.marker);
	  
	  GEvent.addListener(this.marker, 'dragend', function(latlng) {
	  	this.fire('markerdrag', latlng.lat(), latlng.lng());
	  }.bind(this));*/
	},
	
	/*setMarkerPos: function(lat, lng) {
		var pos = new GLatLng(lat, lng);
		this.marker.setLatLng(pos);
		this.map.setCenter(pos);
	}	*/
	
	refreshTour: function(photos) {
		var points = $A();
		
		photos.each(function(photo) {
			//this.displayPhoto(photo);
			latlng = new GLatLng(photo.lat, photo.lng);
			points.push(latlng);
		}.bind(this));
		
		if (this.tourPoly)
			this.map.removeOverlay(this.tourPoly);
		this.tourPoly = new GPolyline(points, '#0000ff', 5, 0.6);
		this.map.addOverlay(this.tourPoly);
		
		var length = this.tourPoly.getLength();
		length = Math.round(length);
		if (length > 0)
			$('phototourlength').setText("Length of photo tour: " + commafy(length) + " m");
		else
			$('phototourlength').setText("");
	},

	displayPhoto: function(photo) {
	  var icon = new GIcon();
	  icon.image = this.serverAddress + photo.image.mapicon.url;
	  icon.iconSize = new GSize(photo.image.mapicon.width, photo.image.mapicon.height);
	  icon.iconAnchor = new GPoint(photo.image.mapicon.width / 2, photo.image.mapicon.height / 2);
	  icon.infoWindowAnchor = icon.iconAnchor;

	  var lat = photo.lat;
	  var lng = photo.lng;
	  if (!photo.customPos) {
	    lat += (Math.random() - 0.5) * 0.05;
	    lng += (Math.random() - 0.5) * 0.05;
	  }
	  var marker = new GMarker(new GLatLng(lat, lng), {
	  	title: photo.title,
	  	icon: icon,
	  	zIndexProcess: function() {
	  		return GOverlay.getZIndex(lat) + 1000000000;
	  	}
	  });
	  
	  marker.itemId = photo.id;
	  
	  var title = photo.title;
	  if (!title)
	  	title = "Photo";
	  
	  var tab = $E('div', {'class': 'tab'},
	  	$E('div', {'class': 'mapphoto'},
	  		$E('a', {'target': '_top', 'class': 'image', 'href': photo.url},
	  			$E('img', {'src': photo.image.portrait.url, 'width': photo.image.portrait.width,
	  				'height': photo.image.portrait.height, 'alt': title})
	  		),
		  	$E('p', {'class': 'center'},
		  		$E('a', {'target': '_top', 'href': photo.url},
		  			$E('strong', $T(title))
		  		)/*,
		  		$E('small',
		  			$T(' by '),
		  			$E('a', {'target': '_top', 'href': photo.userUrl}, $T(photo.user))
		  		)*/
		  	)
	  	)/*,
	  	$E('p',
	  		$E('a', {'target': '_top', 'href': photo.destination.city.url},
	  			$T(photo.destination.city.name)
	  		),
	  		$T(', '),
	  		$E('a', {'target': '_top', 'class': 'iconed',
	  			'style': 'background-image: url(' + photo.destination.country.image.icon.url + ')',
	  			'href': photo.destination.country.url},
	  			$T(photo.destination.country.name)
	  		),
	  		$E('br'),
	  		$E('a', {'class': 'iconed zoom'}, $T("Zoom")).observe('click', function() {
	  			this.zoomToPhoto(photo.lat, photo.lng);
	  		}.bindAsEventListener(this))
	  	)*/
	  );
	  
	  /*GEvent.addListener(marker, "click", function() {
      this.clickedMarker = marker;
      marker.openInfoWindow(tab);
	  }.bind(this));*/
	  
	  marker.bindInfoWindow(tab);
	  
	  //this.markers.push(marker);
	  this.map.addOverlay(marker);
	  
	  return marker;
	}
});