/*Google maps POI overlay

//requires markermanager.js

*/

var aaPOIMarkers = []; 
var aaPOImgr;

/*data*/
var aaSinglePOIData="/aaservlet/poi-summary";



/*hotel variables*/
var iconHotelImagePath="/images/routes/poi/";
var iconHotelShadowFileName="shadow-17x17sq.png";
var iconHotelFileName="hotel-icon.png";
var iconSmallHotelFileName="aa-marker-dot6.gif";
var iconSmallHotelMarkerUrl=iconHotelImagePath + iconSmallHotelFileName;
var iconMedHotelFileName="hotel-icon-mid.gif";
var iconMedHotelShadowFileName="shadow-10x10sq.png";
var iconMedHotelMarkerUrl=iconHotelImagePath + iconMedHotelFileName;

var hotelIcon = new GIcon(G_DEFAULT_ICON,iconHotelImagePath +iconHotelFileName);
		hotelIcon.iconSize = new GSize(17, 17);
		hotelIcon.iconAnchor = new GPoint(9, 9);
		hotelIcon.infoWindowAnchor = new GPoint(8, 0);
		hotelIcon.printImage=iconHotelImagePath +"hotel-icon-print.gif";
		hotelIcon.mozPrintImage=iconHotelImagePath +"hotel-icon-print.gif";
		hotelIcon.shadow = iconHotelImagePath +iconHotelShadowFileName;
		hotelIcon.shadowSize = new GSize(20, 21);
    hotelIcon.imageMap=[0,0,0,17,17,17,17,0];

var hotelMedIcon = new GIcon(G_DEFAULT_ICON,iconMedHotelMarkerUrl);
		hotelMedIcon.iconSize = new GSize(10, 10);
		hotelMedIcon.iconAnchor = new GPoint(5, 5);
		hotelMedIcon.infoWindowAnchor = new GPoint(5, 0);
		hotelMedIcon.printImage=iconMedHotelMarkerUrl;
		hotelMedIcon.mozPrintImage=iconMedHotelMarkerUrl;
		hotelMedIcon.shadow = iconHotelImagePath +iconMedHotelShadowFileName;
		hotelMedIcon.shadowSize = new GSize(12, 13);
		hotelMedIcon.imageMap=[0,0,0,10,10,10,10,0];
		
/*restaurant variables*/
var iconRestaurantImagePath="/images/routes/poi/";
var iconRestaurantShadowFileName="shadow-17x17sq.png";
var iconRestaurantFileName="restaurant-icon.png";
var iconSmallRestaurantFileName="aa-marker-dot6.gif";
var iconSmallRestaurantMarkerUrl=iconRestaurantImagePath + iconSmallRestaurantFileName;
var iconMedRestaurantFileName="restaurant-icon-mid.gif";
var iconMedRestaurantShadowFileName="shadow-10x10sq.png";
var iconMedRestaurantMarkerUrl=iconRestaurantImagePath + iconMedRestaurantFileName;


var restaurantIcon = new GIcon(G_DEFAULT_ICON,iconRestaurantImagePath +iconRestaurantFileName);
		restaurantIcon.iconSize = new GSize(17, 17);
		restaurantIcon.iconAnchor = new GPoint(9, 9);
		restaurantIcon.infoWindowAnchor = new GPoint(8, 0);
		restaurantIcon.printImage=iconRestaurantImagePath +"restaurant-icon-print.gif";
		restaurantIcon.mozPrintImage=iconRestaurantImagePath +"restaurant-icon-print.gif";
		restaurantIcon.shadow = iconRestaurantImagePath +iconRestaurantShadowFileName;
		restaurantIcon.shadowSize = new GSize(20, 21);
    restaurantIcon.imageMap=[0,0,0,17,17,17,17,0];

var restaurantMedIcon = new GIcon(G_DEFAULT_ICON,iconRestaurantImagePath +iconMedRestaurantFileName);
		restaurantMedIcon.iconSize = new GSize(10, 10);
		restaurantMedIcon.iconAnchor = new GPoint(5, 5);
		restaurantMedIcon.infoWindowAnchor = new GPoint(5, 0);
		restaurantMedIcon.printImage=iconRestaurantImagePath +iconMedRestaurantFileName;
		restaurantMedIcon.mozPrintImage=iconRestaurantImagePath +iconMedRestaurantFileName;
		restaurantMedIcon.shadow = iconRestaurantImagePath +iconMedRestaurantShadowFileName;
		restaurantMedIcon.shadowSize = new GSize(12, 13);
    restaurantMedIcon.imageMap=[0,0,0,10,10,10,10,0];

