var gmarkers = [];
var gicons = [];
var gmap;
var glat;
var glng;
var gzoom;

var diconfalse = createIcon('dealer', false);
var piconfalse = createIcon('parts', false);
var siconfalse = createIcon('service', false);
var dicontrue = createIcon('dealer', true);
var picontrue = createIcon('parts', true);
var sicontrue = createIcon('service', true);

//gicons["dealer"] = new GIcon(G_DEFAULT_ICON,"/common/siteimages/googlemaps/ico_blue_prone.png");
//gicons["parts"] = new GIcon(G_DEFAULT_ICON,"/common/siteimages/googlemaps/ico_red_prone.png");
//gicons["service"] = new GIcon(G_DEFAULT_ICON,"/common/siteimages/googlemaps/ico_green.png");

gicons["dealerfalse"] = diconfalse;
gicons["partsfalse"] = piconfalse;
gicons["servicefalse"] = siconfalse;
gicons["dealertrue"] = dicontrue;
gicons["partstrue"] = picontrue;
gicons["servicetrue"] = sicontrue;

function load() {
    glat = gup('lat');
    glng = gup('lng');
    gzoom = gup('zoom');
    if (glat==null) { glat = 50; }
    if (glng==null) { glng = 17; }
    if (gzoom==null) { gzoom = 7; }

    gmap = new GMap2(document.getElementById("map"));
    gmap.addControl( new GLargeMapControl() );
    gmap.addControl( new GMapTypeControl()) ;
    gmap.addControl( new GOverviewMapControl(new GSize(100,100)) );
    gmap.setCenter( new GLatLng(parseFloat(glat),parseFloat(glng)), parseInt(gzoom));
    GDownloadUrl("/opencms/opencms/system/modules/cz.avia.www/elements/googlemaps.jsp?state="+gup('state')+"&lang="+gup('lang'), function(data, responseCode) {parseJson(data);});
    //setTimeout("firstinit()",2000);
}

function createIcon(category, mirrored) {
var Icon = new GIcon();
      if (category=='dealer') {
 Icon.image = "/common/siteimages/googlemaps/ico_blue_prone.png";
      Icon.iconAnchor = new GPoint(0, 28);
Icon.iconSize = new GSize(28, 28);
      Icon.infoWindowAnchor = new GPoint(14, 2);
}
      if (category=='parts') {
Icon.image = "/common/siteimages/googlemaps/ico_red_prone.png";
      Icon.iconAnchor = new GPoint(28, 28);
Icon.iconSize = new GSize(28, 28);
      Icon.infoWindowAnchor = new GPoint(14, 2);
}
      if (category=='service') {
Icon.image = "/common/siteimages/googlemaps/ico_green.png";
      Icon.iconAnchor = new GPoint(9, 32);
Icon.iconSize = new GSize(19, 32);
      Icon.infoWindowAnchor = new GPoint(9, 2);
}

if (mirrored) {
      if (category=='dealer') {
 Icon.image = "/common/siteimages/googlemaps/ico_blue_prone_mirrored.png";
      Icon.iconAnchor = new GPoint(28, 0);
Icon.iconSize = new GSize(28, 28);
      Icon.infoWindowAnchor = new GPoint(14, 2);
}
      if (category=='parts') {
Icon.image = "/common/siteimages/googlemaps/ico_red_prone_mirrored.png";
      Icon.iconAnchor = new GPoint(0, 0);
Icon.iconSize = new GSize(28, 28);
      Icon.infoWindowAnchor = new GPoint(14, 2);
}
      if (category=='service') {
Icon.image = "/common/siteimages/googlemaps/ico_green_mirrored.png";
      Icon.iconAnchor = new GPoint(9, 0);
Icon.iconSize = new GSize(19, 32);
      Icon.infoWindowAnchor = new GPoint(9, 2);
}
}
      
      //Icon.shadow = "myshadow.png";
      //Icon.shadowSize = new GSize(36, 34);
      //Icon.iconAnchor = new GPoint(5, 32);
      //Icon.transparent = "/common/siteimages/googlemaps/ico_blue_prone.png";
      //Icon.printImage = "/common/siteimages/googlemaps/ico_blue_prone.png";
      //Icon.mozPrintImage = "/common/siteimages/googlemaps/ico_blue_prone.png";
      //Icon.printShadow = "/common/siteimages/googlemaps/ico_blue_prone.png";
      //Icon.imageMap=[9,0,6,1,4,2,2,4,0,8,0,12,1,14,2,16,5,19,7,23,8,26,9,30,9,34,11,34,11,30,12,26,13,24,14,21,16,18,18,16,20,12,20,8,18,4,16,2,15,1,13,0];

return Icon;
}

