// JavaScript Document


    var http_request = false;
	var http_request_results = false;
	var url;
	var urlserver = "http://delicategeniusblog.com/door_puzzle_caller.php"
//	var urlserver = "http://localhost/wordpress/door_puzzle_caller.php";
	var timer;
//	mTimer = setTimeout("GetResults();",10000);

    function SendUpdate(winner,swap) {

       http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
			document.getElementById("results_panel").innerHTML = "Get a real browser punk!";
            return false;
        }
        url=urlserver+"?win=" + winner + "&swap=" + swap + "&showresults=no&dud=" + new Date().getTime();
		http_request.onreadystatechange = alertContents;
		
        http_request.open('GET', url, true);
        http_request.send(null);
		GetResults();

    }

    function alertContents() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
//					document.getElementById("results_panel").innerHTML = http_request.responseText;
				
            }
			else {
				document.getElementById("results_panel").innerHTML = "There was a problem with getting the results request";
            }
        }

    }
	
	
function GetResults() {

       http_request_results = false;
//		window.alert("gotcha");
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request_results = new XMLHttpRequest();
            if (http_request_results.overrideMimeType) {
                http_request_results.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request_results = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request_results = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request_results) {
			document.getElementById("results_panel").innerHTML = "Get a real browser punk!";
            return false;
        }
        url=urlserver + "?showresults=yes&dud=" + new Date().getTime();
		http_request_results.onreadystatechange = alertContents_Results;
		
        http_request_results.open('GET', url, true);
        http_request_results.send(null);

    }

    function alertContents_Results() {
        if (http_request_results.readyState == 4) {
            if (http_request_results.status == 200) {
					document.getElementById("results_panel").innerHTML = http_request_results.responseText;
            }
			else {
				document.getElementById("results_panel").innerHTML = "There was a problem with getting the results request";
            }
        }

    }
// display stat off the bat
GetResults();


var clickcount = 0;
var doorclicked = 0;
var doortoopen = 0;
var doorclicked_changedto = "none";
var doorwithprize = Math.floor(Math.random()*3) + 1;
var swapped = "no";
document.getElementById("door1").style.cursor = "pointer";
document.getElementById("door1").style.backgroundImage = 'url(wp-content/images/door_puzzle/background.jpg)';
document.getElementById("door2").style.backgroundImage = 'url(wp-content/images/door_puzzle/background.jpg)';
document.getElementById("door3").style.backgroundImage = 'url(wp-content/images/door_puzzle/background.jpg)';



function changeDivText(divid,divtext)
{
	fadetextin();
    if (document.layers) document.layers[divid].innerHTML=divtext;
   else document.getElementById(divid).innerHTML=divtext;
}

function doorClickEvent(door){
	if (clickcount==0){
		doorClick(door);
	}
}

