<script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?php echo sfConfig::get('app_gmap') ?>&sensor=true"></script>
<?php

$criteria = new Criteria();
$criteria->addAscendingOrderByColumn(KataoNodePeer::CITY);
$nodes = KataoNodePeer::doSelect($criteria);

?>


<form action="<?php echo $ContentTree->getCurrentNode()->getUrl() ?>">
<select name="node" onchange="displayNode(this.value)">
	<option value="0">Tous les noeuds</option>
<?php foreach ($nodes as $node) {
    printf('<option value="%d">%s</option>', $node->getId(), $node->getCity());
}
?>
</select>
</form>

	<div id="map_canvas" style="width: 730px; height: 580px"></div>

<script type="text/javascript" language="JavaScript">
/* <![CDATA[ */

function displayNode(id){
	if (0 == id) {
		displayAllNodes();
	} else {
	<?php foreach ($nodes as $node): ?>
		for(var i = 0; i < node<?php echo $node->getId(); ?>.length; i++){
			node<?php echo $node->getId(); ?>[i].hide();
		}
	<?php endforeach; ?>
	<?php foreach ($nodes as $node): ?>
		for(var i = 0; i < node<?php echo $node->getId(); ?>.length; i++){
			if(<?php echo $node->getId(); ?> == id){
				node<?php echo $node->getId(); ?>[i].show();
			}
		}
	<?php endforeach; ?>
	}
}

function displayAllNodes(){
<?php foreach ($nodes as $node): ?>
	for(var i = 0; i < node<?php echo $node->getId(); ?>.length; i++){
		node<?php echo $node->getId(); ?>[i].show();
	}
<?php endforeach; ?>
}

window.onload = function(){
 if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(45, 3), 6);
        map.setUIToDefault();

		var myIconRed = new GIcon(G_DEFAULT_ICON);
		myIconRed.iconSize = new GSize(32, 32);
		myIconRed.image = 'http://maps.google.com/mapfiles/ms/micons/red-dot.png';
		var markerOptionsRed = { icon: myIconRed};

		var myIconGreen = new GIcon(G_DEFAULT_ICON);
		myIconGreen.iconSize = new GSize(32, 32);
		myIconGreen.image = 'http://maps.google.com/mapfiles/ms/micons/green-dot.png';
		var markerOptionsGreen = { icon: myIconGreen};

		var myIconBlue = new GIcon(G_DEFAULT_ICON);
		myIconBlue.iconSize = new GSize(32, 32);
		myIconBlue.image = 'http://maps.google.com/mapfiles/ms/micons/blue-dot.png';
		var markerOptionsBlue = { icon: myIconBlue};

<?php

foreach ($nodes as $node) {?>
node<?php echo $node->getId(); ?> = [];
<?php
}

$criteria = new Criteria();
$criteria->add(KataoUserPeer::GMAP_LAT, '', Criteria::NOT_EQUAL);
$criteria->add(KataoUserPeer::GMAP_LNG, '', Criteria::NOT_EQUAL);
$criteria->addJoin(KataoUserPeer::KATAO_MEMBER_ID, KataoMemberPeer::ID);
$members = KataoUserPeer::doSelectJoinKataoMember($criteria);
// var_dump($members);
foreach($members as/*(KataoUser)*/ $member):
?>
	var markerM<?php echo $member->getId(); ?> = new GMarker(new GLatLng(<?php printf('%f,%f', $member->getGmapLat(), $member->getGmapLng()); ?>), <?php echo (0 < $member->countKataoNodes())?'markerOptionsBlue':'markerOptionsRed' ?>);
	GEvent.addListener(markerM<?php echo $member->getId(); ?>, "click", function() {
		markerM<?php echo $member->getId(); ?>.openInfoWindowHtml('<?php echo str_replace(array("\r\n", "\n", "\r"), array('\n', '\n', ''), get_partial('katao/gmap_member', array('member' => $member))); ?>');
	});
	map.addOverlay(markerM<?php echo $member->getId(); ?>);
	node<?php echo $member->getKataoMember()->getKataoNodeId(); ?>.push(markerM<?php echo $member->getId(); ?>);
<?php
endforeach;

foreach (KataoSupplierPeer::retrieveByPKs(array_keys(KataoSupplierPeer::getAllVisible(true))) as $katao_supplier):
$supplier = $katao_supplier->getFirstKataoUser();
	if (KataoUser::STATUS_ACTIVE == $supplier->getStatus()) {
?>
	var markerS<?php echo $supplier->getId(); ?> = new GMarker(new GLatLng(<?php printf('%f,%f', $katao_supplier->getGmapLat(), $katao_supplier->getGmapLng()); ?>), markerOptionsGreen);
	GEvent.addListener(markerS<?php echo $supplier->getId(); ?>, "click", function() {
		markerS<?php echo $supplier->getId(); ?>.openInfoWindowHtml('<?php echo str_replace(array("\r\n", "\n", "\r"), array('\n', '\n', ''), get_partial('katao/gmap_supplier', array('supplier' => $supplier, 'popup' => true))); ?>');
	});
	map.addOverlay(markerS<?php echo $supplier->getId(); ?>);
	<?php foreach($katao_supplier->getKataoSupplierNodes() as/*(KataoSupplierNode)*/ $katao_supplier_node): ?>
	node<?php echo $katao_supplier_node->getKataoNodeId() ?>.push(markerS<?php echo $supplier->getId(); ?>);
	<?php endforeach ?>
<?php
	}
endforeach;

?>
      }
}

window.onunload = GUnload;

/* ]]> */
</script>

</div>

<br />
<div class="filter">
	<div class="filter-left">&nbsp;</div>
	<div class="filter-middle">
		<u>Légende</u> :
		&nbsp;<?php echo image_tag('http://maps.google.com/mapfiles/ms/micons/red-dot.png', array('alt' => '', 'align' => 'top', 'width' => 20, 'height' => 20)) ?> Adhérents
		&nbsp;-&nbsp;<?php echo image_tag('http://maps.google.com/mapfiles/ms/micons/blue-dot.png', array('alt' => '', 'align' => 'top', 'width' => 20, 'height' => 20)) ?> Délégués
		&nbsp;-&nbsp;<?php echo image_tag('http://maps.google.com/mapfiles/ms/micons/green-dot.png', array('alt' => '', 'align' => 'top', 'width' => 20, 'height' => 20)) ?> Partenaires
	</div>
	<div class="filter-right">&nbsp;</div>
</div>