var SLICE_STYLE_NORMAL = 1;
var SLICE_STYLE_HIGHLIGHTED = 2;
var SLICE_STYLE_FADED = 3;

function createSliceIcon(num, style) {
	var icon = new GIcon();
	if (num < 0) {
		icon.image = iurl('map/') + ((style == SLICE_STYLE_HIGHLIGHTED) ? 'marker_star_highlighted.png' : 'marker_star.png');
	} else {
		var style_dir;
		if (style == SLICE_STYLE_NORMAL) {
			style_dir = 'marker';
		} else if (style == SLICE_STYLE_HIGHLIGHTED) {
			style_dir = 'marker_highlighted';
		} else if (style == SLICE_STYLE_FADED) {
			style_dir = 'marker_faded';
		}
		icon.image = iurl('map/') + style_dir + '/' + num + '.png';
	}
	icon.iconSize = new GSize(20, 29);
	icon.shadow = iurl('map/marker_shadow.png');
	icon.shadowSize = new GSize(38, 29);
	icon.iconAnchor = new GPoint(15, 29);
	icon.infoWindowAnchor = new GPoint(15, 3);
	return icon;
};

function createBizMarker(biz, number, faded) {
	point = new GPoint(biz['longitude'], biz['latitude']);

	var icon = createSliceIcon(number, faded ? SLICE_STYLE_FADED : SLICE_STYLE_NORMAL);

	// render popup HTML
	var popupHTML = '<div style="text-align: left; padding: 10px;"><table cellspacing="0" cellpadding="0" border="0"><tr valign="top">';
	if (biz['photos'].length > 0) {
		bizPhotoUrl = imagesHostUrl + 'bphoto/' + biz['photos'][0]['id'] + '/ms';
		preloadImage(bizPhotoUrl);
		popupHTML += '<td><img src="' + bizPhotoUrl + '"></td><td width="10">&nbsp;</td>';
	}
	popupHTML += '<td>';
	if (biz['review_count'] > 0) {
		popupHTML += '<span style="vertical-align: -5px;">' + stars(biz['avg_rating'], false) + '</span> <span style="color: #555; font-weight: 500; font-size: 11px; font-style: oblique;">based on ' + biz['review_count'] + ' review' + (biz['review_count'] != 1 ? 's' : '') + '</span><br/>';
	}
	popupHTML += '<h3>' + biz['name'] + '</h3>';

	if (biz['phone']) {
		popupHTML += '<b>' + biz['phone'] + '</b>';
	}
	if (biz['neighborhoods'].length > 0) {
		popupHTML += '<br/>District: ' + biz['neighborhoods'].join(', ');
	}
	if (biz['address1']) {
		popupHTML += '<br/>' + biz['address1'];
	}
	if (biz['address2']) {
		popupHTML += '<br/>' + biz['address2'];
	}
	popupHTML += '<br/>';
	if (biz['city']) {
		popupHTML += biz['city'] + ', ';
	}
	if (biz['state']) {
		popupHTML += biz['state'] + ' ';
	}
	if (biz['zip']) {
		popupHTML += biz['zip'];
	}
	popupHTML += '</td>';
	popupHTML += '</tr></table></div>';

	clickURL = '/biz/' + biz['id'];

	return new GxMarker(point, icon, popupHTML, clickURL);
};

function getAdjustedMapBounds(map, mapContainer) {
	var adjBounds = {};
	var topDead = 25;
	var leftDead = 10;
	var rightDead = 5;
 	var bounds = map.getBounds();
	var lonPerPix = (bounds.getNorthEast().lng() - bounds.getSouthWest().lng())/mapContainer.clientWidth;
	var latPerPix = (bounds.getNorthEast().lat() - bounds.getSouthWest().lat())/mapContainer.clientHeight;
	
	adjBounds['minlon'] = bounds.getSouthWest().lng() + leftDead*lonPerPix;
	adjBounds['maxlon'] = bounds.getNorthEast().lng() - rightDead*lonPerPix;
	adjBounds['minlat'] = bounds.getSouthWest().lat();
	adjBounds['maxlat'] = bounds.getNorthEast().lat() - topDead*latPerPix;
	return adjBounds;
};