// use wz_dragdrop.js
// the IDs of the cards must be card_xy, where x=suit(cdhs), y=rank(2...9tjqka)
// the possible cardplaces should be added this way: ADD_DHTML(placeID+NO_DRAG);
// the IDs of the places shouldn't begin with 'c'
// this module calls the cardPlaceChanged(place,card,userMove) function to indicate change of card positions
//    card!='' if dropped to place
//    card=='' if picked up from place
//    userMove true if the card was moved by the user
// these two functions can be called from outside:
// function defPosCard(c) moves the card to its initial position
// function moveCardTo(c,place) moves the card to the place of the item with id="place"

//################## card movement ##################

 function defPosCard(c)
 {
  var e=pickedCard(c);
  if (!e)
   return;
  e.moveTo(e.defx, e.defy);
 }

 function moveCardTo(c,place)
 {
  var e=pickedCard(c);
  if (!e)
   return;
  var e1=dd.elements[place];
  e.moveTo(e1.defx, e1.defy);
  cardPlaceChanged(place,c);
 }
 
 function pickedCard(c)
 {
  var e=dd.elements['card_'+c];
  if (!e)
   return undefined;
  e.maximizeZ();
  var dropTarget = e.getEltBelow();
  if (dropTarget != null)
   cardPlaceChanged(dropTarget.name,'');
  return e;
 }

//################## drag and drop ##################

 SET_DHTML(CURSOR_MOVE, SCROLL,
  'card_sa', 'card_ha', 'card_da', 'card_ca',
  'card_sk', 'card_hk', 'card_dk', 'card_ck',
  'card_sq', 'card_hq', 'card_dq', 'card_cq',
  'card_sj', 'card_hj', 'card_dj', 'card_cj',
  'card_st', 'card_ht', 'card_dt', 'card_ct',
  'card_s9', 'card_h9', 'card_d9', 'card_c9',
  'card_s8', 'card_h8', 'card_d8', 'card_c8',
  'card_s7', 'card_h7', 'card_d7', 'card_c7',
  'card_s6', 'card_h6', 'card_d6', 'card_c6',
  'card_s5', 'card_h5', 'card_d5', 'card_c5',
  'card_s4', 'card_h4', 'card_d4', 'card_c4',
  'card_s3', 'card_h3', 'card_d3', 'card_c3',
  'card_s2', 'card_h2', 'card_d2', 'card_c2'
 );

 function my_PickFunc()
 {
  var dropTarget = dd.obj.getEltBelow();
  if (dropTarget != null)
   cardPlaceChanged(dropTarget.name,'',1);
 }

 function my_DropFunc()
 {
  var dropTarget = dd.obj.getEltBelow();
  if (dropTarget != null && dropTarget.name.charAt(0)=='c')
  {
   dropTarget.moveTo(dropTarget.defx, dropTarget.defy);
   dropTarget = dd.obj.getEltBelow();
  }
  if (dropTarget != null && dropTarget.name.charAt(0)=='c')
   dropTarget = null;
  if (dropTarget == null || dropTarget.div.style.visibility=='hidden')
   dd.obj.moveTo(dd.obj.defx, dd.obj.defy);
  else
  {
   dd.obj.moveTo(dropTarget.defx, dropTarget.defy);
   cardPlaceChanged(dropTarget.name,dd.obj.name.substr(5),1);
  }
 }
