
// **************************************************************************************************************************
// Google Maps API objects
// --------------------------------------------------------------------------------------------------------------------------
// - elementId : String - Id of HTML element for displaying maps
// **************************************************************************************************************************
function GoogleMapObject(elementId)
{
    // -------------------------------------------------------------------------------------------------
    // ATTRIBUTES - filling from constructor, default values
    // -------------------------------------------------------------------------------------------------
    var map = null;      // general map object
    var mgr = null;      // manager for markers
    var icons = {};      // icons for markers
    var allmarkers = []; // array of all markers
    
    var googleMapsNotSupportedMessage = "Váš prohlížeč nepodporuje Google Maps API.";
    var defaultIconFileURL = "http://maps.google.com/intl/cs_ALL/mapfiles/marker.png";
    var defaultIconSize = new GSize(20,34);

    // -------------------------------------------------------------------------------------------------
    // MAP INITIALIZATION AND SETTINGS METHODS
    // -------------------------------------------------------------------------------------------------
    
    // General method for map initialization. (This must be executed only once.) -----------------------
    // - centerLat : Real    - langitude start
    // - centerLng : Real    - longitude start
    // - initZoom  : Integer - zoom at the start - value from 0 to 17 is supported by this parameter
    // - useMarker : Boolean - if true, enables marker object initialization
    function initialize(centerLat, centerLng, initZoom, useMarker)
    {
        if (GBrowserIsCompatible())
        {
            // map options definition
            var mapOptions =
            {
                googleBarOptions : {
                    style : "new"
                }
            }
            
            // map initialization
            this.map = new GMap2(document.getElementById(elementId), mapOptions);
            
            this.map.setCenter(new GLatLng(centerLat, centerLng), initZoom);
            this.map.enableDoubleClickZoom();
            
            // marker manager activation
            if (useMarker == true)
            {
                this.mgr = new MarkerManager(this.map, {trackMarkers:true});
            }
            //window.setTimeout(setupOfficeMarkers, 0);
        }
        else
        {
            // showing message that web browser does not support Google Maps API
            document.getElementById(elementId).innerHTML = googleMapsNotSupportedMessage;
        }
    }
    this.initialize = initialize;

    // Adding map size remote control. ----------------------------------------------------------------
    // - topAnchor    : Boolean - should be tacked to the top?
    // - rightAnchor  : Boolean - should be tacked to the right side?
    // - bottomAnchor : Boolean - should be tacked to the bottom?
    // - leftAnchor   : Boolean - should be tacked to the left side?
    function addSizesControl(topAnchor, rightAnchor, bottomAnchor, leftAnchor)
    {
        this.map.addControl(new GLargeMapControl(),
            getAnchor(topAnchor, rightAnchor, bottomAnchor, leftAnchor, 10, 20));
    }
    this.addSizesControl = addSizesControl;

    // Adding map size remote control. ----------------------------------------------------------------
    // - topAnchor    : Boolean - should be tacked to the top?
    // - rightAnchor  : Boolean - should be tacked to the right side?
    // - bottomAnchor : Boolean - should be tacked to the bottom?
    // - leftAnchor   : Boolean - should be tacked to the left side?
    function addSmallSizesControl(topAnchor, rightAnchor, bottomAnchor, leftAnchor)
    {
        this.map.addControl(new GSmallMapControl(),
            getAnchor(topAnchor, rightAnchor, bottomAnchor, leftAnchor, 10, 20));
    }
    this.addSmallSizesControl = addSmallSizesControl;

    // Adding search control directly into map. --------------------------------------------------------
    // - topAnchor    : Boolean - should be tacked to the top?
    // - rightAnchor  : Boolean - should be tacked to the right side?
    // - bottomAnchor : Boolean - should be tacked to the bottom?
    // - leftAnchor   : Boolean - should be tacked to the left side?
    function addSearch(topAnchor, rightAnchor, bottomAnchor, leftAnchor)
    {
        this.map.addControl(new google.maps.LocalSearch(), 
            getAnchor(topAnchor, rightAnchor, bottomAnchor, leftAnchor, 10, 20));
    }
    this.addSearch = addSearch;

    // Adding overview panel for huge maps. ------------------------------------------------------------
    // - topAnchor    : Boolean - should be tacked to the top?
    // - rightAnchor  : Boolean - should be tacked to the right side?
    // - bottomAnchor : Boolean - should be tacked to the bottom?
    // - leftAnchor   : Boolean - should be tacked to the left side?
    function addOverview(topAnchor, rightAnchor, bottomAnchor, leftAnchor)
    {
        this.map.addControl(new GOverviewMapControl(),
            getAnchor(topAnchor, rightAnchor, bottomAnchor, leftAnchor, 20, 20));
    }
    this.addOverview = addOverview;
    
    // Adding map type switch. -------------------------------------------------------------------------
    // - topAnchor    : Boolean - should be tacked to the top?
    // - rightAnchor  : Boolean - should be tacked to the right side?
    // - bottomAnchor : Boolean - should be tacked to the bottom?
    // - leftAnchor   : Boolean - should be tacked to the left side?
    function addMapTypeSwitch(topAnchor, rightAnchor, bottomAnchor, leftAnchor)
    {
        // creating control
        this.map.addControl(new GMapTypeControl(), 
            getAnchor(topAnchor, rightAnchor, bottomAnchor, leftAnchor, 10, 10));
    }
    this.addMapTypeSwitch = addMapTypeSwitch;
    
    // Private method for creating anchor. -------------------------------------------------------------
    // - topAnchor    : Boolean - should be tacked to the top?
    // - rightAnchor  : Boolean - should be tacked to the right side?
    // - bottomAnchor : Boolean - should be tacked to the bottom?
    // - leftAnchor   : Boolean - should be tacked to the left side?
    // - sizeWidth    : Integer - margin width
    // - sizeHeight   : Integer - margin height
    function getAnchor(topAnchor, rightAnchor, bottomAnchor, leftAnchor, sizeWidth, sizeHeight)
    {
        var anchor = null;
        // anchor settings - there can be only anchor for 2 sides next to themselves
        if (topAnchor == true)
        {
            if (leftAnchor == true)
                anchor = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(sizeWidth,sizeHeight));
            else
                anchor = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(sizeWidth,sizeHeight));
        }
        else
        {
            if (leftAnchor == true)
                anchor = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(sizeWidth,sizeHeight));
            else
                anchor = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(sizeWidth,sizeHeight));
        }
        return anchor;
    }
    
    // Public method for map center setting. -----------------------------------------------------------
    // - lat  : Float   - center latitude
    // - lng  : Float   - center longitude
    // - zoom : Integer - init zoom
    function setCenter(lat, lng, zoom)
    {
        this.map.setCenter(new GLatLng(lat, lng), zoom);
    }
    this.setCenter = setCenter;
    
    // Public method for zoom setting. -----------------------------------------------------------------
    // - zoom : Integer - new zoom level
    function setZoom(zoom)
    {
        if ((zoom >= 1) && (zoom <= 17))
        {
            this.map.setZoom(zoom);
        }
    }
    this.setZoom = setZoom;
    
    // Public method for increasing actual zoom. -------------------------------------------------------
    function incZoom()
    {
        if (this.map.getZoom() < 17)
        {
            this.map.setZoom(this.map.getZoom() + 1);
        }
    }
    this.incZoom = incZoom;
    
    // Public method for decreasing actual zoom. -------------------------------------------------------
    function decZoom()
    {
        if (this.map.getZoom() > 1)
        {
            this.map.setZoom(this.map.getZoom() - 1);
        }
    }
    this.decZoom = decZoom;
    
    // -------------------------------------------------------------------------------------------------
    // METHODS FOR MARKERS - GENERAL
    // -------------------------------------------------------------------------------------------------

    // Loading markers. --------------------------------------------------------------------------------
    // - objectsCount : Integer          - count of generated markers
    // - iconsArray   : Array of String  - array of URLs to icons
    // - iconWidth    : Integer          - general icon widht
    // - iconHeight   : Integer          - general icon height
    function createMarkersExample(objectsCount, iconsArray, iconWidth, iconHeight, moveableMarkers)
    {
        // Add 10 markers to the map at random locations
        var bounds = this.map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        var lngSpan = northEast.lng() - southWest.lng();
        var latSpan = northEast.lat() - southWest.lat();
        
        if (iconsArray == null)
            iconsArray = new Array();
        if (moveableMarkers == null)
            moveableMarkers = false;

        var markers = [];
        var iconsCounter = 0;
        for (var i = 0; i < objectsCount; i++)
        {
            var latitude = southWest.lat() + latSpan * Math.random();
            var longitude = southWest.lng() + lngSpan * Math.random();
            
            var newIcon = null;
            if (iconsArray.length > 0)
            {
                if (iconsCounter >= iconsArray.length)
                    iconsCounter = 0;
                    
                newIcon = this.createIcon(iconsArray[iconsCounter], iconWidth, iconHeight);
                
                iconsCounter++;
            }
            else
            {
                newIcon = this.createIcon();
            }
            
            var marker = this.createMarker(latitude, longitude, "Example marker", newIcon, moveableMarkers);
            markers.push(marker);
        }
        this.mgr.addMarkers(markers, 0, 17);
        this.mgr.refresh();
        return;
    }
    this.createMarkersExample = createMarkersExample;
    
    // Creating object of icon. ------------------------------------------------------------------------
    // - iconFileURL   : String  - URL of image for icon
    // - iconWidth     : Integer - icon's width
    // - iconHeight    : Integer - icon's height
    // - shadowFileURL : String  - URL of image for icon's shadow
    // - shadowWidth   : Integer - shadow's width
    // - shadowHeight  : Integer - shadow's height
    function createIcon(iconFileURL, iconWidth, iconHeight, shadowFileURL, shadowWidth, shadowHeight)
    {
        // settings for variables if some of them are null
        if (iconFileURL == null)   iconFileURL = defaultIconFileURL;
        if (iconWidth == null)     iconWidth = 0;
        if (iconHeight == null)    iconHeight = 0;
        if (shadowFileURL == null) shadowFileURL = "";
        if (shadowWidth == null)   shadowWidth = 0;
        if (shadowHeight == null)  shadowHeight = 0;
        
        var icon = new GIcon();
        icon.image = iconFileURL;
        icon.shadow = shadowFileURL;
        icon.iconSize = (((iconWidth == 0) || (iconHeight == 0)) ? defaultIconSize : new GSize(iconWidth,iconHeight));
        icon.shadowSize = new GSize(shadowWidth,shadowHeight);
        icon.iconAnchor = new GPoint(19,45);
        icon.infoWindowAnchor = new GPoint(30,10);
        return icon;
    }
    this.createIcon = createIcon;

    // Creating general marker object. -----------------------------------------------------------------
    // - positionLat : Integer - latitude position of the marker
    // - positionLng : Integer - longitude position of the marker
    // - title       : String  - title of the marker
    // - icon        : GIcon   - icon of the marker
    // - isDraggable : Boolean - indication if marker will be draggable
    function createMarker(positionLat, positionLng, title, icon, isDraggable)
    {
        if (title == null)       title = "";
        if (icon == null)        icon = this.createIcon();
        if (isDraggable == null) isDraggable = false;
        
        var marker = new GMarker(new GLatLng(positionLat, positionLng), {
            title: title, 
            icon: icon, 
            draggable:isDraggable 
        });
        return marker;
    }
    this.createMarker = createMarker;
    
    // -------------------------------------------------------------------------------------------------
    // METHODS FOR GETTING DATA FROM THE MAP
    // -------------------------------------------------------------------------------------------------

    // Getting bounds from the map. --------------------------------------------------------------------
    function getBounds()
    {
        return this.map.getBounds();
    }
    this.getBounds = getBounds;
    // Getting SouthWest bounds from the map. ----------------------------------------------------------
    function getSouthWestBounds()
    {
        var bounds = this.map.getBounds();
        return bounds.getSouthWest();
    }
    this.getSouthWestBounds = getSouthWestBounds;
    // Getting NorthEast bounds from the map. ----------------------------------------------------------
    function getNorthEastBounds()
    {
        var bounds = gMapWithXML.getBounds();
        return bounds.getNorthEast();
    }
    this.getNorthEastBounds = getNorthEastBounds;
    
    // -------------------------------------------------------------------------------------------------
    // METHODS FOR EVENTS
    // -------------------------------------------------------------------------------------------------

    // Adding new event listener for the map. ----------------------------------------------------------
    // - eventType      : string   - event type which must occur of execution of the method
    // - method2execute : function - method to execute after event
    function addEvent(eventType, method2execute)
    {
        GEvent.addListener(this.map, eventType, method2execute);
    }
    this.addEvent = addEvent;

    // Adding new event listener for the map. ----------------------------------------------------------
    // - obj            : object   - object with event to handle
    // - eventType      : string   - event type which must occur of execution of the method
    // - method2execute : function - method to execute after event
    function addEventFull(obj, eventType, method2execute)
    {
        GEvent.addListener(obj, eventType, method2execute);
    }
    this.addEventFull = addEventFull;
    
    // -------------------------------------------------------------------------------------------------
    // METHODS FOR MARKERS - CONCRETE
    // -------------------------------------------------------------------------------------------------

    // Putting new marker into map. --------------------------------------------------------------------
    // - marker          : GMarker - created object of new marker to add
    // - zoomVisibleFrom : Integer - value from 0 to 17 - lower bound of visibility according to zoom
    // - zoomVisibleTo   : Integer - value from 0 to 17 - upper bound of visibility according to zoom 
    function addMarker(marker, zoomVisibleFrom, zoomVisibleTo)
    {
        this.mgr.addMarker(marker, zoomVisibleFrom, zoomVisibleTo);
        this.mgr.refresh();
    }
    this.addMarker = addMarker;
    
    // Removes one marker according to its unique number. ----------------------------------------------
    function deleteMarker()
    {
        var markerNum = parseInt(document.getElementById("markerNum").value);
        this.mgr.removeMarker(allmarkers[markerNum]);
    }
    this.deleteMarker = deleteMarker;

    // Removes all markers. ----------------------------------------------------------------------------
    function clearMarkers()
    {
        this.mgr.clearMarkers();
    }
    this.clearMarkers = clearMarkers;
    
}