/*speedcamera variables*/
var iconCameraImagePath="/images/routes/poi/";
var iconCameraShadowFileName="";
var iconCameraFileName="speed-camera17x17.png";
var iconSmallCameraFileName="aa-marker-dot6.gif";
var iconSmallCameraMarkerUrl=iconCameraImagePath + iconSmallCameraFileName;
var iconMedCameraFileName="speed-camera10x10.gif";
var iconMedCameraShadowFileName="";
var iconMedCameraMarkerUrl=iconCameraImagePath + iconMedCameraFileName;

var cameraIcon = new GIcon(G_DEFAULT_ICON,iconCameraImagePath +iconCameraFileName);
		cameraIcon.iconSize = new GSize(17, 17);
		cameraIcon.iconAnchor = new GPoint(9, 9);
		cameraIcon.infoWindowAnchor = new GPoint(8, 0);
		cameraIcon.printImage=iconCameraImagePath +"speed-camera17x17.gif";
		cameraIcon.mozPrintImage=iconCameraImagePath +"speed-camera17x17.gif";
		cameraIcon.shadow = "";
		cameraIcon.shadowSize = new GSize(20, 21);
    cameraIcon.imageMap=[0,0,0,17,17,17,17,0];

var cameraMedIcon = new GIcon(G_DEFAULT_ICON,iconMedCameraMarkerUrl);
		cameraMedIcon.iconSize = new GSize(10, 10);
		cameraMedIcon.iconAnchor = new GPoint(5, 5);
		cameraMedIcon.infoWindowAnchor = new GPoint(5, 0);
		cameraMedIcon.printImage=iconMedCameraMarkerUrl;
		cameraMedIcon.mozPrintImage=iconMedCameraMarkerUrl;
		cameraMedIcon.shadow = "";
		cameraMedIcon.shadowSize = new GSize(12, 13);
		cameraMedIcon.imageMap=[0,0,0,10,10,10,10,0];



function addLocalPOIMarkers(map,lat,lon,poiMsgID,poiTypeID,bolUpdateHotelBar)
{
    	    addPOIJsonData(map,lat,lon,poiMsgID,poiTypeID,bolUpdateHotelBar,'',false);

}

function createQueryParamString(poiType, poiTypeId, maxResults, gazLocString, lat, lon)
{
	var params='poitype=' + poiTypeId + '&maxresults=' + maxResults;
	if (poiType=='hotel')
	{
		if (gazLocString=='' || gazLocString===undefined)
		{
			params += '&lat=' + lat + '&long=' + lon;
		}
		else
		{
			params += '&gazLocation=' + gazLocString;
		}
	}
	else if (poiType == 'restaurant')
	{
		params += '&lat=' + lat + '&long=' + lon;
	}
	return params;
}


function showSinglePOI(map,lat,lng,poiID,poiTypeID,bolShowInfoWindow,bolZoomToPOI,poiTitle)
{
  //display marker if it doesn't exist...

  	
         var poiType=getPOITypeFromID(poiTypeID);
        var   point = new GLatLng(lat,lng);
        var   poiMarker = createPOIMarker(point,poiID,'l',poiTypeID,poiTitle); //actual icon            
        var   poiMediumMarker=createPOIMarker(point,poiID,'m',poiTypeID,poiTitle); //med icons
        var   poiSmallMarker=createPOIMarker(point,poiID,'s',poiTypeID,poiTitle); //small icons
     
        //prevent duplication
        //check key doesn't already exist
        if(aaPOIMarkers[poiID]===undefined)
        {          
          aaPOIMarkers[poiID]=new Array(poiID,poiTypeID,poiTitle,point,poiMarker,poiMediumMarker,poiSmallMarker);
          aaPOImgr.addMarker(poiMarker,11,17); 
          aaPOImgr.addMarker(poiMediumMarker,8,10); 
          aaPOImgr.addMarker(poiSmallMarker,0,7);
        }
       

       aaPOImgr.refresh();
	
	
	
  if(bolZoomToPOI)
    {
	   //zoom to at least 11
      var currentZoomLevel=map.getZoom();

      if(currentZoomLevel<11)
      {
      map.setCenter(new GLatLng(lat,lng), 11);
      }
      else {
      map.setCenter(new GLatLng(lat,lng));
      }
	  }



    //wait for map
    if(bolShowInfoWindow)
    {
      setTimeout(function(){showPOIInfoWindow(point,poiID,poiTypeID);point=null;poiID=null;poiType=null},400);
    }
    
    
}

function showPOIInfoWindow(point,poiID,poiTypeID)
{
  var tmpStrPOItype=getPOITypeFromID(poiTypeID);
	mapGoogle.openInfoWindowHtml(point , "<div id=\'aa-iwindow\' class='iw-" + tmpStrPOItype + "'></div>") 
	//marker.openInfoWindowHtml("<div id=\'aa-iwindow\' class='iw-" + poiType + "'></div>");
	getSinglePOIData(poiID,poiTypeID);
    

}


