var map;
var mapObj;
var localSearch = new GlocalSearch();
var lat;
var lng;
var zoom;
var type;
var area;
var loader;
var center;
var subTypes = {};
var markers = {};
var filter = [];
var pricing = [];
var states = [];
var months = [];
	months[1] = "January";
	months[2] = "February";
	months[3] = "March";
	months[4] = "April";
	months[5] = "May";
	months[6] = "June";
	months[7] = "July";
	months[8] = "August";
	months[9] = "September";
	months[10] = "October";
	months[11] = "November";
	months[12] = "December";

var icon = new GIcon();
icon.image = "http://www.google.com/mapfiles/marker.png";
icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize = new GSize(26, 28);
icon.shadowSize = new GSize(35, 28);
icon.iconAnchor = new GPoint(11, 28);


function getViewBounds() {
	var vars = [], hash;    
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');     
	for(var i = 0; i < hashes.length; i++)    {        
		hash = hashes[i].split('=');        
		vars.push(hash[0]);        
		vars[hash[0]] = hash[1];    
	}

	lng = mapObj["xcen"];
	lat = mapObj["ycen"];
	zoom = parseInt(mapObj["zoom"]);
}

function importanceOrder (marker,b) {
	return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
}

function getMarkers(map, type, area, results, enableInfoWindow, enableClickThrough, mapObj){
	var bounds = map.getBounds();
	var ne = bounds.getNorthEast();
	var sw = bounds.getSouthWest();
	var excludes = "0";
    var states = mapObj["states"];
    var subType = mapObj["subType"];
    var area = mapObj["area"];
    var loader = document.getElementById("loader");
    if (loader) { loader.style.display = ""; }

	if (markers.length || 1 == 1) {
		for (var i in markers) {
            excludes += "," + markers[i].db_id;
		}
	}

	var urlstr="xmllocations.php?minLng=" + sw.lng() + "&maxLng=" + ne.lng() + "&minLat=" + sw.lat() + "&maxLat=" + ne.lat() + "&excludes=" + excludes + "&type=" + type + "&subType=" + subType + "&checkAvailability=" + mapObj["checkAvailability"] + "&checkin=" + mapObj["checkin"] + "&checkout=" + mapObj["checkout"] + "&states=" + states.join(",");

    var request = GXmlHttp.create();
	request.open('GET', urlstr , true);
	request.onreadystatechange = function () {
		if (request.readyState == 4) {
			var xmlDoc = request.responseXML;
			locations = xmlDoc.documentElement.getElementsByTagName("location");
			if (locations.length){ 
                for (var id in results) {
                    for (var l = 0; l < locations.length; l++) {
                        if (locations[l].getAttribute("id") == id) {
                            //locations.removeChild(locations[l]);
                            var xy = locations[l].getAttribute("xy").split(',');
                            var importance = 1;
                            var typeID = 0;
                            var appendChar = "";
                            if (locations[l].getAttribute("type") == 1 || 1){
                                typeID = locations[l].getAttribute("type");
                                if (typeID == "1") {
                                    switch(locations[l].getAttribute("available")) {
                                        case "1":
                                            appendChar = "a";
                                            break;
                                        case "3":
                                            appendChar = "n";
                                            break;
                                        case "2":
                                            appendChar = "q";
                                            break;
                                    }
                                }
                                icon.image = "images/markers/" + typeID + appendChar + ".png";
                                icon.iconSize = new GSize(26, 28);
                                icon.shadowSize = new GSize(35, 28);
                                icon.iconAnchor = new GPoint(11, 28);
                                if (results != 'undefined') {
                                    if (!isNaN(parseInt(results[locations[l].getAttribute("id")]))) {
                                        icon.image = "images/markers/" + typeID + "_" + results[locations[l].getAttribute("id")] + appendChar + ".png";
                                        icon.iconSize = new GSize(26, 39);
                                        icon.shadowSize = new GSize(35, 39);
                                        icon.iconAnchor = new GPoint(11, 39);
                                        importance = 2;
                                    }
                                }

                            }else if (locations[l].getAttribute("type") == "postcodeSearch"){
                                icon.image = "images/markers/1_0.png";
                            }else{
                                icon.image = "images/marker02.png";
                            }
                            var index = locations[l].getAttribute("id");
                            if (markers[index] === undefined) {
                                markers[index] = new GMarker(new GLatLng(xy[0],xy[1]),{icon:icon, title:locations[l].getAttribute("title").replace(/\+/g," "),zIndexProcess:importanceOrder});
                                markers[index].importance = importance;
                                markers[index].available = locations[l].getAttribute("available");
                                markers[index].infowindow = locations[l].getAttribute("title");
                                markers[index].title = markers[index].infowindow;
                                markers[index].slug = locations[l].getAttribute("slug");
                                markers[index].markerindex = index;
                                markers[index].type = type;
                                markers[index].moreInfo = locations[l].getAttribute("moreInfo");
                                markers[index].subtype = locations[l].getAttribute("subtype");
                                markers[index].db_id = locations[l].getAttribute("id");
                                subTypes[locations[l].getAttribute("subtype")] = 1;

                                if (enableInfoWindow) {
                                    GEvent.addListener(markers[index], "click", function() {
                                        var urlstr="locationtab.php?t=" + type + "&a=" + area + "&l=" + this.db_id;
                                        var obj = this;
                                        var request = GXmlHttp.create();
                                        request.open('GET', urlstr , true);

                                        request.onreadystatechange = function () {
                                            if (request.readyState == 4) {
                                                var infoTabs = [];
                                                infoTabs.push(new GInfoWindowTab("title",request.responseText));
                                                map.openInfoWindowTabsHtml(obj.getLatLng(), infoTabs, {pixelOffset: new GSize(0,0)});
                                            }
                                        }
                                        request.send(null);
                                    });
                                }
                                if (enableClickThrough && markers[index].moreInfo == "1") {
                                    GEvent.addListener(markers[index], "click", function() {
                                        var urlstr="l/" + this.type + "/" + this.slug;
                                        location.href = urlstr;
                                    });
                                }
                                map.addOverlay(markers[index]);
                            } else {
                                map.addOverlay(markers[index]);
                            }
                        }
                        
                    }
                }
				for (i = 0; i < locations.length; i++) { // cycle thru locations
					if (results[locations[i].getAttribute("id")] == null) {
                    var xy = locations[i].getAttribute("xy").split(',');
					var importance = 1;
                    var typeID = 0;
                    var appendChar = "";
					if (locations[i].getAttribute("type") == 1 || 1){
                        typeID = locations[i].getAttribute("type");
                        if (typeID == "1") {
                            switch(locations[i].getAttribute("available")) {
                                case "1":
                                    appendChar = "a";
                                    break;
                                case "3":
                                    appendChar = "n";
                                    break;
                                case "2":
                                    appendChar = "q";
                                    break;
                            }
                        }
						icon.image = "images/markers/" + typeID + appendChar + ".png";

						if (results != 'undefined') {
							if (!isNaN(parseInt(results[locations[i].getAttribute("id")]))) {
								icon.image = "images/markers/" + typeID + "_" + results[locations[i].getAttribute("id")] + appendChar + ".png";
                                importance = 2;
							}
						}

					}else if (locations[i].getAttribute("type") == "postcodeSearch"){
						icon.image = "images/markers/1_0.png";
					}else{
						icon.image = "images/marker02.png";
					}
                    var index = locations[i].getAttribute("id");
                    if (markers[index] === undefined) {
                        markers[index] = new GMarker(new GLatLng(xy[0],xy[1]),{icon:icon, title:locations[i].getAttribute("title").replace(/\+/g," "),zIndexProcess:importanceOrder});
                        markers[index].importance = importance;
                        markers[index].available = locations[i].getAttribute("available");
                        markers[index].infowindow = locations[i].getAttribute("title");
                        markers[index].title = markers[index].infowindow;
                        markers[index].slug = locations[i].getAttribute("slug");
                        markers[index].markerindex = index;
                        markers[index].type = type;
                        markers[index].moreInfo = locations[i].getAttribute("moreInfo");
                        markers[index].subtype = locations[i].getAttribute("subtype");
                        markers[index].db_id = locations[i].getAttribute("id");
                        subTypes[locations[i].getAttribute("subtype")] = 1;

                        if (enableInfoWindow) {
                            GEvent.addListener(markers[index], "click", function() {
                                var urlstr="locationtab.php?t=" + type + "&a=" + area + "&l=" + this.db_id;
                                var obj = this;
                                var request = GXmlHttp.create();
                                request.open('GET', urlstr , true);

                                request.onreadystatechange = function () {
                                    if (request.readyState == 4) {
                                        var infoTabs = [];
                                        infoTabs.push(new GInfoWindowTab("title",request.responseText));
                                        map.openInfoWindowTabsHtml(obj.getLatLng(), infoTabs, {pixelOffset: new GSize(0,0)});
                                    }
                                }
                                request.send(null);
                            });
                        }
                        if (enableClickThrough && markers[index].moreInfo == "1") {
                            GEvent.addListener(markers[index], "click", function() {
                                var urlstr="l/" + this.type + "/" + this.slug;
                                location.href = urlstr;
                            });
                        }
                        map.addOverlay(markers[index]);
                    } else {
                        map.addOverlay(markers[index]);
                    }
				}
                }
                if (currentLocation != 'undefined' && currentLocation > 0) {
                    index++;
                    icon.image = "images/markers/" + currentLocationType + "_0.png";
                    markers[index] = new GMarker(map.getCenter(),{icon:icon, title:"Current Location",zIndexProcess:importanceOrder});
                    markers[index].importance = 100000;
					markers[index].infowindow = "Current Location";
					markers[index].title = markers[index].infowindow;
					markers[index].markerindex = index;
					markers[index].db_id = currentLocation;
                    map.addOverlay(markers[index]);
                }
			} else {
                if (loader) {
                    loader.style.display = "none";
                }
            }
            if (i == locations.length && loader) {
                loader.style.display = "none";
            }
		}
	}
	request.send(null);
}

