/*----------------------------------------------------------------------------
Seaway Map - Map
version:   1.0
author:    Marc Lacasse, Scott Vinkle, RealDecoy Inc.
___________________________________________________________________________ */



/* =Markers and labels


___________________________________________________________________________ */

com = {}
com.realdecoy = {};
com.realdecoy.arrays = {};
com.realdecoy.managers = {};

com.realdecoy.ctx =
{
	showBoats: true,
	showPorts: false,
	showCdnPorts: false,
	showUsaPorts: false,
	showLocks: false,
    showBoatLabels: false,
    showPortLabels: false,
    showCdnPortLabels: false,
    showUsaPortLabels: false,
    showLockLabels: false
};

com.realdecoy.timeout_interval = 100;

com.realdecoy.pass = function()
{
 var args = Array.prototype.slice.apply( arguments, [2] );
 var obj = arguments[0];
 var fn = arguments[1];
 return function() { return fn.apply( obj, args ); };
};

com.realdecoy.bind = function( obj, fn ) { return function() { return fn.apply( obj, arguments ); }; };
com.realdecoy.copy = function(a, b) { if(!b) b = {}; if(a) for(var c in a) {var d = a[c]; b[c] = d;} return b; };

com.realdecoy.createMarker = function( opts, type )
{
	if( typeof(type) == "undefined" ) type = "";

	if( !opts ) opts = {};

	if( typeof( opts.lat ) == "undefined" || typeof( opts.lon ) == "undefined" ) return null;

	if( typeof( com.realdecoy.marker_count ) == "undefined" ) com.realdecoy.marker_count = 0;
	else com.realdecoy.marker_count += 1;

	var _marker = null;

	if( typeof( opts.icon ) == "undefined" )
	{
		_marker = new GMarker( new GLatLng( opts.lat, opts.lon ), { title: opts.title || "Marker " + com.realdecoy.marker_count, clickable: ( opts.fn_onclick ? true : false ) } );
	}
	else
	{

		var _h_icon = opts.height || 16;
		var _w_icon = opts.width || 16;

	    var	_icon = new GIcon();

		    	_icon.iconSize = new GSize( _w_icon, _h_icon );
		    	_icon.iconAnchor = new GPoint( (_w_icon / 2), _h_icon );
	    		_icon.image = opts.icon || "";

		_marker = new GMarker( new GLatLng( opts.lat, opts.lon ), { title: opts.title || "Marker " + com.realdecoy.marker_count, icon: _icon, clickable: ( opts.fn_onclick ? true : false ) } );
	}

	if( opts.fn_onclick && _marker )
	{
        switch( type )
        {
           	case 'lock': GEvent.addListener( _marker, "click", function() { com.realdecoy.map.setCenter( new GLatLng( opts.lat, opts.lon ), 14 ); showLockDetails(opts.id); } ); break;
			case 'boat': GEvent.addListener( _marker, "click", function() { com.realdecoy.map.setCenter( new GLatLng( opts.lat, opts.lon ), 14 ); showVesselDetails(opts.id); } ); break;
			case'port': GEvent.addListener( _marker, "click", function() { com.realdecoy.map.setCenter( new GLatLng( opts.lat, opts.lon ), 14 ); showPortDetails(opts.id); } ); break;
        }
	}

	return _marker;
}

com.realdecoy.createLabel = function( opts )
{
	if( !opts ) opts = {};

	if( typeof( opts.lat ) == "undefined" || typeof( opts.lon ) == "undefined" ) return null;

	if( typeof( com.realdecoy.label_count ) == "undefined" ) com.realdecoy.label_count = 0;
	else com.realdecoy.label_count += 1;

	var _desc = opts.desc || "Label " + com.realdecoy.label_count;
	var _class = opts.className || "";
	var _pixel = opts.pixelOffset || "";
	var _opacity = opts.percentOpacity || "";
	var _overlap = opts.overlap || 1;

	return new ELabel( new GLatLng( opts.lat, opts.lon ), _desc, _class, _pixel, _opacity, _overlap );
}

com.realdecoy.init_locks = function()
{
	var _map = com.realdecoy.map;

	if( typeof( com.realdecoy.managers.lock ) != "undefined" )
	{
		var _i = 0;
		var _imax = com.realdecoy.arrays.locks.length;

		for( ; _i < _imax; _i++ )
		{
			var _lock = com.realdecoy.arrays.locks[_i];
			if( !_lock ) break;

			_map.removeOverlay( _lock );
		}

		_imax = com.realdecoy.arrays.lockLabels.length;

		for( ; _i < _imax; _i++ )
		{
			var _label = com.realdecoy.arrays.locksLabels[_i];
			if( !_label ) break;

			com.realdecoy.managers.lock.labels.removeMarker( _label );

			_map.removeOverlay( _label );
		}
	}

	com.realdecoy.managers.lock = null;
	com.realdecoy.managers.lock = { locks: null, labels: null };

	com.realdecoy.managers.lock.locks = new GMarkerManager( _map );
	com.realdecoy.managers.lock.labels = new MarkerManager( _map );

	var _i = 0;
	var _imax = sw_loc_locks.length;

	var _locks 	= com.realdecoy.arrays.locks = [];
	var _labels 	= com.realdecoy.arrays.lockLabels = [];
	
	var _yCord = new Array();
	var _labelRange = 0.5;
	var _moveRange = 0.8;
	var _newLat = 0;
	var _labelLat = 0;
	var _moveCount = 0;
	var _moves = 0;

   	// IMPORTANT NOTE!!!  A longitude of 122 degress 45 minutes equates to 122.75
	// Decimal value = Degrees + (Minutes/60) + (Seconds/3600)

	for( ; _i < _imax; _i++ )
	{		
	    var 	_lock = sw_loc_locks[_i];

	    var 	_lat 	= _lock.lat[0] + (_lock.lat[1] / 60) + (_lock.lat[2] / 3600);
	    var 	_lon	= _lock.lon[0] + (_lock.lon[1] / 60) + (_lock.lon[2] / 3600);
				_lon *= -1;	// To get it on our hemisphere
								
        if (com.realdecoy.lang == 'en') {
            _locks.push( com.realdecoy.createMarker( { id: _lock.id, lat: _lat, lon: _lon, desc: _lock.desc_en, title: _lock.title_en, icon: "/images/map/lock.gif", fn_onclick: true  }, 'lock' ) );
            _labels.push( com.realdecoy.createLabel( { lat: _lat, lon: _lon, desc: '<div class="elabel"><a href="" onClick="showLockDetails(' + _lock.id + '); return false;">' + _lock.desc_en + '</a></div>', pixelOffset: { width: 15, height: 0 }  } ) );
        } else {
            _locks.push( com.realdecoy.createMarker( { id: _lock.id, lat: _lat, lon: _lon, desc: _lock.desc_fr, title: _lock.title_fr, icon: "/images/map/lock.gif", fn_onclick: true  }, 'lock' ) );
            _labels.push( com.realdecoy.createLabel( { lat: _lat, lon: _lon, desc: '<div class="elabel"><a href="" onClick="showLockDetails(' + _lock.id + '); return false;">' + _lock.desc_fr + '</a></div>', pixelOffset: { width: 15, height: 0 }  } ) );
        }
	}

	com.realdecoy.managers.lock.locks.addMarkers( com.realdecoy.arrays.locks, 3 );
	com.realdecoy.managers.lock.labels.addMarkers( com.realdecoy.arrays.lockLabels, 3 );

	com.realdecoy.managers.lock.locks.refresh();
	com.realdecoy.managers.lock.labels.refresh();
}