function createPOIMarker(point,poiID,clusterIconSize,poiTypeID,poiTitle) 
{

//SpeedCamera - poiTypeID=-1

  var poiMarker;
  poiTitle=unescape(poiTitle);
  if(poiTypeID==102 || poiTypeID==103 || poiTypeID==118 || poiTypeID==130)
  {
    poiTitle=poiTitle + " (accommodation)";
  }
  else if(poiTypeID==100){
    poiTitle=poiTitle + " (restaurant)";
  }

  if(clusterIconSize=='l'||clusterIconSize=='m')
  {
    if(clusterIconSize=='l')
    {
      if(poiTypeID==102||poiTypeID==103||poiTypeID==104||poiTypeID==118 || poiTypeID==130)
      {
        poiMarker = new GMarker(point,{icon:hotelIcon,title:poiTitle});
      }
      else if (poiTypeID==100||poiTypeID==113)
      {
        poiMarker=new GMarker(point,{icon:restaurantIcon,title:poiTitle});
      }
      else if (poiTypeID==-1)
      {
        poiMarker=new GMarker(point,{icon:cameraIcon,clickable:false,title:poiTitle});
      }
      else {
        //default icon
        poiMarker=new GMarker(point,{title:poiTitle});
      }
    }
    else if (clusterIconSize=='m')
    {
      if(poiTypeID==102||poiTypeID==103||poiTypeID==104||poiTypeID==118 || poiTypeID==130)
      {
        poiMarker = new GMarker(point,{icon:hotelMedIcon,title:poiTitle});
      }
        else if (poiTypeID==100||poiTypeID==113)
      {
        poiMarker=new GMarker(point,{icon:restaurantMedIcon,title:poiTitle});
      }
        else if (poiTypeID==-1)
      {
        poiMarker=new GMarker(point,{icon:cameraMedIcon,clickable:false,title:poiTitle});
      }
      else {
        //default icon
        poiMarker=new GMarker(point,{title:poiTitle});
      }
    }
    
    if(poiTypeID==102||poiTypeID==103||poiTypeID==104||poiTypeID==118 || poiTypeID==130)
    {
      poiType='hotel';
    }
    else if (poiTypeID==100||poiTypeID==113)
    {
      poiType='restaurant';
    }
    
    //no info windows for certain poitypes
    if(!(poiTypeID==-1))
    {
      GEvent.addListener(poiMarker, 'click', function() {  
              // When clicked, open an Info Window  
              poiMarker.openInfoWindowHtml("<div id=\'aa-iwindow\' class=\'iw-" + poiType + "\'></div>"); 
       });        
      /*only fire contents call once window is open*/        
      GEvent.addListener(poiMarker,'infowindowopen',function() {
        getSinglePOIData(poiID,poiTypeID);
      });
    }
            
          
  }
  else if (clusterIconSize=='s')
  {
    //small icons ,light marker, default hotel
    var tmpIconURL=iconSmallHotelMarkerUrl;
    if(poiTypeID==100||poiTypeID==113)
    {
      tmpIconURL=iconSmallRestaurantMarkerUrl;
    }
    poiMarker=new MarkerLight(point,{image:tmpIconURL,height:6,width:6});
  }

	return poiMarker;
}
	

function clearPOIMarkers(mgr) {
	try {
		
  		mgr.clearMarkers();
	}
	catch(e) {};
}

function clearAllPOIMarkers()
{
	try{
		aaPOImgr.clearMarkers();
		aaPOIMarkers = [];  
	}
	catch(e) {}
}	

function showPOIMarkers(mgr)
{
  mgr.show();
}

function hidePOIMarkers(mgr)
{
  mgr.hide();
}


