function move(value) {
	if(value > 0 && $('chooser').className != 'chooser')
     return false;
    
    var url = 'quartett.php';
    var myAjax = new Ajax.Request(
    url, {
        method: 'post',
        parameters: 'action=turnResult&row='+value, 
        onSuccess: showResult,
        onFailure: reportError
        }
    );
}


function nextTurn() {
	
    $('compcard').style.background = 'url(/img/quartett/card_blank.gif) no-repeat';
    
    var url = 'quartett.php';
    var myAjax = new Ajax.Request(
    url, {
        method: 'post',
        parameters: 'action=nextTurn', 
        onSuccess: showPair,
        onFailure: reportError
        }
    );
}


function startOver() {
    //resets
    $('overLayer').style.display = 'none';
    $('nextAction').style.display = 'inline';
    $('score').innerHTML = '0 : 0';
    var url = 'quartett.php';
    var myAjax = new Ajax.Request(
    url, {
        method: 'post',
        parameters: 'action=startOver', 
        onSuccess: showPair,
        onFailure: reportError
        }
    );
}


	

function showResult(response) {
	
    
    //disable moueseover hiliting
	$('chooser').className = '';
    disableList();
    responseObj = eval(response.responseText);
    
    //uncover cards
    $('chooser').style.background = 'url(/img/quartett/card_'+(responseObj.mycard)+'.gif) no-repeat';
    $('compcard').style.background = 'url(/img/quartett/card_'+(responseObj.compcard)+'.gif) no-repeat';

    //set link to nextTurn action
    $('nextAction').onclick = function(){nextTurn();};
    //set linktext
    if(responseObj.winner == 'me') {
        $('myProp_'+responseObj.row).className = 'rowActive_win';
        $('compProp_'+responseObj.row).className = 'rowActive_lost';
        $('nextAction').innerHTML = 'Du hast den Stich gewonnen!<br /><br />weiter &raquo;';
    }        
    else {
        $('myProp_'+responseObj.row).className = 'rowActive_lost';
        $('compProp_'+responseObj.row).className = 'rowActive_win';
        $('nextAction').innerHTML = 'Der Computer hat den Stich gewonnen<br /><br />weiter &raquo;';
    }        
    //set score
    $('score').innerHTML = responseObj.myscore+' : '+responseObj.compscore;
    
    //game is finished, show end results
    if(responseObj.totalwinner) {
        $('nextAction').style.display = 'none';
       
        //show finalscreen
        if(responseObj.totalwinner == 'me')
           $('overLayer').innerHTML = 'Gratuliere, du hast das Spiel gewonnen, nochmal spielen?<br /><br /><img src="/img/quartett_270x286_WIS.png" width="270" height="236" alt="" /><br /><a href="/nerdchandising.html">Jetzt kaufen</a>';
        if(responseObj.totalwinner == 'comp')
           $('overLayer').innerHTML = 'Du hast gegen den Computer verloren, noch ein Versuch starten?<br /><br /><img src="/img/quartett_270x286_WIS.png" width="270" height="236" alt="" /><br /><a href="/nerdchandising.html">Jetzt kaufen</a>';
        if(responseObj.totalwinner == 'remis')
           $('overLayer').innerHTML = 'Unentschieden, versuch es nocheinmal!<br /><br /><img src="/img/quartett_270x286_WIS.png" width="270" height="236" alt="" /><br /><a href="/nerdchandising.html">Jetzt kaufen</a>';
        $('overLayer').style.display = 'block';
        $('overLayer').onclick = function(){startOver()};
    }
}

function showPair(response) {
	responseObj = eval(response.responseText);
	
	//reset hiliting
	for(i=1;i<7;i++) {
	   $('myProp_'+i).className = '';
       $('compProp_'+i).className = '';
	}       
	if(responseObj.turns>0) {
        $('turns').innerHTML = responseObj.turns;
    	if(responseObj.cardType == 'comp') {
            //disable choosing
            $('chooser').className = '';
            disableList();
            //show computer's card
            $('compcard').style.background = 'url(/img/quartett/card_'+(responseObj.cardName)+'.gif) no-repeat';
            $('chooser').style.background = 'url(/img/quartett/card_blank.gif) no-repeat';
            //hilite random property
            $('compProp_'+responseObj.row).className = 'rowActive_choose';
            $('nextAction').onclick = function(){move(0)};
            $('nextAction').innerHTML = 'Decke Deine Karte auf!<br /><br />weiter &raquo;';
    	}            
        else {
            //enable choosing
            $('chooser').className = 'chooser';
            enableList();
            $('chooser').style.background = 'url(/img/quartett/card_'+(responseObj.cardName)+'.gif) no-repeat';
            $('compcard').style.background = 'url(/img/quartett/card_blank.gif) no-repeat';
            $('nextAction').onclick = function(){alert("Bitte wähle eine Eigenschaft!")};
            $('nextAction').innerHTML = 'Wähle eine Eigenschaft!<br /><br />weiter &raquo;';
        }            
    }        
}	


function reportError(response) {
    alert(response.responseText);
}

function enableList() {
    //ie6 crap
    if (document.all && document.getElementById) {
    	navRoot = $("chooseList");
	    
	    for (i = 0; i < navRoot.childNodes.length; i++)         {
	        node = navRoot.childNodes[i];
	        if ("LI" == node.nodeName) {
	        	node.onmouseover = function()  {
	        	    this.className = 'rowActive_choose';
	            }
	            
	            node.onmouseout = function() {
	                this.className = '';
	            }
	            
	        }
	    }
    }	    
}


function disableList() {
    //ie6 crap
    if (document.all && document.getElementById) {
    	navRoot = $("chooseList");
	    
	    for (i = 0; i < navRoot.childNodes.length; i++)         {
	        node = navRoot.childNodes[i];
	        if ("LI" == node.nodeName) {
	        	node.onmouseover = function()  {
	            }
	            
	            node.onmouseout = function() {
	            }
	            
	        }
	    }
    }	    
}
