<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED ^ E_STRICT);
$rootPath = is_file("../../../../library/document.php") ? "../../../../" : "../../";
define("CMS", "korCMS");
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https" : "http";
include($rootPath."config/odyssey.ini.php");
include($rootPath."library/db.php");
include($rootPath."library/system.php");
include($rootPath."library/messages.php");
include($rootPath."library/content.php");
include($rootPath."library/variables.php");
include($rootPath."library/language.php");
include($rootPath."library/sef.php");
global $domainPath;
$domainPath = $protocol . "://" . $_SERVER['HTTP_HOST'] . "/";
$sef = new sef();
$db = new db();
$system = new system();
$messages = new messages();
global $content;
$content = new content();
$variables = new variables();
$language = new language();

$variables->getSafeVars(array_keys($_REQUEST));
$langID = ((int)$_GET['langID']) ?(int)$_GET['langID'] : $language->getDefaultLanguage();
$defaultLAT = "37.98413";
$defaultLNG = "23.72808";
$defaultZOOM = 6;

$lat = ($lat) ? $lat: $defaultLAT;
$lng = ($lng) ? $lng: $defaultLNG;
$zoom = ((int)$_GET['zoom'] > 0) ? (int)$_GET['zoom'] : $defaultZOOM;


function t($key, $langID=NULL, $context=NULL){ // shortcut of $language->getText($key, $langID=NULL, $context=NULL)
    global $language;
    return $language->getText($key, $langID=NULL, $context=NULL);
}