function urlencode(str) {
    str = escape(str);
    return str.replace(/[*+\/@]|%20/g,
        function (s) {
            switch (s) {
                case "*": s = "%2A"; break;
                case "+": s = "%2B"; break;
                case "/": s = "%2F"; break;
                case "@": s = "%40"; break;
                case "%20": s = "+"; break;
            }
            return s;
        }
        );
}

function usePointFromPostcode(postcode, callbackFunction) {
    if (postcode.length < 4 && isNaN(parseInt(postcode.substr(postcode.length - 1,1)))) {
        postcode = postcode + "1";
    }
    localSearch.setSearchCompleteCallback(null, 
        function() {
            
            if (localSearch.results[0])
            {        
                var resultLat = localSearch.results[0].lat;
                var resultLng = localSearch.results[0].lng;
                var point = new GLatLng(resultLat,resultLng);
                if (allowedBounds.contains(point)) {
                    callbackFunction(point, postcode);
                } else {
                    alert("Postcode not found.");
                }
            }else{
                alert("Postcode not found!");
            }
        });    
        
    localSearch.execute(postcode + ", UK");
}


function placeMarkerAtPoint(point, postcode)
{
	icon.image = "images/markers/1_0.png";
	var marker = new GMarker(point, {icon:icon, title:"Postcode Search Result - " + postcode + " (Click to remove)",zIndexProcess:importanceOrder});
    marker.importance = 200000;
    GEvent.addListener(marker, "click", function() {
        map.removeOverlay(this);
        savePostcode();
    });
 	map.addOverlay(marker);
    if (mapObj["postcode"][0] != postcode) {
        var urlstr="postcode.php?lat=" + point.lat() + "&lng=" + point.lng() ;
        var request = GXmlHttp.create();
        request.open('GET', urlstr , true);
        request.onreadystatechange = function () {
            if (request.readyState == 4) { 
                map.setCenter(point, parseInt(request.responseText.trim()));
            }
        }
        request.send(null);
    }
    savePostcode(postcode, point);
}

