var map = 0;
var icon = 0;
var selects_prefix = 'locations_list_'
var icon_path = '/f/1/global/flag.png';
var icon_width = '32';
var icon_height = '32';
var map_center = {
        lat: 40.371838716147,
        lng: 47.944305515289
}

var optiongroups = [];
        optiongroups[0] = {
                label: 'Müştəri Xidmətləri ofisləri',
                id: 'services'
        };
        optiongroups[1] = {
                label: 'Azercell Ekspres ofisləri',
                id: 'expresses'
        };

var location_arr = [];
        location_arr[0] = {
                lat: '40.400218294643', // latitude
                lng: '49.8097', // longitude
                type: 'capital', // CAPITAL or REGION - kind of office placement (category)
                optionsgroup: false,
                heading: 'Azercell Baş ofisi &mdash; Tbilisi pr-ti, ev 61a', // an select option text
                address: 'Azərbaycan, Bakı ş.,&lt;br /&gt; Tbilisi pr-ti, ev 61a', // pop-up
                html: '&lt;strong&gt;Azercell Baş ofisi&lt;/strong&gt;&lt;br /&gt;Azərbaycan, Bakı ş., Tbilisi pr-ti, ev 61a&lt;br /&gt;&lt;strong&gt;Tel:&lt;/strong&gt; +99 412 496-70-07&lt;br /&gt;&lt;strong&gt;Faks:&lt;/strong&gt; +99 412 430-05-68' // description (useful info below the select-box)
        }
        location_arr[1] = {
                lat: '40.370961675665',
                lng: '49.838058767336',
                type: 'capital',
                optionsgroup: '0',
                heading: 'Müştəri Xidmətləri ofisləri &mdash; Z. Tağıyev küç. ev 18',
                address: 'Azərbaycan, Bakı ş., Z. Tağıyev küç. ev 18',
                html: 'Iş rejimi: Bazar ertəsi — Şənbə: 09:00—20:00; Bazar, bayram günləri və ümumxalq hüzn günü - 09:00—18:00'
        }
        location_arr[2] = {
                lat: '40.360961675665',
                lng: '49.828058767336',
                type: 'capital',
                optionsgroup: '0',
                heading: 'Müştəri Xidmətləri ofisləri &mdash; B. Bağırova küç. ev 2',
                address: 'Azərbaycan, Bakı ş., B. Bağırova küç. ev 2',
                html: 'Iş rejimi: Bazar ertəsi — Bazar: 10:00—19:00 (nahar fasiləsi: 14:00—15:00).Bayram günləri və ümumxalq hüzn günü — rəsmi iş günü hesab edilmir. '
        }

function showMap() {
        if (GBrowserIsCompatible()) { // MAP INITIALIZING
                map = new GMap2(document.getElementById("contactsMap"));
                map.setCenter(new GLatLng(map_center.lat, map_center.lng), 6);
                map.enableDoubleClickZoom();
                map.addControl(new GSmallZoomControl());
                //map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10, 10)));
                var place = 0;
                var marker = 0;
                office = new google.maps.Icon(); // ICONS DEFINITION
                office.image = icon_path;
                office.iconSize = new GSize(icon_width, icon_height);
                office.iconAnchor = new GPoint(0, icon_height);
                //office.infoWindowAnchor = new GPoint(9, 25);
                office.shadow = "";
                office.shadowSize = new GSize(22, 20);
                var option = 0;
                var container = 0;
                var container_id = 0;
                for (point in location_arr) { // ADDING POINTS TO MAP AND OPTIONS TO SELECTS
                        place = location_arr[point];
                        option = document.createElement('option');
                        option.value = point;
                        option.innerHTML = place.heading;
                        if (place.optionsgroup) {
                                container_id = place.type+optiongroups[place.optionsgroup].id;
                                container = document.getElementById(container_id)
                                if (!container) {
                                        container = document.createElement('optgroup');
                                        container.id = container_id;
                                        container.label = optiongroups[place.optionsgroup].label;
                                        document.getElementById(selects_prefix+place.type).appendChild(container);
                                }
                                container.appendChild(option);
                        } else {
                                document.getElementById(selects_prefix+place.type).appendChild(option);
                        }
                        marker = new GMarker(new GLatLng(place.lat, place.lng), {icon: office}); // MARKER PLACEMENT TO CONCRETE LOCATION
                        marker.num = ''+point+'';
                        GEvent.addListener(marker, "click", function(coords, info, description){
                                return function(){
                                        //map.openInfoWindowHtml(coords, info);
                                        document.getElementById('location_description').innerHTML = (description? description: info);
                                };
                        }(new GLatLng(place.lat, place.lng), html_entity_decode(place.address), html_entity_decode(place.html)));
                        map.addOverlay(marker);
                }
        }
}

function getPlace(id, type, zoom) {
        if (id==='') {return false;}
        var lat, lng, zoom, point, html, heading, address;
        lat = location_arr[id]['lat'];
        lng = location_arr[id]['lng'];
        html = location_arr[id]['html'];
        heading = location_arr[id]['heading'];
        address = location_arr[id]['address'];
        if (!zoom) {zoom = 10;}
        point = new GLatLng(lat, lng);
        var info = html_entity_decode(address);
        var description = html_entity_decode(html);
        map.setCenter(new GLatLng(lat, lng), zoom);
        //map.openInfoWindowHtml(point, info);
        document.getElementById('location_description').innerHTML = (description? description: info);
}