com.realdecoy.init_boats = function()
{
	var _map = com.realdecoy.map;

	if( typeof( com.realdecoy.managers.boat ) != "undefined" )
	{
		var _i = 0;
		var _imax = com.realdecoy.arrays.boats.length;

		for( ; _i < _imax; _i++ )
		{
			var _boat = com.realdecoy.arrays.boats[_i];
			if( !_boat ) break;

			_map.removeOverlay( _boat );
		}

		_imax = com.realdecoy.arrays.boatLabels.length;

		for( ; _i < _imax; _i++ )
		{
			var _label = com.realdecoy.arrays.boatLabels[_i];
			if( !_label ) break;

			com.realdecoy.managers.boat.labels.removeMarker( _label );

			_map.removeOverlay( _label );
		}
	}

	com.realdecoy.managers.boat = null;
	com.realdecoy.managers.boat = { boats: null, labels: null };

    com.realdecoy.managers.boat.boats = new GMarkerManager(  _map );
	com.realdecoy.managers.boat.labels = new MarkerManager(  _map );

	var _i = 0;
	var _imax = sw_loc_boats.length;

	var _boats 	= com.realdecoy.arrays.boats = [];
	var _labels = com.realdecoy.arrays.boatLabels = [];

   	// IMPORTANT NOTE!!!  A longitude of 122 degress 45 minutes equates to 122.75
	// Decimal value = Degrees + (Minutes/60) + (Seconds/3600)

	for( ; _i < _imax; _i++ )
	{
	    var _boat = sw_loc_boats[_i];

	    var 	_lat 	= _boat.lat;//_boat.lat[0] + (_boat.lat[1] / 60) + (_boat.lat[2] / 3600);
	    var 	_lon	= _boat.lon;//_boat.lon[0] + (_boat.lon[1] / 60) + (_boat.lon[2] / 3600);
				//_lon *= -1;	// To get it on our hemisphere

        _boats.push( com.realdecoy.createMarker( { id: _boat.id, lat: _lat, lon: _lon, desc: _boat.name, title: _boat.name, icon: _boat.icon, fn_onclick: true  }, 'boat' ) );
	    _labels.push( com.realdecoy.createLabel( { lat: _lat, lon: _lon, desc: '<div class="elabel"><a href="" onClick="showVesselDetails(' + _boat.id + '); return false;">' + _boat.name + '</a></div>', pixelOffset: { width: 10, height: -10 }  } ) );
	}

	com.realdecoy.managers.boat.boats.addMarkers( com.realdecoy.arrays.boats, 3 );
	com.realdecoy.managers.boat.labels.addMarkers( com.realdecoy.arrays.boatLabels, 3 );

	com.realdecoy.managers.boat.boats.refresh();
	com.realdecoy.managers.boat.labels.refresh();
}

com.realdecoy.init_ports = function()
{
	var _map = com.realdecoy.map;

	if( typeof( com.realdecoy.managers.port ) != "undefined" )
	{
		var _i = 0;
		var _imax = com.realdecoy.arrays.ports.length;

		for( ; _i < _imax; _i++ )
		{
			var _port = com.realdecoy.arrays.ports[_i];
			if( !_port ) break;

			_map.removeOverlay( _port );
		}

		_imax = com.realdecoy.arrays.portLabels.length;

		for( ; _i < _imax; _i++ )
		{
			var _label = com.realdecoy.arrays.portLabels[_i];
			if( !_label ) break;

			com.realdecoy.managers.port.labels.removeMarker( _label );

			_map.removeOverlay( _label );
		}
	}

	com.realdecoy.managers.port = null;
	com.realdecoy.managers.port = { ports: null, labels: null };

	com.realdecoy.managers.port.ports = new GMarkerManager( _map );
	com.realdecoy.managers.port.labels = new MarkerManager( _map );

	var _i = 0;
	var _imax = sw_loc_ports.length;

	var _ports 	= com.realdecoy.arrays.ports = [];
	var _labels 	= com.realdecoy.arrays.portLabels = [];

   	// IMPORTANT NOTE!!!  A longitude of 122 degress 45 minutes equates to 122.75
	// Decimal value = Degrees + (Minutes/60) + (Seconds/3600)

	for( ; _i < _imax; _i++ )
	{
	    var _port = sw_loc_ports[_i];

	    var 	_lat 	= _port.lat[0] + (_port.lat[1] / 60) + (_port.lat[2] / 3600);
	    var 	_lon	= _port.lon[0] + (_port.lon[1] / 60) + (_port.lon[2] / 3600);
				_lon *= -1;	// To get it on our hemisphere


        if (com.realdecoy.lang == 'en') {
            _ports.push( com.realdecoy.createMarker( { id: _port.id, country: _port.country, lat: _lat, lon: _lon, desc: _port.desc_en, title: _port.title_en, icon: _port.icon, fn_onclick: true  }, 'port' ) );
            _labels.push( com.realdecoy.createLabel( { country: _port.country, lat: _lat, lon: _lon, desc: '<div class="elabel"><a href="" onClick="showPortDetails(' + _port.id + '); return false;">' + _port.desc_en + '</a></div>', pixelOffset: { width: 15, height: 0 }  } ) );
        } else {
            _ports.push( com.realdecoy.createMarker( { id: _port.id, country: _port.country, lat: _lat, lon: _lon, desc: _port.desc_fr, title: _port.title_fr, icon: _port.icon, fn_onclick: true  }, 'port' ) );
            _labels.push( com.realdecoy.createLabel( { country: _port.country, lat: _lat, lon: _lon, desc: '<div class="elabel"><a href="" onClick="showPortDetails(' + _port.id + '); return false;">' + _port.desc_fr + '</a></div>', pixelOffset: { width: 15, height: 0 }  } ) );
        }
	}

	com.realdecoy.managers.port.ports.addMarkers( com.realdecoy.arrays.ports, 3 );
	com.realdecoy.managers.port.labels.addMarkers( com.realdecoy.arrays.portLabels, 3 );

	com.realdecoy.managers.port.ports.refresh();
	com.realdecoy.managers.port.labels.refresh();
}

