﻿var map, manager;
var centerLatitude = 50.419, centerLongitude = 30.5291, startZoom = 12;


function createMarkerClickHandler(marker, text, link) {
  return function() {
    marker.openInfoWindowHtml(
      '<h3>' + text + '</h3>' +
      '<p><a href="' + link + '" target="_blank" >Страница магазина &raquo;</a></p>'  
    );
    return false;
  };
}


function createMarker(pointData) {
  var latlng = new GLatLng(pointData.latitude, pointData.longitude);

  var icon = new GIcon();
  icon.image = 'marker-red-ws.png';
  icon.iconSize = new GSize(32, 32);
  icon.iconAnchor = new GPoint(16, 16);
  icon.infoWindowAnchor = new GPoint(25, 7);

  opts = {
    "icon": icon,
    "clickable": true,
    "labelText": pointData.abbr,
    "labelOffset": new GSize(-16, -16)
  };
 
  var marker = new LabeledMarker(latlng, opts);
  var handler = createMarkerClickHandler(marker, pointData.name, pointData.wp);
	
  GEvent.addListener(marker, "click", handler);

  var listItem = document.createElement('li');
  listItem.innerHTML = '<a href="' + pointData.wp + '">' + pointData.name + '</a>';
  listItem.getElementsByTagName('a')[0].onclick = handler;

  document.getElementById('sidebar-list').appendChild(listItem);

  return marker;
}

function windowHeight() {
  // Standard browsers (Mozilla, Safari, etc.)
  if (self.innerHeight) {
    return self.innerHeight;
  }
  // IE 6
  if (document.documentElement && document.documentElement.clientHeight) {
   return document.documentElement.clientHeight;
  }
  // IE 5
  if (document.body) {
    return document.body.clientHeight;
  }
  // Just in case. 
  return 0;
}

function handleResize() {
  var height = windowHeight() - document.getElementById('toolbar').offsetHeight ;
  document.getElementById('map').style.height = height + 'px';
  document.getElementById('sidebar').style.height = height + 'px';
  
}

function init() {
 handleResize();
	
  map = new GMap(document.getElementById("map"));
 // map.addControl(new GSmallMapControl());
 
  var customUI = map.getDefaultUI();
        // Remove MapType.G_HYBRID_MAP
        customUI.maptypes.hybrid = false;
        map.setUI(customUI);
   
   
   
  map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
  //map.addControl(new GMapTypeControl());

  manager = new MarkerManager(map);
	
  // This is a sorting trick, don't worry too much about it.
  //markers.sort(function(a, b) { return (a.abbr > b.abbr) ? +1 : -1; }); 
	
  batch = [];
  for(id in markers) {
    batch.push(createMarker(markers[id]));
  }
  manager.addMarkers(batch, 6);
 // manager.refresh();
  
   if(window.location.hash){
    for(id in markers){ 
    	// showCurentLincen(markers[id]);
		var topBAR = window.location.href.split('|')[1];
		var hash = window.location.href.split('#')[1];
		if(topBAR=="1"){
		document.getElementById('toolbar').style.display="none";
		document.getElementById('sidebar').style.display="none";
		document.getElementById('map-wrapper').style.margin="0";
		   handleResize();
		   
		   
		  hash=hash.replace("|1", "");
		}
		
		//document.title=hash ;
		
		 if (markers[id].hash == hash) {
		   manager.refresh();   
			     //document.title+=" _epp_";
				//markers[id].handler();
				map.setCenter(new GLatLng(markers[id].latitude, markers[id].longitude), 16);
				setTimeout(batch[id].__e_['click'][0].xh,1000);
				 
			 }
		 }

   } else {
  
      manager.refresh(); 
   
   }
   manager.refresh(); 
}

//window.onresize = handleResize;
window.onload = init;
window.onunload = GUnload;