// **************************************************************************************************************************
// GPS conversions
// **************************************************************************************************************************

// conversion from GPS into 
function GetGPS_LatLon(value)
{
	while (value.indexOf(" ") > -1)
	{
		value = value.replace(" ", "");
	}
	
	var val4lat = value.split(",")[0];
	var latHH = parseFloat(val4lat.split(String.fromCharCode(176))[0]);
	var latMM = parseFloat(val4lat.split("'")[0].split(String.fromCharCode(176))[1]);
	var latSS = parseFloat(val4lat.split("'")[1].replace("\"",""));
	//alert(val4lat +":   "+ latHH + "," + latMM + "," + latSS);
	
	
	var val4lon = value.split(",")[1];
	var lonHH = parseFloat(val4lon.split(String.fromCharCode(176))[0]);
	var lonMM = parseFloat(val4lon.split("'")[0].split(String.fromCharCode(176))[1]);
	var lonSS = parseFloat(val4lon.split("'")[1].replace("\"",""));
	//alert(val4lon +":   "+ lonHH + "," + lonMM + "," + lonSS);
	
	var lat = 0;
	var lon = 0;
	
    try
	{           
		lat = latSS / 60.0;
		lat = (lat + latMM) / 60.0;
		lat = lat + latHH;
                
		lon = lonSS / 60.0;
        lon = (lon + lonMM) / 60.0;
        lon = lon + lonHH;
    }
    catch (exception) {}
    
    return lat.toString() +";"+ lon.toString();
}

function GetGPS_Deg(value)
{
	value = value.toString().replace(" ","").replace("(","").replace(")","");
	var value1 = "", value2 = "";
	var hh = 0, mm = 0, ss = 0, actual = 0;
	var hh1 = 0, mm1 = 0, ss1 = 0, actual1 = 0;
	actual = parseFloat(value.split(",")[0]);
	actual1 = parseFloat(value.split(",")[1]);
						
	hh = parseInt(actual);
	var remainder = actual - (hh * 1.0);
	mm = remainder * 60.0;
	var remainder2 = mm - (parseInt(mm)*1.0);
	ss = Math.round(remainder2 * 60.0 * 1000.0) / 1000.0;
	mm = parseInt(mm);
						
	var hh1 = parseInt(actual1);
	remainder = actual1 - (hh1 * 1.0);
	mm1 = remainder * 60.0;
	remainder2 = mm1 - (parseInt(mm1)*1.0);
	ss1 = Math.round(remainder2 * 60.0 * 1000.0) / 1000.0;
	mm1 = parseInt(mm1);
						
	return hh +String.fromCharCode(176)+ mm +'\''+ ss +'"N ' + hh1 +String.fromCharCode(176)+ mm1 +'\''+ ss1 +'"E';
}
