﻿var map = null;
var gmarkers = new Array(); //jednotlive body na mape
var firma = new Array();
var x = 0;
var y = 0;
var zoomLevel = 0;
var zzID = -1;
var bc = "";
var oblast = "";

function FirmaInfo(id, x, y, nazev, ulice, cislo, psc, mesto, pobocka)
{
    this.x = x;
    this.y = y;
    this.id = id;
    this.nazev = nazev;
    this.ulice = ulice;
    this.cislo = cislo;
    this.psc = psc;
    this.mesto = mesto;
    this.pobocka = pobocka
}

function vratHTML(fi)
{
    var back = "";
    if (bc != "")
        back = "&bc=" + bc;

    if (back == "")
        back = "&oblast=" + oblast
    
    var html = "<p class='nazev'>" + fi.nazev + "</p><br/>";
    html += fi.ulice + " " + fi.cislo + "<br/>";
    html += fi.psc + ", " + fi.mesto + "<br/>";
    html += "<br/><br/>"
    html += "<a href='Detail.aspx?firma=" + fi.id + back +"'>Detail</a>";
    
    return html;
}

function ZviraznitFirmu(id, zviraznit)
{
    //alert(id +" - "+ zviraznit);
    if (zviraznit)
        $(".link_" + id).css("font-weight", "bold");
    else
        $(".link_" + id).css("font-weight", "normal");
}

function addClickevent(marker)
{ // Add a click listener to the markers

    GEvent.addListener(marker, "click", function()
    {
        marker.openInfoWindowHtml(marker.content);
    });
    GEvent.addListener(marker, "mouseover", function()
    {
        ZviraznitFirmu(marker.title, true);
    });
    GEvent.addListener(marker, "mouseout", function()
    {
        ZviraznitFirmu(marker.title, false);
    }); 

    return marker;
}

//Mapa
function IniMapa() 
{
    if (GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setUIToDefault();

        map.clearOverlays()
        gmarkers = new Array();
        var rectObj = new GLatLngBounds();
        for (var i = 0; i < firma.length; i++)
        {
            var tinyIcon = new GIcon();
            tinyIcon.image = "MapaBublina.ashx?i=" + (i + 1) + "&t=" + (firma[i].pobocka ? "2" : "1");
            tinyIcon.iconSize = new GSize(20, 30);

            tinyIcon.shadowSize = new GSize(25, 32);
            tinyIcon.iconAnchor = new GPoint(10, 33);
            tinyIcon.infoWindowAnchor = new GPoint(12, 2);
            tinyIcon.infoShadowAnchor = new GPoint(12, 25);
            
            var gm = new GMarker(new GLatLng(firma[i].y, firma[i].x), tinyIcon);
            gm.content = vratHTML(firma[i]);
            gm.title = firma[i].id;
            addClickevent(gm);
            gmarkers.push(gm);
            
            rectObj.extend(new GLatLng(firma[i].y, firma[i].x));
            map.addOverlay(gmarkers[i]);
        }

        if (x != 0 && y != 0)
        {
            rectObj = new GLatLngBounds();
            rectObj.extend(new GLatLng(y, x));
            
            //map.setCenter(new GLatLng(x, y), zoomLevel); //7
        }
        
        map.setCenter(rectObj.getCenter(), map.getBoundsZoomLevel(rectObj));
    }
}
