//ShipPlotting.com map_functions.php for Google Maps implementation
//Copyright (C) 2007 ShipPlotting.com
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; version 2
//of the License.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//For a written copy of this license, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

//We would like to ask that you leave the overlay with our logo in tact
//so others may know where they too can get this script.  Thank you.

//You do NOT need to modify anything in this script!
//All variables are available on the map_data.php page.
var PromoControl = function(url) {
this.url_ = 'http://www.shipplotting.com';
};
PromoControl.prototype = new GControl(true);
PromoControl.prototype.initialize = function(map) {
var container = document.createElement("div");
container.innerHTML = '<img style="cursor:pointer" src="http://www.shipplotting.com/images/stories/maps/providedby.jpg" border="0">';
container.style.width='120px';
container.style.height='50px';
url = this.url_;
GEvent.addDomListener(container, "click", function() {
document.location = url;
});
 
map.getContainer().appendChild(container);
 
return container;
};
 
PromoControl.prototype.getDefaultPosition = function() {
return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(70, 0));
};

//Initialization function
function init() {
var type;
var allTypes = { 'All':[] };

//Changes the body style and opens/closes the sidebar.
document.getElementById('button-sidebar-hide').onclick = function() { return changeBodyClass('sidebar-right', 'sidebar-off'); };
document.getElementById('button-sidebar-show').onclick = function() { return changeBodyClass('sidebar-off', 'sidebar-right'); };
handleResize();

//Runs the function below (gbrowsercompat) and then loads the google map.
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.addControl(new PromoControl());
map.setCenter(new GLatLng(slat, slon), zlvl);
map.openInfoWindowHtml(map.getCenter(),	(map_desc));

//This is going to google map and sidebar.			
for(id in markers) {
initializePoint(markers[id]);
allTypes[markers[id].stype] = true;
}

//This creates the sort tabs at the top
for(stype in allTypes)    {
initializeSortTab(stype);
}
}
changeBodyClass('loading', 'standby');
}	
	
	
//Initialize variables
var map;

//List the FUNctions
//create the ToolTip overlay object
function ToolTip(marker,html,width) {
this.html_ = html;
this.width_ = (width ? width + 'px' : 'auto');
this.marker_ = marker;
}
 
ToolTip.prototype = new GOverlay();
 
ToolTip.prototype.initialize = function(map) {
var div = document.createElement("div");
div.style.display = 'none';
map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
 
this.map_ = map;
this.container_ = div;
}
 
ToolTip.prototype.remove = function() {
this.container_.parentNode.removeChild(this.container_);
}
 
ToolTip.prototype.copy = function() {
return new ToolTip(this.html_);
}
 
ToolTip.prototype.redraw = function(force) {
if (!force) return;
 
var pixelLocation = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
this.container_.innerHTML = this.html_;
this.container_.style.position = 'absolute';
this.container_.style.left = pixelLocation.x + "px";
this.container_.style.top = pixelLocation.y + "px";
this.container_.style.width = this.width_;
this.container_.style.font = 'bold 10px/10px verdana, arial, sans';
this.container_.style.border = '1px solid black';
this.container_.style.background = '#CCFFFF';
this.container_.style.padding = '4px';
 
//one line to desired width
this.container_.style.whiteSpace = 'nowrap';
if(this.width_ != 'auto') this.container_.style.overflow = 'hidden';
this.container_.style.display = 'block';
}
 
GMarker.prototype.ToolTipInstance = null;
 
GMarker.prototype.openToolTip = function(content) {
//don't show the tool tip if there is acustom info window
if(this.ToolTipInstance == null) {
this.ToolTipInstance = new ToolTip(this,content)
map.addOverlay(this.ToolTipInstance);
}
}
 
GMarker.prototype.closeToolTip = function() {
if(this.ToolTipInstance != null) {
map.removeOverlay(this.ToolTipInstance);
this.ToolTipInstance = null;
}
}

