//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.

function TLabel(){}
TLabel.prototype.initialize=function(a){
 this.parentMap=a;
 var b=document.createElement('span');
 b.setAttribute('id',this.id);
 b.innerHTML=this.content;
 document.body.appendChild(b);
 b.style.position='absolute';
 b.style.zIndex=1;
 if(this.percentOpacity){this.setOpacity(this.percentOpacity);}
 this.w = document.getElementById(this.id).offsetWidth;
 this.h = document.getElementById(this.id).offsetHeight;
 this.mapTray=a.getPane(G_MAP_MAP_PANE);
 this.mapTray.appendChild(b);
 if(!this.markerOffset){this.markerOffset=new GSize(0,0);}
 this.setPosition();
 GEvent.bind(a,"zoomend",this,function(){this.setPosition()});
 GEvent.bind(a,"moveend",this,function(){this.setPosition()});
}
TLabel.prototype.setPosition=function(a){
 if(a){this.anchorLatLng=a;}
 var b=this.parentMap.fromLatLngToDivPixel(this.anchorLatLng);
 var x=parseInt(b.x);
 var y=parseInt(b.y);
 with(Math){switch(this.anchorPoint){
  case 'topLeft':break;
  case 'topCenter':x-=floor(this.w/2);break;
  case 'topRight':x-=this.w;break;
  case 'midRight':x-=this.w;y-=floor(this.h/2);break;
  case 'bottomRight':x-=this.w;y-=this.h;break;
  case 'bottomCenter':x-=floor(this.w/2);y-=this.h;break;
  case 'bottomLeft':y-=this.h;break;
  case 'midLeft':y-=floor(this.h/2);break;
  case 'center':x-=floor(this.w/2);y-=floor(this.h/2);break;
  default:break;
 }}
 var d=document.getElementById(this.id);
 d.style.left=x-this.markerOffset.width+'px';
 d.style.top=y-this.markerOffset.height+'px';
}
TLabel.prototype.setOpacity=function(b){
 if(b<0){b=0;} if(b>100){b=100;}
 var c=b/100;
 var d=document.getElementById(this.id);
 if(typeof(d.style.filter)=='string'){d.style.filter='alpha(opacity:'+b+')';}
 if(typeof(d.style.KHTMLOpacity)=='string'){d.style.KHTMLOpacity=c;}
 if(typeof(d.style.MozOpacity)=='string'){d.style.MozOpacity=c;}
 if(typeof(d.style.opacity)=='string'){d.style.opacity=c;}
}
GMap2.prototype.addTLabel=function(a){
 a.initialize(this);
}
GMap2.prototype.removeTLabel=function(a){
 var b=document.getElementById(a.id);
 this.getPane(G_MAP_MAP_PANE).removeChild(b);
 delete(b);
}



var iconBlue = new GIcon(); 
    iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
    iconBlue.iconSize = new GSize(6, 10);
   

var redirecturl="http://www.javascriptkit.com/script/script2/countredirect2.shtml"
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));
GEvent.addListener(map,'zoomend',function() {
var slvl = map.getZoom();
// var sUrlBase = "http://sasesptl.sytes.net/indexptl.php";
// var sNewURL = sUrlBase + "?Zoom=" + slvl + "&Long=" + slong;
// var redirecturl="http://sasesptl.sytes.net/indexptl.php"+"?Lat="+slat+"&Long="+slong+"&zlvl="+zlvl;
//window.location=redirecturl;
});

//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 arial, 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 iconBlue = new GIcon(); 
    iconBlue.image = 'http://maps.google.com/mapfiles/kml/pal4/icon25.png';
    //iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconBlue.iconSize = new GSize(13, 13);
    iconBlue.shadowSize = new GSize(11, 10);
    iconBlue.iconAnchor = new GPoint(3, 10);
    iconBlue.infoWindowAnchor = new GPoint(5, 1);

// var o = new GMarkerOptions();
//o.Icon = iconblue;
//o.title = pointData.sname; 
//var marker = new BpMarker(point,iconBlue);

var marker = new GMarker(point,iconBlue);
//marker.title = pointData.sname;




var label924 = new TLabel();
label924.id = pointData.sname;
label924.anchorLatLng = new GLatLng (pointData.latitude,pointData.longitude);
label924.anchorPoint = 'bottomleft';
label924.content = '<table id="xray" bgcolor="#FFFF00" >'+'<tr><th  scope="col"><FONT face="arial" size="1">'+pointData.sname+'</font></th></tr>';
label924.markerOffset = new GSize (-10,12);
label924.percentOpacity = 65;

map.addTLabel(label924);


//var icon = new GIcon(point);
//icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
//icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
//icon.iconSize = new GSize(12, 20);
//icon.shadowSize = new GSize(22, 20);
//icon.iconAnchor = new GPoint(6, 20);


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 + ' ,  '+pointData.dest +'</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;
