
/********************************************************************
*       SOUND EFFECTS                                               *
********************************************************************/

   
// works in IE but not FF
var aySound = new Array();
        // Below: source for sound files to be preloaded
        aySound[0] = "newsbreak.wav";
        aySound[1] = "error.wav";
	aySound[2] = "applause-2.wav";
     
        document.write('<BGSOUND ID="auIEContainer">')
        IE = (navigator.appVersion.indexOf("MSIE")!=-1 && document.all)? 1:0;
        NS = (navigator.appName=="Netscape" && navigator.plugins["LiveAudio"])? 1:0;
        ver4 = IE||NS? 1:0;
        //onload=auPreload;
     
        function auPreload() {
        if (!ver4) return;
        if (NS) auEmb = new Layer(0,window);
        else {
        Str = "<DIV ID='auEmb' STYLE='position:absolute;'></DIV>";
        document.body.insertAdjacentHTML("BeforeEnd",Str);
        }
        var Str = '';
        for (i=0;i<aySound.length;i++)
        Str += "<EMBED SRC='"+aySound[i]+"' AUTOSTART='FALSE' HIDDEN='TRUE'>"
        if (IE) auEmb.innerHTML = Str;
        else {
        auEmb.document.open();
        auEmb.document.write(Str);
        auEmb.document.close();
        }
        auCon = IE? document.all.auIEContainer:auEmb;
        auCon.control = auCtrl;
        }
        function auCtrl(whSound,play) {
        if (IE) this.src = play? aySound[whSound]:'';
        else eval("this.document.embeds[whSound]." + (play? "play()":"stop()"))
        }
        function playSound(whSound) { if (window.auCon) auCon.control(whSound,true); }
        function stopSound(whSound) { if (window.auCon) auCon.control(whSound,false); }


// works in FF but not IE  
function playSoundFF(soundobj) {
  var thissound=document.getElementById(soundobj);  	
	thissound.Play();  
}

function determinePlaySound(result){
	if(result == 'match'){
		if(IE){
			playSoundIE(0);
		}
		else{
			playSoundFF('matchSound');
		}
	}
	if(result == 'noMatch'){
		if(IE){
			playSoundIE(1);
		}
		else{
			playSoundFF('noMatchSound');
		}
	}
	if(result == 'end'){
		if(IE){
			playSoundIE(2);
		}
		else{
			playSoundFF('endSound');
		}
	}
}

/********************************************************************
*       END SOUND EFFECTS                                           *
********************************************************************/

    var cardTotal = 0;
    var currentImg = "";
    var numSelectedCards = 0;
    var clicks = 0;
	var matches = 0;
	var step = 22;
	
	var cards = [];
     
    var folks = [
                    "letterA.gif",
		    "letterB.gif",
		    "letterC.gif",
		    "letterD.gif",
		    "letterE.gif",
		    "letterF.gif",
		    "letterG.gif",
		    "letterH.gif",
		    "letterI.gif",
		    "letterJ.gif",
		    "letterK.gif",
		    "letterL.gif",
		    "letterM.gif",
		    "letterN.gif",
		    "letterO.gif",
		    "letterP.gif",
		    "letterQ.gif",
		    "letterR.gif",
		    "letterS.gif",
		    "letterT.gif",
		    "letterU.gif",
		    "letterV.gif",
		    "letterW.gif",
		    "letterX.gif",
		    "letterY.gif",
		    "letterZ.gif"
                ];
 
// *********** Constructor fxn  for Card obj which sets all non-method params
    function Card(id,cssName,imgSrc,j){
        this.id = id;
        this.cssName = cssName;
        this.imgSrc = imgSrc;
        this.ele = j;  // NOT USED ? ? DELETE
        cardTotal ++;
    }

