/* Called when the control is Shown*/
void CColosseumCtrl::OnShowWindow(BOOL bShow, UINT nStatus)
{
	/* This method is called the instance the control is show to the user
	 * What happens here is that we get the file name from m_server and convert it from
	 * CString to char* and send it to the CIFCEngineInteract object to retrieve information 
	 * from the file and initialize the CIFCEngineInteract object.
	 * We then initialize the m_width and m_height with the width and height of the control
	 * sent with <OBJECT> tag in the html file.
	 * After that we assign to m_hwndRenderWindow the handle of the window that we are going to draw
	 * in because we are going to use to initialize the DirectX device module and initialize the device buffer .
	 * Lastly we render the changes.
	 */
	
	/*if ( 0 == m_engineInteract->retrieveObjectGroups("C:\\temp1.ifc"))//(m_server.GetBuffer(0))))
		m_engineInteract->enrichObjectGroups();
	else	
		ASSERT(1==0);*/
	
	CObjectTransferer::getSingleton().setCtrl(this);
	CObjectTransferer::getSingleton().setEndpoint(m_server.GetBuffer(0));
	

	CRect rc;
	GetWindowRect(&rc);
	m_width = rc.Width();
	m_height = rc.Height();
	m_hwndRenderWindow = this->m_hWnd;
	initializeDevice();
	initializeDeviceBuffer();
	
	
	
	initializeGUI();
	m_objectVector.push_back(IFC_WINDOW);
	m_objectVector.push_back(IFC_DOOR);
	if(m_initialized)
			render();		
	CObjectTransfererThread *t = new CObjectTransfererThread(m_endpointModelVector);
	t->CreateThread();
				
}
LRESULT CColosseumCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	int		iMouseX = LOWORD(lParam),
			iMouseY = HIWORD(lParam);

	switch (message)
	{
		case WM_LBUTTONDOWN:
		case WM_MBUTTONDOWN:
		case WM_RBUTTONDOWN:
			iZoomMouseX = iMouseX;
			iZoomMouseY = iMouseY;
			break;
		case WM_MOUSEMOVE:
			/*
			 *	Mouse moved
			 */
			
			if(MK_LBUTTON&wParam)
			{
				
				SetCapture();
				SetCursor(NULL);
				
				m_camera->rotateCamera((float)(iMouseY-iZoomMouseY), (float)(iMouseX-iZoomMouseX)); 

				if (m_initialized) {
					render();
				}
				ReleaseCapture();
				iZoomMouseX = iMouseX;
				iZoomMouseY = iMouseY;
			}
			
			break;
	
		case WM_KEYDOWN: //If a Key is down
			switch(wParam)
			{
			case 0x57: //W KEY
				
				m_camera->moveForward(MULTIPLY_RATIO);
								
				if  (m_initialized) {
					render();
				}
				break;
			case 0x53: // S KEY
				m_camera->moveForward(-1.0f * MULTIPLY_RATIO);
				if  (m_initialized) {
					render();
				}
				break;
			case 0x41:// A KEY
				m_camera->moveRight(-1.0f * MULTIPLY_RATIO);
				if(m_initialized)
					render();
				break;
			
			case 0x44: // D KEY
				m_camera->moveRight( MULTIPLY_RATIO );
				if(m_initialized)
					render();
				break;
			}
			break;

		case WM_SHOWWINDOW:
			switch	(m_engineInteract->retrieveObjectGroups((m_server.GetBuffer(0)))) {
			case 0:
				m_engineInteract->enrichObjectGroups();
				break;
			default:
				ASSERT(1==0);
				break;
			}
			CRect rc;
			this->GetWindowRect(&rc);
			m_width = rc.Width();
			m_height = rc.Height();

			m_hwndRenderWindow = this->m_hWnd;
			initializeDevice();
			initializeDeviceBuffer();
			
			
			break;
	}
	if(m_initialized)
			render();
	return COleControl::WindowProc(message, wParam, lParam);
	
}