Event.observe(
	window,
	'load',
	function(event){
		new KisrtsMap();
	}
);

Event.observe(
	window,
	'unload',
	function(event){
		GUnload();
	}
);


var KisrtsMap = Class.create({

	initialize: function(){
	
		var self = this;

		if(!GBrowserIsCompatible()){
			return;
		}
		
		this.map = null;
		this.markerManager = null;
		
		this.recordIcon = new GIcon(G_DEFAULT_ICON);
		this.recordIcon.image = SITE_ROOT + 'img/bull.png';
		this.recordIcon.iconSize = new GSize(30, 31);
		this.recordIcon.shadow = '';
	
		this.phpThumbUrl = SITE_ROOT_URL.replace(/index.php/, '') + 'app/vendors/php_thumb/phpThumb.php';
		
		this.initMap();
		this.addRecords(records);

	},

	 initMap: function(){

		this.map = new GMap2($('mapCanvas'));

		this.map.addControl(new GLargeMapControl());

		var lastRecord = records[records.length - 1];
		var lastRecordLocation = lastRecord.RecordLocation[0];
		var lat = lastRecordLocation.latitude;
		var long = lastRecordLocation.longitude;
		this.map.setCenter(new GLatLng(lat, long), 15);

		this.markerManager = new MarkerManager(this.map);

	 },
	 
	addRecords: function(records){

		var markers = []; 
	
		for(var i = 0; i < records.length; i ++){
			var record = records[i];
			var recordLocation = record.RecordLocation[0];
			var marker = this.getMarker(i, recordLocation);
			markers.push(marker);
		}
		
		this.markerManager.addMarkers(markers, 1);
		this.markerManager.refresh();
		
	},
	
	getMarker: function(recordId, recordLocation){

		var latitude = recordLocation.latitude;
		var longitude = recordLocation.longitude;
		var point = new GLatLng(latitude, longitude);
		
		var markerOptions = {
			icon: this.recordIcon,
		};
		var marker = new GMarker(point, markerOptions);

		var user = recordLocation.User;
		var created = recordLocation.created;
		var countryCode = recordLocation.country_code;
		var city = recordLocation.city;
		var message = '';
		message += '<p>';
		if(user.first_name){ 
			message += '<strong>' + user.first_name + ' ' + user.last_name + '</strong>';
		}
		if(!city.blank() && !countryCode.blank()){
			message += ' (' + city + ', ' + countryCode + ')';
		} else if(!countryCode.blank()){
			message += '(' + countryCode + ')';
		}
		message += '<br />'; 
		if(user.picture && !user.picture.blank()){
			var imageUrl = SITE_ROOT_PATH + 'app/webroot/img/users/' + user.picture;
			var imageSrc = this.phpThumbUrl + '?src=' + imageUrl + '&h=125';
			message += '<img src="' + imageSrc + '" height="125" />';
			message += '<br />'; 
		}
		if(created != '0000-00-00'){
			var day = created.substr(8, 2);
			var month = created.substr(5, 2);
			var year = created.substr(0, 4);
			message += 'Registered on ' + day + '/' + month + '/' + year;
		}
		message += '<br />'; 
		if(user.website && !user.website.blank()){
			message += '<a href="' + user.website + '" target="_blank">' + user.website + '</a>';
			message += '<br />'; 
		}
		message += '</p>'; 
		if(user.comment && !user.comment.blank()){
			message += '<p class="comment">'; 
			var comment = user.comment.stripTags();
			message += nl2br(user.comment.stripTags()); 
			message += '</p>'; 
		}

		GEvent.addListener(marker, 'click', function(){
			marker.openInfoWindow(message);
		});
		
		return marker;

	},
	
	showHistory: function(recordImage){
	
		this.hideActiveHistory();

		var recordId = recordImage.recordId;
		var record = records[recordId];
		if(record.RecordLocation.length == 1){
			return;
		}

		var xPoints = [stripPx(recordImage.style.left)];
		var yPoints = [stripPx(recordImage.style.top)];

		for(var i = 1; i < record.RecordLocation.length; i++){ 

			var recordLocation = record.RecordLocation[i];

			var longitude = recordLocation.longitude;
			var latitude = recordLocation.latitude;
		    var coordinate = longLatToXY({long: longitude, lat: latitude}, this.worldRect);
		    xPoints.push(coordinate.x);
		    yPoints.push(coordinate.y);

			var historyRecord = this.addRecord(false, recordLocation);
			this.historyRecords.push(historyRecord);

		}
		
		this.activeRecord = record;

	},
	
	hideActiveHistory: function(){
	
		for(var i = 0; i < this.historyRecords.length; i ++){
			$(this.historyRecords[i]).remove();
		}
		this.historyRecords = [];
		
		this.activeRecord = null;
	
	}
	
});

// see http://snipplr.com/view/634/replace-newlines-with-br-platform-safe/
function nl2br(text){
	return text;
	var re_nlchar;
	text = escape(text);
	if(text.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g ;
	}else if(text.indexOf('%0A') > -1){
		re_nlchar = /%0A/g ;
	}else if(text.indexOf('%0D') > -1){
		re_nlchar = /%0D/g ;
	}
	if(!re_nlchar){
		return text;
	}
	return unescape( text.replace(re_nlchar,'<br />') );
}