function getFPath(){
    $parts = explode("/", $_SERVER["PHP_SELF"]);
    array_shift($parts); // the first element will be empty to we get rid of it
    array_pop($parts); // get rid of script name
    $fPath =implode("/", $parts);
    if(substr($fPath, -1) != "/") $fPath.="/";
    $fPath = ($fPath == "/") ? null : $fPath;
    return $fPath;
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="shortcut icon" href="favicon.ico">
    <style type="text/css">
        @import url(https://fonts.googleapis.com/css?family=Ubuntu+Condensed&subset=latin,greek);

        body {
            color: #fff;
            background: #272727 url(images/blank.gif);
            padding: 2px;
            font-family: 'Ubuntu Condensed', sans-serif;
            font-size: 1em !important;
            overflow: auto;
        }

        input, textarea, keygen, select, button {
            font-family: inherit !important;
            font-size: inherit !important;
        }

        #results a {
            display: block;
            padding: 4px 6px;
            background: #3f3f3f;
            color: #ffffff !important;
            margin: 2px 0;
            text-decoration: none;
        }

        #results a:hover {
            color: #ffffff !important;
            background-color: #bf3167;
        }

        .listbox {
            color: #000000;
            background: #f7f7f7;
            padding: 3px;
            border: none;
        }

        .error {
            background: #7a0000;
            padding: 20px 5px;
            color: white;
            width: 95%;
            max-width: 400px;
            margin: 15px auto;
            text-align: center;
            text-shadow: 1px 1px 1px rgba(0, 0, 0, 1);
        }

        .error div {
            text-align: left;
        }

        #search_wrapper {
            text-align: center;
            margin: 20px auto;
            width: 100%;
            max-width: 300px;
        }

        #search {
            display: inline-block;
            position: relative;
            margin-left: -40px;
            width: 80%;
        }

        #search_btn {
            display: block;
            position: absolute;
            top: 0;
            right: -43px;
            width: 30px;
            height: 30px;
            background: #bf3167 url(images/search_bg_magnifier.png) center center no-repeat;
        }

        #address {
            padding-right: 40px;
            width: 100%;
            max-width: 400px;
            height: 24px;
            outline: none;
        }

        .edit_category {
            display: block;
            font-size: 1em;
            color: #21252A;
            padding: 3px 3px 4px 10px;
            text-decoration: none;
            line-height: 25px;
            border-width: 2px;
            border-style: solid;
            border-color: transparent transparent #E0E0E0;
            -moz-border-top-colors: none;
            -moz-border-right-colors: none;
            -moz-border-bottom-colors: none;
            -moz-border-left-colors: none;
            border-image: none;
        }

        .edit_category:hover {
            color: #FFF;
            background: transparent linear-gradient(to bottom, #BF3167 0%, #9A2A54 97%, #9A2A54 97%, #9A2A54 100%) repeat scroll 0% 0%;
            border-color: #E32D72 #753B51 #753B51 #E32D72;
        }

        #kml_results {
            display: none;
            margin: 20px 10px 10px;
            padding: 10px;
            background-color: #EDEDED;
        }

        #load_kml {
            display: inline-block;
            padding: 4px 20px;
            color: white;
            background-color: #9c2150;
            float: right;
            border: none;
            margin-right: 10px;
            text-decoration: none;
        }

        #load_kml:hover {
            background-color: #BF3167;
        }

        #cancel_kml {
            display: table;
            margin: 5px auto;
            padding: 4px 20px;
            color: white;
            background-color: #a60000;
            text-decoration: none;
        }

        #cancel_kml:hover {
            background-color: #d10000;
        }
    </style>
    <script type="text/javascript" src="<?php echo $rootPath;?>js/jquery.js"></script>
    <script src="https://maps.google.com/maps/api/js?key=AIzaSyBthyH-eMgLYEplcPIOQRrqW3PLywGtHFQ<?php if($langID==1){echo "&amp;language=el";}?>" type="text/javascript"></script>
    <script type="text/javascript">
        function initializeMap() {
            var lat = (parent.document.getElementById('lat').value) ? parent.document.getElementById('lat').value : <?php echo $lat; ?>;
            var lng = (parent.document.getElementById('lng').value) ? parent.document.getElementById('lng').value : <?php echo $lng; ?>;
            var zoom = (parent.document.getElementById('zoom').value > 0) ? parent.document.getElementById('zoom').value : <?php echo $zoom; ?>;

            var markersArray = [];
            var myOptions = {
                zoom: parseInt(zoom),
                center: new google.maps.LatLng(lat, lng),
                mapTypeControl: true,
                mapTypeControlOptions: {
                    style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
                },
                navigationControl: true,
                navigationControlOptions: {
                    style: google.maps.NavigationControlStyle.SMALL
                },
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            window.theMap=map;
            <?php
            if($lat && $lng && $zoom){
            $markerSet = true;
            if(($lat == $defaultLAT) && ($lng == $defaultLNG)) {$markerSet = false;}

            ?>
            window.setDefaultMarker = function(){
                var theLocation = new google.maps.LatLng(lat,lng);
                var marker = new google.maps.Marker({
                    position: theLocation,
                    map: map
                });
                markersArray.push(marker);
                map.setCenter(theLocation);
            }
            setDefaultMarker();
            <?php
            }
            ?>
            google.maps.event.addListener(map, 'click', function(event) {
                placeMarker(event.latLng);
            });

            window.placeMarker = function(location) {
                if (location) {
                    deleteOverlays(); // custom function
                    var marker = new google.maps.Marker({
                        position: location,
                        map: map
                    });
                    markersArray.push(marker);
                    setTimeout (function(){
                        if(confirm("<?php echo t("Θέλετε να αποθηκευτεί το επιλεγμένο σημείο;");?>")){
                            parent.document.getElementById('lat').value= location.lat().toFixed(5);
                            parent.document.getElementById('lng').value= location.lng().toFixed(5);
                            var zoom= map.getZoom();
                            parent.document.getElementById('zoom').value=zoom;
                            parent.document.getElementById('mapInfo').innerHTML = "<?php echo t("Έχουν επιλεγεί οι κάτωθι συντεταγμένες");?>:<br><?php echo t("Γεωγραφικό μήκος");?>: "+location.lng().toFixed(5)+"<br><?php echo t("Γεωγραφικό πλάτος");?>: "+location.lat().toFixed(5)+"<br>Zoom Level:"+zoom;
                            parent.$.fancybox.close();
                        }else{
                            deleteOverlays();
                            <?php
                            if($markerSet){
                            ?>
                            setDefaultMarker();
                            <?php
                            }
                            ?>
                        }
                    }, 100);


                }
            }

            window.deleteOverlays = function() {
                if (markersArray) {
                    for (i in markersArray) {
                        markersArray[i].setMap(null);
                    }
                    markersArray.length = 0;
                }
            }


        } // end map


        window.restoreLocation = function(){
            parent.document.getElementById('lat').value = "<?php echo $lat; ?>";
            parent.document.getElementById('lng').value = "<?php echo $lng; ?>";
            parent.document.getElementById('zoom').value = "<?php echo $zoom; ?>";
            parent.document.getElementById('mapInfo').innerHTML = "<?php echo t("Οι συντεταγμένες επανήλθαν στις αρχικές τιμές");?>:<br><?php echo t("Γεωγραφικό μήκος");?>: <?php echo $lng; ?><br><?php echo t("Γεωγραφικό πλάτος");?>: <?php echo $lat; ?><br>Zoom Level: <?php echo $zoom; ?>";
            parent.$.fancybox.close();
        }


        window.submitSearchForm = function(){
            var address = document.getElementById("address").value;
            if (!address) return false;
            var latlngStr = address.split(',', 2);
            var lat = parseFloat(latlngStr[0]);
            var lng = parseFloat(latlngStr[1]);
            if(lat && lng){
                geocode_type = 'location';
                var address = new google.maps.LatLng(lat, lng);
                placeMarker(address);
                return;
            }else{
                var geocode_type = 'address';
            }
            document.getElementById('results').innerHTML = "<div style=' text-align:center'><img src='images/loader.gif' alt='loading data'><br><br></div>";
            geocoder = new google.maps.Geocoder();
            geocoder.geocode({[geocode_type]: address}, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if(results.length == 1){
                        pos = results[0].geometry.location;
                        window.theMap.setCenter(pos);
                        if(window.tempMarker){window.tempMarker.setMap(null);}
                        var marker = new google.maps.Marker({
                            map: window.theMap,
                            position: pos
                        });
                        window.tempMarker = marker;
                        document.getElementById('results').innerHTML ="";
                        setTimeout (function(){
                            if(confirm("<?php echo t('Θέλετε να αποθηκευτεί το επιλεγμένο σημείο;');?>")){
                                parent.document.getElementById('lat').value= pos.lat().toFixed(5);
                                parent.document.getElementById('lng').value= pos.lng().toFixed(5);
                                var zoom= window.theMap.getZoom();
                                parent.document.getElementById('zoom').value=zoom;
                                var mapLink = parent.document.getElementById('mapLink');
                                if(mapLink != null) {
                                    mapLink.href="gmap.php?zoom=" + zoom + "&lat=" + pos.lat().toFixed(5) + "&lng=" + pos.lng().toFixed(5);
                                }
                                parent.document.getElementById('mapInfo').innerHTML = "<?php echo t("Έχουν επιλεγεί οι κάτωθι συντεταγμένες");?>:<br><?php echo t("Γεωγραφικό μήκος");?>: "+pos.lng().toFixed(5)+"<br><?php echo t("Γεωγραφικό πλάτος");?>: "+pos.lat().toFixed(5)+"<br>Zoom Level:"+zoom;
                                parent.$.fancybox.close();
                            }else{
                                if(window.tempMarker) window.tempMarker.setMap(null);
                            }
                        },1000);
                    }else{ // βρέθηκαν περισσότερα σημεία
                        document.getElementById('results').innerHTML ="<div style='color:#ea2771; font-weight:bold; margin: 0 0 5px 0'><?php echo t("Αποτελέσματα αναζήτησης");?>:</div>";
                        for(var i=0; i<results.length; i++){
                            place = results[i].formatted_address;
                            lat = results[i].geometry.location.lat();
                            lng = results[i].geometry.location.lng();
                            document.getElementById('results').innerHTML+="<a href='javascript:showOnMap("+lat+", "+lng+", \""+place.replace(/, Ελλάς/i, "")+"\")' class='dark_link' style='display:block'>"+place.replace(/, Ελλάς/i, "")+"</a>";
                        }
                    }
                } else {
                    document.getElementById('results').innerHTML ="<?php echo t("Δεν αναγνωρίστηκε η διεύθυνση. Παρακαλούμε δοκιμάστε ξανά...");?>";
                    if(window.tempMarker) window.tempMarker.setMap(null);
                }
            });
        }



        window.showOnMap = function(lat, lng, place){
            point = new google.maps.LatLng(lat, lng);
            window.theMap.setCenter(point);
            if(window.tempMarker){window.tempMarker.setMap(null);}
            var marker = new google.maps.Marker({
                map: window.theMap,
                position: point
            });
            window.tempMarker = marker;
            window.location.href="#";
            setTimeout(function(){
                if(confirm("<?php echo t('Θέλετε να αποθηκευτεί το επιλεγμένο σημείο;');?>")){
                    parent.document.getElementById('lat').value= lat.toFixed(5);
                    parent.document.getElementById('lng').value= lng.toFixed(5);
                    var zoom= window.theMap.getZoom();
                    parent.document.getElementById('zoom').value=zoom;
                    var mapLink = parent.document.getElementById('mapLink');
                    if(mapLink != null) {
                        mapLink.href="gmap.php?zoom=" + zoom + "&lat=" + lat.toFixed(5) + "&lng=" + lng.toFixed(5);
                    }
                    parent.document.getElementById('mapInfo').innerHTML = "<?php echo t("Έχουν επιλεγεί οι κάτωθι συντεταγμένες");?>:<br><?php echo t("Γεωγραφικό μήκος");?>: "+lng.toFixed(5)+"<br><?php echo t("Γεωγραφικό πλάτος");?>: "+lat.toFixed(5)+"<br>Zoom Level:"+zoom;
                    parent.$.fancybox.close();
                }else{marker.setMap(null);}
            }, 1000);
        }

        window.resetMap = function(){
            parent.document.getElementById('lat').value= <?php echo $defaultLAT;?>;
            parent.document.getElementById('lng').value= <?php echo $defaultLNG;?>;
            var zoom= <?php echo $defaultZOOM;?>;
            parent.document.getElementById('zoom').value=zoom;
            parent.document.getElementById('mapInfo').innerHTML = "<?php echo t("Ο γεωγραφικός προσδιορισμός ακυρώθηκε");?>";
            parent.$.fancybox.close();
        }

        $(document).keypress(function(e) {
            if(e.which == 13) {
                if(!$('#address').val()) return false;
                submitSearchForm();
            }
        });

    </script>
</head>
<body onload="initializeMap()">
<div style="border: solid 1px white;height: 370px;">
    <div id="map_canvas" style="width: 100%; height: 370px"></div>
</div>
<div id="bottom" style="margin-top:10px">
    <?php echo t("Εντοπίστε το σημείο που επιθυμείτε στο χάρτη και κάντε κλικ");?>.<br>
    <?php echo t("Αν θέλετε να ακυρώσετε τον χάρτη, πατήστε");?> <a href="javascript:void(0)" onclick="resetMap()" style="color:orange"><?php echo t("εδώ");?></a>
    <br>
    <br>
    <?php echo t("Εναλλακτικά μπορείτε να εισάγετε την διεύθυνση που αναζητάτε και θα γίνει προσπάθεια για αυτόματο εντοπισμό της στον χάρτη").":";?>
</div>
<div id="search_wrapper">
    <div id="search">
        <input name="address" type="text" class="listbox" id="address">
        <a id="search_btn" href="javascript:void(0)" onclick="submitSearchForm();"></a>
    </div>
</div>
<div id="results" style="margin:20px; min-height:400px"></div>

</body>
</html>