function html_entity_decode(string, quote_style) {
    var histogram = {}, symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();
    if (false === (histogram = get_html_translation_table('HTML_ENTITIES', quote_style))) {
        return false;
    }
    delete(histogram['&']);
    histogram['&'] = '&amp;';
    for (symbol in histogram) {
        entity = histogram[symbol];
        tmp_str = tmp_str.split(entity).join(symbol);
    }
    return tmp_str;
}

function get_html_translation_table(table, quote_style) {
    var entities = {}, histogram = {}, decimal = 0, symbol = '';
    var constMappingTable = {}, constMappingQuoteStyle = {};
    var useTable = {}, useQuoteStyle = {};
    constMappingTable[0]      = 'HTML_SPECIALCHARS';
    constMappingTable[1]      = 'HTML_ENTITIES';
    constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
    constMappingQuoteStyle[2] = 'ENT_COMPAT';
    constMappingQuoteStyle[3] = 'ENT_QUOTES';
    useTable     = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
    useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';
    if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
        //throw Error("Table: "+useTable+' not supported');
                return false;
    }
    entities['38'] = '&amp;';
    if (useQuoteStyle !== 'ENT_NOQUOTES') {
        entities['34'] = '&quot;';
    }
    if (useQuoteStyle === 'ENT_QUOTES') {
        entities['39'] = '&#039;';
    }
    entities['60'] = '&lt;';
    entities['62'] = '&gt;';
    if (useTable === 'HTML_ENTITIES') {
      entities['160'] = '&nbsp;';
      entities['161'] = '&iexcl;';
      entities['162'] = '&cent;';
      entities['163'] = '&pound;';
      entities['164'] = '&curren;';
      entities['165'] = '&yen;';
      entities['166'] = '&brvbar;';
      entities['167'] = '&sect;';
      entities['168'] = '&uml;';
      entities['169'] = '&copy;';
      entities['170'] = '&ordf;';
      entities['171'] = '&laquo;';
      entities['172'] = '&not;';
      entities['173'] = '&shy;';
      entities['174'] = '&reg;';
      entities['175'] = '&macr;';
      entities['176'] = '&deg;';
      entities['177'] = '&plusmn;';
      entities['178'] = '&sup2;';
      entities['179'] = '&sup3;';
      entities['180'] = '&acute;';
      entities['181'] = '&micro;';
      entities['182'] = '&para;';
      entities['183'] = '&middot;';
      entities['184'] = '&cedil;';
      entities['185'] = '&sup1;';
      entities['186'] = '&ordm;';
      entities['187'] = '&raquo;';
      entities['188'] = '&frac14;';
      entities['189'] = '&frac12;';
      entities['190'] = '&frac34;';
      entities['191'] = '&iquest;';
      entities['192'] = '&Agrave;';
      entities['193'] = '&Aacute;';
      entities['194'] = '&Acirc;';
      entities['195'] = '&Atilde;';
      entities['196'] = '&Auml;';
      entities['197'] = '&Aring;';
      entities['198'] = '&AElig;';
      entities['199'] = '&Ccedil;';
      entities['200'] = '&Egrave;';
      entities['201'] = '&Eacute;';
      entities['202'] = '&Ecirc;';
      entities['203'] = '&Euml;';
      entities['204'] = '&Igrave;';
      entities['205'] = '&Iacute;';
      entities['206'] = '&Icirc;';
      entities['207'] = '&Iuml;';
      entities['208'] = '&ETH;';
      entities['209'] = '&Ntilde;';
      entities['210'] = '&Ograve;';
      entities['211'] = '&Oacute;';
      entities['212'] = '&Ocirc;';
      entities['213'] = '&Otilde;';
      entities['214'] = '&Ouml;';
      entities['215'] = '&times;';
      entities['216'] = '&Oslash;';
      entities['217'] = '&Ugrave;';
      entities['218'] = '&Uacute;';
      entities['219'] = '&Ucirc;';
      entities['220'] = '&Uuml;';
      entities['221'] = '&Yacute;';
      entities['222'] = '&THORN;';
      entities['223'] = '&szlig;';
      entities['224'] = '&agrave;';
      entities['225'] = '&aacute;';
      entities['226'] = '&acirc;';
      entities['227'] = '&atilde;';
      entities['228'] = '&auml;';
      entities['229'] = '&aring;';
      entities['230'] = '&aelig;';
      entities['231'] = '&ccedil;';
      entities['232'] = '&egrave;';
      entities['233'] = '&eacute;';
      entities['234'] = '&ecirc;';
      entities['235'] = '&euml;';
      entities['236'] = '&igrave;';
      entities['237'] = '&iacute;';
      entities['238'] = '&icirc;';
      entities['239'] = '&iuml;';
      entities['240'] = '&eth;';
      entities['241'] = '&ntilde;';
      entities['242'] = '&ograve;';
      entities['243'] = '&oacute;';
      entities['244'] = '&ocirc;';
      entities['245'] = '&otilde;';
      entities['246'] = '&ouml;';
      entities['247'] = '&divide;';
      entities['248'] = '&oslash;';
      entities['249'] = '&ugrave;';
      entities['250'] = '&uacute;';
      entities['251'] = '&ucirc;';
      entities['252'] = '&uuml;';
      entities['253'] = '&yacute;';
      entities['254'] = '&thorn;';
      entities['255'] = '&yuml;';
    }
    for (decimal in entities) {
        symbol = String.fromCharCode(decimal);
        histogram[symbol] = entities[decimal];
    }

    return histogram;
}