function savePostcode(postcode, point) {
    var urlstr = "setfilter.php?p=" + urlencode(postcode) + "&plat=" + point.lat() + "&plng=" + point.lng();
    var request = GXmlHttp.create();
    request.open('GET', urlstr , true);
    request.send(null);
}

function toggleIntro() {
    var intro = document.getElementById("introHeader");
    var showBar = document.getElementById("showBar");
    var show = true;
    if (intro.style.display == "none") {
        show = true;
        intro.style.display = "";
        showBar.style.display = "none";
    } else {
        show = false;
        intro.style.display = "none";
        showBar.style.display = "";
    }
    var urlstr = "setfilter.php?intro=" + show;
    var request = GXmlHttp.create();
    request.open('GET', urlstr , true);
    request.send(null);
}


function showPointLatLng(point)
{
    alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad(div, type, area, results, mapObj) {
    if (GBrowserIsCompatible()) {
        var mapOptions = {
            googleBarOptions : {
                style : "new"
            }
        }

        map = new GMap2(document.getElementById(div), mapOptions);

        var mt = map.getMapTypes();
        // Overwrite the getMinimumResolution() and getMaximumResolution() methods
        for (var i=0; i<mt.length; i++) {
          mt[i].getMinimumResolution = function() {return 6;}
          mt[i].getMaximumResolution = function() {return 20;}
        }


        map.setCenter(new GLatLng(33.956461,-118.396225), 13);
        map.setUIToDefault();
        //svOverlay = new GStreetviewOverlay();
        //map.addOverlay(svOverlay);
        //map.enableGoogleBar();

        getViewBounds();
        map.setCenter(new GLatLng(lng, lat), zoom);
        map.savePosition();
        map.enableScrollWheelZoom();

        filter = mapObj["filter"].split(",");

        map.addControl(new LoadingControl());

        getMarkers(map, type, area, results, true, false, mapObj);
        showAvailability();

        var latlang = map.getCenter();
        var icon = new GIcon();

        var imageUrl = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";

        icon.image = imageUrl;
        icon.iconSize = new GSize(49,52);
        icon.iconAnchor =  new GPoint(25,36);
        icon.infoWindowAnchor = new GPoint(25,6);

        

        marker = new GMarker(latlang, {"icon":icon, "draggable":true});
        //map.addOverlay(marker);

        var streetOverlay = new GStreetviewOverlay();

        GEvent.addListener(marker, "dragstart", function()
        {
            map.addOverlay(streetOverlay);
        }
        );

        GEvent.addListener(marker, "dragend", function(overlay,latlang)
        {
            map.removeOverlay(streetOverlay);
            openWindow();
        }
        );
        
        GEvent.addListener(map, "moveend", function() {
    		getMarkers(map, type, area, results, true, false, mapObj);
            showAvailability();
            var refresh = document.getElementById("mapFooter");
            refresh.style.display = "";
            checkBounds();
    	});
/* maybe add another listener to call setCoords_no_redirect() automatically? */
        if (mapObj["postcode"].length > 0) {
            point = new GLatLng(mapObj["postcode"][1],mapObj["postcode"][2]);
            placeMarkerAtPoint(point, mapObj["postcode"][0]);
        }
        
    }
}
// The allowed region which the whole map must be within
      var allowedBounds = new GLatLngBounds(new GLatLng(54.418,-11.228), new GLatLng(59.277,3.054));

      // If the map position is out of range, move it back
      function checkBounds() {
        // Perform the check and return if OK
        if (allowedBounds.contains(map.getCenter())) {
          return;
        }
        // It`s not OK, so find the nearest allowed point and move there
        var C = map.getCenter();
        var X = C.lng();
        var Y = C.lat();

        var AmaxX = allowedBounds.getNorthEast().lng();
        var AmaxY = allowedBounds.getNorthEast().lat();
        var AminX = allowedBounds.getSouthWest().lng();
        var AminY = allowedBounds.getSouthWest().lat();

        if (X < AminX) {X = AminX;}
        if (X > AmaxX) {X = AmaxX;}
        if (Y < AminY) {Y = AminY;}
        if (Y > AmaxY) {Y = AmaxY;}
        //alert ("Restricting "+Y+" "+X);
        map.setCenter(new GLatLng(Y,X));
       
      }

var myPano;
function openWindow()
{
  myPano = new GStreetviewPanorama(document.getElementById("stview"));
  var latlang = marker.getLatLng();
  myPano.setLocationAndPOV(latlang);
  stview = document.getElementById("stview");
  stview.style.zIndex = 5;
  mapholder = document.getElementById("map");
  mapholder.style.visibility = 'hidden';
}

function mapLoadSmall(div, type, area, results, lng, lat, zoom, mapObj) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(div));
        //getViewBounds();
        map.setCenter(new GLatLng(lng, lat), parseInt(zoom));
        //map.enableScrollWheelZoom();
        map.disableDragging();
        filter = mapObj["filter"].split(",");
        getMarkers(map, type, area, results, false, true, mapObj);

    }
}