function addPOIJsonData(map,lat,lon,poiMsgID,poiTypeID,bolUpdateHotelBar,gazLocString,bolZoomToLocation)
{

//max results determined by zoom level?

  var url='/aaservlet/poi-search';
  var params='poitype=' + poiTypeID + '&lat=' + lat + '&long=' + lon + '&maxresults=25';
  if(aaMapPage!='') params+='&ref=' + aaMapPage;
  if(gazLocString!='') params+='&gazLocation=' + gazLocString;
    
  //display loading
  displayPOIMsg("#" + poiMsgID,'Loading...',false);
  

	 $.ajax({
  url:url,
  type: "get",
  data: params,
  dataType: "json",
  error: function(xhr, desc, exceptionobj) { 
          displayPOIMsg("#" + poiMsgID,"There was a problem loading the data. Please try again later.",true);
  },
  success:function(json)
  {
  
        var tmpLmarker=[];
        var tmpMmarker=[];
        var tmpSmarker=[];
  
      //now we have an array of poi
      $.each(json.summaries, function(i,poiItem)
      {
      
        var strName=poiItem.name;
        var strPOIid=poiItem.id;
        var  tmpMarkerFunc=createPOIMarker;
        var   point = new GLatLng(poiItem.latitude,poiItem.longitude);
        var   poiMarker = tmpMarkerFunc(point,strPOIid,'l',poiTypeID,strName); //actual icon            
        var   poiMediumMarker=tmpMarkerFunc(point,strPOIid,'m',poiTypeID,strName); //med icons
        var   poiSmallMarker=tmpMarkerFunc(point,strPOIid,'s',poiTypeID,strName); //small icons
          
        //only add if POI ID does not exist in array
        //or simply clear down existing markers?


        /* checking array
        if(!chkMarkerExists(strPOIid))
        {
            aaPOIMarkers.push(new Array(strPOIid,poiTypeID,strName,point));
          tmpLmarker.push(poiMarker);
          tmpMmarker.push(poiMediumMarker);
          tmpSmarker.push(poiSmallMarker);
        }
        */
        //prevent duplication
        //check key doesn't already exist
        if(aaPOIMarkers[strPOIid]===undefined)
        {
          tmpLmarker.push(poiMarker);
          tmpMmarker.push(poiMediumMarker);
          tmpSmarker.push(poiSmallMarker);
          
          aaPOIMarkers[strPOIid]=new Array(strPOIid,poiTypeID,strName,point,poiMarker,poiMediumMarker,poiSmallMarker);
                    
        }


       });
       
       aaPOImgr.addMarkers(tmpLmarker,11,17); 
       aaPOImgr.addMarkers(tmpMmarker,8,10); 
       aaPOImgr.addMarkers(tmpSmarker,0,7);
       aaPOImgr.refresh();
	
  //clear loading
  hidePOIMsg("#overlayMapMessage");
  
  if(bolZoomToLocation&&lat!=''&&lon!='')
  {
    mapGoogle.setCenter(new GLatLng(lat,lon),11);
  }
  else if(bolZoomToLocation&&gazLocString!='')
  {
    //get northings and eastings from gaz string
    
    var arrGazString=gazLocString.split("%7C");
    var tmpEastings=arrGazString[3];
    var tmpNorthings=arrGazString[4]; 
    
    var geo=ConvertBNGtoWGS84(tmpEastings,tmpNorthings);
    var newGLocation=new GLatLng(geo.latitude, geo.longitude)
    mapGoogle.setCenter(newGLocation); 

  }
  if(bolZoomToLocation)
  {
   //zoom to at least 11
      var currentZoomLevel=map.getZoom();

      if(currentZoomLevel<11)
      {
        mapGoogle.setZoom(11);
      }    
  } 

  if(bolUpdateHotelBar&&poiTypeID==118)
	{

    var maxHotelsToDisplay=25;
    
    //get number of hotels to return from hotel bar width
    if(aaMapPage==''||aaMapPage=='mapfinder'||aaMapPage=='mapfinder')
    {
        var nearbyHotelsElement = $('#nearbyHotels');
        var padding = 14;
        var margin = 20;
        var hotelBarWidth = nearbyHotelsElement.outerWidth(true) - padding - margin;
        maxHotelsToDisplay = Math.floor( hotelBarWidth/213 );
    }

      // Populate hotel bar with results of search that excludes non-AA hotels
				$.ajax({
					url:url,
					type: "get",
					data: createQueryParamString('hotel', '131', maxHotelsToDisplay, gazLocString, lat, lon),
					dataType: "json",
					error: function(xhr, desc, exceptionobj) { 
						// Do not display an error message
					},
					success:function(aaratedjson)
					{
						populateHotelsBarFromJson(aaratedjson,true,poiMsgID,'');
					} 
				});
	}
	if(poiTypeID==118&&json.summaries.length==0)
	{
		displayPOIMsg("#" + poiMsgID,"Sorry, we couldn't find any suitable accommodation in your selected area.",true);
	}
     	if(poiTypeID==102&&json.summaries.length==0)
	{
		displayPOIMsg("#" + poiMsgID,"Sorry, we couldn't find any suitable restaurants in your selected area.",true);
	}

    } //end if success
    
    });//end ajax request


}