// *********** prototype method to attach method to Card object
    Card.prototype.build = function(){
        // create outer DIV element
        var cardDiv = document.createElement('DIV');
        cardDiv.id = this.id;
        cardDiv.imgSrc = this.imgSrc;
        cardDiv.className = this.cssName;
        // delete ? ? cardDiv.imgSrc = this.imgSrc;
        // assign event handler - not great this way b/c creates fxn each time...to-do: modifiy
        cardDiv.onclick = function (){if(flipcard(this))cardClickHandler(this)};
        // create inner IMG element
        var imgElement = document.createElement("IMG");
        imgElement.src = this.imgSrc;
        imgElement.width="109";
        imgElement.height="136";
        imgElement.style.visibility = "hidden";
        cardDiv.appendChild(imgElement);
		
		var added = cards.push(cardDiv);
		
        //  insert in <body>
       // var cont = document.getElementById("container");
        //cont.appendChild(cardDiv);
    }

    function flipcard(obj){ 
        // toggle picture
        if(obj.childNodes[0].style.visibility == 'hidden' && numSelectedCards <2){
            obj.childNodes[0].style.visibility = 'visible';
            clicks++;
            document.getElementById('score').innerHTML = "clicks: " + clicks;
            numSelectedCards++;
            return true;
        }
        else{     
            return false;
        }
    }

    function cardClickHandler(o){
        // check to see if match
        if(currentImg == ""){
            // first card flipped, set id to be matched with next card
            currentImg = o.imgSrc;
            selectedCrd1_id = o.id;
        }
        else{
            selectedCrd2_id = o.id;
            // one card already has been clicked and ID to match already exists
            if(o.imgSrc == currentImg){
                // add 'whooohoooo !' sound
                playSound(0);
		//determinePlaySound('match');
                currentImg = "";
                numSelectedCards = 0;
				matches ++;
				if(matches == cardTotal/2) endOfGame();
                // call to fxn to keep these cards flipped and disable clicking on them
            }
            else{
                // add 'sorry' sound
                playSound(1);
		//determinePlaySound('noMatch');
                currentImg = "";
                var delay = setTimeout("hideCards()", 1000);
            }
        } 
    }
 
    function hideCards(){
        document.getElementById(selectedCrd1_id).childNodes[0].style.visibility = "hidden";
        document.getElementById(selectedCrd2_id).childNodes[0].style.visibility = "hidden";
        numSelectedCards = 0;
    }

    function shuffle(passedArray){
        for (i = 1; i < passedArray.length; i++) {
        newnumber = (Math.random() * passedArray.length-1) + 1
        newnumber = parseInt(newnumber, 10)
        temp = passedArray[i]
        passedArray[i] = passedArray[newnumber]
        passedArray[newnumber] = temp
        }
    }
 
    function getHalf(){
    // use only half of the kids so game is easier
        folks = folks.slice(0,10);
    // create duplicate array so can add to array and have duplicate pix display so can have matches
    var folksMatches = folks.slice(0,10);
    folks = folks.concat(folksMatches);
    }
 
    function init(){
        shuffle(folks);
        getHalf();
        for(i=0;i<folks.length;i++){
            if(i<folks.length/2){
                curCard = new Card(folks[i],"basicCard",folks[i],i);
                curCard.build();
            }
            else{  // need to attach 'copy' to copies of images so fxn 'hideCards()' has unique id to hide
                curCard = new Card("COPY_" + folks[i],"basicCard",folks[i],i);
                curCard.build();
            }
        }
		shuffle(cards); 
		
		//  insert in <body>
       		var cont = document.getElementById("container");
			for(i=0;i<cards.length;i++){
				cont.appendChild(cards[i]);
			}
        //cont.appendChild(cardDiv);
    }

	function endOfGame(){
		playSound(2);
		//determinePlaySound('end');
		step += 2;
		document.getElementById('play').style.visibility = 'visible';
		var goodJob = document.getElementById('goodJob');
		goodJob.style.visibility = "visible";
		var gdSize = goodJob.style.fontSize.substring(0,goodJob.style.fontSize.indexOf('p'));
		//alert(gdSize);
		if(gdSize < 78){
			goodJob.style.fontSize = step + "px";
			window.setTimeout("endOfGame()",5);					
		}				
	} 
