var map1 = 0;
var map2 = 0;
var map1x = 19.67376708984375;
var map1y = 46.09073379814453;
var map2x = 19.757537841796875;
var map2y = 46.0864478202876;
var places;
var unit_id = 0;
var year = 0;
var month = 0;
var day = 0;
var icons = "";
var baseIcon = new GIcon();

function createMarker(point, clss, placeidx, html, polen) {
    
    var icon = new GIcon(baseIcon);
    
    if (polen) {
        icon.image = 'http://www.manufaktura.rs/gis/map/gfx/polen/'+clss+'.png';
    } else {
        icon.image = 'http://www.manufaktura.rs/gis/map/gfx/'+clss+'_'+ placeidx + '.png';
    }
    
    icons += icon.image + '\n';
    var marker = new GMarker(point, icon);

    // Show this marker's index in the info window when it is clicked.
    GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
    });

    return marker;

}
function center_map(idx) {
    if (idx == 1) {
        map1.centerAndZoom(new GPoint(map1x, map1y), 4);
    } else {
        map2.centerAndZoom(new GPoint(map2x, map2y), 4);
    }
};



function init_maps(pstr) {
    pstr = unescape(pstr);
    var arr = pstr.split('__');
    var arr2 = new Array();
    places = new Array();
    for (i=0;i<arr.length;i++) {
        arr2 = arr[i].split('--');
        place = new Array();
        place['idx'] = arr2[0];
        place['class'] = arr2[1];
        place['x'] = arr2[2];
        place['y'] = arr2[3];
        place['html'] = arr2[4];
        place['category'] = arr2[5];
        places[i] = place;
    }
    icons = '';
    init_baseIcon(place['category'] == 'polen' ? 1 : 0);
    if (!map1) {
        map1 = init_map(1, map1x, map1y);
        center_map(1);
    }
    load_overlays(map1);
    if (!map2) {
        map2 = init_map(2, map2x, map2y);
        center_map(2);
    }
    load_overlays(map2);

    ch_unit_style();
}
function init_baseIcon(polen) {
    if (polen) {
        baseIcon.iconSize = new GSize(23, 46);
        baseIcon.shadowSize = new GSize(39, 46);
        baseIcon.iconAnchor = new GPoint(12, 46);
        baseIcon.infoWindowAnchor = new GPoint(12, 15);
        baseIcon.infoShadowAnchor = new GPoint(23, 25);
        baseIcon.shadow = 'http://www.manufacture.rs/gis/map/gfx/polen/senka.png';
    } else {
        baseIcon.iconSize = new GSize(18, 46);
        baseIcon.shadowSize = new GSize(38, 46);
        baseIcon.iconAnchor = new GPoint(9, 46);
        baseIcon.infoWindowAnchor = new GPoint(9, 15);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);
        baseIcon.shadow = 'http://www.manufacture.rs/gis/map/gfx/q_shadow.png';
    }
}
function init_map(id, cx, cy) {
    var map = new GMap(document.getElementById('map'+id));
    map.addControl(new GSmallMapControl());
/*
    GEvent.addListener(map, "moveend", function() {
            var center = map.getCenterLatLng();
            var latLngStr = '(' + center.x + ', ' + center.y + ')';
            document.getElementById('message'+id).innerHTML = latLngStr;
        }
    );
*/
    map.setMapType(G_SATELLITE_MAP);
    return map;
}
function load_overlays(map) {
    for (i=0;i<places.length;i++) {
        if (places[i]['class'] != undefined) {
            var point = new GPoint(places[i]['x'], places[i]['y']);
            var marker = createMarker(point, places[i]['class'], places[i]['idx'], places[i]['html'], places[i]['category']=='polen'?1:0);
            map.addOverlay(marker);
        }
    }
}
function clear_maps() {
    if (map1) {
        map1.clearOverlays();
    }
    if (map2) {
        map2.clearOverlays();
    }
}


function ch_unit(id, row) {
    var old_unit_id = unit_id;
    unit_id = id;
    if (old_unit_id) {
        clear_maps();
        if (o = find_handle('unit_'+old_unit_id)) {
           o.backgroundColor = '#cce0b8'; 
        }
        //xajax_ch_unit(id);
        xmlreq("ch_map.php?unit_id="+unit_id, ch_map_resp);
    } else {
        ch_unit_style();
    }
    for (j=0;j<3;j++) {
        if (h = find_handle('datum_'+j)) {
            if (j == row) {
                h.display = 'inline';
            } else {
                h.display = 'none';
            }

        }
    }
}
function ch_map_resp(rid) {
    init_maps(me_request[rid].response);
}
function ch_unit_style() {
    if (o = find_handle('unit_'+unit_id)) {
       o.backgroundColor = '#fff'; 
    }
}
function ch_date(row) {
    var yf = 'show_year'+row;
    var mf = 'show_month'+row;
    var df = 'show_day'+row;
    var ny = document.values.elements[yf].value;
    var nm = document.values.elements[mf].value;
    var nd;
    if (row != 2) {
        nd = document.values.elements[df].value;
    } else {
        nd = 0;
    }
    if ((ny != year) || (nm != month) || (nd != day)) {
        year = ny;
        month = nm;
        day = nd;
        for (i=0;i<3;i++) {
            yf = 'show_year'+i;
            mf = 'show_month'+i;
            df = 'show_day'+i;
            document.values.elements[yf].value = year;
            document.values.elements[mf].value = month;
            if (i != 2) {
                document.values.elements[df].value = day;
            }
        }
        xmlreq("x_refreshrow.php?row="+row+"&show_year="+year+"&show_month="+month+"&show_day="+day, ch_date_resp);
    }
}
function ch_date_resp(rid) {
    aresp = me_request[rid].response.split('__');
    for (i=0;i<aresp.length;i++) {
        arow = aresp[i].split('--');
        if (o = find_object('unitrow_'+arow[0])) {
            o.innerHTML = arow[1];
        }
    }
    clear_maps();
    xmlreq("ch_map.php", ch_map_resp);
}
function open_tree(place) {
    if ((h = find_handle('placeunits_'+place)) && (o=find_object('pm_'+place))) {
        if (o.innerHTML.indexOf('-') != -1) {
            o.innerHTML = '+';
            h.display = 'none';
        } else {
            o.innerHTML = '-';
            h.display = 'block';
        }
    }
}