function urldecode( str ) {

    var histogram = {}, histogram_r = {}, code = 0, str_tmp = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urlencode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    for (replace in histogram) {
        search = histogram[replace]; // Switch order when decoding
        ret = replacer(search, replace, ret) // Custom replace. No regexing   
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);
 
    return ret;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
    var oldonunload = window.onunload;
    if (typeof window.onunload != 'function') {
      window.onunload = func;
    } else {
      window.onunload = function() {
        oldonunload();
        func();
      }
    }
}

function setCoords_no_redirect()
{
    var send_x = document.getElementById("send_x");
    var send_y = document.getElementById("send_y");
    var send_z = document.getElementById("send_z");
    if (send_x !== undefined && send_y !== undefined && send_z !== undefined && map !== undefined)
    {
        send_x.value = map.getCenter().lat();
        send_y.value = map.getCenter().lng();
        send_z.value = map.getZoom();
        alert ("X: " + send_x.value + ", Y: " + send_y.value + ", Zoom: " + send_z.value);
    }

}

function setCoords() {
    var map_x = document.getElementById("map_x");
    var map_y = document.getElementById("map_y");
    var map_z = document.getElementById("map_z");
    if (map_x !== undefined && map_y !== undefined && map_z !== undefined && map !== undefined) {
        map_x.value = map.getCenter().lat();
        map_y.value = map.getCenter().lng();
        map_z.value = map.getZoom();
//        alert ("X: " + map.getCenter().lat() + ", Y: " + map.getCenter().lng() + ", Zoom: " + map.getZoom());
        var slugs = [];
        var typeslug = "";
        switch (mapObj["type"]) {
            case 1:
            case "1":
                typeslug = "Accommodation";
                break;
            case 2:
            case "2":
                typeslug = "Attractions";
                break;
            case 3:
            case "3":
                typeslug = "Eating-And-Drinking";
                break;
        }
        slugs[slugs.length] = typeslug;
        if (mapObj["subType"] != "") {
            var urlstr = "lookup.php?subtype=" + mapObj["subType"];
            var request = GXmlHttp.create();
            request.open('GET', urlstr , false);
            request.send(null);
            slugs[slugs.length] = request.responseText;
        } else {
            slugs[slugs.length] = "All";
        }
        document.location="/" + slugs.join("/") + "/?&lat=" + map.getCenter().lat() + "&lng=" + map.getCenter().lng() + "&zoom=" + map.getZoom() + "#m";
    }
}