com.realdecoy.showall = function(kind, state)
{
	if( typeof(state) == "undefined" ) state = "";
	if( typeof(kind) == "undefined" ) return;

    switch(kind)
    {
        case "locks": //showTab(getActiveTab(), 'locks');

			com.realdecoy.managers.lock.locks.refresh();

            var _imax = com.realdecoy.arrays.lockLabels.length;
            var _i = 0;

			var _labels = com.realdecoy.arrays.lockLabels;

            for( ; _i < _imax; _i++ )
            {
            	_labels[_i].hide();
           	}

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'locksButton' ).innerHTML = '<input type="button" name="locks" id="locks" class="button selected" onClick="com.realdecoy.hideall(\'locks\');" />';
                document.getElementById( 'lockNamesCheckbox' ).innerHTML = '<input type="checkbox" name="lock_names" id="lock_names" onclick="com.realdecoy.showLabels(\'locks\');" /> <label for="lock_names">Display Lock Names</label>';
            } else {
                document.getElementById( 'locksButton' ).innerHTML = '<input type="button" name="locks" id="locks" class="button selected" onClick="com.realdecoy.hideall(\'locks\');" />';
                document.getElementById( 'lockNamesCheckbox' ).innerHTML = '<input type="checkbox" name="lock_names" id="lock_names" onclick="com.realdecoy.showLabels(\'locks\');" /> <label for="lock_names">Noms des &eacute;cluses</label>';
            }

			com.realdecoy.ctx.showLocks = true;
		break;    
		
		case 'ports': //showTab(getActiveTab(), 'ports');

			com.realdecoy.managers.port.ports.refresh();

            var _imax = com.realdecoy.arrays.portLabels.length;
            var _i = 0;

			var _labels = com.realdecoy.arrays.portLabels;
			var _cdnState = document.getElementById( 'can_port_names' ).checked;
			var _usaState = document.getElementById( 'us_port_names' ).checked;

            for( ; _i < _imax; _i++ )
            {
				_labels[_i].hide();			
            }

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'portsButton' ).innerHTML = '<input type="button" name="ports" id="ports" class="button selected" onClick="com.realdecoy.hideall(\'ports\');" />';
                document.getElementById( 'cdnports' ).innerHTML = '<input type="checkbox" name="can_port_names" id="can_port_names" onclick="com.realdecoy.hideall(\'cdnports\');" checked /> <label for="can_port_names">Canadian Ports</label>';
				document.getElementById( 'usaports' ).innerHTML = '<input type="checkbox" name="us_port_names" id="us_port_names" onclick="com.realdecoy.hideall(\'usaports\');" checked /> <label for="us_port_names">US Ports</label>';
				document.getElementById( 'portnames' ).innerHTML = '<input type="checkbox" name="port_names" id="port_names" onclick="com.realdecoy.showLabels(\'portnames\');" /> <label for="port_names">Port Names</label>';
            } else {
                document.getElementById( 'portsButton' ).innerHTML = '<input type="button" name="ports" id="ports" class="button selected" onClick="com.realdecoy.hideall(\'ports\');" />';
                document.getElementById( 'cdnports' ).innerHTML = '<input type="checkbox" name="can_port_names" id="can_port_names" onclick="com.realdecoy.hideall(\'cdnports\');" checked /> <label for="can_port_names">Ports canadiens</label>';
				document.getElementById( 'usaports' ).innerHTML = '<input type="checkbox" name="us_port_names" id="us_port_names" onclick="com.realdecoy.hideall(\'usaports\');" checked /> <label for="us_port_names">Ports am&eacute;ricains</label>';
				document.getElementById( 'portnames' ).innerHTML = '<input type="checkbox" name="port_names" id="port_names" onclick="com.realdecoy.showLabels(\'portnames\');" /> <label for="port_names">Noms des ports</label>';
            }

			com.realdecoy.ctx.showPorts = true;

			if (_cdnState == false) {				
				com.realdecoy.ctx.showCdnPorts = false;
			} else {
				com.realdecoy.ctx.showCdnPorts = true;
			}
			if (_usaState == false) {				
				com.realdecoy.ctx.showUsaPorts = false;
			} else {
				com.realdecoy.ctx.showUsaPorts = true;
			}
			
		break;
		
		case 'cdnports': //showTab(getActiveTab(), 'ports');
		
			com.realdecoy.managers.port.ports.refresh();

            var _imax = com.realdecoy.arrays.portLabels.length;
            var _i = 0;

			var _labels = com.realdecoy.arrays.portLabels;
			
			var _cdnState = document.getElementById( 'can_port_names' ).checked;
			var _usaState = document.getElementById( 'us_port_names' ).checked;
			var _portNamesState = document.getElementById( 'port_names' ).checked;

			if (_cdnState == false && _usaState == false) {
				document.getElementById( 'port_names' ).disabled=true;
				document.getElementById( 'port_names' ).checked=false;
            } else {
				document.getElementById( 'port_names' ).disabled=false;
			}

            for( ; _i < _imax; _i++ )
            {
				if (_portNamesState == false) {
					if (sw_loc_ports[_i].country == "cdn") {
						_labels[_i].hide();
					}
				} else {
					if (sw_loc_ports[_i].country == "cdn") {
						_labels[_i].show();
					}
				}
				
			}			

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'cdnports' ).innerHTML = '<input type="checkbox" name="can_port_names" id="can_port_names" onclick="com.realdecoy.hideall(\'cdnports\');" checked /> <label for="can_port_names">Canadian Ports</label>';
            } else {
                document.getElementById( 'cdnports' ).innerHTML = '<input type="checkbox" name="can_port_names" id="can_port_names" onclick="com.realdecoy.hideall(\'cdnports\');" checked /> <label for="can_port_names">Ports canadiens</label>';
            }

			com.realdecoy.ctx.showCdnPorts = true;
			
			if (_usaState == false) {
				com.realdecoy.hideall( "usaports" );
				com.realdecoy.ctx.showUsaPorts = false;
			}
		break;
		
		case 'usaports': //showTab(getActiveTab(), 'ports');

			com.realdecoy.managers.port.ports.refresh();

            var _imax = com.realdecoy.arrays.portLabels.length;
            var _i = 0;

			var _labels = com.realdecoy.arrays.portLabels;
			
			var _cdnState = document.getElementById( 'can_port_names' ).checked;
			var _usaState = document.getElementById( 'us_port_names' ).checked;	
			var _portNamesState = document.getElementById( 'port_names' ).checked;

			if (_cdnState == false && _usaState == false) {
				document.getElementById( 'port_names' ).disabled=true;
				document.getElementById( 'port_names' ).checked=false;
            } else {
				document.getElementById( 'port_names' ).disabled=false;
			}

            for( ; _i < _imax; _i++ )
            {
				if (_portNamesState == false) {
					if (sw_loc_ports[_i].country == "usa") {
						_labels[_i].hide();
					}
				} else {
					if (sw_loc_ports[_i].country == "usa") {
						_labels[_i].show();
					}
				}
            }

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'usaports' ).innerHTML = '<input type="checkbox" name="us_port_names" id="us_port_names" onclick="com.realdecoy.hideall(\'usaports\');" checked /> <label for="us_port_names">US Ports</label>';
            } else {
                document.getElementById( 'usaports' ).innerHTML = '<input type="checkbox" name="us_port_names" id="us_port_names" onclick="com.realdecoy.hideall(\'usaports\');" checked /> <label for="us_port_names">Ports am&eacute;ricains</label>';
            }

			com.realdecoy.ctx.showUsaPorts = true;
			
			if (_cdnState == false) {
				com.realdecoy.hideall( "cdnports" );
				com.realdecoy.ctx.showCdnPorts = false;
			}
		break;

        case 'boats': //showTab(getActiveTab(), 'boats');

            if( state.match( /^$/ ) ) showAllShipDetails();
			else if( state.match( /^checked$/ ) ) com.realdecoy.displayCheckedBoats();

			com.realdecoy.managers.boat.boats.refresh();

            var _imax = com.realdecoy.arrays.boatLabels.length;
            var _i = 0;

			var _labels = com.realdecoy.arrays.boatLabels;

            for( ; _i < _imax; _i++ )
            {
                _labels[_i].hide();
            }

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'transitButton' ).innerHTML = '<input type="button" name="transit" id="transit" class="button selected" onClick="com.realdecoy.hideall(\'boats\');" />';
                document.getElementById( 'vesselNamesCheckbox' ).innerHTML = '<input type="checkbox" name="vessel_names" id="vessel_names" onclick="com.realdecoy.showLabels(\'boats\');" /> <label for="vessel_names">Display Vessel Names</label>';
            } else {
                document.getElementById( 'transitButton' ).innerHTML = '<input type="button" name="transit" id="transit" class="button selected" onClick="com.realdecoy.hideall(\'boats\');" />';
                document.getElementById( 'vesselNamesCheckbox' ).innerHTML = '<input type="checkbox" name="vessel_names" id="vessel_names" onclick="com.realdecoy.showLabels(\'boats\');" /> <label for="vessel_names">Noms des navires</label>';
            }

			com.realdecoy.ctx.showBoats = true;
		break

        case 'all':

            com.realdecoy.showall( 'locks' );
            com.realdecoy.showall( 'ports' );
            com.realdecoy.showall( 'boats' );

		break
    }
}

