/*
 * Map class, contains methods for interacting and populating all of the google maps.
 */

var MapManager = function(maptype){
	this.type = maptype;
	this.response = new Array();
	this.section = document.location.pathname.split('/')[1];

	this.url = '/json/?controller=directory&method=search';
	
	if(maptype == 'business'){
		ll = this.getLatLng();
		map.setCenter(new GLatLng(ll[0], ll[1]), 10);
	} else {
		// First check to see if there is a title
		
		var lat = 55.378051;
		var lon = -3.435973;
		var zoom = 5;
		
		if($('#google_map').attr('title')){
			 // Change the lat and lon to fit the title
			 var details = $('#google_map').attr('title').split('/');

			 lat = parseFloat(details[0]);
			 lon = parseFloat(details[1]);
			 zoom = parseFloat(details[2]);
		}
		
		map.setCenter(new GLatLng(lat, lon), zoom);
	}
	
	this.createIcons();
}

MapManager.prototype.showLoading = function(){
	// Create a div with the ID loadingMap
	if(!$('#loadingMap').length){
		$('#google_map').prepend('<div id="loadingMap" style="display: none;"></div>');
		$('#google_map #loadingMap').css({width: $('#google_map').width(), height: $('#google_map').height(), opacity: 0, display: 'block'});
		$('#google_map #loadingMap').fadeTo('slow', 0.70);
	}
}


MapManager.prototype.hideLoading = function(){
	if($('#loadingMap').length){	
		$('#google_map #loadingMap').fadeOut('slow', function(){
			$(this).remove();
		});
	}	
}


/*
 * Creates the individual icons for the map
 */
 
MapManager.prototype.createIcons = function(){

	if($('#google_map').attr('class') == 'small'){
// Fuel Point Icon
		this.iconFuelPoint = new GIcon();
		this.iconFuelPoint.shadow = '/images/map_icons/shadow_small.png';
		this.iconFuelPoint.shadowSize = new GSize(12, 12);
		this.iconFuelPoint.iconSize = new GSize(10, 10);
		this.iconFuelPoint.iconAnchor = new GPoint(11, 11);
		this.iconFuelPoint.infoWindowAnchor = new GPoint(5, 5);
		this.iconFuelPoint.infoShadowAnchor = new GPoint(5, 5);
		this.iconFuelPoint.image = '/images/map_icons/icon_fuelpoint_small.png';
		
		// Marine Related Icon
		this.iconMarine = new GIcon();
		this.iconMarine.shadow = '/images/map_icons/shadow_small.png';
		this.iconMarine.shadowSize = new GSize(12, 12);
		this.iconMarine.iconSize = new GSize(10, 10);
		this.iconMarine.iconAnchor = new GPoint(11, 11);
		this.iconMarine.infoWindowAnchor = new GPoint(5, 5);
		this.iconMarine.infoShadowAnchor = new GPoint(5, 5);
		this.iconMarine.image = '/images/map_icons/icon_marine_business_small.png';
		
		// Non Marine Related Icon
		this.iconNonMarine = new GIcon();
		this.iconNonMarine.shadow = '/images/map_icons/shadow_small.png';
		this.iconNonMarine.shadowSize = new GSize(12, 12);
		this.iconNonMarine.iconSize = new GSize(10, 10);
		this.iconNonMarine.iconAnchor = new GPoint(11, 11);
		this.iconNonMarine.infoWindowAnchor = new GPoint(5, 5);
		this.iconNonMarine.infoShadowAnchor = new GPoint(5, 5);
		this.iconNonMarine.image = '/images/map_icons/icon_waterside_service_small.png';
	} else {
		// Fuel Point Icon
		this.iconFuelPoint = new GIcon();
		this.iconFuelPoint.shadow = '/images/map_icons/shadow.png';
		this.iconFuelPoint.shadowSize = new GSize(30, 30);
		this.iconFuelPoint.iconSize = new GSize(25, 25);
		this.iconFuelPoint.iconAnchor = new GPoint(13, 13);
		this.iconFuelPoint.infoWindowAnchor = new GPoint(13, 13);
		this.iconFuelPoint.infoShadowAnchor = new GPoint(13, 13);
		this.iconFuelPoint.image = '/images/map_icons/icon_fuelpoint.png';
		
		// Marine Related Icon
		this.iconMarine = new GIcon();
		this.iconMarine.shadow = '/images/map_icons/shadow.png';
		this.iconMarine.shadowSize = new GSize(30, 30);
		this.iconMarine.iconSize = new GSize(25, 25);
		this.iconMarine.iconAnchor = new GPoint(13, 13);
		this.iconMarine.infoWindowAnchor = new GPoint(13, 13);
		this.iconMarine.infoShadowAnchor = new GPoint(13, 13);
		this.iconMarine.image = '/images/map_icons/icon_marine_business.png';
		
		// Non Marine Related Icon
		this.iconNonMarine = new GIcon();
		this.iconNonMarine.shadow = '/images/map_icons/shadow.png';
		this.iconNonMarine.shadowSize = new GSize(30, 30);
		this.iconNonMarine.iconSize = new GSize(25, 25);
		this.iconNonMarine.iconAnchor = new GPoint(13, 13);
		this.iconNonMarine.infoWindowAnchor = new GPoint(13, 13);
		this.iconNonMarine.infoShadowAnchor = new GPoint(13, 13);
		this.iconNonMarine.image = '/images/map_icons/icon_waterside_service.png';
	}
}

/*
 * Get the lat/lng from the directory page
 */
MapManager.prototype.getLatLng = function(){
	rtn = new Array();

	rtn[0] = parseFloat($('span.lat').attr('title'));
	rtn[1] = parseFloat($('span.lng').attr('title'));
		
	return rtn;
}

/*
 * Processes the response from a json request
 */
MapManager.prototype.processResponse = function(response){
	mapObj.response = response;
	map.clearOverlays();
	mapObj.populate();
}

/*
 * Generates the html to append to the speech box
 */
MapManager.prototype.generateHTML = function(data){
	
	var directory = 'directory';
	if(data.business.folderId == 4){
		directory = 'fuelpoints';
	}
	
	// Create the DOM elements and create the final chunk of HTML
	html = '<div class="business-info-box"';
	html += '<h2>' + data.business.name + '</h2>';
	html += '<p><strong>Town:</strong> ' + data.address.town.town + '</p>';
	html += '<p><a href="/' + directory + '/view/?id=' + data.business.id + '">More details...</a></p>';
	
	return html;
}

/*
 * Adds a point to the map
 */
MapManager.prototype.addPoint = function(data){

	var point = new GLatLng(data.address.latitude, data.address.longitude);
	var marker = this.getMarker(data.business.folderId, point);
		
	var html = this.generateHTML(data);
	GEvent.addListener(marker, 'click', function(){
		map.openInfoWindow(point, html, {maxWidth: 300});
	});

	map.addOverlay(marker);
	
}

/*
 * Retrieves the appropiate marker
 */
MapManager.prototype.getMarker = function(folderId, point){

	switch(folderId) {
	case '1':
		options = {icon: this.iconMarine};
		break;
	case '2':
		options = {icon: this.iconMarine};
		break;
	case '3':
		options = {icon: this.iconNonMarine};
		break;
	case '4':	
		options = {icon: this.iconFuelPoint};
		break;
	}

	
	rtn = new GMarker(point, options);
	
	return rtn;
}

/*
 * Populates the map from the data retrieved from the search
 */
MapManager.prototype.populate = function(){

	for(var i = 0; i < this.response.data.length; i++){
		this.addPoint(this.response.data[i]);
	}
}