﻿// JScript File
/* Copyright (c) 2007 WalkIzmir.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 *
 * Version: 1.0
 * 
 * Marker manager is an interface between the map and the user, designed
 * to manage adding and removing many points when the viewport changes.
 *
 *
 * Algorithm: The MM places its markers onto a grid, similar to the map tiles.
 * When the user moves the viewport, the MM computes which grid cells have
 * entered or left the viewport, and shows or hides all the markers in those
 * cells.
 * (If the users scrolls the viewport beyond the markers that are loaded,
 * no markers will be visible until the EVENT_moveend triggers an update.)
 *
 * In practical consequences, this allows 10,000 markers to be distributed over
 * a large area, and as long as only 100-200 are visible in any given viewport,
 * the user will see good performance corresponding to the 100 visible markers,
 * rather than poor performance corresponding to the total 10,000 markers.
 *
 * Note that some code is optimized for speed over space,
 * with the goal of accommodating thousands of markers.
 *
    //  Create our "tiny" marker icon
    //  var blueIcon = new GIcon(G_DEFAULT_ICON);
    //  blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";                              
    //  markerOptions = { icon:blueIcon };                          
    //  var marker = new GMarker(point, markerOptions);  

 */

function createPortMarker() 
{  
    var imagesrc = 'img/cruise_01.png';
    var point = new GLatLng(parseFloat("38.443119"),parseFloat("27.141724"));  

    var baseIcon = new GIcon();
    baseIcon.iconSize = new GSize(150, 82);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);

    var letteredIcon = new GIcon(baseIcon);
    letteredIcon.image = imagesrc;

          // Set up our GMarkerOptions object
    markerOptions = { icon:letteredIcon };

    //var marker = new GMarker(point, {draggable: false});
    var marker = new GMarker(point, markerOptions);
    
    GEvent.addListener(marker, "click", function() 
    {    
        marker.openInfoWindowHtml("<b><div style='width:200px'>Alsancak Port</b></div>"
                                 );
                                 
    });  
    return marker;
}    


function createMarker(place,imagesrc) 
{  
    if (imagesrc == null || imagesrc == '')
        imgagesrc = 'img/tarih1';
    var point = new GLatLng(parseFloat(place.Lat),parseFloat(place.Lng));  
    var marker = null;
    if (imagesrc != 'none')
    {
        var baseIcon = new GIcon();
        baseIcon.iconSize = new GSize(20, 19);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
    
        var letteredIcon = new GIcon(baseIcon);
        letteredIcon.image = imagesrc;

        markerOptions = { icon:letteredIcon };

        marker = new GMarker(point, markerOptions);
    }
    else
    {
        marker = new GMarker(point, {draggable: false});
    }        
    
    GEvent.addListener(marker, "click", function() 
    {    
        marker.openInfoWindowHtml("<b><div style='width:200px'>" + 
                                    place.Name + 
                                    "</b></div>"+
                                    "<div style='width:200px; font-family: Verdana; font-size: 9px;'> " +place.MapText + "</div><br/>"+
                                     place.Html
                                 );
                                 
    });  
    return marker;
}    

function createThePoint(Lat,Lng) 
{  
    var point = new GLatLng(parseFloat(Lat),parseFloat(Lng));  

    var baseIcon = new GIcon();
    baseIcon.iconSize = new GSize(24, 45);
    baseIcon.iconAnchor = new GPoint(10, 10);
    //baseIcon.infoWindowAnchor = new GPoint(9, 2);

    var letteredIcon = new GIcon(baseIcon);
    letteredIcon.image = "img/start.png";

          // Set up our GMarkerOptions object
    markerOptions = { icon:letteredIcon };

    //var marker = new GMarker(point, {draggable: false});
    var marker = new GMarker(point, markerOptions);
    
    return marker;
}    
function createCamera(place) 
{  
    
    var imagesrc = 'img/camera.png';
    var point = new GLatLng(parseFloat(place.Lat),parseFloat(place.Lng));  
    var marker = null;
    if (imagesrc != 'none')
    {
        var baseIcon = new GIcon();
        baseIcon.iconSize = new GSize(20, 19);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
    
        var letteredIcon = new GIcon(baseIcon);
        letteredIcon.image = imagesrc;

        markerOptions = { icon:letteredIcon };

        marker = new GMarker(point, markerOptions);
    }
    else
    {
        marker = new GMarker(point, {draggable: false});
    }        
    
    GEvent.addListener(marker, "click", function() 
    {    
        marker.openInfoWindowHtml(place.CargoString);
                                 
    });  
    return marker;
}    

function MapNewPlace(p)
{
    var s = p.toString().substring(1);
    s = s.substring(0,s.length-1);
    OpenPlaceLookupForm(s);   
}

function markPlace(map,places)
{
    var markers = new Array();
    var marker = null;
    for (var i = 0; i < places.length; i++)
    {        
        marker = createMarker(places[i],places[i].IconFile);
        map.addOverlay(marker);        
        markers.push(marker);
    }    
    return markers;
}

function drawShortRoute(map)
{
    var polyline = new GPolyline([  
                                  new GLatLng(38.44241334065756, 27.144116163253784),  
                                  new GLatLng(38.440799911463316,27.143923044204712), 
                                  new GLatLng(38.43996797294322, 27.143847942352295),
                                  new GLatLng(38.43853936927761, 27.14351534843445),
                                  new GLatLng(38.43790069025867, 27.143354415893554), 
                                  new GLatLng(38.43467359407861, 27.142313718795776),
                                  new GLatLng(38.433875201672286, 27.140650749206543),
                                  new GLatLng(38.43013524602425, 27.141551971435547),
                                  new GLatLng(38.42804246721992, 27.141079902648926),
                                  new GLatLng(38.42887454316429, 27.135082483291626),
                                  new GLatLng(38.42901742392759, 27.135071754455566),
                                  new GLatLng(38.4294124457432, 27.134803533554077),
                                  new GLatLng(38.429723418843885, 27.13425636291504),
                                  new GLatLng(38.429916725772045, 27.134299278259277),
                                  new GLatLng(38.434236580381466, 27.138333320617676),
                                  new GLatLng(38.43613588990794, 27.140564918518066),
                                  new GLatLng(38.44170746982264, 27.14274287223816),
                                  new GLatLng(38.442228470391804, 27.143332958221435),
                                  new GLatLng(38.44241334065756, 27.144116163253784)
                                 ], 
                                 "#ff00ff", 
                                 8);
    map.addOverlay(polyline);
    return polyline;
}    
    var index = 0;
    
    function DrawRoute(map,points)
    {
        var GPoints = new Array();
        var x = 0;
        for(var i = 0; i < points.POINTS.length; i++)
        {
            GPoints.push(new GLatLng(points.POINTS[i].Lat,points.POINTS[i].Lng));
            
        }        
        var polyline = new GPolyline(GPoints,points.Color,points.Thick); 
        map.addOverlay(polyline);
        return polyline;        
    }
