//<![CDATA[
function Listing() {};
Listing.prototype.orderId = 0;
Listing.prototype.id = 0;
Listing.prototype.title = "";
Listing.prototype.address1 = "";
Listing.prototype.address2 = "";
Listing.prototype.city = "";
Listing.prototype.state = "";
Listing.prototype.zipCode = "";
Listing.prototype.lat = 0;
Listing.prototype.lng = 0;

Listing.prototype.addressOneLine = function() {
	var stringBuilder = new Array();
	if( this.address1 != "" ) {
		stringBuilder.push( this.address1 );
	}
	if( this.address2 != "" ) {
		if( stringBuilder.length > 0 ) {
			stringBuilder.push( ", " );
		}
		stringBuilder.push( this.address2 );
	}
	if( this.city != "" ) {
		if( stringBuilder.length > 0 ) {
			stringBuilder.push( ", " );
		}
		stringBuilder.push( this.city );
	}
	if( this.state != "" ) {
		if( stringBuilder.length > 0 ) {
			stringBuilder.push( ", " );
		}
		stringBuilder.push( this.state );
	}
	if( this.zipCode != "") {
	    if( stringBuilder.length > 0 ) {
	        if( this.state != "" ) {
	            stringBuilder.push( " " );
	        } else {
	            stringBuilder.push( ", " );
	        }
	    }
	    stringBuilder.push( this.zipCode );
	}
	return stringBuilder.join( "" );
}

Listing.prototype.addressFormal = function() {
    var stringBuilder = new Array();
    if( this.title != "" ) {
        stringBuilder.push( this.title );
        stringBuilder.push( "<br />" );
    }
    if( this.address1 != "" ) {
        stringBuilder.push( this.address1 );
        stringBuilder.push( "<br />" );
    }
    if( this.address2 != "" ) {
        stringBuilder.push( this.address2 );
        stringBuilder.push( "<br />" );
    }
    if( this.city != "" ) {
		stringBuilder.push( this.city );
	}
	if( this.state != "" ) {
		if( this.city != "" ) {
			stringBuilder.push( ", " );
		}
		stringBuilder.push( this.state );
	}
	if( this.zipCode != "") {
        if( this.state != "" ) {
            stringBuilder.push( " " );
        } else {
            stringBuilder.push( ", " );
        }
	    stringBuilder.push( this.zipCode );
	}
	return stringBuilder.join( "" );
}


var baseIcon = new GIcon();
baseIcon.iconSize = new GSize(30, 33);
baseIcon.iconAnchor = new GPoint(9, 34);
//baseIcon.infoWindowAnchor = new GPoint(9, 2);
//baseIcon.infoShadowAnchor = new GPoint(18, 25);

function createMarker(imagesBaseUrl, detailsBaseUrl, latLng, index, listingUrl) {
    var iconImageUrl = new Array();
    iconImageUrl.push(imagesBaseUrl);
    iconImageUrl.push(index);
    iconImageUrl.push("_pinpoint.png");

    var icon = new GIcon(baseIcon);
    icon.image = iconImageUrl.join("");
    
    var marker = new GMarker(latLng, {"icon" : icon});
    GEvent.addListener(marker, "click", function() {window.location = listingUrl;})
    
    return marker;
}


function renderSingleMarkerMap(mapDivId, markerLat, markerLng) {
    var markerLatLng = new GLatLng(markerLat, markerLng);
    var marker = new GMarker(markerLatLng, {"icon" : baseIcon});
    
    var map = new GMap2(document.getElementById(mapDivId));
    map.addControl(new GSmallZoomControl());
    map.setCenter(markerLatLng, 15);
    map.addOverlay(marker);
}

function renderMultiMarkerMap(imagesBaseUrl, detailsBaseUrl, mapDivId, listings, mainMarkerLat, mainMarkerLng, hasMainMarker) {
    var mainLatLng = new GLatLng(mainMarkerLat, mainMarkerLng);
    var mainMarker = new GMarker(mainLatLng, {"icon" : baseIcon});

    var map = new GMap2(document.getElementById(mapDivId));
    map.addControl(new GSmallZoomControl());
    map.setCenter(mainLatLng, 0);
    mapMarkerManager = new GMarkerManager(map);
    
    var markers = new Array();
    var bounds = new GLatLngBounds();
    for(i=0; i<listings.length; i++) {
        var latLng = new GLatLng(listings[i].lat, listings[i].lng);
        var marker = createMarker(imagesBaseUrl, detailsBaseUrl, latLng, listings[i].orderId, listings[i].url);
        markers.push(marker);
        bounds.extend(latLng);
    }
    
    if(hasMainMarker)
    {
        markers.push(mainMarker);
        bounds.extend(mainLatLng);
    }
    
    mapMarkerManager.addMarkers(markers, 0);
    mapMarkerManager.refresh();
    
    map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter());
}
//]]>
