////////////////////////////////
// This is called when mouse clicks
////////////////////////////////
static void OnMouseClick( int button, int state, int x, int y )
{
   int temp; // temp card place 
   // !!!TODO!!!: Need mouse interaction?
   //             read the glut docs/manpage to find out how to query 
   //             which button was pressed...
   //             you may have to get this from the glut website 
   //             (use www.google.com to search for it)
   if (button==GLUT_LEFT_BUTTON && state == GLUT_DOWN){
      if(((temp=app.dek.isACard(mouseX, mouseY))!=-1) && (!app.dek.isSelected(temp))){
         app.dek.selectCard(temp);
         if(app.dek.numSel()==3){
            if(app.checkForWinner()){
               msleep(100);
               for(int i=0;i<12;i++){
                  if(app.dek.isSelected(i))
                     app.dek.deselectCard(i);
                  app.dek.setCardAttributeRandom(i);
               }
            }else{
               msleep(100);
               for(j=0;j<12;j++){
                  if(app.dek.isSelected(j))
                     app.dek.deselectCard(j);
                  app.dek.setCardAttributeRandom(j);
               }
            }
         }
      }
   }
}
//////////////////////////////////////////////////
// This is called on a glutPostRedisplay
//////////////////////////////////////////////////
static void OnRedisplay()
{ 
	glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
	glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
	glEnable( GL_DEPTH_TEST );
	glEnable( GL_BLEND );
	glEnable( GL_TEXTURE_2D);
 
	// set up the projection matrix
	glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        gluOrtho2D( 0,640,0,480 );
                           
	// initialize your matrix stack used for transforming your models
    glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();      

	// !!!TODO!!!: replace the following with your own opengl commands!



	count=0;
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
	glLoadIdentity();									// Reset The View
	app.dek.draw();
	app.drawMeter();
   app.drawScore();
   count=0;

   // update the last time
   lasttime = currenttime;
   currenttime = getTime();
   timechange = currenttime - lasttime;
   app.ltime = app.ltime + timechange;
   app.myMeter = DIFFI - app.ltime;

   if(app.ltime >= DIFFI){
	  for(int i=0;i<12;i++){
         if(app.dek.isSelected(i))
            app.dek.deselectCard(i);
            app.dek.setCardAttributeRandom(i);
         }
	  
      app.checkForWinner();
   }



   glutSwapBuffers();

}