function addMarkersFromLatLngArray(arrLatLng,poiTypeID)
{


      var tmpLmarker=[];
      var tmpMmarker=[];
      var tmpSmarker=[];
  
      //now we have an array of poi
      for(i=0;i<arrLatLng.length;i++)
      {
      
        var strName="";
        if(poiTypeID==-1)
        {
          strName='Speed camera';
        }
        var strPOIid="";
        //generate ID from latlng
        strPOIid=arrLatLng[i][0] + "" + arrLatLng[i][1];
        strPOIid=strPOIid.replace(/-/g, "");
        strPOIid=strPOIid.replace(/\./g, "");
        strPOIid=strPOIid.replace(/,/g, "");
        
        
        var  tmpMarkerFunc=createPOIMarker;
        var   point = new GLatLng(arrLatLng[i][0],arrLatLng[i][1]);
        var   poiMarker = tmpMarkerFunc(point,strPOIid,'l',poiTypeID,strName); //actual icon            
        var   poiMediumMarker=tmpMarkerFunc(point,strPOIid,'m',poiTypeID,strName); //med icons
        var   poiSmallMarker=tmpMarkerFunc(point,strPOIid,'s',poiTypeID,strName); //small icons
          
        //prevent duplication
        //check key doesn't already exist
        if(aaPOIMarkers[strPOIid]===undefined)
        {
          tmpLmarker.push(poiMarker);
          tmpMmarker.push(poiMediumMarker);
          tmpSmarker.push(poiSmallMarker);
          
          aaPOIMarkers[strPOIid]=new Array(strPOIid,poiTypeID,strName,point,poiMarker,poiMediumMarker,poiSmallMarker);
                    
        }
       }
       
       aaPOImgr.addMarkers(tmpLmarker,11,17); 
       aaPOImgr.addMarkers(tmpMmarker,8,10); 
       aaPOImgr.addMarkers(tmpSmarker,0,7);
       aaPOImgr.refresh();
}


function chkMarkerExists(strPOIid)
{
  for(i=0;i<aaPOIMarkers.length;i++)
  {
            if(aaPOIMarkers[i][0]==strPOIid)
            {
              return true;
            }
  }
  return false;
}

function chkMarkerExistsInArrayString(strPOIid)
{
  var strMarkers=aaPOIMarkers.toString();
  if(strMarkers.indexOf(strPOIid)>-1)
  {
    return true;
  }
   return false;
}







function checkAndCreateMgr(mgr,poiType,map)
{
  if(mgr===undefined && poiType=='hotel')
  {
    hotelmgr=new MarkerManager(map);
  }
  else if(mgr===undefined && poiType=='restaurant')
  {
    restaurantmgr=new MarkerManager(map);
  }
  else if(mgr===undefined && poiType=='bandb')
  {
    bandbmgr=new MarkerManager(map);
  }
}





function getSinglePOIData(poiID,poiTypeID)
{
  $("#aa-iwindow").html("Loading...");
  //timeout required for infowindow to initialise
  setTimeout(function(){callSinglePOIAjax(poiID,poiTypeID)},200);
  
  
}