function setUrl(obj, base, subtype) {
    var refresh = document.getElementById("refresh");
    var map_x = document.getElementById("map_x");
    var map_y = document.getElementById("map_y");
    var map_z = document.getElementById("map_z");
    if (refresh.style.display == "" && map_x !== undefined && map_y !== undefined && map_z !== undefined && map !== undefined) { 
        map_x.value = map.getCenter().lat();
        map_y.value = map.getCenter().lng();
        map_z.value = map.getZoom();
        var slugs = [];
        slugs[slugs.length] = base;
        if (subtype != null) {
            slugs[slugs.length] = subtype;
        } else {
            slugs[slugs.length] = "All";
        }
        obj.href = slugs.join("/") + "/?&lat=" + map.getCenter().lat() + "&lng=" + map.getCenter().lng() + "&zoom=" + map.getZoom();
    }
}

function show(id) {
    var mapobj = document.getElementById('m');
    //mapobj.scrollTop = mapobj.offsetTop;
    scroll(0,mapobj.offsetTop);
    for (var i in markers) {
        if (markers[i].db_id == id) {
            GEvent.trigger(markers[i],'click');
        }
    }
}

function showMarkers(type, subtype) {
    getMarkers(map, type, mapObj["area"], results, true, false, mapObj);
    showAvailability();
}