function doorClick(door){
	// step 1 - original door selected
	clickcount++;
	if (clickcount == 1){
		changeDivText('actions','Now that you have chosen a door, I am going to remove a door without the prize.<br /><br /><input type="button" onclick="doorClick(this);" value="next" />');
		doorclicked = door.id;
		document.getElementById(door.id).style.borderColor = '#999999';
		document.getElementById("door1").style.cursor = 'default';
		document.getElementById("door2").style.cursor = 'default';
		document.getElementById("door3").style.cursor = 'default';

	}
	// step 2 - determine door to reveal
	if (clickcount == 2){
		if (doorclicked == "door1"){
			if (doorwithprize == 2) {
			doortoopen = 3;
			}
			else{
			doortoopen = 2;
			}
			document.getElementById("door"+doortoopen).style.backgroundImage = 'url(wp-content/images/door_puzzle/background_noprize.jpg)';
		}
		if (doorclicked == "door2"){
			if (doorwithprize == 1) {
			doortoopen = 3;
			}
			else{
			doortoopen = 1;
			}
			document.getElementById("door"+doortoopen).style.backgroundImage = 'url(wp-content/images/door_puzzle/background_noprize.jpg)';
		}
		
		if (doorclicked== "door3"){
			if (doorwithprize == 1) {
			doortoopen = 2;
			}
			else{
			doortoopen = 1;
			}
		document.getElementById("door"+doortoopen).style.backgroundImage = 'url(wp-content/images/door_puzzle/background_noprize.jpg)';
		}

	changeDivText('actions','Now that I have revealed a door without the prize, do you wish to change?<br /><br /><input type="button" id="next_yes" onclick="doorClick(this);" value="yes" /> or <input type="button" id="next_no" onclick="doorClick(this);" value="no" />');
	}
	// step 3 - offer change
	if (clickcount == 3){
		if (door.id == "next_yes"){
			changeDivText('actions','You have chosen <strong>to change</strong> your selection, let\'s see if it worked to your advantage<br /><br /><input type="button" id="next" onclick="doorClick(this);" value="next" />');
			//doorclicked = original door clicked on
			//doortoopen = door revealed

			if (doorclicked=="door1"){
				if (doortoopen ==2){
				doorclicked_changedto="door3";
				}
				else{
				doorclicked_changedto="door2";
				}
			}
			if (doorclicked=="door2"){
				if (doortoopen ==1){
				doorclicked_changedto="door3";
				}
				else{
				doorclicked_changedto="door1";
				}
			}
			
			if (doorclicked=="door3"){
				if (doortoopen ==1){
				doorclicked_changedto="door2";
				}
				else{
				doorclicked_changedto="door1";
				}
			}

			document.getElementById(doorclicked_changedto).style.borderColor = '#999999';
			document.getElementById(doorclicked).style.borderColor = '#FFFFFF';
			swapped = "yes";
			
		}
		if (door.id == "next_no"){
			changeDivText('actions','You have chosen <strong>not to change</strong> your selection, let\'s see if it worked to your advantage<br /><br /><input type="button" id="next" onclick="doorClick(this);" value="next" />');
			swapped = "no";
		}
	}
	// step 2 - reveal result
	if (clickcount == 4){
		// show door with prize
		document.getElementById("door"+doorwithprize).style.backgroundImage = 'url(wp-content/images/door_puzzle/background_prize.jpg)';
		
		if (swapped == "yes"){
			if (doorclicked_changedto == "door"+doorwithprize){
				SendUpdate("yes","yes");
				changeDivText('actions','You <strong>swapped</strong> and <strong>won</strong> the prize.<br /><br /><input type="button" id="play_again" onclick="doorClick(this);" value="play again" />');
				document.getElementById(doorclicked).style.backgroundImage = 'url(wp-content/images/door_puzzle/background_noprize.jpg)';
			}
			else{
				SendUpdate("no","yes");
				document.getElementById(doorclicked_changedto).style.backgroundImage = 'url(wp-content/images/door_puzzle/background_noprize.jpg)';
				changeDivText('actions','You <strong>swapped</strong> and <strong>lost</strong> the prize.<br /><br /><input type="button" id="play_again" onclick="doorClick(this);" value="play again" />');
			}
		}
		if (swapped == "no"){
			if (doorclicked == "door"+doorwithprize){
				SendUpdate("yes","no");
				changeDivText('actions','You <strong>did not swap</strong> and <strong>won</strong> the prize.<br /><br /><input type="button" id="play_again" onclick="doorClick(this);" value="play again" />');
				if (document.getElementById("door1").style.backgroundImage == 'url(wp-content/images/door_puzzle/background.jpg)'){
					document.getElementById("door1").style.backgroundImage = 'url(wp-content/images/door_puzzle/background_noprize.jpg)';
				}
				if (document.getElementById("door2").style.backgroundImage == 'url(wp-content/images/door_puzzle/background.jpg)'){
					document.getElementById("door2").style.backgroundImage = 'url(wp-content/images/door_puzzle/background_noprize.jpg)';
				}
				if (document.getElementById("door3").style.backgroundImage == 'url(wp-content/images/door_puzzle/background.jpg)'){
					document.getElementById("door3").style.backgroundImage = 'url(wp-content/images/door_puzzle/background_noprize.jpg)';
				}

			}
			else{
				SendUpdate("no","no");
				document.getElementById(doorclicked).style.backgroundImage = 'url(wp-content/images/door_puzzle/background_noprize.jpg)';
				changeDivText('actions','You <strong>did not swap</strong> and <strong>lost<strong> the prize.<br /><br /><input type="button" id="play_again" onclick="doorClick(this);" value="play again" />');
				if (document.getElementById("door1").style.backgroundImage == 'url(wp-content/images/door_puzzle/background.jpg)'){
					document.getElementById("door1").style.backgroundImage = 'url(wp-content/images/door_puzzle/background_noprize.jpg)';
				}
				if (document.getElementById("door2").style.backgroundImage == 'url(wp-content/images/door_puzzle/background.jpg)'){
					document.getElementById("door2").style.backgroundImage = 'url(wp-content/images/door_puzzle/background_noprize.jpg)';
				}
				if (document.getElementById("door3").style.backgroundImage == 'url(wp-content/images/door_puzzle/background.jpg)'){
					document.getElementById("door3").style.backgroundImage = 'url(wp-content/images/door_puzzle/background_noprize.jpg)';
				}
			}
		}
		
	}
	if (clickcount == 5){
		// reset everything
		clickcount = 0;
		doorclicked = 0;
		doortoopen = 0;
		doorclicked_changedto = "none";
		doorwithprize = Math.floor(Math.random()*3) + 1;
		swapped = "no";
		document.getElementById("door1").style.borderColor = '#FFFFFF';
		document.getElementById("door2").style.borderColor = '#FFFFFF';
		document.getElementById("door3").style.borderColor = '#FFFFFF';
		document.getElementById("door1").style.backgroundImage = 'url(wp-content/images/door_puzzle/background.jpg)';
		document.getElementById("door2").style.backgroundImage = 'url(wp-content/images/door_puzzle/background.jpg)';
		document.getElementById("door3").style.backgroundImage = 'url(wp-content/images/door_puzzle/background.jpg)';
		document.getElementById("door1").style.cursor = 'pointer';
		document.getElementById("door2").style.cursor = 'pointer';
		document.getElementById("door3").style.cursor = 'pointer';


		changeDivText('actions','click on the door of your choice.');
	}	

}


hex=255 // Initial color value.

function fadetextin()
{ 
	if(hex>0) { //If color is not black yet
		hex-=11; // increase color darkness
		document.getElementById("actions").style.color="rgb("+hex+","+hex+","+hex+")";
		setTimeout("fadetextin()",20); 
	}
	else{
	hex=255 //reset hex value
	}
}