com.realdecoy.hideall = function(kind)
{
    switch(kind)
    {
        case "locks":

            var _imax = com.realdecoy.arrays.locks.length;
            var _i = 0;

			var _locks = com.realdecoy.arrays.locks;
			var _labels = com.realdecoy.arrays.lockLabels;

            for( ; _i < _imax; _i++ )
            {
            	_locks[_i].hide();
            	_labels[_i].hide();
           	}

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'locksButton' ).innerHTML = '<input type="button" name="locks" id="locks" class="button" onClick="com.realdecoy.showall(\'locks\');" />';
                document.getElementById( 'lockNamesCheckbox' ).innerHTML = '<input type="checkbox" name="lock_names" id="lock_names" onclick="com.realdecoy.showLabels(\'locks\');" disabled /> <label for="lock_names" class="disabled">Display Lock Names</label>';
            } else {
                document.getElementById( 'locksButton' ).innerHTML = '<input type="button" name="locks" id="locks" class="button" onClick="com.realdecoy.showall(\'locks\');" />';
                document.getElementById( 'lockNamesCheckbox' ).innerHTML = '<input type="checkbox" name="lock_names" id="lock_names" onclick="com.realdecoy.showLabels(\'locks\');" disabled /> <label for="lock_names" class="disabled">Noms des &eacute;cluses</label>';
            }

			com.realdecoy.ctx.showLockLabes = false;
			com.realdecoy.ctx.showLocks = false;

		break;        
		
		case "ports":

            var _imax = com.realdecoy.arrays.ports.length;
            var _i = 0;

			var _ports = com.realdecoy.arrays.ports;
			var _labels = com.realdecoy.arrays.portLabels;

            for( ; _i < _imax; _i++ )
            {
	            _ports[_i].hide();
    	        _labels[_i].hide();			
            }

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'portsButton' ).innerHTML = '<input type="button" name="ports" id="ports" class="button" onClick="com.realdecoy.showall(\'ports\');" />';
                document.getElementById( 'cdnports' ).innerHTML = '<input type="checkbox" name="can_port_names" id="can_port_names" onclick="com.realdecoy.showall(\'cdnports\');" disabled /> <label for="can_port_names" class="disabled">Canadian Ports</label>';
				document.getElementById( 'usaports' ).innerHTML = '<input type="checkbox" name="us_port_names" id="us_port_names" onclick="com.realdecoy.showall(\'usaports\');" disabled /> <label for="us_port_names" class="disabled">US Ports</label>';
				document.getElementById( 'portnames' ).innerHTML = '<input type="checkbox" name="port_names" id="port_names" onclick="com.realdecoy.showLabels(\'portnames\');" disabled /> <label for="port_names" class="disabled">Port Names</label>';
            } else {
                document.getElementById( 'portsButton' ).innerHTML = '<input type="button" name="ports" id="ports" class="button" onClick="com.realdecoy.showall(\'ports\');" />';
                document.getElementById( 'cdnports' ).innerHTML = '<input type="checkbox" name="can_port_names" id="can_port_names" onclick="com.realdecoy.showall(\'cdnports\');" disabled /> <label for="can_port_names" class="disabled">Ports canadiens</label>';
				document.getElementById( 'usaports' ).innerHTML = '<input type="checkbox" name="us_port_names" id="us_port_names" onclick="com.realdecoy.showall(\'usaports\');" disabled /> <label for="us_port_names" class="disabled">Ports am&eacute;ricains</label>';
				document.getElementById( 'portnames' ).innerHTML = '<input type="checkbox" name="port_names" id="port_names" onclick="com.realdecoy.showLabels(\'portnames\');" disabled /> <label for="port_names" class="disabled">Noms des ports</label>';
            }

			com.realdecoy.ctx.showPortLabels = false;
			com.realdecoy.ctx.showPorts = false;
			com.realdecoy.ctx.showCdnPortLabels = false;
 			com.realdecoy.ctx.showCdnPorts = false;
			com.realdecoy.ctx.showUsaPortLabels = false;
			com.realdecoy.ctx.showUsaPorts = false;

		break;
		
		case "cdnports":
            var _imax = com.realdecoy.arrays.ports.length;
            var _i = 0;

			var _ports = com.realdecoy.arrays.ports;
			var _labels = com.realdecoy.arrays.portLabels;
			
			var _cdnState = document.getElementById( 'can_port_names' ).checked;
			var _usaState = document.getElementById( 'us_port_names' ).checked;			

			if (_cdnState == false && _usaState == false) {
				document.getElementById( 'port_names' ).disabled=true;
				document.getElementById( 'port_names' ).checked=false;
            } else {
				document.getElementById( 'port_names' ).disabled=false;
			}

            for( ; _i < _imax; _i++ )
            {
				if (sw_loc_ports[_i].country == "cdn") {
	                _ports[_i].hide();
    	            _labels[_i].hide();
				}
            }

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'cdnports' ).innerHTML = '<input type="checkbox" name="can_port_names" id="can_port_names" onclick="com.realdecoy.showall(\'cdnports\');" /> <label for="can_port_names">Canadian Ports</label>';
            } else {
                document.getElementById( 'cdnports' ).innerHTML = '<input type="checkbox" name="can_port_names" id="can_port_names" onclick="com.realdecoy.showall(\'cdnports\');" /> <label for="can_port_names">Ports canadiens</label>';
            }

			com.realdecoy.ctx.showCdnPortLabels = false;
			com.realdecoy.ctx.showCdnPorts = false;
			
			if ( _usaState == false ) com.realdecoy.hideall('ports');

		break;
		
		case "usaports":

            var _imax = com.realdecoy.arrays.ports.length;
            var _i = 0;

			var _ports = com.realdecoy.arrays.ports;
			var _labels = com.realdecoy.arrays.portLabels;
			
			var _cdnState = document.getElementById( 'can_port_names' ).checked;
			var _usaState = document.getElementById( 'us_port_names' ).checked;			

			if (_cdnState == false && _usaState == false) {
				document.getElementById( 'port_names' ).disabled=true;
				document.getElementById( 'port_names' ).checked=false;
            } else {
				document.getElementById( 'port_names' ).disabled=false;
			}

            for( ; _i < _imax; _i++ )
            {
				if (sw_loc_ports[_i].country == "usa") {
	                _ports[_i].hide();
    	            _labels[_i].hide();
				}
            }

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'usaports' ).innerHTML = '<input type="checkbox" name="us_port_names" id="us_port_names" onclick="com.realdecoy.showall(\'usaports\');" /> <label for="us_port_names">US Ports</label>';
            } else {
                document.getElementById( 'usaports' ).innerHTML = '<input type="checkbox" name="us_port_names" id="us_port_names" onclick="com.realdecoy.showall(\'usaports\');" /> <label for="us_port_names">Ports am&eacute;ricains</label>';
			}

			com.realdecoy.ctx.showUsaPortLabels = false;
			com.realdecoy.ctx.showUsaPorts = false;
			
			if ( _cdnState == false ) com.realdecoy.hideall('ports');

		break;

        case "boats":

            hideAllShipDetails();

            var _imax = com.realdecoy.arrays.boats.length;
            var _i = 0;

			var _boats = com.realdecoy.arrays.boats;
			var _labels = com.realdecoy.arrays.boatLabels;

            for( ; _i < _imax; _i++ )
            {
                _boats[_i].hide();
                _labels[_i].hide();
            }

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'transitButton' ).innerHTML = '<input type="button" name="transit" id="transit" class="button" onClick="com.realdecoy.showall(\'boats\');" />';
                document.getElementById( 'vesselNamesCheckbox' ).innerHTML = '<input type="checkbox" name="vessel_names" id="vessel_names" onclick="com.realdecoy.showLabels(\'boats\');" disabled /> <label for="vessel_names" class="disabled">Display Vessel Names</label>';
            } else {
                document.getElementById( 'transitButton' ).innerHTML = '<input type="button" name="transit" id="transit" class="button" onClick="com.realdecoy.showall(\'boats\');" />';
                document.getElementById( 'vesselNamesCheckbox' ).innerHTML = '<input type="checkbox" name="vessel_names" id="vessel_names" onclick="com.realdecoy.showLabels(\'boats\');" disabled /> <label for="vessel_names" class="disabled">Noms des navires</label>';
            }

			com.realdecoy.ctx.showBoatLabels = false;
			com.realdecoy.ctx.showBoats = false;

		break;

        case "all":

            com.realdecoy.hideall( 'locks' );
            com.realdecoy.hideall( 'ports' );
            com.realdecoy.hideall( 'boats' );

		break
    }
};

