function SearchMap(map_id, selector_id, house_id, latitude, longitude) {
    this.selector_id    = selector_id;
    this.house_id       = house_id;
    this.center         = new google.maps.LatLng(latitude, longitude);

    var anchor  = document.getElementById(map_id);
    var options = {
	    zoom:                   15,
	    center:                 this.center,
	    mapTypeId:              google.maps.MapTypeId.ROADMAP,
		disableDefaultUI:       false,
	    scrollwheel:            false,
	    disableDoubleClickZoom: true
    };
    this.map = new google.maps.Map(anchor, options);
    
	this.loaded = false;
    this.house      = null;
    this.houses     = new Array();
    this.services   = new Array();
    
    this.bindClick();
    this.showHouseMarkers();
}

SearchMap.prototype.showHouseMarkers = function() {
    if (this.house !== null) {
		for (house in this.houses) 
			this.houses[house].show();
		this.house.show();
	}
	else if(this.loaded == false) {
		this.loaded == true;
		this.loadHouseMarkers();
	}
};

SearchMap.prototype.hideHouseMarkers = function() {
    if (this.house !== null) {
        for (house in this.houses)
            this.houses[house].hide();
        this.house.hide();    
    }
};

SearchMap.prototype.showServiceMarkers = function(type) {
    if (this.services.hasOwnProperty(type)) {
        for (service in this.services[type])
            this.services[type][service].show();
    }
    else
        this.loadServiceMarkers(type);
};

SearchMap.prototype.hideServiceMarkers = function(type) {
    if (this.services.hasOwnProperty(type)) {
        for (service in this.services[type])
            this.services[type][service].hide();
    }
};

SearchMap.prototype.bindClick = function() {
    var self = this;
    var selector = '.' + this.selector_id;
    var handler = function(eventObject) { self.onClickHandler(eventObject); };
    $(selector).click(handler);
};

SearchMap.prototype.onClickHandler = function(eventObject) {
	var type = eventObject.originalTarget.id;
	if (eventObject.originalTarget.checked)
		this.showServiceMarkers(type);
	else
		this.hideServiceMarkers(type);
};

SearchMap.prototype.buildHouseMarkerURL = function() {
    return '/houses/' + this.center.lng() + '/' + this.center.lat() + '/';
};

SearchMap.prototype.loadHouseMarkers = function() {
    var self = this;
    var handler = function(data) { self.handleLoadHouseMarkers(data); };
    $.getJSON(this.buildHouseMarkerURL(), handler);
};

SearchMap.prototype.handleLoadHouseMarkers = function(data) {
    for (row in data) {
        var isPrincipal = (data[row].id == this.house_id);
        var htmlContent = data[row].html;
        var longitude   = data[row].longitude;
        var latitude    = data[row].latitude;
        var house       = new HouseMarker(this, latitude, longitude, isPrincipal);
        house.createIcon();
        house.createShadow();
        house.createInfoWindow(htmlContent);
        if (isPrincipal === true)
            this.house = house;
        else
            this.houses.push(house);
    }
	if(this.house === null && this.houses.length > 0) {
		this.house = this.houses[0];
	}
    this.showHouseMarkers();
};

SearchMap.prototype.buildServiceMarkerURL = function(type) {
    return '/markers/' + type + '/' + this.center.lng() + '/' + this.center.lat() + '/';
};

SearchMap.prototype.loadServiceMarkers = function(type) {
    this.services[type] = new Array();
    var self = this;
    var handler = function(data) { self.handleLoadServiceMarkers(data); };
    $.getJSON(this.buildServiceMarkerURL(type), handler);
};

SearchMap.prototype.handleLoadServiceMarkers = function(data) {
    for (row in data) {
        var htmlContent = data[row].html;
        var type        = data[row].type;
        var longitude   = data[row].longitude;
        var latitude    = data[row].latitude;
        var service     = new ServiceMarker(this, latitude, longitude, type);        
        service.createIcon();
        service.createShadow();
        service.createInfoWindow(htmlContent);
        this.services[type].push(service);
    }
    this.showServiceMarkers(type);
};

SearchMap.prototype.pins = {
    HOUSE:          'pin_bien.png',
    HOUSE_XS:       'pin_XS.png',
    BABY:           'pin_creche.png',
    KID:            'pin_maternelle.png',
    SMALL_SCHOOL:   'pin_elementaire.png',
    COLLEGE:        'pin_college.png',
    HIGH_SCHOOL:    'pin_lycee.png',
    STADE:          'pin_stade.png',
    SUPERMARKET:    'pin_supermarche.png',
    HOSPITAL:       'pin_hopital.png',
    VELIB:          'pin_velib.png'
};


function SearchMarker(searchMap, latitude, longitude) {
    this.searchMap = searchMap;
    this.imageName = null;
    this.center = new google.maps.LatLng(latitude, longitude);
    var options = {
        position:   this.center,
        map:        this.searchMap.map
    };
    this.marker = new google.maps.Marker(options);
}

SearchMarker.prototype.show = function() {
    this.marker.setMap(this.searchMap.map);
};

SearchMarker.prototype.hide = function() {
    this.marker.setMap(null);
};

SearchMarker.prototype.createIcon = function() {
    var icon = new google.maps.MarkerImage(
        '/images/pict/' + this.imageName,
        new google.maps.Size(35, 42),
        this.center,
        new google.maps.Point(18, 42)
    );
    this.marker.setIcon(icon);
};

SearchMarker.prototype.createShadow = function() {
    var shadow = new google.maps.MarkerImage(
        '/images/pict/shadow.png',
        new google.maps.Size(64, 36),
        this.center,
        new google.maps.Point(18, 42)
    );
    this.marker.setShadow(shadow);
};

SearchMarker.prototype.createInfoWindow = function(htmlContent) {
    var infoWindow = new google.maps.InfoWindow({
        content: htmlContent
    });
    var self = this;
    google.maps.event.addListener(this.marker, 'click', function() {
        infoWindow.open(self.searchMap.map, self.marker);
    });
};

HouseMarker.prototype = SearchMarker.prototype;
HouseMarker.prototype.constructor = SearchMarker;
function HouseMarker(searchMap, latitude, longitude, isPrincipal) {
    SearchMarker.call(this, searchMap, latitude, longitude);
    if (isPrincipal === true)
        this.imageName = searchMap.pins['HOUSE'];
    else
        this.imageName = searchMap.pins['HOUSE_XS'];
}

ServiceMarker.prototype = SearchMarker.prototype;
ServiceMarker.prototype.constructor = SearchMarker;
function ServiceMarker(searchMap, latitude, longitude, type) {
    SearchMarker.call(this, searchMap, latitude, longitude);
    this.type = type;
    this.imageName = searchMap.pins[type];
}

