/**********************
 HOTELS AND APARTMENTS
 *********************/

function showProperties(hotels, apts, theatres) {
    if (propertyIcon == null) {
        propertyIcon = new GIcon();
        propertyIcon.iconSize = new GSize(22, 28);
        propertyIcon.iconAnchor = new GPoint(11, 28);
        propertyIcon.infoWindowAnchor = new GPoint(9, 2);
    }
    deleteMarkers();
    if (hotels) {
        hotelMarkers = new Array();
        loadProperties("hotel");
    }
    if (apts) {
        aptMarkers = new Array();
        loadProperties("apartment");
    }
    if (theatres) {
        theatreMarkers = new Array();
        loadProperties("theatre");
    }
    return false;
}

function loadProperties(property_type) {
    GDownloadUrl("/xml/xml.php?type="+property_type, function(data, responseCode) {
        var xml = GXml.parse(data);
        
        switch (property_type) {
            case "hotel":
                var properties = xml.documentElement.getElementsByTagName(property_type);
                for (var i=0; i<properties.length; i++) {
                    var property = new Array();
                        property['i'] = i;
                        property['lat'] = parseFloat(properties[i].getAttribute("lat"));
                        property['lng'] = parseFloat(properties[i].getAttribute("lng"));
                        property['name'] = properties[i].getAttribute("name");
                        property['class'] = properties[i].getAttribute("class");
                        property['link'] = properties[i].getAttribute("link");
                    addMarker(property, property_type);
                }
                break;
            case "apartment":
                var cities = xml.documentElement.getElementsByTagName("city");
                for (var i=0; i<cities.length; i++) {
                    if (cities[i].getAttribute("name") == "London") {
                        var properties = cities[i].getElementsByTagName(property_type);
                        for (var i=0; i<properties.length; i++) {
                            var property = new Array();
                                property['i'] = i;
                                property['lat'] = parseFloat(properties[i].getAttribute("lat"));
                                property['lng'] = parseFloat(properties[i].getAttribute("lng"));
                                property['name'] = properties[i].getAttribute("name");  
                                property['link'] = properties[i].getAttribute("link"); 
                                property['folder'] = properties[i].getAttribute("folder");
                                property['minstay'] = properties[i].getAttribute("minstay");     
                                property['class'] = properties[i].getAttribute("class");             
                                property['lowrate'] = properties[i].getAttribute("lowrate");
                            addMarker(property, property_type);
                        }
                    }
                }
                break;
            case "theatre":
                var theatres = xml.documentElement.getElementsByTagName("theatre");
                for (var i=0; i<theatres.length; i++) {
                    var theatre = new Array();
                        theatre['i'] = i;
                        theatre['lat'] = parseFloat(theatres[i].getAttribute("lat"));
                        theatre['lng'] = parseFloat(theatres[i].getAttribute("lng"));
                        theatre['name'] = theatres[i].getAttribute("name");
                    addMarker(theatre, property_type);
                }
        }
    });
}

function addMarker(property, property_type) {
    
    // set up custom icon
    var currentIcon = new GIcon(propertyIcon);
    switch (property_type) {
        case "hotel": currentIcon.image = "/gfx/icon_hotel.png"; break;
        case "apartment": currentIcon.image = "/gfx/icon_apt.png"; break;
        case "theatre": currentIcon.image = "/gfx/icon_theatre.png"; break;
    }
    
    var point = new GLatLng(property['lat'], property['lng']);
    var marker = new GMarker(point, currentIcon);
    map.addOverlay(marker);
    switch (property_type) {
        case "hotel":
            hotelMarkers[property['i']] = marker;
            GEvent.addListener(marker, "click", function() {
                var plink = "http://www.londonnights.com/"+property['link']+".htm";
                var pimg = "http://www.londonnights.com/gfx/photos/"+property['link']+"_main_tn.jpg";
                var pclass = "http://www.londonnights.com/gfx/rating_"+property['class']+".gif";
                var html = "<div id='infowindow'>";
                html += "<a href='"+plink+"' target='_blank'><img src='"+pimg+"' style='float:left;padding-right:5px' /></a>";
                html += "<a href='"+plink+"' style='color:#1AE1FC;text-decoration:underline;font-weight:bold' target='_blank'>"+property['name']+"</a>";
                html += "<br /><br />";
                html += "<a href='"+plink+"' target='_blank' style='text-decoration:underline;font-size:11px'>view more hotel details</a>";
                html += "<div>";
                marker.openInfoWindowHtml(html);
            });
            break;
        case "apartment":
            aptMarkers[property['i']] = marker;
            GEvent.addListener(marker, "click", function() {
                var plink = "http://www.citybaseapartments.com/affiliate.php?id=65&page="+property['link'];
                var pimg = "http://www.citybaseapartments.com"+property['folder']+"/listing.jpg";
                var html = "<div id='infowindow' style='height:120px;width:300px'>";
                html += "<a href='"+plink+"' style='color:#1AE1FC;text-decoration:underline;font-weight:bold' target='_blank'>"+property['name']+"</a>";
                html += "<br />";
                html += "<a href='"+plink+"' target='_blank'><img src='"+pimg+"' style='float:left;padding-right:5px;padding-top:5px' /></a>";
                html += "<div style='color:#000000;font-size:12px;padding-top:5px'>";
                html += "<b>Class:</b> "+property['class']+"<br />";
                html += "<b>Minimum Stay:</b> "+property['minstay']+" Nights<br />";
                html += "<b>Rates From:</b> &pound;"+property['lowrate']+"<br />";
                html += "</div>";
                html += "<br /><br />";
                html += "<a href='"+plink+"' target='_blank' style='text-decoration:underline;font-size:11px'>view more apartment details</a>";
                html += "<div>";
                marker.openInfoWindowHtml(html);
            });
            break;
        case "theatre":
            theatreMarkers[property['i']] = marker;
            GEvent.addListener(marker, "click", function() {
                var html = "<div id='infowindow' style='height:30px;color:#000000'><b>"+property['name']+"</b></div>";
                marker.openInfoWindow(html);
            });
            break;
    }
}

function deleteMarkers() {
    for (marker in hotelMarkers) {
        map.removeOverlay(hotelMarkers[marker]);
    }
    for (marker in aptMarkers) {
        map.removeOverlay(aptMarkers[marker]);
    }
    for (marker in theatreMarkers) {
        map.removeOverlay(theatreMarkers[marker]);
    }
    hotelMarkers = null;
    aptMarkers = null;
}