function callSinglePOIAjax(inpPoiID,inpPoiTypeID)
{
  var strOutput='';
  var strMoreInfoURL='';
  var inpPOIType=getPOITypeFromID(inpPoiTypeID);
  
  $.ajax({
  url:aaSinglePOIData,
  type: "get",
  data: "poiId=" + inpPoiID,
  dataType: "json",
  error: function(xhr, desc, exceptionobj) { 
  var strErrorOutput="Sorry, there was a problem showing these details. ";
    if(inpPOIType=='hotel'||inpPOIType=='bandb')
    { 
      strErrorOutput+="You can <a href='travel/index.jsp'>search for accommodation here</a>.";
    }
    else if (inpPOIType=='restaurant')
    {
      strErrorOutput+="You can <a href='travel/index.jsp'>search for a restaurant here</a>.";
    }
    $("#aa-iwindow").html(strErrorOutput);
  },
  success:function(json)
  {
    
    var isRedStars=isEmpty(json.isRedStars); //false;
   
    var poiName=isEmpty(json.poiName);
    var category=isEmpty(json.category);
    var imageUrl=isEmpty(json.imageUrl);
    var detailsUrl=isEmpty(json.detailsUrl);
    var poiTypeID=isEmpty(json.poiType); //102
    var percent=isEmpty(json.percent); //76;
    var bookingUrl=isEmpty(json.bookingUrl); 
    var description=isEmpty(json.description);
    //var poiId=json.poiId;
    var rosettes=isEmpty(json.rosettes); //2
    var qualityRating=isEmpty(json.qualityRating); //4
    var priceFrom=isEmpty(json.priceFrom);//105}
    var postcode=isEmpty(json.postcode);
    var bolaaRated=isEmpty(json.aaRated);
    var bolMembershipDiscountAvailable=isEmpty(json.membershipDiscountAvailable);
    var encodedBookingURL=isEmpty(json.encodedBookingURL);
    var appointmentCurrent=isEmpty(json.appointmentCurrent);
    var additionalParms='';
    
    var starImage="star_" + qualityRating + ".gif";
    if(isRedStars)
    {
        starImage="redstar_" + qualityRating + ".gif";
    }
    
    
    if(poiTypeID=='102'||poiTypeID=='103')
    { 
      strMoreInfoURL="/travel/details/hotel/" + inpPoiID;
    }
    else if (poiTypeID=='104')
    {
      strMoreInfoURL="/travel/details/bandb/" + inpPoiID;
    }
    else if (poiTypeID=='100')
    {
      strMoreInfoURL="/travel/details/restaurant/" + inpPoiID;
    }
    
	if(description=='...')
	{
	description='';
	}	


     var isYellowStars=false;
      var isPieRated=false;
      var isEggRated=false;
    
    strOutput+='<p id="iw-poiTitle" >' + poiName + '</p>'
    + '\n<div id="iw-rhs">';

	if(imageUrl!='')
	{
	strOutput+='\n<div id="iw-poiImage"><a href="' + strMoreInfoURL +'""><img src="' + imageUrl + '" alt="' + poiName + '"/></a></div>'
	}
	else
	{
	strOutput+='\n<div id="iw-poiImage"><img src="/images/routes/poi/no-poi-image.gif" alt="No image available"/></div>'
	}

    strOutput+='\n<div id="iw-poiData">';


    var strRHSOutput="";
    
    
  
    if(bolaaRated && (poiTypeID==102 || poiTypeID==104))
    {
    strRHSOutput+= '\n<li >';
    strRHSOutput+='<img src="/images/logos/aa-logo-16.gif" alt="AA logo" />';
    

      if(appointmentCurrent=='U')
      {
        //no star ratings if unclassified
        strRHSOutput+='<img class="rating-unconfirmed" src="/images/getaway/hotels/ungraded_icon.gif" alt="Unconfirmed grading" />';
      }
      else 
      {

        if(qualityRating!='' && !isNaN(qualityRating))
        {
          if(appointmentCurrent=='A')
          {
            strRHSOutput+='<img class="rating-associate" src="/images/getaway/hotels/associate_cross.gif" alt="AA Associate" />';
          }
          strRHSOutput+= '<a href="/travel/accommodation_restaurants_grading.html#stars" id="iw-starrating"><img src="/images/travel/places_to_stay/' + starImage + '" alt="' + qualityRating + ' star" /></a>';
        }
      }
      
    strRHSOutput+='</li>'; 
     //end bolAArated 
    }  
    
    strRHSOutput+= '\n<li id="iw-poiDesignator">' + category + '</li>';
    
    if(rosettes!=0&&rosettes!='' && !isNaN(rosettes))
    {
    strRHSOutput+='\n<li ><a href="/travel/accommodation_restaurants_grading.html#restaurants"></a><img src="/images/common/rosette_' + rosettes +'.gif" alt="' + rosettes + ' AA Rosettes" /></li>';
    }
    if(percent!='' && !isNaN(percent) && !isRedStars && poiTypeID==102)
    {
    strRHSOutput+= '\n<li >AA merit score: ' + percent + '%</li>';
    }
    
    //strOutput+='\n<a href="/travel/accommodation_restaurants_grading.html#symbols"><img src="/images/eggcup.gif" alt="egg cup"/></a> '
    //strOutput+= '\n<a href="/travel/accommodation_restaurants_grading.html#symbols"><img src="/images/pie.gif" alt="pie"/></a>'
    //strOutput+= '\n<a href="/travel/accommodation_restaurants_grading.html#symbols"><img src="/images/yellow-star-1.gif" alt="yellow star"/></a>'

    if(priceFrom!='')
    {
      strRHSOutput+=  '\n<li >Price from <strong>&pound;' + priceFrom + '</strong></li>'
    }

    if (strRHSOutput!="" && poiTypeID!=130) 
    {
      strOutput+="\n<ul>" + strRHSOutput + "\n</ul>";
    }
    if(bookingUrl!='')
    {
        strOutput+=  '\n<a href="' + bookingUrl + '" id="iw-poiBook"><img src="/images/buttons/buttonyellow_bookonline-ni.gif" alt="Book online"></a>';
        additionalParms='?only_available=true&bookingURL=' + escape(bookingUrl);
        //Members link
        //check flag
        
        if(bolMembershipDiscountAvailable&&encodedBookingURL!='')
        {
        strOutput+= '\n<a href="/travel/basicsearch/hotels/hotels_member_confirm.jsp?sucessRedirectURL=' + encodedBookingURL + '" id="iw-memberlink">5% discount<br/>for AA Members &raquo;</a>';
        }
    }
    
       

    strOutput+=  '\n</div>'
    + '\n</div>'
    + '\n<p id="iw-poiContent">';
    /*if(poiTypeID==130)
    {
      strOutput+="This establishment is not currently a member of the <a href='/travel_editorial/hotel_services_index.html'>AA ratings scheme</a>.";
    }*/
    strOutput+=description;

    strOutput+='</p>'
    + '\n<p id="iw-moreinfolink">';
    
    // Is this establishment an AA-rated hotel, B and B or restaurant?
    if (poiTypeID==100 || poiTypeID==102  || poiTypeID==103 || poiTypeID==104 )
    { 
        if (additionalParms!='' && (poiTypeID==102  || poiTypeID==103 || poiTypeID==104 )) {
              strOutput+= '\n<a href="' + strMoreInfoURL + additionalParms + '">More info</a>&nbsp;&#0187;';
        }

        else {
           strOutput+= '\n<a href="' + strMoreInfoURL + '">More info</a>&nbsp;&#0187;';
        }
    }
    strOutput+="\n</p>";
    
    if(postcode!='')
    {
    
    
      if(bolRedirectToRP)
      {
        strOutput+='\n<p id="iw-newroute">Plan a new route <a href="/route-planner/index.jsp?toPlace='  +  escape(poiName)  + ", " + escape(postcode) + '">to here</a> or <a href="/route-planner/index.jsp?fromPlace='+  escape(poiName)  + ", " + escape(postcode) + '">from here</a>';
      }
      else
      {
        strOutput+='\n<p id="iw-newroute">Plan a new route <a href="#" onclick="startNewRoute(' + "'mmRouteTo','"  +  escape(poiName)  + ", " + escape(postcode) + "'" + ');"> to here</a> '
    + 'or <a  href="#" onclick="startNewRoute(' + "'mmrouteFrom','" +  escape(poiName)  + ", " + escape(postcode) + "'"+ ');">from here</a>';
      }
	
      if(bolShowVia)
      {
      //via link
      strOutput+='<br/>Update route <a href="#" onclick="startNewViaFromPOI(' + "'" + escape(poiName) + "','" + escape(postcode) + "'," + inpPoiID + ",'" + poiTypeID + "')" + ';">via here</a>';
      strOutput+='</p>';
      }
    }//end if postcode
    strOutput+='\n<br style="clear:both;"/>';

    
    $("#aa-iwindow").html(strOutput);
  }
  });
}


