﻿//////////////////////////////////////////////////////////////////////
//
// Tworzenie mapy i wlasnych obiektow.
// (C) PHZ VIPOL Sp. z o.o.
// Powered by Google Maps
// Autor: Marcin Ziemian
// Wykorzystywanie niniejszego skryptu oraz zawartych w nim danych
//  bez zgody autora niedozwolone!
//
/////////////////////////////////////////////////////////////////////////

Array.prototype.inArray = function(v){
  for(var i in this){if(this[i] == v){return true;}}return false;
};

var map;
var gmarkers = [];
var mapCenter;
var initialZoom = 17;
var initialMapType = G_HYBRID_MAP;
var budynek33;
var budynek37a;
var budynek42;
var budynek44;
var budynek46;
var budynek48;
var hqIcon;
var Icon33;
var Icon37a;
var Icon42;
var Icon44;
var Icon46;
var IconWierz;
var IconXiamen;

var texty = ["pl","en"];
texty["pl"] = {
    clickToMoveTo: "Kliknij, aby przenieść się do",
    naviInfo: "Aby powiększyć/pomniejszyć skalę użyj kółka myszki, lub kontrolek po lewej stronie.",
    start: "Początek",
    hqIconTitle: "Siedziba PHZ VIPOL Sp. z o.o., Warszawa, ul. Młynarska 48",
    icon46Title: "Bud. Młynarska 46",
    icon44Title: "projektowany bud. Młynarska 44",
    icon42Title: "Bud. Młynarska 42",
    icon37aTitle: "Bud. Młynarska 37a",
    icon33Title: "Bud. Młynarska 33",
    iconXiamenTitle: "Placówka PHZ VIPOL Sp. z o.o. w Xiamen, Chińska Republika Ludowa",
    iconWierzTitle: "Yacht Club Marina w Wierzbicy"
}

texty["en"] = {
    clickToMoveTo: "Click, to move to location",
    naviInfo: "You may use of mouse scroll wheel or left-sided controls to zoom-in/zoom-out the map.",
    start: "Start",
    hqIconTitle: "Home location of VIPOL Foreign Trade Company LTD., Warsaw, 48 Mlynarska Str.",
    icon46Title: "Mlynarska 46 building",
    icon44Title: "projected Mlynarska 44 building",
    icon42Title: "Mlynarska 42 building",
    icon37aTitle: "Mlynarska 37a building",
    icon33Title: "Mlynarska 33 building",
    iconXiamenTitle: "our trade office in Xiamen, Peoples Republic of China",
    iconWierzTitle: "Marina Yacht Club - Wierzbica"
}

var lang = readCookie("lang");


function appendOnloadEvent(eventHandler) {
  if (window.onload) {
    window.onload = new Function(
    window.onload.toString().replace(/[\r\n]/g, '').replace(/\s+$|^\s+/g, '').replace(/^function\s*\w+\s*\(\w*\)\s*\{\s*(.*)\s*\}$/,'$1\n' + eventHandler));
  } else {
    window.onload = new Function(eventHandler);
  }
}

function PrzesunMapeDoPunktu(i) {
	if (i==0) {
		map.setCenter(mapCenter, initialZoom);
	} else {
		map.setCenter(gmarkers[i].getPoint(), 18);
	}
}