function getActiveTab() {
    var activeTab = 'tab' + 0;

    for (i = 1; i < 6; i++) {
        if (document.getElementById('tab' + i).className == "active") {
            activeTab = 'tab' + i;
        }
    }

    return activeTab;
}

showTab = function(id, action, zoom)
{
	var _map = com.realdecoy.map;

    // Set all classes to blank
    for (i = 1; i < 6; i++) {
        document.getElementById('tab' + i).className="";
    }

    switch(id) {
       case "tab1":
            _map.setCenter(new GLatLng(45.33670, -78.22266), 5);
            document.getElementById(id).className="active";
            break
        case "tab2":
            _map.setCenter(new GLatLng(45.1201, -83.6279), 6);
            document.getElementById(id).className="active";
            break
        case "tab3":
            _map.setCenter(new GLatLng(44.3553, -76.5967), 7);
            document.getElementById(id).className="active";
            break
        case "tab4":
            _map.setCenter(new GLatLng(43.02573, -79.22928), 10);
            document.getElementById(id).className="active";
            break
        case "tab5":
            _map.setCenter(new GLatLng(44.8870, -75.0146), 8);
            document.getElementById(id).className="active";
            break
        default:
            _map.setCenter(new GLatLng(45.53, -73.14), 4);
            break
    }

    if( zoom == 1)
    {
        switch( action )
        {
            case "hide":
			break

            case "boats":
                window.setTimeout(function() { com.realdecoy.showall('locks'); }, com.realdecoy.timeout_interval);
                window.setTimeout(function() { com.realdecoy.showall('ports'); }, com.realdecoy.timeout_interval);
			break

            case "ports":
                window.setTimeout(function() { com.realdecoy.showall('locks'); }, com.realdecoy.timeout_interval);
                window.setTimeout(function() { com.realdecoy.showall('boats'); }, com.realdecoy.timeout_interval);
			break

            case "locks":
                window.setTimeout(function() { com.realdecoy.showall('ports'); }, com.realdecoy.timeout_interval);
                window.setTimeout(function() { com.realdecoy.showall('boats'); }, com.realdecoy.timeout_interval);
			break
        }

        showAllShipDetails();
    }

    com.realdecoy.clearPreloadPage();
}