function createMarker(input) {
    var marker = new GMarker(new GLatLng(input.lat,input.lon), gicons[input.category+input.mirrored]);
    // === Store the category and name info as a marker properties ===
    marker.mycategory = input.category;                                 
    marker.myname = input.fullName;
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(formatWindow(input));
    });
    // save the info we need to use later for the side_bar
    gmarkers.push(marker);
    return marker;
}

// == shows all markers of a particular category, and ensures the checkbox is checked ==
function show(category) {
    for (var i=0; i<gmarkers.length; i++) {
        if (gmarkers[i].mycategory == category) {
            gmarkers[i].show();
        }
    }
    // == check the checkbox ==
    document.getElementById(category+"box").checked = true;
}

// == hides all markers of a particular category, and ensures the checkbox is cleared ==
function hide(category) {
    for (var i=0; i<gmarkers.length; i++) {
        if (gmarkers[i].mycategory == category) {
            gmarkers[i].hide();
        }
    }
    // == clear the checkbox ==
    document.getElementById(category+"box").checked = false;
    // == close the info window, in case its open on a marker that we just hid
    gmap.closeInfoWindow();
}

// == a checkbox has been clicked ==
function boxclick(box,category) {
    if (box.checked) {
        show(category);
    } else {
        hide(category);
    }
    // == rebuild the side bar
    makeSidebar();
}


// This function picks up the click and opens the corresponding info window
function myclick(i) {
    GEvent.trigger(gmarkers[i], "click");
}

// == rebuilds the sidebar to match the markers currently displayed ==
function makeSidebar() {
    var html = "";
    var cssclass = "";
    for (var i=0; i<gmarkers.length; i++) {
        if (!gmarkers[i].isHidden()) {
            if (gmarkers[i].mycategory=="dealer") cssclass="class='dealer'"; if (gmarkers[i].mycategory=="service") cssclass="class='service'"; if (gmarkers[i].mycategory=="parts") cssclass="class='parts'";
            html += '<a ' + cssclass + ' href="javascript:myclick(' + i + ')">' + gmarkers[i].myname + '</a><br>';
        }
    }
    document.getElementById("side_bar").innerHTML = html;
}

function parseJson (doc) {
    //alert(doc);
    var jsonData = eval("("+ doc+")");
    for (var i = 0; i < jsonData.markers.length; i++) {
	try {
            var marker = createMarker(jsonData.markers[i]);
            gmap.addOverlay(marker);
        } catch (x) {

        }
    }

firstinit();
}
function formatWindow (input) {
    return input.html;
}

function firstinit() {
    if (gup('cat')==null) {
        document.getElementById("allbox").checked=true;
        show("dealer");
        show("parts");
        show("service");
    }
    if (gup('cat')=="dealer") {
        show("dealer");
        hide("parts");
        hide("service");
    }
    if (gup('cat')=="parts") {
        hide("dealer");
        show("parts");
        hide("service");
    }
    if (gup('cat')=="service") {
        hide("dealer");
        hide("parts");
        show("service");
    }
    // == create the initial sidebar ==
    makeSidebar();
}

function gup(name)
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec(window.location.href);
    if( results == null )
        return null;
    else
        return results[1];
}

function selectAll9() {
    if (document.getElementById("allbox").checked==true) {
        document.getElementById("dealerbox").checked=true;
        document.getElementById("partsbox").checked=true;
        document.getElementById("servicebox").checked=true;
        show("dealer");
        show("parts");
        show("service");
        makeSidebar();
    } else {
        document.getElementById("dealerbox").checked=false;
        document.getElementById("partsbox").checked=false;
        document.getElementById("servicebox").checked=false;
        hide("dealer");
        hide("parts");
        hide("service");
        makeSidebar();
    }
}

function showCurCenter() {
    alert(gmap.getCenter() + " --- zoom: " + gmap.getZoom());
}

window.onload = load;
window.onunload = GUnload;