function showMarkerFromPOIID(poiID,lat,lon)
{
  
  $.ajax({
  url:aaSinglePOIData,
  type: "get",
  data: "poiId=" + poiID,
  dataType: "json",
  error: function(xhr, desc, exceptionobj) { 
    
  },
  success:function(json)
  {
     var strPOIName='';
      var strPOIType='';

      //showSinglePOI(mapGoogle,json.latitude,json.longitude,poiID,json.poiType,false,true,json.poiName);
  }
  });
}


function showPOIByDestination(e,lat,lon,poiTypeID,poiMsgID,gazLocString,bolUpdateHotelBar)
{
  //stop any other events firing;
  /*
  if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
  */
  mapGoogle.closeInfoWindow();

  if(poiTypeID==102&&(gazLocString==''||gazLocString===undefined))
  {
    //show hotels on map
    //displayLocalPOIs(mapGoogle,lat,lon,poiMsgID,poiType,url,params,hotelmgr,'',true);
    addPOIJsonData(mapGoogle,lat,lon,poiMsgID,118,bolUpdateHotelBar,'',true)
  }
  else if (poiTypeID==100)
  {
    //show restaurants on map
    //displayLocalPOIs(mapGoogle,lat,lon,poiMsgID,poiType,url,params,restaurantmgr,'',false);
    addPOIJsonData(mapGoogle,lat,lon,poiMsgID,100,bolUpdateHotelBar,'',true)
  }
  
  if(poiTypeID==102&&gazLocString!=''&&gazLocString!==undefined)
  {
    addPOIJsonData(mapGoogle,lat,lon,poiMsgID,118,bolUpdateHotelBar,gazLocString,true)
  }

}


function isEmpty(tmp)
{
    if(tmp===undefined)
    {
      tmp='';
    }
   return tmp;
}


function startNewRoute(field,location)
{
  location=unescape(location);
  $('#fragment-1').attr("class","map-tabs-panel");
  $('#fragment-2').attr("class","map-tabs-panel map-tabs-hide");

  if(field=='mmRouteTo')
  {
	$("#routeTo").val(location).css("background-color","#FFFFFF");
	setTimeout(function(){$("#routeFrom").focus().css("background-color","#FFE192");},500);
  }
  else if (field=='mmrouteFrom')
  {
	$("#routeFrom").val(location).css("background-color","#FFFFFF");;
	setTimeout(function(){$("#routeTo").focus().css("background-color","#FFE192");},500);

  }
  return false;
}