// Show all labels
com.realdecoy.showLabels = function( type )
{
    switch (type)
    {
        case 'boats':
            var _found = -1;

            var _i = 0;
            var _imax = com.realdecoy.arrays.boats.length;

            var _boats =  sw_loc_boats;
            var _labels = com.realdecoy.arrays.boatLabels;

            for( ; _i < _imax; _i++ )
            {
                // If the page with the checkboxes is showing
               	var _checked = document.getElementById( 'showVessel_' + _boats[_i].id );

                if( _checked && typeof( _checked.checked )  )
                {
                    if( _checked.checked == true )
                    {
                        _labels[_i].show();
                        _found = 1;
                    }
                }
            }

            // If no checkboxes are selected, show all labels
            for( _i = 0; _found == -1 && _i < _imax; _i++ )
            {
                _labels[_i].show();
            }

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'vesselNamesCheckbox' ).innerHTML = '<input type="checkbox" name="vessel_names" id="vessel_names" onclick="com.realdecoy.hideLabels(\'boats\');" checked /> <label for="vessel_names">Display Vessel Names</label>';
            } else {
                document.getElementById( 'vesselNamesCheckbox' ).innerHTML = '<input type="checkbox" name="vessel_names" id="vessel_names" onclick="com.realdecoy.hideLabels(\'boats\');" checked /> <label for="vessel_names">Noms des navires</label>';
            }
			com.realdecoy.ctx.showBoatLabels = true;

            break;

        case 'locks':
           var _i = 0;
           var _imax = com.realdecoy.arrays.locks.length;

           var _labels = com.realdecoy.arrays.lockLabels;

           for( ; _i < _imax; _i++ )
           {
               _labels[_i].show();
           }

           if (com.realdecoy.lang == 'en') {
               document.getElementById( 'lockNamesCheckbox' ).innerHTML = '<input type="checkbox" name="lock_names" id="lock_names" onclick="com.realdecoy.hideLabels(\'locks\');" checked /> <label for="lock_names">Display Lock Names</label>';
           } else {
               document.getElementById( 'lockNamesCheckbox' ).innerHTML = '<input type="checkbox" name="lock_names" id="lock_names" onclick="com.realdecoy.hideLabels(\'locks\');" checked /> <label for="lock_names">Noms des &eacute;cluses</label>';
           }
           com.realdecoy.ctx.showLockLabels = true;

           break;
        case 'portnames':

            var _i = 0;
            var _imax = com.realdecoy.arrays.ports.length;

            var _labels = com.realdecoy.arrays.portLabels;
			
			var _cdnState = document.getElementById( 'can_port_names' ).checked;
			var _usaState = document.getElementById( 'us_port_names' ).checked;			

            for( ; _i < _imax; _i++ )
            {
				if (_cdnState == true && _usaState == false) {
					if (sw_loc_ports[_i].country == "cdn") {
		                _labels[_i].show();
					}
				} else if (_cdnState == false && _usaState == true) {
					if (sw_loc_ports[_i].country == "usa") {
		                _labels[_i].show();
					}
				} else if (_cdnState == true && _usaState == true) {
					_labels[_i].show();
				} else {
					document.getElementById( 'port_names' ).disable=true;
				}
            }

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'portnames' ).innerHTML = '<input type="checkbox" name="port_names" id="port_names" onclick="com.realdecoy.hideLabels(\'portnames\');" checked /> <label for="port_names">Port Names</label>';
            } else {
                document.getElementById( 'portnames' ).innerHTML = '<input type="checkbox" name="port_names" id="port_names" onclick="com.realdecoy.hideLabels(\'portnames\');" checked /> <label for="port_names">Noms des ports</label>';
            }

			com.realdecoy.ctx.showPortLabels = true;
			if (_cdnState == true && _usaState == false) {
				com.realdecoy.ctx.showCdnPortLabels = true;
			} else if (_cdnState == false && _usaState == true) {
				com.realdecoy.ctx.showUsaPortLabels = true;
			}

            break;	
		
    }
};

// Hide all labels
com.realdecoy.hideLabels = function( type, state )
{
	if( typeof(state) == "undefined" ) state = "";

    switch (type)
    {
        case 'boats':
            var _i = 0;
            var _imax = com.realdecoy.arrays.boatLabels.length;

            var _labels = com.realdecoy.arrays.boatLabels;

            for( ; _i < _imax; _i++ )
            {
                _labels[_i].hide();
            }

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'vesselNamesCheckbox' ).innerHTML = '<input type="checkbox" name="vessel_names" id="vessel_names" onclick="com.realdecoy.showLabels(\'boats\');" ' + state + '/> <label for="vessel_names" class="' + state + '">Display Vessel Names</label>';
            } else {
                document.getElementById( 'vesselNamesCheckbox' ).innerHTML = '<input type="checkbox" name="vessel_names" id="vessel_names" onclick="com.realdecoy.showLabels(\'boats\');" ' + state + '/> <label for="vessel_names" class="' + state + '">Noms des navires</label>';
            }

            com.realdecoy.ctx.showBoatLabels = false;

            break;

        case 'portnames':
            var _i = 0;
            var _imax = com.realdecoy.arrays.portLabels.length;

            var _labels = com.realdecoy.arrays.portLabels;

            for( ; _i < _imax; _i++ )
            {
                _labels[_i].hide();
            }

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'portnames' ).innerHTML = '<input type="checkbox" name="port_names" id="port_names" onclick="com.realdecoy.showLabels(\'portnames\');" ' + state + ' /> <label for="port_names" class="' + state + '">Port Names</label>';
            } else {
                document.getElementById( 'portnames' ).innerHTML = '<input type="checkbox" name="port_names" id="port_names" onclick="com.realdecoy.showLabels(\'portnames\');" ' + state + ' /> <label for="port_names" class="' + state + '">Noms des ports</label>';
            }

            com.realdecoy.ctx.showPortLabels = false;

            break;		

        case 'locks':
            var _i = 0;
            var _imax = com.realdecoy.arrays.lockLabels.length;

            var _labels = com.realdecoy.arrays.lockLabels;

            for( ; _i < _imax; _i++ )
            {
                _labels[_i].hide();
            }

            if (com.realdecoy.lang == 'en') {
                document.getElementById( 'lockNamesCheckbox' ).innerHTML = '<input type="checkbox" name="lock_names" id="lock_names" onclick="com.realdecoy.showLabels(\'locks\');" ' + state + '/> <label for="lock_names" class="' + state + '">Display Lock Names</label>';
            } else {
                document.getElementById( 'lockNamesCheckbox' ).innerHTML = '<input type="checkbox" name="lock_names" id="lock_names" onclick="com.realdecoy.showLabels(\'locks\');" ' + state + '/> <label for="lock_names" class="' + state + '">Noms des &eacute;cluses</label>';
            }

            com.realdecoy.ctx.showLockLabels = false;

            break;
    }
};

com.realdecoy.displayCheckedBoats = function()
{
	if( com.realdecoy.ctx.showBoats == false ) return false;

	var _i = 0;
	var _imax = sw_loc_boats.length;

    var _found = -1;

    var _boats = com.realdecoy.arrays.boats;
    var _labels = com.realdecoy.arrays.boatLabels;

    for( ; _i < _imax; _i++ )
    {
    	var _checked = document.getElementById( 'showVessel_' + sw_loc_boats[_i].id );
        if( _checked && typeof(_checked.checked) != "undefined" && _checked.checked == true )
        {
        	if( com.realdecoy.ctx.showBoatLabels ) _labels[_i].show();

            _boats[_i].show();
            _found = _i;
        }
        else
        {
			_labels[_i].hide();
            _boats[_i].hide();
        }
    }

    for( _i = 0; _found == -1 && _i < _imax; _i++ )
    {
        if( com.realdecoy.ctx.showBoatLabels ) _labels[_i].show();

        _boats[_i].show();
    }
};

com.realdecoy.showAllShips = function()
{
    showTab( getActiveTab(),  'boats', 0 );

   	var _i = 0;
   	var _imax = sw_loc_boats.length;

    for( ; _i < _imax; _i++ )
    {
    	var _boat = sw_loc_boats[_i];

    	var _checked = document.getElementById( 'showVessel_' + _boat.id );
		if( _checked && typeof( _checked.checked ) != "undefined" ) _checked.checked = false;

    }

    com.realdecoy.displayCheckedBoats();
}

