<?xml version="1.0"?>
<bindings xmlns="http://www.mozilla.org/xbl" 
          xmlns:xbl="http://www.mozilla.org/xbl" 
          xmlns:html="http://www.w3.org/1999/xhtml" 
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<binding id="memo_stack">
  <implementation>
    <field name="initialisedCard">0</field>
    
    <method name="smile">
      <body>
        this.childNodes[1].setAttribute("hidden","false");
      </body>
    </method>

    <method name="clone">
      <parameter name="node"/>
      <body>
        copy = node.cloneNode(true);
        copy.cloned = true;
        this.insertBefore(copy, this.childNodes[2]);
        copy.setpos();
      </body>
    </method>
  </implementation>
</binding>

<binding id="memo_cell">
  <content>
     <xul:image maxwidth="100" maxheight="100" top="10" left="10" src="images/backcard.png"/>
     <children/>
  </content>
  <implementation>
    <field name="currentState">0</field>
    <field name="cloned">false</field>

    <constructor><![CDATA[
	/* Is the construtor called on cloned node ???? */
        /* when a node is cloned it has no parent */
        /* Hummmm. ???? */
        if (!this.cloned){
          // direct clone returns recursion error
          this.parentNode.clone(this);
 	}

	this.setpos();
    ]]></constructor>

    <method name="setpos">
      <body>
        <![CDATA[

       card = this.parentNode.initialisedCard;

       n_col = card % columns;
       n_line = (card - n_col) / columns;

       this.parentNode.initialisedCard++;

       //apparently elements are initialised in reverse order
       index = this.parentNode.childNodes.length -1 - Math.floor(Math.random()*(card+1));

       dest = this.parentNode.childNodes[index];

       // this case covers dest == this.
       if (!dest.hasAttribute("top")){
	  this.setAttribute("left", n_col*this.width);
	  this.setAttribute("top", n_line*this.height);
          return;
	}

	this.setAttribute("top",dest.getAttribute("top"));
	this.setAttribute("left",dest.getAttribute("left"));

	dest.setAttribute("left", n_col*this.width);
	dest.setAttribute("top", n_line*this.height);

        ]]>
      </body>
    </method>

    <method name="flip">
      <parameter name="state"/>
      <body>
        <![CDATA[
          if (this.currentState == 2)
	     return;

	  if (state !=2){
	     this.setAttribute("selectedIndex",state);
	     this.currentState = state;
	  } else
             this.parentNode.removeChild(this);

        ]]>
      </body>
    </method>

    <method name="cardClicked">
      <body>
	if (delai != false)
           return;
	
	if (this.currentState == 1){
	   return;
	   }

	if (!firstcard){
	   firstcard=this;
           this.flip(1);
           return;
	   }
        else {
   	   secondcard=this;
           this.flip(1);
           delai = setTimeout("checkWin()",2000);
	}

      </body>
    </method>


  </implementation>
  <handlers>
  <handler event="click" button="0" action="this.cardClicked();"/>
  </handlers>
</binding>

</bindings>