<?php
//define this module's name and path
$mn = basename(__FILE__ , ".php");
include("modules/modules.php");
/*
Available variables:
All system classes
All request variables
$p (the template path)
$mp (the module path)
$sufixID (id sufix for this module's divs)
*/

if ($moduleParameters['moduleAlign'] && $moduleParameters['moduleHR']) { // there are system parameters...
    $marginTop = ($moduleParameters['marginTop'] == "__EMPTY__" || empty($moduleParameters['marginTop'])) ? 0 : $moduleParameters['marginTop'];
    $marginRight = ($moduleParameters['marginRight'] == "__EMPTY__" || empty($moduleParameters['marginRight'])) ? 0 : $moduleParameters['marginRight'];
    $marginBottom = ($moduleParameters['marginBottom'] == "__EMPTY__" || empty($moduleParameters['marginBottom'])) ? 0 : $moduleParameters['marginBottom'];
    $marginLeft = ($moduleParameters['marginLeft'] == "__EMPTY__" || empty($moduleParameters['marginLeft'])) ? 0 : $moduleParameters['marginLeft'];
    $paddingTop = ($moduleParameters['paddingTop'] == "__EMPTY__" || empty($moduleParameters['paddingTop'])) ? 0 : $moduleParameters['paddingTop'];
    $paddingRight = ($moduleParameters['paddingRight'] == "__EMPTY__" || empty($moduleParameters['paddingRight'])) ? 0 : $moduleParameters['paddingRight'];
    $paddingBottom = ($moduleParameters['paddingBottom'] == "__EMPTY__" || empty($moduleParameters['paddingBottom'])) ? 0 : $moduleParameters['paddingBottom'];
    $paddingLeft = ($moduleParameters['paddingLeft'] == "__EMPTY__" || empty($moduleParameters['paddingLeft'])) ? 0 : $moduleParameters['paddingLeft'];
    $useModuleContainer = ($moduleParameters['useModuleContainer'] == "__EMPTY__" || empty($moduleParameters['useModuleContainer'])) ? 0 : (int)$moduleParameters['useModuleContainer'];
    $widthInPixels = ($moduleParameters['widthInPixels'] == "__EMPTY__" || empty($moduleParameters['widthInPixels'])) ? null : (int)$moduleParameters['widthInPixels'];


    if (empty($marginTop) && empty($marginRight) && empty($marginBottom) && empty($marginLeft)) {
        $Mmargin = "0";
    } else {
        $Mmargin = $marginTop . " " . $marginRight . " " . $marginBottom . " " . $marginLeft . " ";
    }
    if (empty($paddingTop) && empty($paddingRight) && empty($paddingBottom) && empty($paddingLeft)) {
        $Mpadding = "0";
    } else {
        $Mpadding = $paddingTop . " " . $paddingRight . " " . $paddingBottom . " " . $paddingLeft . " ";
    }

    global $langExtension;
    global $protocol;
    $mapLanguage = ($langExtension == "_gr") ? "&language=el" : null;
    $mapWidth = $moduleParameters['mapWidth'];
    $mapHeight = $moduleParameters['mapHeight'];
    // we add a px sufix if no sufix is given
    if (!preg_match('/px$/', $mapWidth) && !preg_match('/%$/', $mapWidth)) $mapWidth = (int)$mapWidth . "px";
    if ((!preg_match('/px$/', $mapHeight) && !preg_match('/%$/', $mapHeight)) && (int)$mapHeight) $mapHeight = (int)$mapHeight . "px";
    $apiKey = $moduleParameters['apiKey'];
    $cssContent = $moduleParameters['cssContent'];
    $mapMode = $moduleParameters['mapMode'];
    $customCoordinates = $moduleParameters['customCoordinates'];

    $dataSource = $moduleParameters['dataSource'];
    $dataSource = ($dataSource == "auto" || empty($dataSource)) ? $section : $dataSource;
    if ($system->getTableSectionID($dataSource)) { // only if a valid section
        $lat = $this->item->lat;
        $lng = $this->item->lng;
        $zoom = $this->item->zoom;
        $itemID = $id;
    }

    if ($customCoordinates) { // override coords
        list($lat, $lng, $zoom) = explode("|", $customCoordinates);
    }


    if ($useModuleContainer) {
        ?>
        <div id="<?php echo $mn . $sufixID; ?>" data-editable="editable" class="responsive_float">
        <?php
    }
    ?>
    <style type="text/css">
        #<?php echo $mn.$sufixID;?>{
	<?php 
	$alignRule = ($moduleParameters['moduleAlign'] == "center") ? "text-align" : "float";
	echo $alignRule.": ".$moduleParameters['moduleAlign'].";".PHP_EOL;
	?>
	margin : <?php echo $Mmargin;?>;
	padding : <?php echo $Mpadding;?>;
<?php
if($widthInPixels){
	?>
	max-width: <?php echo $widthInPixels;?>px;
	<?php
}	
if($moduleParameters['moduleHR'] == "top" || $moduleParameters['moduleHR'] == "bottom"){
?>
	border-<?php echo $moduleParameters['moduleHR'];?> : 1px solid black;
<?php	
}
?>
}<?php
if($cssContent){ // load custom css if a cssContent has been found
	echo $variables->restoreModuleValue($cssContent);
}
?> .map_button {
            display: block;
            margin: 10px auto;
        }

        .gm-style img {
            max-width: none !important;
        }
    </style>

    <script src="<?php echo $protocol; ?>://maps.google.com/maps/api/js?<?php echo $mapLanguage; ?>&amp;key=<?php echo $apiKey; ?>" type="text/javascript"></script>
    <script type="text/javascript">
        jQuery().ready(function () {
            <?php
            if($moduleParameters['useDirections']){
            ?>
            $('a[data-rel="map"]').fancybox({
                /*onClosed			: function() {if(window.start){parent.document.calcRoute(window.start);}},*/
                afterClose: function () {
                    if (window.start) {
                        document.calcRoute(window.start);
                    }
                },
                'overlayOpacity': 0.9,
                'overlayColor': '#000',
                'width': 700,
                'height': 600,
                'autoScale': false,
                'transitionIn': 'elastic',
                'transitionOut': 'elastic',
                'type': 'iframe',
                'showNavArrows': false,
                'titleShow': false
            });
            <?php
            }
            ?>
            document.directionsService = new google.maps.DirectionsService();
            document.directionsDisplay = new google.maps.DirectionsRenderer();

            var lat = <?php echo $lat; ?>;
            var lng = <?php echo $lng; ?>;
            var zoom = <?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
                },
                <?php
                if($moduleParameters['nightVision'] == '1'){
                ?>
                styles: [
                    {elementType: 'geometry', stylers: [{color: '#242f3e'}]},
                    {elementType: 'labels.text.stroke', stylers: [{color: '#242f3e'}]},
                    {elementType: 'labels.text.fill', stylers: [{color: '#746855'}]},
                    {
                        featureType: 'administrative.locality',
                        elementType: 'labels.text.fill',
                        stylers: [{color: '#d59563'}]
                    },
                    {
                        featureType: 'poi',
                        elementType: 'labels.text.fill',
                        stylers: [{color: '#d59563'}]
                    },
                    {
                        featureType: 'poi.park',
                        elementType: 'geometry',
                        stylers: [{color: '#263c3f'}]
                    },
                    {
                        featureType: 'poi.park',
                        elementType: 'labels.text.fill',
                        stylers: [{color: '#6b9a76'}]
                    },
                    {
                        featureType: 'road',
                        elementType: 'geometry',
                        stylers: [{color: '#38414e'}]
                    },
                    {
                        featureType: 'road',
                        elementType: 'geometry.stroke',
                        stylers: [{color: '#212a37'}]
                    },
                    {
                        featureType: 'road',
                        elementType: 'labels.text.fill',
                        stylers: [{color: '#9ca5b3'}]
                    },
                    {
                        featureType: 'road.highway',
                        elementType: 'geometry',
                        stylers: [{color: '#746855'}]
                    },
                    {
                        featureType: 'road.highway',
                        elementType: 'geometry.stroke',
                        stylers: [{color: '#1f2835'}]
                    },
                    {
                        featureType: 'road.highway',
                        elementType: 'labels.text.fill',
                        stylers: [{color: '#f3d19c'}]
                    },
                    {
                        featureType: 'transit',
                        elementType: 'geometry',
                        stylers: [{color: '#2f3948'}]
                    },
                    {
                        featureType: 'transit.station',
                        elementType: 'labels.text.fill',
                        stylers: [{color: '#d59563'}]
                    },
                    {
                        featureType: 'water',
                        elementType: 'geometry',
                        stylers: [{color: '#17263c'}]
                    },
                    {
                        featureType: 'water',
                        elementType: 'labels.text.fill',
                        stylers: [{color: '#515c6d'}]
                    },
                    {
                        featureType: 'water',
                        elementType: 'labels.text.stroke',
                        stylers: [{color: '#17263c'}]
                    }
                ],
                <?php
                }
                ?>
                mapTypeId: google.maps.MapTypeId.<?php echo $mapMode;?>
            }
            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            document.directionsDisplay.setMap(map);
            document.directionsDisplay.setPanel(document.getElementById("gdir"));
            google.maps.event.addDomListener(window, 'resize', function () {
                map.setCenter(new google.maps.LatLng(lat, lng));
            });


            function setDefaultMarker() {
                var theLocation = new google.maps.LatLng(lat, lng);
                var marker = new google.maps.Marker({
                    position: theLocation,
                    map: map
                });
                markersArray.push(marker);
                map.setCenter(theLocation);
            }
            <?php
            if(!empty($lat) && !empty($lng)){
            ?>
            setDefaultMarker();
            <?php
            }
            ?>

            document.calcRoute = function (start) {
                start = start.replace(/\(|\)/g, "");
                start = start.split(",");
                var lat = parseFloat(start[0]);
                var lng = parseFloat(start[1]);
                document.getElementById('gdirLink').innerHTML = '<a href="<?php $sef->url("index.php?section=" . $this->section . "&view=" . $this->view . "&id=" . $id . "&r=" . rand());?>#map_canvas" class="map_button" style="max-width:<?php echo $mapWidth; ?>"><?php echo t("Επιστροφή στον προηγούμενο χάρτη");?></a>';
                document.getElementById('printDirections').style.display = "block";
                var request = {
                    origin: new google.maps.LatLng(lat, lng),
                    destination: new google.maps.LatLng(<?php echo $lat; ?>, <?php echo $lng; ?>),
                    travelMode: google.maps.TravelMode.DRIVING
                };
                document.directionsService.route(request, function (response, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        document.directionsDisplay.setDirections(response);
                    }
                });
            }

            document.printDIV = function (id) {
                try {
                    var oIframe = document.getElementById('ifrmPrint');
                    var oContent = document.getElementById(id).innerHTML;
                    var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
                    if (oDoc.document) oDoc = oDoc.document;
                    oDoc.write("<head><title>title</title>");
                    oDoc.write("</head><body onload='this.focus(); this.print();'>");
                    oDoc.write(oContent + "</body>");
                    oDoc.close();
                }
                catch (e) {
                    self.print();
                }
            }
        });
    </script>
    <div id="map_canvas" style="max-width: <?php echo $mapWidth; ?>; height: <?php echo $mapHeight; ?>; border: solid 1px #272727; margin: 0 auto; color:black"></div>
    <iframe id="ifrmPrint" src="" style="width:0px; height:0px; border: 0; position: absolute; left: -100px; top: -100px"></iframe>
    <?php
    if ($moduleParameters['googleMapsLink']) {
        $gmapLink = "https://maps.google.com/?q=" . $lat . "," . $lng . "&t=m&z=" . $zoom;
        ?>
        <a id="view_in_gmaps" href="<?php echo $gmapLink; ?>" class="map_button" data-rel="external">View in Google Maps</a>
        <?php
    }
    ?>
    <div id="gdirLink">
        <?php
        if ($moduleParameters['useDirections']) {
            ?>
            <a href="gmapDIR.php?langID=<?php echo $langID; ?>" data-rel="map" class="map_button" style="max-width:<?php echo $mapWidth; ?>">
                <?php echo t("Πώς θα μας βρείτε (αναλυτικές οδηγίες στο χάρτη)"); ?></a>
            <?php
        }
        if ($moduleParameters['printMap']) {
            ?>
            <a href="javascript:document.printDIV('map_canvas')" class="map_button" style="max-width:<?php echo $mapWidth; ?>"><?php echo t("Εκτύπωση χάρτη"); ?></a>
            <?php
        }
        ?>
    </div>
    <?php
    if ($moduleParameters['useDirections']) {
        ?>
        <div id="gdir" onclick="document.location.href='<?php $sef->url("index.php?section=" . $this->section . "&view=" . $this->view . "&id=" . $id); ?>#map_canvas'"></div>
        <div id="printDirections">
            <a href="javascript:document.printDIV('gdir')" class="map_button" style="max-width:<?php echo $mapWidth; ?>"><?php echo t("Εκτύπωση των οδηγιών"); ?></a>
        </div>
        <?php
    }
    if ($useModuleContainer) {
        ?>
        </div>
        <?php
    }
}
?>