com.realdecoy.checkStatus = function()
{
	var _map = com.realdecoy.map;

    if( com.realdecoy.ctx.showBoats ) { com.realdecoy.showall( "boats", "checked" ); }
    else { com.realdecoy.hideall( "boats" ); }

    if( com.realdecoy.ctx.showLocks ) { com.realdecoy.showall( "locks" ); }
    else { com.realdecoy.hideall( "locks" ); }

    if( com.realdecoy.ctx.showPorts ) { com.realdecoy.showall( "ports" ); }
    else { com.realdecoy.hideall( "ports" ); }
	
	if( com.realdecoy.ctx.showCdnPorts ) { com.realdecoy.showall( "cdnports" ); }
    else if ( com.realdecoy.ctx.showPorts ) { com.realdecoy.hideall( "cdnports" ); }
	
	if( com.realdecoy.ctx.showUsaPorts ) { com.realdecoy.showall( "usaports" ); }
    else if ( com.realdecoy.ctx.showPorts ) { com.realdecoy.hideall( "usaports" ); }

    if( com.realdecoy.ctx.showBoats == true && com.realdecoy.ctx.showBoatLabels == true ) { com.realdecoy.showLabels('boats'); }
    else { com.realdecoy.hideLabels('boats',( com.realdecoy.ctx.showBoats ? '' : 'disabled' )); }

    if( com.realdecoy.ctx.showLocks == true  && com.realdecoy.ctx.showLockLabels == true ) { com.realdecoy.showLabels('locks'); }
    else { com.realdecoy.hideLabels('locks',( com.realdecoy.ctx.showLocks ? '' : 'disabled' )); }

	if( com.realdecoy.ctx.showPorts == true || com.realdecoy.ctx.showCdnPorts == true || com.realdecoy.ctx.showUsaPorts == true  &&  com.realdecoy.ctx.showPortLabels == true ) { com.realdecoy.showLabels('ports'); }
    else { com.realdecoy.hideLabels('ports',( com.realdecoy.ctx.showPorts ? '' : 'disabled' )); }
	
	//alert(com.realdecoy.ctx.showPortLabels);
	if( com.realdecoy.ctx.showPorts == true  &&  com.realdecoy.ctx.showPortLabels == true || com.realdecoy.ctx.showCdnPortLabels == true || com.realdecoy.ctx.showUsaPortLabels == true ) { com.realdecoy.showLabels('portnames'); }
    else { com.realdecoy.hideLabels('portnames',( com.realdecoy.ctx.showPorts ? '' : 'disabled' )); }
	
    com.realdecoy.displayCheckedBoats();

    com.realdecoy.clearPreloadPage();
}

com.realdecoy.setPreloadPage = function()
{
	var _div = document.getElementById('map');
	var _loading = document.getElementById('loading');

	if( _div ) _div.style.display='none';
	if( _loading) _loading.style.display='';
}

com.realdecoy.clearPreloadPage = function()
{
	var _div = document.getElementById('map');
	var _loading = document.getElementById('loading');

	if( _div ) _div.style.display='';
	if( _loading) _loading.style.display='none';
}

/* =Main load()
___________________________________________________________________________ */

com.realdecoy.fn_preproc1 = function(delay)
{
	com.realdecoy.hideall('locks');
	com.realdecoy.refresh = window.setTimeout( function(){ com.realdecoy.fn_preproc2.apply(this, [delay]) }, delay);
}

com.realdecoy.fn_preproc2 = function(delay)
{
	com.realdecoy.hideall('ports');
	com.realdecoy.refresh = window.setTimeout( com.realdecoy.fn_refresh, 900000 );
}

com.realdecoy.fn_preproc = function(delay)
{
	com.realdecoy.refresh = window.setTimeout( function(){ com.realdecoy.fn_preproc1.apply(this, [delay]) }, delay);
}

