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

$address = '';
//$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';";
//echo $q;
$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;
	
	$street = trim(stripslashes($row['street']));
	$street_no = trim(stripslashes($row['street_no']));
	$floor = trim(stripslashes($row['floor']));
	$flat = trim(stripslashes($row['flat']));
	
	$postalcode = trim(stripslashes($row['postalcode']));
	$city = trim(stripslashes($row['city']));
	$area = trim(stripslashes($row['area']));
	$district = trim(stripslashes($row['district']));
	
	$pobox = trim(stripslashes($row['pobox']));
	$pobox_postalcode = trim(stripslashes($row['pobox_postalcode']));
	
	$po = array();
	if($pobox != "") $po [] = 'P.O.Box: ' . $pobox;
	if($pobox_postalcode != "") $po [] = 'P.O.Box Postal Code: ' . $pobox_postalcode;
	
	
	
	$address = "";
	if($street != "") $address .= $street;
	if($street_no != "") $address .= ' ' . $street_no;
	if($flat != "") $address .= ', flat ' . $flat;
	if($floor != "") $address .= ', floor ' . $floor;
	
	if(count($po) > 0) {
		$address .= "<br/>" . implode("<br/>", $po);
	}
	elseif($postalcode != "") {
		$address .= "<br/>Postal Code: $postalcode";
	}
	
	$loc = "";
	
	if($city != "") $loc .= ucfirst($city) . ', ';
	if($area != "") $loc .= ucfirst($area) . ', ';
	
	$address .= "<br/>" . $loc . ' CYPRUS';	
	
}
//$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;
    }


#map_canvas {
  height: 100%;
}


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


#total_dist {
	border-bottom: 1px solid #000000; 
	/* /* border-right: 1px solid #000000; */ */ 
	padding:3px; 
	font-size: 12px; 
	clear: both;
}


#back_link {
	border-bottom: 1px solid #000000;
	/* border-right: 1px solid #000000; */
	text-align: center;
	padding: 10px 0;
}

.map_back_link {
	background: #f8ffe8;
	background: -moz-linear-gradient(top,  #f8ffe8 0%, #e3f5ab 33%, #b7df2d 100%);
	background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f8ffe8), color-stop(33%,#e3f5ab), color-stop(100%,#b7df2d));
	background: -webkit-linear-gradient(top,  #f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%);
	background: -o-linear-gradient(top,  #f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%);
	background: -ms-linear-gradient(top,  #f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%);
	background: linear-gradient(to bottom,  #f8ffe8 0%,#e3f5ab 33%,#b7df2d 100%);
	filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f8ffe8', endColorstr='#b7df2d',GradientType=0 );
	
	-webkit-box-shadow: 2px 2px 4px rgba(50, 50, 50, 0.9);
	-moz-box-shadow:    2px 2px 4px rgba(50, 50, 50, 0.9);
	box-shadow:         2px 2px 4px rgba(50, 50, 50, 0.9);
	
	
	display: block;
	margin: 10px auto;
	width: 160px;
	height: 30px;
	
	border: 1px solid #B7DF2F;
	color: #333333;
	font-size: 14px;
	font-weight: bold;
	text-decoration: none;
	text-align: center;
	padding: 14px 0 0 0;
}

#address {
	margin: 0; 
	padding: 4px 4px; 
	font-size: 13px; 
	line-height: 16px;
}

@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 = {
			'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
		});
		
		
		
		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="map_back_link" target="_self">GO BACK</a>
                        </div>
                        <?php
                    }

                }
                ?>
                <div id="total_dist"><strong>Address:</strong></div>
                <div id="address">
                    <?php
                    echo $address;
                    ?>
                </div>

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