<?php
$location_id = $_GET['location_id'];
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
require_once(dirname(__FILE__) . "/../../adbcon.php");

//$q = "select i.*, c.title as venue_category from wedding_items i, wedding_categories c where i.location_id='$location_id' and i.main_category=c.categ_id";
$q = "select * from advertisment_locations where location_id='$location_id';";
$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
if($r && mysql_num_rows($r) > 0) {
	$row = mysql_fetch_assoc($r);
	$advertisment_id = $row['advertisment_id'];
	$title = (trim($row['location_title']) != "") ? trim($row['location_title']) : 'Map Location';
	$lat = $row['lat'];
	$lng = $row['lng'];
	$zoom = $row['zoom'];
	$o_lat = $lat - 0.001;
	$o_lng = $lng - 0.001;
}
//$center_lat = $_REQUEST['lat'];
//$center_lng = $_REQUEST['lng'];
//$center_title = $_REQUEST['title'];
?>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

    <style type="text/css">
html, body {
  margin: 0;
  padding: 0;
  font-size: 11px;
  color: #333333;
  height: 100% !important;
}

.col-map {
    margin: 0 !important;
    padding: 0 !important;
    height: 100%;
}

.col-map-info {
    padding: 0 4px !important;
}

#help {
    margin: 4px auto;
    border: 1px solid #FCEFA1;
    background-color: #FBF9EE;
    overflow: hidden;
}

#help .info {
    font-size: 11px;
    color: #151515;
    margin: 0 4px;
    padding: 4px;
    min-height: 45px;
}

#map_canvas {
  height: 100%;

}

#directionsPanel {
	background-color: #ffffff;
}

#directions_res {

}

.adp-summary {
	font-weight: bold;
	color: #0000FF;
	
}

table.adp-directions {
	width: 100%;
}

.adp-directions {
	font-size: 11px;
	color: #333333;
}

#total_dist {
	border-bottom: 1px solid #ccc;
	padding: 3px;
	font-size: 12px; 
	clear: both;
}

table.adp-placemark {
	font-size: 11px;
	color: #000000;
	margin: 0px 0px 5px 0px;
}

.adp-placemark {
	font-size: 11px;
	color: #000000;
	margin: 0px 0px 5px 0px;
	font-weight: bold;
}

.adp-legal {
	display: none;
}

#back_link {
	border-bottom: 1px solid #ccc;
	text-align: center;
	padding: 10px 0;
}


@media print {
  html, body {
    height: auto;
  }

  #map_canvas {
    height: 650px;
  }
}
</style>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDxpjfVxlLazmPt1gkiw_wIy55ofWAUJvY"></script>
<script type="text/javascript">

	var rendererOptions = {
		//draggable: true,
		preserveViewport: true,
		suppressMarkers : true
	
	};
	var directionsDisplay, directionsService;
	var map;
	
	var map_location, user_location;
	var pp = {};

	
	
	function initialize() {
		pp = {
			'origin' : [<?php echo $o_lat;?>, <?php echo $o_lng;?>],
			'dest' : [<?php echo $lat;?>, <?php echo $lng;?>]
		};
		
		
		map_location = new google.maps.LatLng(parseFloat('<?php echo $lat;?>'), parseFloat('<?php echo $lng;?>'));
		user_location = new google.maps.LatLng(parseFloat('<?php echo $o_lat;?>'), parseFloat('<?php echo $o_lng;?>'));
		
		var myOptions = {
			zoom: 14,
			mapTypeId: google.maps.MapTypeId.HYBRID,
			center: new google.maps.LatLng(pp.dest[0], pp.dest[1]),
			draggable: true,
            scrollwheel: false
		};
		
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		
		
		// Creating a marker and positioning it on the map
		var dest_marker = new google.maps.Marker({
			position: new google.maps.LatLng(pp.dest[0], pp.dest[1]),
			icon: '<?php echo $protocol.$_SERVER['HTTP_HOST'];?>/amodules/adverts_map/res/markers/directions_finish.png',
			map: map,
			draggable: false
		});

		var orig_marker = new google.maps.Marker({
			position: new google.maps.LatLng(pp.origin[0], pp.origin[1]),
			icon: '<?php echo $protocol.$_SERVER['HTTP_HOST'];?>/amodules/adverts_map/res/markers/directions_start_blue.png',
			map: map,
			draggable: true
		});
		
		directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);;
		directionsService = new google.maps.DirectionsService();
		
		google.maps.event.addListener(orig_marker, "dragend", function() {
			var point = orig_marker.getPosition();
			map.panTo(point);

			user_location = point;
			calcRoute();
		});	
		
		google.maps.event.addListener(map, "dragend", function() {
			
		});
		
		
		directionsDisplay.setMap(map);
		directionsDisplay.setPanel(document.getElementById("directionsPanel"));
		
		google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
			computeTotalDistance(directionsDisplay.directions);
		});
		
		google.maps.event.trigger(map, 'resize');
		
		//calcRoute();
		
		
	}

	function calcRoute() {
	
		var request = {
			origin: user_location,
			destination: map_location,
			provideRouteAlternatives: false,
			//waypoints:[{location: "Bourke, NSW"}, {location: "Broken Hill, NSW"}],
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};
		directionsService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				directionsDisplay.setDirections(response);
			}
		});
	}

	function computeTotalDistance(result) {
		var total = 0;
		var myroute = result.routes[0];
		for (i = 0; i < myroute.legs.length; i++) {
			total += myroute.legs[i].distance.value;
		}
		total = total / 1000;
		document.getElementById("total").innerHTML = total + " km";
	}   