com.realdecoy.load = function() {

    if( GBrowserIsCompatible() )
    {
        // Add a new map to the page
        com.realdecoy.map = null;
        com.realdecoy.map = new GMap2(document.getElementById("map"), {mapTypes:[G_PHYSICAL_MAP,G_SATELLITE_MAP,G_NORMAL_MAP]});

        // Set the starting point
        com.realdecoy.map.setCenter(new GLatLng(45.33670, -78.22266), 5);

        // Add some controls to the screen
        com.realdecoy.map.addControl(new GLargeMapControl());
        com.realdecoy.map.addControl(new GMapTypeControl());

        com.realdecoy.map.setMapType(G_PHYSICAL_MAP);

        GEvent.addListener(com.realdecoy.map, "zoomend", function()
        {
        	window.setTimeout( com.realdecoy.checkStatus, com.realdecoy.timeout_interval	);
        });

        GEvent.addListener(com.realdecoy.map, "moveend", function()
        {
        	window.setTimeout( com.realdecoy.checkStatus, com.realdecoy.timeout_interval	);
        });

		// refreshing components
		com.realdecoy.fn_refresh = function(state,delay)
		{
			state = state || null;
			delay = delay || null;			

			GDownloadUrl( com.realdecoy.seedurl + "?noCache=" + new Date().getTime() + "&", function(data, responseCode)
			{
	            // To ensure against HTTP errors that result in null or bad data,
	            // always check status code is equal to 200 before processing the data
	            if( responseCode == 200 )
    	        {
	            	if( data.length > 0 )
	            	{
				        var _loading = document.getElementById( 'loading' );
						var _map = document.getElementById('map');

						if( !_loading || !_map )
						{
							var _error = document.getElementById( "error" );

                            if (com.realdecoy.lang == 'en') {
                                if( !_error ) { alert( "Data request timed out. Please try again later." ); }
                                else { _error.innerHTML = "<p>Data request timed out. Please try again later.</p>"; }
                            } else {
		                        if( !_error ) { alert( "Le temps de r&eacute;ponse est &eacute;coul&eacute;. Veuillez essayer de nouveau." ); }
		                        else { _error.innerHTML = "<p>Le temps de r&eacute;ponse est &eacute;coul&eacute;. Veuillez essayer de nouveau.</p>"; }
                            }

							com.realdecoy.refresh = null;
						}
						else
						{
							// Initialize locks, boats and ports
							com.realdecoy.map.clearOverlays();

							// Update sw_loc_boats
							var _data = eval( data );
                            var _tmp = _data[1].Loc;

                            if( _tmp.length > 0 )
                            {
                                sw_loc_boats.length = 0;
                                var _i = 0;

                                var _imax = _tmp.length;


								for( ; _i < _imax; _i++ )
								{
									if( typeof( _tmp[_i].TRANSIT_ID ) == "undefined" || _tmp[_i].TRANSIT_ID == "" ) continue;

									var _boat = { id: _tmp[_i].TRANSIT_ID };

									_boat.name = ( typeof( _tmp[_i].VESSEL_NAME ) == "undefined" || _tmp[_i].VESSEL_NAME == "" ? "N/A" : _tmp[_i].VESSEL_NAME );
									_boat.icon = ( typeof( _tmp[_i].DIRECTION ) == "undefined" || _tmp[_i].DIRECTION == "" ? ( com.realdecoy.prefix + "/images/map/uboat.gif" ) : ( _tmp[_i].DIRECTION.match( /^U$/ ) ? ( com.realdecoy.prefix + "/images/map/uboat.gif" ) : ( com.realdecoy.prefix + "/images/map/dboat.gif" ) ) );

									_boat.next_e = ( typeof( _tmp[_i].NEXT_LOC_E_NAME ) == "undefined" || _tmp[_i].NEXT_LOC_E_NAME == "" ? "N/A" : _tmp[_i].NEXT_LOC_E_NAME );
									_boat.next_f = ( typeof( _tmp[_i].NEXT_LOC_F_NAME ) == "undefined" || _tmp[_i].NEXT_LOC_F_NAME == "" ? "N/A" :  _tmp[_i].NEXT_LOC_F_NAME );
									_boat.last_e = ( typeof( _tmp[_i].LAST_LOC_E_NAME ) == "undefined" || _tmp[_i].LAST_LOC_E_NAME == "" ? "N/A" : _tmp[_i].LAST_LOC_E_NAME );
									_boat.last_f = ( typeof( _tmp[_i].LAST_LOC_F_NAME ) == "undefined" || _tmp[_i].LAST_LOC_F_NAME == "" ? "N/A" : _tmp[_i].LAST_LOC_F_NAME );

									_boat.ata = ( typeof( _tmp[_i].ATA ) == "undefined" ? "" : _tmp[_i].ATA );
									_boat.eta = ( typeof( _tmp[_i].ETA ) == "undefined" ? "" : _tmp[_i].ETA );

									_boat.length = ( typeof( _tmp[_i].LENGTH_METRIC ) == "undefined" || _tmp[_i].LENGTH_METRIC == "" ? "N/A" : _tmp[_i].LENGTH_METRIC );
									_boat.width = ( typeof( _tmp[_i].WIDTH_METRIC ) == "undefined" || _tmp[_i].WIDTH_METRIC == "" ? "N/A" : _tmp[_i].WIDTH_METRIC );
									_boat.status_e = ( typeof( _tmp[_i].TRANSITSTATUSE ) == "undefined" || _tmp[_i].TRANSITSTATUSE == "" ? "N/A" : _tmp[_i].TRANSITSTATUSE );
									_boat.status_f = ( typeof( _tmp[_i].TRANSITSTATUSF ) == "undefined" || _tmp[_i].TRANSITSTATUSF == "" ? "N/A" : _tmp[_i].TRANSITSTATUSF );
									_boat.country_e = ( typeof( _tmp[_i].COUNTRY_E ) == "undefined" || _tmp[_i].COUNTRY_E == "" ? "N/A" : _tmp[_i].COUNTRY_E );
									_boat.country_f = ( typeof( _tmp[_i].COUNTRY_F ) == "undefined" || _tmp[_i].COUNTRY_F == "" ? "N/A" : _tmp[_i].COUNTRY_F );
									_boat.fleet = ( typeof( _tmp[_i].FLEET ) == "undefined" || _tmp[_i].FLEET == "" ? "N/A" : _tmp[_i].FLEET );
									_boat.no_vessel = ( typeof( _tmp[_i].NO_OF_VESSEL ) == "undefined" || _tmp[_i].NO_OF_VESSEL == "" ? "N/A" : _tmp[_i].NO_OF_VESSEL );

		    						var _lat = parseFloat( _tmp[_i].LAT ); //_tmp[_i].LAT.split( "," );
		    						var _lon = parseFloat( _tmp[_i].LONG ); //_tmp[_i].LONG.split( "," );

                                    _boat.lat = _lat;
                                    _boat.lon = _lon;

									sw_loc_boats.push( _boat );
								}
							}

							com.realdecoy.init_locks();
							com.realdecoy.init_ports();
							com.realdecoy.init_boats();

							com.realdecoy.checkStatus();
                            showAllShipDetails();

							var _date = document.getElementById( "date" );

                            if( !_date ){}
                            else
                            {

                                if( typeof(_data[0].Date) == "undefined" || _data[0].Date == null || _data[0].Date == "" )
                                {
                                    var _now = new Date();

                                    if (com.realdecoy.lang == 'en') {
                                        _date.innerHTML =  "<p class=\"updated\"><span>Last Updated:</span> " + _now.getFullYear() + "/" + ( parseInt( _now.getMonth() ) + 1 ) + "/" + _now.getDate() + " at " + ( _now.getHours() <= 12 ? _now.getHours() : _now.getHours() - 12 )  + ":" +  ( (""+ _now.getMinutes() ).length < 2 ? ("0" + _now.getMinutes()) : _now.getMinutes() )  + ( _now.getHours() < 12 ? " AM" : " PM" ) + "</p>" ;
                                    } else {
                                        _date.innerHTML =  "<p class=\"updated\"><span>Mise &agrave; jour :</span> " + _now.getFullYear() + "/" + ( parseInt( _now.getMonth() ) + 1 ) + "/" + _now.getDate() + ", " + _now.getHours()  + ":" +  ( (""+ _now.getMinutes() ).length < 2 ? ("0" + _now.getMinutes()) : _now.getMinutes() )  + "</p>" ;
                                    }
                                }
                                else
                                {
                                    if (com.realdecoy.lang == 'en') {
                                        _date.innerHTML =  "<p class=\"updated\"><span>Last Updated:</span> " + _data[0].Date + "</p>" ;
                                    } else {
                                        _date.innerHTML =  "<p class=\"updated\"><span>Mise &agrave; jour :</span> " + _data[0].Date + "</p>" ;
                                    }
                                }
			           		}

							if( !state || !delay ) {
							    com.realdecoy.refresh = window.setTimeout( com.realdecoy.fn_refresh, 900000 );
							} else {
								com.realdecoy.refresh = window.setTimeout( function(){ com.realdecoy.fn_preproc.apply( this, [delay] ) }, delay );
							}
						}
					}
					else {}
	            }
				else if( responseCode == -1 )
				{
					var _error = document.getElementById( "error" );
                    if (com.realdecoy.lang == 'en') {
                        if( !_error ) { alert( "Data request timed out. Please try again later." ); }
                        else { _error.innerHTML = "<p>Data request timed out. Please try again later.</p>"; }
                    } else {
                        if( !_error ) { alert( "Le temps de r&eacute;ponse est &eacute;coul&eacute;. Veuillez essayer de nouveau." ); }
                        else { _error.innerHTML = "<p>Le temps de r&eacute;ponse est &eacute;coul&eacute;. Veuillez essayer de nouveau.</p>"; }
                    }
            	}
            	else
            	{
					var _error = document.getElementById( "error" );
                    if (com.realdecoy.lang == 'en') {
                        if( !_error ) { alert( "Request failed (file is inaccessible). Please try again later.." ); }
                        else { _error.innerHTML = "<p>Request failed (file is inaccessible). Please try again later.</p>"; }
                    } else {
                        if( !_error ) { alert( "Votre demande a &eacute;chou&eacute; (fichier inaccessible). Veuillez essayer de nouveau." ); }
                        else { _error.innerHTML = "<p>Votre demande a &eacute;chou&eacute; (fichier inaccessible). Veuillez essayer de nouveau.</p>"; }
                    }
            	}
			} );
		};
		com.realdecoy.refresh = window.setTimeout( function(){ com.realdecoy.fn_refresh.apply( this, ['init', '400'] ) }, 50 );
   	}
}