var bigLat      = -90.0;
var bigLon      = -180.0;
var avgLat      = 0.0;
var avgLon      = 0.0;
var smallLat    = 90.0;
var smallLon    = 180.0;


//------------------------------------------------------------------------------
// Initializes the map's common variables. These variables determine the zoom
// level of the map based on the map size and number of coordinates. 
//------------------------------------------------------------------------------
function initializeTheMap (points) {
    var numCoords = 0;

    for(i = 0; i < points.length; i++) {
        if (points[i].type == 'H') {
            numCoords++;
            avgLat += points[i].lat;
            avgLon += points[i].lon;

            var tmpLat = points[i].lat;
            if (smallLat > tmpLat) smallLat = tmpLat;
            if (bigLat < tmpLat) bigLat = tmpLat;
            var tmpLon = points[i].lon;
            if (smallLon > tmpLon) smallLon = tmpLon;
            if (bigLon < tmpLon) bigLon = tmpLon;
        }
    }
    
    if (numCoords > 1) {
        avgLat /= numCoords;
        avgLon /= numCoords;
    }
}

//------------------------------------------------------------------------------
// Adjusts the zoom level based on the distance
//------------------------------------------------------------------------------
function AdjustZoomLevel(d) {
    if (d < 0.7)    return 14;  // Zoomlevel 14 = 0.7  miles
    if (d < 1.0)    return 13;  // Zoomlevel 13 = 1    miles
    if (d < 2.5)    return 12;  // Zoomlevel 12 = 2.5  miles
    if (d < 5.0)    return 11;  // Zoomlevel 11 = 5    miles
    if (d < 10.0)   return 10;  // Zoomlevel 10 = 10   miles
    if (d < 20.0)   return 9;   // Zoomlevel  9 = 20   miles
    if (d < 40.0)   return 8;   // Zoomlevel  8 = 40   miles
    if (d < 90.0)   return 7;   // Zoomlevel  7 = 90   miles
    if (d < 150.0)  return 6;   // Zoomlevel  6 = 150  miles
    if (d < 300.0)  return 5;   // Zoomlevel  5 = 300  miles
    if (d < 700.0)  return 4;   // Zoomlevel  4 = 700  miles
    if (d < 1000.0) return 3;   // Zoomlevel  3 = 1000 miles
    if (d < 2500.0) return 2;   // Zoomlevel  2 = 2500 miles
    else return 1;              // Zoomlevel  1 = 5000 miles
}

//------------------------------------------------------------------------------
// Returns distance on map 
//------------------------------------------------------------------------------
function DistanceOnMap(tempLat1, tempLong1, tempLat2, tempLong2) {
    var RADIUS_OF_EARTH     = 3963.191; // radius at equator in miles
    var SCALE_OF_MAP        = 145;      // map scale bar in pixels
    var SMALL_EDGE_OF_MAP   = 400;      // taken from div tag in map.jsp
    var lat1    = tempLat1  / 180 * Math.PI;
    var long1   = tempLong1 / 180 * Math.PI;
    var lat2    = tempLat2  / 180 * Math.PI;
    var long2   = tempLong2 / 180 * Math.PI;
    var e = Math.acos( Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(long2-long1) );
    var d = e * RADIUS_OF_EARTH;
    return d / (SMALL_EDGE_OF_MAP / SCALE_OF_MAP);
}

//------------------------------------------------------------------------------
// Returns Star image
//------------------------------------------------------------------------------
function Stars(stars) {
    switch (stars) {
        case 0.0: return '';
        case 0.5: return '<img src="/images/0-5_stars.gif" />';
        case 1.0: return '<img src="/images/1_star.gif" />';
        case 1.5: return '<img src="/images/1-5_stars.gif" />';
        case 2.0: return '<img src="/images/2_stars.gif" />';
        case 2.5: return '<img src="/images/2-5_stars.gif" />';
        case 3.0: return '<img src="/images/3_stars.gif" />';
        case 3.5: return '<img src="/images/3-5_stars.gif" />';
        case 4.0: return '<img src="/images/4_stars.gif" />';
        case 4.5: return '<img src="/images/4-5_stars.gif" />';
        case 5.0: return '<img src="/images/5_stars.gif" />';
    }
}

//------------------------------------------------------------------------------
// Returns thumbnail urls for BU1, BU9, BU40 and BU60
// For BU40 and BU60, the string "//images.travelnow.com" precedes the image
// location. In that case, we ensure that "//images.travelnow.com" is not 
// pre pended to the Thumbnail image.
// For BU1 and BU9, we construct URL by prepending the 
// string "//images.travelnow.com" 
//------------------------------------------------------------------------------
function getThumbNailUrlForDiffBaseUsers(pt) {
    var startTdTag = '<td id="thumbNailTd" width="64" height="100%" >';
    var endTdTag = '</td><td width="20"></td>'
    
    if (pt.thumbNailUrl != null && pt.thumbNailUrl != "") {
        return pt.thumbNailUrl.indexOf('//images.travelnow.com') > -1
            ? startTdTag + '<img src="http:'+pt.thumbNailUrl+'" height="64px" width="64px" />' + endTdTag 
            : startTdTag + '<img src="http://images.travelnow.com'+pt.thumbNailUrl+'" height="64px" width="64px" />' + endTdTag 
    }
    
    return '';
}


//------------------------------------------------------------------------------
// Gets the description for the info window of a marker/point
//------------------------------------------------------------------------------
function getMapDescription(pt) {
    var fromRate = propertyMap || pt.price == null || pt.price == '$0.00' ? "" : pt.price;
    var starsStr = pt.star ? Stars(pt.star) : "";
    var propNameLink = pt.type=='H' && !disableHotelLinks 
        ? '<b><a href="javascript:viewHotel(' + pt.id + ');">' + pt.name + '</a></b>'
        : pt.name;

	return '<table height="100%"  border="0" cellpadding="0" cellspacing="0">'
	    + '<tr height="100%" >'
        + getThumbNailUrlForDiffBaseUsers(pt)
	    + '<td id="starsTd" style="padding: 4px;" valign="top" height="100%">'
	    + (propertyMap || pt.type != 'H' ? "" : ('<b>' + pt.seqNum + '. </b>'))
	    + propNameLink + '<br>'
	    + fromRate + '<br>'
	    + starsStr + '<br>'
	    + '</td><td width="40"></td>'
	    + '</tr>'
	    + '</table>';
}