</script>
</head>
<body onload="initialize()">
<div class="container-fluid">
<div class="row">
    <div class="col-md-8 col-map col-map-map">
        <div id="map_canvas_container">
            <div id="map_canvas"></div>
        </div>
    </div>
    <div class="col-md-4 col-map col-map-info">
        <div id="directions_res">
            <?php
            if(isset($_REQUEST['domain_id'])) {
                $domain_id = intval($_REQUEST['domain_id']);
                $root_domain = $_SERVER['HTTP_HOST'];
                $q = "select * from domaincategories where catdom_id='$domain_id' and parent_id='0';";
                $r = @mysql_query($q);
                if($r && mysql_num_rows($r) == 1) {
                    $row = mysql_fetch_assoc($r);
                    $domainname = $row['domainname'];
                    $addonname = trim($row['addonname']);

                    if(str_replace('www.', '', $root_domain) != str_replace('www.', '', $domainname)) {
                        $root_domain .= '/' . $addonname;
                    }
                }

                $q = "select * from advertisment where advertisment_id='$advertisment_id';";
                $r = @mysql_query($q);
                if($r && mysql_num_rows($r) == 1) {
                    $row = mysql_fetch_assoc($r);

                    $friendly_url = $row['title_tr'];
                    ?>
                    <div id="back_link">
                        <a href="<?php echo $protocol . $root_domain . '/' . $friendly_url; ?>?page=1" class="btn btn-primary btn-sm" target="_self"><i class="fa fa-arrow-circle-left"></i> GO BACK</a>
                    </div>
                    <?php
                }

            }
            ?>
            <div id="total_dist"><strong><?php echo "Total Distance";?></strong>: <span id="total">TBD</span></div>
            <div class="clearfix">
                <div id="help">
                    <?php
                    $start_marker = '<img src="' . $protocol.$_SERVER['HTTP_HOST'].'/amodules/adverts_map/res/markers/directions_start_blue.png' . '" border="0" style="float: left; margin: 0px 3px 3px 0px; clear: both;">';
                    $end_marker = '<img src="' . $protocol.$_SERVER['HTTP_HOST'].'/amodules/adverts_map/res/markers/directions_finish.png' . '" border="0" style="float: left; margin: 0px 3px 3px 0px; clear: both;">';
                    ?>
                    <div class="info"><?php echo $end_marker;?><?php echo "The red marker represents the address location on the map.";?></div>
                    <div class="info"><?php echo $start_marker;?><?php echo "Please drag the blue marker (on the map) to your desired starting point in order to get driving directions from your desired location.";?></div>
                </div>
            </div>
            <div id="directionsPanel" class="clearfix">
            </div>
        </div>
    </div>
</div>

</div>
</body>
</html>