var arrAll = []; var arrHotel = [];var arrRestaurant = [];var arrPoi = [];var arrShopping = [];
var all = 'all';
var hotel = 'hotel';
var shopping = 'shopping';
var restaurant = 'restaurant';
var landmark = 'landmark';
var arrTypeRes = [5,6,7,8];
var currAccm = currAccm || null;
var currSite = currSite || null;
var currMarker = currMarker || null;
//var accmId = $('#accmId')?$('#accmId').val():currAccm;
var controls = [
{
    id: 'uxShowShop',
    label:'Shopping Places',
    type: shopping,
    arrMarker: arrShopping,
    url: '/?cmd=map&act=getListMapShopping',
    params: 'item_type=1'},
    {
        id: 'uxShowRes',
        label:'Restaurants',
        type: restaurant,
        arrMarker: arrRestaurant,
        url: '/?cmd=map&act=getListRestaurant',
        params: 'item_type='+arrTypeRes},
        {
            id: 'uxHotel',
            label:'Hotels',
            type: hotel,
            arrMarker: arrHotel,
            url: '/?cmd=map&act=getHotelMap',
            params: 'accm_id='+currAccm},
            {
                id: 'uxShowPoi',
                label:'Point of Interest',
                type: landmark,
                arrMarker: arrPoi,
                url: '/?cmd=map&act=getListPoint',
                params: ''
            }
            ];
            function FilterMarkerControl(page) { this.page = page; };

            FilterMarkerControl.prototype = new GControl();

            FilterMarkerControl.prototype.initialize = function() {

                var container = document.createElement("div");
                container.style.marginTop = '20px';

                for(var i in controls){
                    container.appendChild(this.ImageButton(controls[i].id, controls[i].label, controls[i].type, controls[i].arrMarker, controls[i].url, controls[i].params));
                }

                map.getContainer().appendChild(container);
                return container;
            };

            FilterMarkerControl.prototype.getDefaultPosition = function() {
                return new GControlPosition(G_ANCHOR_TOP_RIGHT  , new GSize(12, 12));
            };

            // Sets the proper CSS for the given button element.
            FilterMarkerControl.prototype.setButtonStyle_ = function(elm){
                elm.style.color = "red";
                elm.style.marginTop = '5px';
                elm.style.padding = '3px';
                elm.style.border = '1px solid #36BEEB';
                elm.style.backgroundColor = '#ffffff';
            };

            FilterMarkerControl.prototype.ImageButton = function(elmId, textLabel, type, arrMarker, path, params){
                var containElement = document.createElement("div");
                this.setButtonStyle_(containElement);
                //create image
                var image = document.createElement("img");
                image.id = elmId;
                image.name = elmId;
                if(type == hotel || type == landmark){
                    image.src = "/images/blue/en/"+type+"_on.gif";
                }else if(this.page == type){
                    image.src = "/images/blue/en/"+type+"_on.gif";
                }else{
                    image.src = "/images/blue/en/"+type+"_off.gif";
                }
                containElement.appendChild(image);
                var page = this.page;
                GEvent.addDomListener(image, "click", function(){
                    var flag = image.src.split('_');
                    if(flag[1] == 'off.gif'){
                        GetListMarker(path, params, type, arrMarker, page);
                        document.getElementById(elmId).src = "/images/blue/en/"+type+"_on.gif";
                    }else {
                        for (var i=0; i < arrMarker.length; i++) {
                            if(arrMarker[i]) map.removeOverlay(arrMarker[i]);
                        }
                        document.getElementById(elmId).src = "/images/blue/en/"+type+"_off.gif";
                    }
                });
                return containElement;
            };

            function GetListMarker(path, params, type, arrMarker, page){
                $.ajax({
                    type: 'POST',
                    url: path,
                    dataType: 'text',
                    data: params+(currSite != null?'&site_id='+currSite:''),
                    success: function(response){
                        arrMarker = AddOtherMarker(eval('(' + response + ')'), type, arrMarker, page);
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                    }
                });
                return arrMarker;
            }

            function AddOtherMarker(pointList, type, arrMarker, page){
                var icon = null;
                //check the type of marker for selecting icon
                if(type == hotel){
                    icon = hotel;
                }
                else if(type == shopping) {
                    icon = shopping;
                }
                else if(type == landmark) {
                    icon = landmark;
                }
                else if(type == restaurant) {
                    icon = restaurant;
                }
                var data = pointList.data;
                for(var i in data.rows){
                    var item = data.rows[i];
                    if(item.mLocationLat && item.mLocationLon && item.mLocationLon != '0' && item.mLocationLat != '0'){
                        var marker = null;
                        if(page == hotel || page == shopping || page == restaurant){
                            if((pointList.mCurrAccm && pointList.mCurrAccm == item.mItemId) || (pointList.mCurrItem && pointList.mCurrItem == item.mItemId)){
                                marker = new GMarker(new GLatLng(item.mLocationLat,item.mLocationLon),{title: item.mItemName, icon: InitIcon(icon+'_higlight')});
                                map.setCenter(new GLatLng(item.mLocationLat, item.mLocationLon),parseInt(item.mLocationZoom));
                            }else{
                                marker = new GMarker(new GLatLng(item.mLocationLat,item.mLocationLon),{title: item.mItemName, icon: InitIcon(icon)});
                            }
                        }else{
                            marker = new GMarker(new GLatLng(item.mLocationLat,item.mLocationLon),{title: item.mItemName, icon: InitIcon(icon)});
                        }
                        AddEventListener(marker, item, type, pointList.mSiteDomain);
                        arrMarker[i] = marker;
                        marker.value = item.mItemType;
                        map.addOverlay(marker);
                    }
                }
                return arrMarker;
            }

            function AddEventListener(obj, item, type, siteDomain){
                GEvent.addListener(obj, "click", function(latlng){
                    var prefix = (typeof urlPrefix != "undefined")?urlPrefix:"./";
                    var embeded = (typeof isEmbed != "undefined")?isEmbed:false;
                    //Adding information marker
                    var strHtml = "<div class='content_popup'>";
                    if(type == hotel){
                        strHtml += "<a href='"+prefix+item.mUrlName+"'><b>"+item.mItemName+"</b></a><br/>";
                        strHtml += "<b>Address:</b> "+item.mAddress+"<br/>";
                        strHtml += GetMinMax(item.mItemId);
                    }else if(type == landmark){
                        if(embeded){
                            strHtml += "<b>"+item.mItemName+"</b><br/>";//not link to point of interest in embeded version
                        }else{
                            strHtml += "<a href='"+prefix+"visit/"+item.mUrlName+"'><b>"+item.mItemName+"</b></a><br/>";
                        }
                        if(item.mPhoto) strHtml += '<p style="float:left;padding-right:4px;"><img src="' + item.mPhoto + '" /></p>';
                        if(item.mDescription) strHtml += item.mDescription;
                        if((item.mPhoto || item.mDescription) && !embeded ){
                            strHtml += '<br /><a href="'+prefix+'visit/'+item.mUrlName+'" style="float:right">['+Message.readMore.toLowerCase()+']</a>';
                        }
                    }else{
                        strHtml += "<font color='blue'><b>"+item.mItemName+"</b></font><br/>";
                    }
                    if(type == restaurant || type == shopping){
                        strHtml += GetInfoService(item.mItemId, type);
                    }
                    strHtml += "</div>";
                    map.openInfoWindow(latlng, strHtml, {maxWidth: '400'});
                    currMarker = obj;
                });
            }

            function GetMinMax(itemId)
            {
                var result = '';
                $.ajax({
                    type: 'POST',
                    url: '?cmd=map&act=getMinMax',
                    dataType: 'text',
                    data: 'accm_id='+itemId,
                    async: false,
                    success: function(response){
                        response = eval('(' + response + ')');
                        if(response.mMinPrice > 0) result += "<b>Min Price:</b> "+response.mCurrency+" "+response.mMinPrice+" - <b>Max Price:</b> "+response.mCurrency+" "+response.mMaxPrice+"<br/>";
                    }
                });
                return result;
            }

            function GetInfoService(itemId, type)
            {
                var result = '';
                $.ajax({
                    type: 'POST',
                    url: '?cmd=map&act=getInfo',
                    dataType: 'text',
                    data: 'item_id='+itemId,
                    async: false,
                    success: function(response){
                        response = eval('(' + response + ')');
                        if(response.mItemType != '') result += "<b>Type:</b> "+response.mItemType+"<br/>";
                        if(type == restaurant && response.mOpenHour != '') result += "<b>Opening hours:</b> "+response.mOpenHour+"<br/>";
                        if(response.mAddress != '') result += "<b>Address:</b> "+response.mAddress+"<br/>";
                        if(response.mPhone != '') result += "<b>Phone:</b> "+response.mPhone+"<br/>";
                    }
                });
                return result;
            }
            /* Add destination point */
            function AddDestPoint(pointList)
            {
                data = pointList.data;
                for(var i in data.rows){
                    if(data.rows[i].mLocationLat && data.rows[i].mLocationLon && data.rows[i].mLocationLon != '0' && data.rows[i].mLocationLat != '0'){
                        var marker = new GMarker(new GLatLng(data.rows[i].mLocationLat,data.rows[i].mLocationLon),{title: data.rows[i].mDestName, icon: InitIcon('flag')});
                        //Adding information marker
                        /*var strHtml = "<div>";
                        strHtml += "<b><a href='#' onClick='LoadDestService(event,"+data.rows[i].mDestId+")'>"+data.rows[i].mDestName+"</a></b><br/>";
                        strHtml += "<div>"+data.rows[i].mIntro+"</div>";
                        strHtml += "</div>";
                        AddEventListener(marker, strHtml);	 */
                        AddEventDest(marker, data.rows[i].mDestId);
                        map.addOverlay(marker);
                    }
                }
            }

            function AddEventDest(obj, destId)
            {
                GEvent.addListener(obj, "click", function(latlng){
                    currMarker = obj;
                    LoadDestService(destId);
                });
            }

            function LoadDestService(siteId)
            {
                currSite = siteId;
                map.removeOverlay(currMarker);
                map.closeInfoWindow();
                $('#uxHotel').attr('src', '/images/blue/en/hotel_on.gif');
                $('#uxShowPoi').attr('src', '/images/blue/en/landmark_on.gif');
                /* get list accommodations */
                $.ajax({
                    type: 'POST',
                    url: '?cmd=map&act=getHotelMap',
                    dataType: 'text',
                    data: 'site_id='+siteId,
                    success: function(response){
                        response = eval('(' + response + ')');
                        if(response.mLatSite && response.mLonSite){
                            map.setCenter(new GLatLng(response.mLatSite, response.mLonSite),parseInt(response.mZoomSite));
                            AddOtherMarker(response, hotel, arrHotel);
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                    }
                });
                /* get list points of interest */
                $.ajax({
                    type: 'POST',
                    url: '?cmd=map&act=getListPoint',
                    dataType: 'text',
                    data: 'site_id='+siteId,
                    success: function(response){
                        AddOtherMarker(eval('(' + response + ')'), landmark, arrPoi);
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                    }
                });
            }

            function InitIcon(iconName){
                var icon = new GIcon();
                icon.image = "/images/blue/en/"+iconName+".gif";
                icon.iconSize = new GSize(16, 16);
                icon.iconAnchor = new GPoint(8, 15);
                return icon;
            }

            function InitGoogleGMap()
            {
                if (!GBrowserIsCompatible()) return;
                var arrMap = [G_NORMAL_MAP, G_SATELLITE_MAP, G_PHYSICAL_MAP];
                map = new GMap2(document.getElementById("gmap"));
                map.setCenter(new GLatLng(default_lat, default_lon),parseInt(default_zoom));
                //custom ui control
                var customUI = map.getDefaultUI();
                map.setUI(customUI);
                map.setMapType(arrMap[mapType]);
                map.disableScrollWheelZoom();
                map.addControl(new FilterMarkerControl(landmark));

                //get list accommodations
                $.ajax({
                    type: 'POST',
                    url: '?cmd=map&act=getHotelMap',
                    dataType: 'text',
                    data: 'accm_id='+currAccm,
                    success: function(response){
                        AddOtherMarker(eval('(' + response + ')'), hotel, arrHotel);
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                    }
                });
                //get list point of interest
                $.ajax({
                    type: 'POST',
                    url: '?cmd=map&act=getListPoint',
                    dataType: 'text',
                    success: function(response){
                        AddOtherMarker(eval('(' + response + ')'), landmark, arrPoi);
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                    }
                });
            }