function startNewViaFromPOI(poiName,poiPostCode,poiId,poiTypeID)
{
	var confFromStr=$("#confirmFrom").val();
	var confToStr=$("#confirmTo").val();
	
	var confFromPlace=getGazPlaceFromString(confFromStr);
	var confToPlace=getGazPlaceFromString(confToStr);

  if(confFromPlace==''||confToPlace==''||poiPostCode=='')
  {
    //can't auto re-route so display error
  }
  


  var viaValue=poiName + ', ' + poiPostCode;
	startNewViaPCRoute(confFromPlace,confToPlace,viaValue,poiId,poiTypeID,poiName);
	return false;
}

function startNewViaPCRoute(fromPlace,toPlace,viaValue,poiId,poiTypeID,poiTitle)
{
  var GInfoWindow=mapGoogle.getInfoWindow();
  var GInfoWindowPoint=GInfoWindow.getPoint();
  var tmpPOILat=GInfoWindowPoint.lat();
  var tmpPOILng=GInfoWindowPoint.lng();
  
  //grab info window lat long
  mapGoogle.closeInfoWindow();
  
  //check existing vias
	if(viaCount>viaLimit)
	{
    displayPOIMsg("#overlayMapMessage",'Sorry, only ' + (viaLimit+1) + ' via locations allowed.',true);
    return; 
	}
  
  
  displayPOIMsg("#overlayMapMessage","Re-calculating your route...",false)
  
        $('#fragment-1').attr("class","map-tabs-panel");
	$('#fragment-2').attr("class","map-tabs-panel map-tabs-hide");
	
	//existing vias?
	repopulateStartVias();
	
	//create via
	createVia();
  
  var tmpViaID="#via" + (viaCount-1);
	$(tmpViaID).val(unescape(viaValue));
	
	//override original inputs with gaz place
	$("#routeFrom").val(unescape(fromPlace));
	$("#routeTo").val(unescape(toPlace));
	//and submit route...
	onGetRouteClicked();
	
	hidePOIMsg("#overlayMapMessage");
	
	//display marker for via
	setTimeout(function(){showSinglePOI(mapGoogle,tmpPOILat,tmpPOILng,poiId,poiTypeID,false,false,poiTitle);tmpPOILat=null;tmpPOILng=null;poiId=null;poiTypeID=null;},500);
	
	
  return false;
}

function repopulateStartVias()
{
  var tmpViaCount=viaCount;
  var arrViaPlaces=new Array();
  for (i=0;i<tmpViaCount;i++)
  {
    arrViaPlaces[i]=getGazPlaceFromString($("#viaChoice" + i).val());
  }
 
  clearViaInputs(); 
    
  for(i=0;i<arrViaPlaces.length;i++)
  {
    createVia();
    $("#via" + i).val(arrViaPlaces[i]);
  }
}

function getGazPlaceFromString(str)
{
	strOut="";
	try {
		var arrStr=str.split("|");
		strOut=arrStr[1];
	}
	catch(e) {
	}
	return strOut;
}



function displayPOIMsg(id,msg,bolIsError)
{
	if(bolIsError)
	{
		$(id).html(msg).append(" <a href='#' onclick=" + '"hidePOIMsg(' + "'#overlayMapMessage'); return false;" + '">Close message</a>').show();
	}
	else {
		$(id).html(msg).show();	
	}

}




function hidePOIMsg(id)
{
  $(id).hide();
}

function removePOIsByPOIType(poiTypeID)
{
  //call marker manager remove marker method
  //aaPOIMarkers[strPOIid]=new Array(strPOIid,poiTypeID,strName,point,poiMarker,poiMediumMarker,poiSmallMarker);
  for(var i in aaPOIMarkers) {

	var tmpPOITypeID=aaPOIMarkers[i][1];
		
  if(tmpPOITypeID==poiTypeID)
  {
    aaPOImgr.removeMarker(aaPOIMarkers[i][4]);
    aaPOImgr.removeMarker(aaPOIMarkers[i][5]);
    aaPOImgr.removeMarker(aaPOIMarkers[i][6]);
    
    delete aaPOIMarkers[i];
  }
}
  aaPOImgr.refresh();

}


function getPOITypeFromID(id) {

//NB - 118 and 130 are poi groups

  var strOut;
  switch(id) {
 
  case 100: 
	strOut='restaurant';
	break;
  case 102: 
	strOut='hotel';
	break;
	case 103: 
	strOut='hotel';
	break;
  case 104: 
	strOut='bandb';
	break;
  case 118: 
	strOut='hotel';
	break;
	case 130: 
	strOut='hotel';
	break;
  default:  
	strOut='';
  }
  return strOut;
}
function getPOIIDFromType(strPOIType) {

  var intOut='';
  switch(strPOIType) {
 
  case 'restaurant': 
	intOut=100;break;
  case 'hotel': 
	intOut=102;break;
  case 'bandb': 
	intOut=104;break;
  case 'accommodation': 
	intOut=118;break;
  default:intOut='';
  }
  return intOut;
}

