OverlayItemInternal* OverlayView::getClickedItem(const WFAPI::ScreenPoint& p)
{
   StackedSet::StackVec& stackedItems = m_stackSet.getStackedItems();
   OverlayItemInternal* highlightedItem=NULL;

   //first check if there is a highlighted item visible   
   for( StackedSet::StackVec::reverse_iterator itr = stackedItems.rbegin();
        itr != stackedItems.rend(); itr++ )
   {
      OverlayItemInternal* curItem = *itr;
      if(curItem->m_isHighlighted) {
         highlightedItem=curItem;
         break;
      }
   }
   if(highlightedItem) {

      MC2Point screenPoint;
      m_projection.transform(screenPoint, highlightedItem->getPosition());
      WFAPI::wf_uint32 pixScale =
         static_cast<WFAPI::wf_uint32>(m_projection.getPixelScale());
      const WFAPI::OverlayItemVisualSpec* visualSpec = 
         OverlayItemUtil::getCurrentVisualSpec(highlightedItem, pixScale);

      const WFAPI::ImageSpec* background = visualSpec->getBackgroundImage();
   
      WFAPI::ScreenPoint offsetPoint = visualSpec->getFocusPoint();
   
      unsigned int width = background->getWidth();
      unsigned int height = background->getHeight();

      // Create our offset box
      PixelBox box( MC2Point(-offsetPoint.getX(), -offsetPoint.getY()),
                    MC2Point(-offsetPoint.getX() + width,
                             -offsetPoint.getY() + height));

      //Offset the box to the correct position on the screen
      box.move(screenPoint);

      //now check if we clicked the highlighted item
      if(box.pointInside(p)) {
         return highlightedItem;
      }
   }

   //if we got this far we clicked a non-highlighted item
   for( StackedSet::StackVec::reverse_iterator itr = stackedItems.rbegin();
        itr != stackedItems.rend(); itr++ )
   {
      OverlayItemInternal* curItem = *itr;
      //skip highlighted item
      if(curItem == highlightedItem) {
         continue;
      }

      PixelBox box = curItem->getPixelBox();
      //check if we clicked an item that is not the currently highlighted item
      if(box.pointInside(p)) {
         return curItem;
      }
   }

   return NULL;
}