﻿function Location() {
    this.id = null;
    this.lat = null;
    this.lng = null;
    this.marker = null;
    this.type = null;
    this.name = null;
    this.address = null;
    this.contact = null;
    this.phone = null;
    this.fax = null;
    this.mobile = null;
    this.desc = null;
    this.dist = null;
}

Location.prototype.loadFromXml = function(xml) {

    for (var i = 0; i < xml.childNodes.length; i++) {
        var node = xml.childNodes[i];
        var value = null;

        if (node.childNodes.length > 0) {
            value = node.childNodes[0].nodeValue;
        }

        switch (node.nodeName) {

            case "latitude":
                this.lat = value;
                if (this.lat != null) {
                    this.lat = parseFloat(this.lat);
                }
                break;

            case "longitude":
                this.lng = value;
                if (this.lng != null) {
                    this.lng = parseFloat(this.lng);
                }
                break;

            case "company":
                this.name = value;
                break;

            case "type":
                this.type = value;
                break;

            case "address":
                this.address = value;
                break;

            case "contact":
                this.contact = value;
                break;

            case "phone":
                this.phone = value;
                break;

            case "fax":
                this.fax = value;
                break;

            case "mobile":
                this.mobile = value;
                break;

            case "id":
                this.id = value;
                break;

        }

    }

    this.loadMarker();

}

Location.prototype.loadMarker = function() {

    if (this.marker == null) {
        if (this.lat != null && this.lng != null && !isNaN(this.lat) && !isNaN(this.lng)) {
            Debug.write(this.lat + ", " + this.lng);
            this.marker = new GMarker(new GLatLng(this.lat, this.lng, 13));
        }
        else {
            geoCodeLocation(this);
        }
    }

    return this.marker;
}

Location.prototype.isValid = function() {
    return (this.marker != null
        && !isNaN(this.marker.getLatLng().lat())
        && !isNaN(this.marker.getLatLng().lng())
        );
}

Location.prototype.bindMarkerEvents = function(map) {
    _bindMarkerEvents(map, this);
}

function _bindMarkerEvents(map, location) {
    if (!location.isValid()) {
        return;
    }
    GEvent.addListener(location.marker, "click",
				function() {
				    location.displayInfoWindow();
				});
    GEvent.addListener(map, "addoverlay",
				function(overlay) {
				    Debug.write(location.name + ' added');
				    var displayType = getSelectedCompanyType();
				    if (displayType == null || location.type == displayType) {
				        location.marker.show();
				    } else {
				        location.marker.hide();
				    }
				});
    GEvent.addListener(map, "removeoverlay",
				function(overlay) {
				    Debug.write(location.name + ' removed');
				});

}

Location.prototype.displayInfoWindow = function() {
    var info = "<div style='font-size:10px'>" +
        "<strong>" + this.name + "</strong><p>";

    if (this.contact != null && this.contact.length > 0) {
        info += this.contact + "<br />";
    }

    if (this.address != null && this.address.length > 0) {
        info += this.address + "<br />";
    }

    if (this.phone != null && this.phone.length > 0) {
        info += "Ph: " + this.phone + "<br />";
    }

    if (this.fax != null && this.fax.length > 0) {
        info += "Fax: " + this.fax + "<br />";
    }

    if (this.mobile != null && this.mobile.length > 0) {
        info += "Mob: " + this.mobile + "<br />";
    }

    info = info + "</p></div>";

    this.marker.openInfoWindow(info);

}

/************************************** DEBUG ************************************/

var Debug = new MyDebug();

function MyDebug() {
    this.enabled = false;
    this.glogEnabled = true;
}

MyDebug.prototype.write = function(text) {

    if (this.enabled) {
        //console.log(text);
        if (this.glogEnabled && GLog != null) {
            GLog.write(text);
        }
    }
}