function Location(location_id) {
    this.location_id = parseInt(location_id, 10);
    this.language_id = 1;

    this.save = function() {
        var formObject = document.getElementById('location_form');
        YAHOO.util.Connect.setForm(formObject, true);
        YAHOO.util.Connect.asyncRequest('POST', '/rpc/location.php', locationSaveCallback);
    };

    this.remove = function() {
        var formObject = document.getElementById('locations_list_form');
        YAHOO.util.Connect.setForm(formObject);
        YAHOO.util.Connect.asyncRequest('POST', '/rpc/delete_location.php', locationDeleteCallback);
    };

    this.getLocations = function() {
        if (document.getElementById('services_form')) {
        	var formObject = document.getElementById('services_form');
        	YAHOO.util.Connect.setForm(formObject);
        	var customer_id = 1;
        	var url = location.href;
        	if (url.indexOf('visitnordjylland') != -1) {
        		customer_id = 2;
        	}
        }
        YAHOO.util.Connect.asyncRequest('GET', '/rpc/get_locations.php?customer_id='+customer_id+'&language_id='+Location.language_id, getLocationsCallback);
    };

    this.showLocations = function(o) {
        map.clearOverlays();
        var result =  eval('(' + o.responseText + ')');
        if(result.length > 0) {
            var icon = this.initIcon();
            var markers = new Array();
            for(var i = 0; i < result.length; i++) {
                var point = new GLatLng(result[i].lat, result[i].lng);
                var marker = createMarker(point, result[i].description, icon, result[i].options);
                markers[markers.length] = marker;
                map.addOverlay(marker);
                //clusterer.AddMarker(marker, result[i].name);
            }
           /* markerManager.addMarkers(markers, 6);
            markerManager.refresh();*/
        }
    }

    /*
    this.search = function() {
        var formObject = document.getElementById('search_form');
        YAHOO.util.Connect.setForm(formObject);
        YAHOO.util.Connect.asyncRequest('POST', 'rpc/search_locations.php', locationSearchCallback);
    };
    */

    this.handleSuccess = function(o) {
        var result =  eval('(' + o.responseText + ')');
        if(result.messages.errors.length == 0) {
            //removeDraggableIcon();
            this.getLocations();
            toogle_lightbox(new Array('fade', 'add_location_form'), 'none');
            location.href = "admin_index.php";
        } else {
            this.displayError(result.messages);
        }
    };

    this.handleFailure = function(o) {
        var result =  o.responseText;
    }

    this.handleUploads = function(eventType, args) {
        //var result = eventType.responseText;
        var result =  eval('(' + eventType.responseText + ')');
        if(result.messages.errors.length == 0) {
            //removeDraggableIcon();
            this.getLocations();
            toogle_lightbox(new Array('fade', 'add_location_form'), 'none');
            // go directly to admin page, current location
            location.href = "admin_index.php";
        } else {
            this.displayError(result.messages);
        }
    }

    this.handleDeleteSuccess = function(o) {
        var result = o.responseText;
        switch (result) {
            case 'OK':
                location.href = 'admin_index.php';
                break;

            case 'Invalid session':
                location.href = 'index.php';
                break;

            case 'Error':
                //this.displayDeleteError();
                break;

            default:
                location.href = 'admin_index.php';
        }
    };

    /*
    this.showSearchResults = function(o) {
        var result =  eval('(' + o.responseText + ')');
        if(result.length > 0) {
            for(var i = 0; i < result.length; i++) {

            }
        }
    };
*/
    this.displayError = function(messages) {
    	var error_string = '';
    	for (var i = 0; i < messages.errors.length; i++) {
    		error_string = error_string + messages.errors[i] + '<br/>';
    	}
		if (document.getElementById('error_container')) {
			document.getElementById('error_container').innerHTML = error_string;
		}

		toogle_lightbox(new Array('fade', 'add_location_form', 'location_error'), 'block');
    }

    this.displayDeleteError = function() {
        toogle_lightbox(new Array('fade', 'add_location_form', 'location_error'), 'block');
    }

    this.initIcon = function() {
        var icon = new GIcon();
        icon.image = "http://mobilmap.dk/images/telefon.png";
        icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        icon.iconSize = new GSize(9, 16);
        icon.shadowSize = new GSize(22, 20);
        icon.iconAnchor = new GPoint(6, 20);
        icon.infoWindowAnchor = new GPoint(5, 1);

        return icon;
    }
}