function mapaStart() {
//    if (google.maps.BrowserIsCompatible()) {
	var lista_lokacji = texty[lang].clickToMoveTo+':<ul><li><a href="javascript:PrzesunMapeDoPunktu(0)">'+texty[lang].start + '</a></li>';
	var htmls = [];
	var i = 0;

	// basic functions for navigation and display of objects:
	function importanceOrder (marker) {
		return (marker.importance*1000);
	}

	function createMarker(punkt, ikona, waga) {
		var marker = new PdMarker(punkt, {icon: ikona, name: ikona.name, zIndexProcess: importanceOrder});

		marker.importance = waga;
		marker.tooltip = '<div class="tooltip">'+ikona.title+'</div>';
		google.maps.Event.addListener(marker, "mouseover", function() {
			showTooltip(marker);
		});
		google.maps.Event.addListener(marker, "mouseout", function() {
			tooltip.style.visibility="hidden";
		});
		gmarkers[i] = marker;
		htmls[i] = ikona.title;
		lista_lokacji += '<li><a href="javascript:PrzesunMapeDoPunktu(' + i + ')">' + ikona.title + '</a></li>';
		i++;
		return marker;
	}

	// ====== This function displays the tooltip ======
	// it can be called from an icon mousover or a side_bar mouseover
	function showTooltip(marker) {
		tooltip.innerHTML = marker.tooltip;
		var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
		var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
		var anchor=marker.getIcon().iconAnchor;
		var width=marker.getIcon().iconSize.width;
		var pos = new google.maps.ControlPosition(G_ANCHOR_BOTTOM_LEFT, new google.maps.Size(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); 
		pos.apply(tooltip);
		tooltip.style.visibility="visible";
	}


	//deklaracje obiektów i zmiennych
	var gruboscLinii = 1; // pixels
	var gruboscLiniiProj = 1; //pixels
	var gruboscLiniiGranicy = 3;
	var kolorLinii = '#074085'; // blue
	var kolorLiniiProj = '#4062DB';
	var kolorLiniiGranicy = '#76D41E';
	var przezLinii = 0.7;
	var przezLiniiProj = 0.5;
	var przezLiniiGranicy = 0.8;
	var kolorWypelnienia = '#074085';
	var kolorWypelnieniaProj = '#4062DB';
	var kolorWypelnieniaGranicy = '#76D41E';
	var przezWypelnienia = 0.5;
	var przezWypelnieniaProj = 0.3;
	var przezWypelnieniaGranicy = 0;

	//tworzenie poligonów budynków, oraz linii granicznej
	budynek42 = new google.maps.Polygon([
		new google.maps.LatLng(52.23978, 20.97123),
		new google.maps.LatLng(52.23987, 20.97184),
		new google.maps.LatLng(52.24004, 20.97179),
		new google.maps.LatLng(52.23995, 20.97115),
		new google.maps.LatLng(52.23978, 20.97121)], kolorLinii, gruboscLinii, przezLinii, kolorWypelnienia, przezWypelnienia);

	budynek44 = new google.maps.Polygon([
		new google.maps.LatLng(52.240515, 20.971740),
		new google.maps.LatLng(52.240175, 20.971870),
		new google.maps.LatLng(52.240230, 20.972206),
		new google.maps.LatLng(52.240564, 20.972057),
		new google.maps.LatLng(52.240515, 20.97174)], kolorLiniiProj, gruboscLiniiProj, przezLiniiProj, kolorWypelnieniaProj, przezWypelnieniaProj);

	budynek46 = new google.maps.Polygon([
		new google.maps.LatLng(52.240134, 20.970974),
		new google.maps.LatLng(52.240088, 20.971065),
		new google.maps.LatLng(52.240138, 20.971150),
		new google.maps.LatLng(52.240397, 20.971042),
		new google.maps.LatLng(52.240390, 20.970980),
		new google.maps.LatLng(52.240472, 20.970955),
		new google.maps.LatLng(52.240385, 20.970883),
		new google.maps.LatLng(52.240134, 20.970974)], kolorLinii, gruboscLinii, przezLinii, kolorWypelnienia, przezWypelnienia);

	budynek48 = new google.maps.Polygon([
		new google.maps.LatLng(52.240390, 20.970980),
		new google.maps.LatLng(52.240438, 20.971320),
		new google.maps.LatLng(52.240648, 20.971240),
		new google.maps.LatLng(52.240605, 20.970905),
		new google.maps.LatLng(52.240390, 20.970980)], kolorLinii, gruboscLinii, przezLinii, kolorWypelnienia, przezWypelnienia);

	budynek37a = new google.maps.Polygon([
		new google.maps.LatLng(52.240347, 20.970555),
		new google.maps.LatLng(52.240077, 20.970657),
		new google.maps.LatLng(52.240045, 20.970453),
		new google.maps.LatLng(52.240314, 20.970330),
		new google.maps.LatLng(52.240347, 20.970555)], kolorLinii, gruboscLinii, przezLinii, kolorWypelnienia, przezWypelnienia);

	budynek33 = new google.maps.Polygon([
		new google.maps.LatLng(52.239822, 20.970705),
		new google.maps.LatLng(52.239526, 20.970813),
		new google.maps.LatLng(52.239506, 20.970598),
		new google.maps.LatLng(52.239565, 20.970582),
		new google.maps.LatLng(52.239523, 20.970110),
		new google.maps.LatLng(52.239743, 20.970024),
		new google.maps.LatLng(52.239822, 20.970705)], kolorLinii, gruboscLinii, przezLinii, kolorWypelnienia, przezWypelnienia);

	granica = new google.maps.Polygon([
		new google.maps.LatLng(52.239660, 20.971040),
		new google.maps.LatLng(52.239860, 20.972410),
		new google.maps.LatLng(52.240945, 20.971955),
		new google.maps.LatLng(52.240790, 20.970630),
		new google.maps.LatLng(52.239660, 20.971040)], kolorLiniiGranicy, gruboscLiniiGranicy, przezLiniiGranicy, kolorWypelnieniaGranicy, przezWypelnieniaGranicy);

	granica37a = new google.maps.Polygon([
		new google.maps.LatLng(52.240075, 20.970781),
		new google.maps.LatLng(52.240032, 20.970426),
		new google.maps.LatLng(52.240314, 20.970330),
		new google.maps.LatLng(52.240360, 20.970684),
		new google.maps.LatLng(52.240075, 20.970781)], kolorLiniiGranicy, gruboscLiniiGranicy, przezLiniiGranicy, kolorWypelnieniaGranicy, przezWypelnieniaGranicy);

	granica33 = new google.maps.Polygon([
		new google.maps.LatLng(52.239523, 20.970979),
		new google.maps.LatLng(52.239460, 20.970105),
		new google.maps.LatLng(52.239763, 20.970024),
		new google.maps.LatLng(52.239864, 20.970861),
		new google.maps.LatLng(52.239523, 20.970979)], kolorLiniiGranicy, gruboscLiniiGranicy, przezLiniiGranicy, kolorWypelnieniaGranicy, przezWypelnieniaGranicy);

	hqIconOptions = {
	    name: "hqIcon",
	    title: texty[lang].hqIconTitle,
	    image: "http://maps.google.com/mapfiles/kml/pal3/icon21.png",
	    shadow: "http://maps.google.com/mapfiles/kml/pal3/icon21s.png",
	    iconSize: new google.maps.Size(32, 32),
	    shadowSize: new google.maps.Size(59, 32),
	    iconAnchor: new google.maps.Point(16, 16),
	    infoWindowAnchor: new google.maps.Point(32, 1)};

	icon33Options = {
	    name: "Icon33",
	    title: texty[lang].icon33Title,
	    image: "http://maps.google.com/mapfiles/kml/pal3/icon21.png",
	    shadow: "http://maps.google.com/mapfiles/kml/pal3/icon21s.png",
	    iconSize: new google.maps.Size(32, 32),
	    shadowSize: new google.maps.Size(59, 32),
	    iconAnchor: new google.maps.Point(16, 16),
	    infoWindowAnchor: new google.maps.Point(32, 1)};

	icon37aOptions = {
	    name: "Icon37a",
	    title: texty[lang].icon37aTitle,
	    image: "http://maps.google.com/mapfiles/kml/pal3/icon21.png",
	    shadow: "http://maps.google.com/mapfiles/kml/pal3/icon21s.png",
	    iconSize: new google.maps.Size(32, 32),
	    shadowSize: new google.maps.Size(59, 32),
	    iconAnchor: new google.maps.Point(16, 16),
	    infoWindowAnchor: new google.maps.Point(32, 1)};

	icon42Options = {
	    name: "Icon42",
	    title: texty[lang].icon42Title,
	    image: "http://maps.google.com/mapfiles/kml/pal3/icon21.png",
	    shadow: "http://maps.google.com/mapfiles/kml/pal3/icon21s.png",
	    iconSize: new google.maps.Size(32, 32),
	    shadowSize: new google.maps.Size(59, 32),
	    iconAnchor: new google.maps.Point(16, 16),
	    infoWindowAnchor: new google.maps.Point(32, 1)};

	icon44Options = {
	    name: "Icon44",
	    title: texty[lang].icon44Title,
	    image: "http://maps.google.com/mapfiles/kml/pal3/icon21.png",
	    shadow: "http://maps.google.com/mapfiles/kml/pal3/icon21s.png",
	    iconSize: new google.maps.Size(32, 32),
	    shadowSize: new google.maps.Size(59, 32),
	    iconAnchor: new google.maps.Point(16, 16),
	    infoWindowAnchor: new google.maps.Point(32, 1)};

	icon46Options = {
	    name: "Icon46",
	    title: texty[lang].icon46Title,
	    image: "http://maps.google.com/mapfiles/kml/pal3/icon21.png",
	    shadow: "http://maps.google.com/mapfiles/kml/pal3/icon21s.png",
	    iconSize: new google.maps.Size(32, 32),
	    shadowSize: new google.maps.Size(59, 32),
	    iconAnchor: new google.maps.Point(16, 16),
	    infoWindowAnchor: new google.maps.Point(32, 1)};

	iconXiamenOptions = {
	    name: "IconXiamen",
	    title: texty[lang].iconXiamenTitle,
	    image: "http://maps.google.com/mapfiles/kml/pal3/icon56.png",
	    shadow: "http://maps.google.com/mapfiles/kml/pal3/icon56s.png",
	    iconSize: new google.maps.Size(32, 32),
	    shadowSize: new google.maps.Size(59, 32),
	    iconAnchor: new google.maps.Point(16, 16),
	    infoWindowAnchor: new google.maps.Point(32, 1)};

	iconWierzOptions = {
	    name: "IconWierz",
	    title: texty[lang].iconWierzTitle,
	    image: "http://maps.google.com/mapfiles/kml/pal3/icon56.png",
	    shadow: "http://maps.google.com/mapfiles/kml/pal3/icon56s.png",
	    iconSize: new google.maps.Size(32, 32),
	    shadowSize: new google.maps.Size(59, 32),
	    iconAnchor: new google.maps.Point(16, 16),
	    infoWindowAnchor: new google.maps.Point(32, 1)};

	//tworzenie punktu osadzenia ikon
	hqIcon = new google.maps.Icon(hqIconOptions);
	Icon33 = new google.maps.Icon(icon33Options);
	Icon37a = new google.maps.Icon(icon37aOptions);
	Icon42 = new google.maps.Icon(icon42Options);
	Icon44 = new google.maps.Icon(icon44Options);
	Icon46 = new google.maps.Icon(icon46Options);
	IconWierz = new google.maps.Icon(iconWierzOptions);
	IconXianmen = new google.maps.Icon(iconXiamenOptions);

	//okre¶lanie punktów osadzenia markerów budynków i miejsc firmowych
	mapCenter = granica.getBounds().getCenter();
	var CenterOfHQ = budynek48.getBounds().getCenter();
	var CenterOf42 = budynek42.getBounds().getCenter();
	var CenterOf44 = budynek44.getBounds().getCenter();
	var CenterOf46 = budynek46.getBounds().getCenter();
	var CenterOf37a = budynek37a.getBounds().getCenter();
	var CenterOf33 = budynek33.getBounds().getCenter();
	var CenterOfWierzbica = new google.maps.LatLng(52.531440,21.075317);
	var CenterOfXianmenOffice = new google.maps.LatLng(24.486201,118.115059);

	//utworzenie instancji mapy i okreslenie srodka
	map = new google.maps.Map2(document.getElementById("map"));
	map.setCenter(mapCenter, initialZoom, initialMapType);

	//generowanie layout-u mapy (wzbogacanie o kontrolki i wła¶ciwo¶ci)
	map.addControl(new google.maps.LargeMapControl());
	map.addControl(new google.maps.MapTypeControl());
	// map.addControl(new google.maps.OverviewMapControl());
	map.enableContinuousZoom();
	map.enableScrollWheelZoom();

	//nakładanie na mapę własnych obiektów (budynki, punkty, granice)
	map.addOverlay(granica);
	map.addOverlay(granica33);
	map.addOverlay(granica37a);
	map.addOverlay(budynek33);
	map.addOverlay(budynek37a);
	map.addOverlay(budynek42);
	map.addOverlay(budynek44);
	map.addOverlay(budynek46);
	map.addOverlay(budynek48);

	//markery chowane w zależno¶ci od skali mapy
	map.addOverlay(createMarker(CenterOf33, Icon33, 2));
	map.addOverlay(createMarker(CenterOf37a, Icon37a, 3));
	map.addOverlay(createMarker(CenterOf42, Icon42, 4));
	map.addOverlay(createMarker(CenterOf44, Icon44, 5));
	map.addOverlay(createMarker(CenterOf46, Icon46, 6));

	// markery od 0 do 2- zawsze widoczne.
	map.addOverlay(createMarker(CenterOfHQ, hqIcon, 10));
	map.addOverlay(createMarker(CenterOfWierzbica, IconWierz, 1));
	map.addOverlay(createMarker(CenterOfXianmenOffice, IconXianmen, 1));

	var tooltip = document.createElement("div");
	document.getElementById("map").appendChild(tooltip);
	tooltip.style.visibility="hidden";

	// put the assembled side_bar_html contents into the side_bar div
	lista_lokacji += "</ul><br><i>"+texty[lang].naviInfo+"</i>";
	document.getElementById("lokacje").innerHTML = lista_lokacji;

}

appendOnloadEvent('mapaStart()');
window.onunload = new function() { GUnload(); };