function windowHeight() {
// Standard browsers (Mozilla, Safari, etc.)
if (self.innerHeight)
return self.innerHeight;
// IE 6
if (document.documentElement && document.documentElement.clientHeight)
return document.documentElement.clientHeight;
// IE 5
if (document.body)
return document.body.clientHeight;
// Just in case.
return 0;
}

//Function to handle the window resizing, may get rid of this later
function handleResize() {
var height = windowHeight() - document.getElementById('toolbar').offsetHeight - 50;
document.getElementById('map').style.height = height + 'px';
document.getElementById('sidebar').style.height = height + 'px';
}

window.onresize = handleResize;

//Function for opening and closing the side bar
function changeBodyClass(from, to) {
	document.body.className = document.body.className.replace(from, to);
}

//This function allows users to sort their tabs
function initializeSortTab(stype) {
var listItem = document.createElement('li');
var listItemLink = listItem.appendChild(document.createElement('a'));

listItemLink.href = "#";
listItemLink.innerHTML = stype;
listItemLink.onclick = function() {

for(id in markers) {
if (markers[id].stype == stype || 'All' == stype)
markers[id].show();
else
markers[id].hide();
}
return false;
}
document.getElementById('filters').appendChild(listItem);
}


//This function adds markers to the map.
function initializePoint(pointData) {
var visible = false;
var point = new GLatLng(pointData.latitude, pointData.longitude);
var marker = new GMarker(point);

GEvent.addListener(marker,'mouseover',function() {

//These open and close the tooltips, Modify Carefully.
marker.openToolTip(pointData.sname + '<br />Last Update: ' + pointData.timestamp);
});
GEvent.addListener(marker,'mouseout',function() {
marker.closeToolTip();
});

var listItem = document.createElement('li');
var listItemLink = listItem.appendChild(document.createElement('a'));
listItemLink.href = "#";

//This is the info that goes into the sidebar, modify carefully.
listItemLink.innerHTML = '<strong>' + pointData.sname + ' </strong><span>' + pointData.mmsi + ', ' + pointData.stype + '</span>';

//This is the information that shows up in the info window, modify carefully.
var focusPoint = function() {
marker.openInfoWindowHtml(
'<table id="info" width=350px >'
+ '<tr><td width="75px" align="left">Ship Name: </td><td>' + pointData.sname + ' </td><td align="left">Speed : </td><td>'+ pointData.speed + '</td></tr>'
+ '<tr><td align="left">MMSI: </td><td>' + pointData.mmsi + '</td><td align="left">Course:</td><td>'+ pointData.course + '</td></tr>'
+'<tr><td align="left">Ship Type: </td><td>' + pointData.stype + '</td><td align="left">Heading:</td><td>'+ pointData.heading + '</td></tr>'
+'<tr><td align="left">Ship Lat: </td><td>' + pointData.latitude + '</td><td align="left">&nbsp;</td><td>&nbsp;</td></tr>'
+'<tr><td align="left">Ship Long: </td><td>' + pointData.longitude + '</td><td align="left">Dimensions: </td><td>L: ' + pointData.length + '  </td></tr>'
+'<tr><td align="left">&nbsp;</td><td>&nbsp;</td><td align="left">&nbsp;</td><td>W: ' + pointData.width + ' </td></tr>'
+'<tr><td align="left">Destination: </td><td>' + pointData.dest + '</td><td align="left">&nbsp;</td><td>D: ' + pointData.draft + '</td></tr>'
+ '<tr><td align="left">ETA:</td><td>' + pointData.eta + '</td><td align="left">&nbsp;</td><td>&nbsp;</td></tr>'
+ '</table>');
map.panTo(point);
return false;
}

GEvent.addListener(marker, 'click', focusPoint);
listItemLink.onclick = focusPoint;
pointData.show = function() {
if (!visible) {
document.getElementById('sidebar-list').appendChild(listItem);
map.addOverlay(marker);
visible = true;
}
}

pointData.hide = function() {
if (visible) {
document.getElementById('sidebar-list').removeChild(listItem);
map.removeOverlay(marker);
visible = false;
}
}
pointData.show();
}
//Some start and stop stuff.
window.onload = init;
window.onunload = GUnload;