function hideMarkers(type, subtype) { 
    for (var i in markers) {
        if (markers[i] !== undefined && markers[i].subtype == subtype) {
            map.removeOverlay(markers[i]);
            delete markers[i];
        }
    }
    showAvailability();
}

function showAllMarkers(type) {
    for (var i in mapObj["stats"][type]) {
        if (IsNumeric(i) && i > 0) {
            //if (subTypes[i] === undefined) {
                showMarkers(type, i);
            //}
        }
    }
}

function changeFilter(type, value) {
    var states = mapObj["states"].slice(0);
    mapObj['states'][type - 1] = value;
    if (states[type - 1] == -1) {
        if (states[type - 1] == 0) {
            showAllMarkers(type);
        } else {
            showMarkers(type, states[type -1]);
        }
    } else if (states[type - 1] == 0) {
        for (var i in mapObj["stats"][type]) {
            if (i != value) {
                hideMarkers(type, i);
            }
        }
    } else { 
        hideMarkers(type, states[type -1]); 
        if (value > -1) {
            if (value == 0) {
                showAllMarkers(type); 
            } else {
                showMarkers(type, value); 
            }
        }
    }
    

    var urlstr = "setfilter.php?state=" + type + "&stateValue=" + value;
    var request = GXmlHttp.create();
    request.open('GET', urlstr , true);
    request.send(null);
    var refresh = document.getElementById("mapFooterNoMove");
    refresh.style.display = "";
}

function IsNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}


function showAvailability() {
    if (document.getElementById("available") == null) {
        return;
    }
    var available = document.getElementById("available").checked;
    var unknown = document.getElementById("unknown").checked;
    var unavailable = document.getElementById("unavailable").checked;
    var arr = [];
    if (available) {
        arr["1"] = true;
    }
    if (unknown) {
        arr["3"] = true;
    }
    if (unavailable) {
        arr["2"] = true;
    }
    for (var i in markers) {
        if (markers[i] !== undefined) {
            if (arr[markers[i].available] !== undefined) {
                markers[i].show();
            } else {
                markers[i].hide();
            }
        }
    }
}

addUnLoadEvent(GUnload);

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

// We define the function first
function RefreshControl() {
}

RefreshControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
RefreshControl.prototype.initialize = function(map) {
  var container = document.createElement("div");
  container.setAttribute("id", "refresh2");
  container.setAttribute("style", "display:none");

  var img = document.createElement("img");
  img.src = "http://pdev.ladarnas.com/development/privatehousestays/images/refresh.png";
  img.setAttribute("alt", "Refresh Results");
  container.appendChild(img);

  GEvent.addDomListener(img, "click", function() {
    setCoords();
  });

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

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
RefreshControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(22, 290));
}

function LoadingControl() {
}

LoadingControl.prototype = new GControl();

LoadingControl.prototype.initialize = function(map) {
  var container = document.createElement("div");
  container.setAttribute("id", "loader");
  //container.setAttribute("style", "display:none; position: absolute; width:840px; height:420px; opacity: .55; background-color: #eeeeee");

  var img = document.createElement("img");
  img.src = "http://www.privatehousestays.com/images/loader.gif";
  img.setAttribute("alt", "Loading");
  img.setAttribute("style", "position: absolute; top:160px; left:310px;");
  container.appendChild(img);

  GEvent.addDomListener(img, "click", function() {
    setCoords();
  });

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

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
LoadingControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
}


