

/************************/
/****CONCH GAME LOGIC****/
/************************/

var imgs=new Array();
imgs[0]='/newcastlecollection/images/10.jpg';
imgs[1]='/newcastlecollection/images/1.jpg';
imgs[2]='/newcastlecollection/images/3.jpg';
imgs[3]='/newcastlecollection/images/9.jpg';
imgs[4]='/newcastlecollection/images/4.jpg';
imgs[5]='/newcastlecollection/images/1.jpg';
imgs[6]='/newcastlecollection/images/2.jpg';
imgs[7]='/newcastlecollection/images/9.jpg';
imgs[8]='/newcastlecollection/images/5.jpg';
imgs[9]='/newcastlecollection/images/8.jpg';
imgs[10]='/newcastlecollection/images/6.jpg';
imgs[11]='/newcastlecollection/images/7.jpg';
imgs[12]='/newcastlecollection/images/6.jpg';
imgs[13]='/newcastlecollection/images/8.jpg';
imgs[14]='/newcastlecollection/images/7.jpg';
imgs[15]='/newcastlecollection/images/5.jpg';
imgs[16]='/newcastlecollection/images/4.jpg';
imgs[17]='/newcastlecollection/images/3.jpg';
imgs[18]='/newcastlecollection/images/10.jpg';
imgs[19]='/newcastlecollection/images/2.jpg';
imgs.sort( function() {return 0.1 - Math.random()} );

var completedIDs=new Array();

var clickCount = 0;
var clickedIDs = [];
var matchCount = 0;
var started = false;
var startTimeMilli = 0;
var restartLink = "<a href='#' onclick='resetGame()'> Reset</a>";
var attemptCount = 0;



function selectObject(id){

	if (clickCount < 2) {
		
		if (started == false) { 
			var startTime = new Date();
			startTimeMilli = startTime.getTime(); started = true;
		}
		
		if (id == clickedIDs[0]) { return false;}
		if (completedIDs.inArray(id)){ return false;}
		new Effect.Shake(id, {distance:3});
		clickedIDs[clickCount] = id;
		
		clickCount = clickCount + 1;
		$(id).src = imgs[id-1];
		
		if (clickCount == 2) {
			attemptCount = attemptCount + 1;
			
			if (checkMatch()) {
				clickCount = 0;
				clickedIDs = [];
			} else {
				setTimeout('resetImages()',1000);
			}
		}
		$('matchCount').innerHTML = 'You have matched <strong>'+matchCount +'</strong> from <strong>'+ attemptCount +'</strong> attempts. ';
	}
		
}

function resetGame(){
	clickCount = 0;
	
	matchCount = 0;
	started = false;
	startTimeMilli = 0;
	attemptCount = 0;
	
	$('1').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('1', {distance:3})",100);
	$('2').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('2', {distance:3})",200);
	$('3').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('3', {distance:3})",300);
	$('4').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('4', {distance:3})",400);
	$('5').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('5', {distance:3})",500);
	$('6').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('6', {distance:3})",600);
	$('7').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('7', {distance:3})",700);
	$('8').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('8', {distance:3})",800);
	$('9').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('9', {distance:3})",900);
	$('10').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('10', {distance:3})",1000);
	$('11').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('11', {distance:3})",1100);
	$('12').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('12', {distance:3})",1200);
	$('13').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('13', {distance:3})",1300);
	$('14').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('14', {distance:3})",1400);
	$('15').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('15', {distance:3})",1500);
	$('16').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('16', {distance:3})",1600);
	$('17').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('17', {distance:3})",1700);
	$('18').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('18', {distance:3})",1800);
	$('19').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('19', {distance:3})",1900);
	$('20').src = "/newcastlecollection/images/holder.jpg" ; setTimeout("new Effect.Shake('20', {distance:3})",2000);
	clickedIDs = [];
	
	$('matchCount').innerHTML = "<strong>Are you ready???</strong>";
	$('completionText').innerHTML = "";
}

function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }
 
    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}

function resetImages(){
	clickCount = 0;
	
	$(clickedIDs[0]).src = "/newcastlecollection/images/holder.jpg" ;
	$(clickedIDs[1]).src = "/newcastlecollection/images/holder.jpg" ;

	new Effect.Shake(clickedIDs[0], {distance:3});
	new Effect.Shake(clickedIDs[1], {distance:3});
	clickedIDs = [];
}

function checkMatch(){
	if ( $(clickedIDs[0]).src == $(clickedIDs[1]).src ) {
		var sound = $RF('conch', 'sound');
		if(sound=='on'){ Sound.play('http://www.newcastle.gov.uk/newcastlecollection/sounds/applause3.mp3',{replace:true});}
		new Effect.Pulsate(clickedIDs[0]);
		new Effect.Pulsate(clickedIDs[1]);
		completedIDs[completedIDs.length] = clickedIDs[0];
		completedIDs[completedIDs.length] = clickedIDs[1];
	
		matchCount = matchCount + 1;
		
		if (matchCount == (imgs.length/2)) {
			var endTime = new Date();
			endTimeMilli = endTime.getTime();
			var timeTaken = Math.round( ((endTimeMilli - startTimeMilli)/1000) *100)/100 ;
			var newGameHTML = "<a href='#game'onclick='resetGame()'>New Game?</a>";
			if (timeTaken<35.00) {
				Sound.play('http://www.newcastle.gov.uk/newcastlecollection/sounds/applause5.mp3',{replace:true});
				$('completionText').innerHTML = "<p><strong>SCREAMER!!!</strong> You is 'da bomb' Are you cheating? You did it in <strong>"+timeTaken+"</strong> seconds! "+newGameHTML+"</p>";
			} else if (timeTaken>= 35.00 && timeTaken<= 45.00){
				Sound.play('http://www.newcastle.gov.uk/newcastlecollection/sounds/applause5.mp3',{replace:true});
				$('completionText').innerHTML = "<p><strong>WOW!<strong> You are great! You did it in <strong>"+timeTaken+"</strong> seconds! "+newGameHTML+"</p>";
			} else if (timeTaken>45.00 && timeTaken<=55.00){
				Sound.play('http://www.newcastle.gov.uk/newcastlecollection/sounds/applause5.mp3',{replace:true});
				$('completionText').innerHTML = "<p><strong>Pretty Good...</strong>  You can do better! You did it in <strong>"+timeTaken+"</strong> seconds! "+newGameHTML+"</p>";
			} else if (timeTaken>55.00 && timeTaken<=65.00){
				Sound.play('http://www.newcastle.gov.uk/newcastlecollection/sounds/applause5.mp3',{replace:true});
				$('completionText').innerHTML = "<p><strong>Thats about average...</strong> You did it in <strong>"+timeTaken+"</strong> seconds! "+newGameHTML+"</p>";
			} else if (timeTaken>65.00 && timeTaken<=75.00){
				Sound.play('http://www.newcastle.gov.uk/newcastlecollection/sounds/applause5.mp3',{replace:true});
				$('completionText').innerHTML = "<p><strong>A little but slow...</strong> You did it in <strong>"+timeTaken+"</strong> seconds! "+newGameHTML+"</p>";
			} else if (timeTaken>75.00 && timeTaken<=90.00){
				Sound.play('http://www.newcastle.gov.uk/newcastlecollection/sounds/boo.mp3',{replace:true});
				$('completionText').innerHTML = "<p><strong>I'm getting embarrassed for you...</strong> You did it in <strong>"+timeTaken+"</strong> seconds! "+newGameHTML+"</p>";
			} else if (timeTaken > 90.00 ){
				Sound.play('http://www.newcastle.gov.uk/newcastlecollection/sounds/boo.mp3',{replace:true});
				$('completionText').innerHTML = "<p><strong>Did you give up?</strong> You did it in <strong>"+timeTaken+"</strong> seconds! "+newGameHTML+"</p>";
			}
			/*
			var url = '/collections.nsf/AJAX_qbt?openagent';
			var params = '&t=' + timeTaken;
			var ajax = new Ajax.Request(url, {
				parameters: params,
				method: 'get',
				onSuccess: function(xm){
					xmr = xm.responseXML;
					tposition = xmr.getElementsByTagName("position")[0].firstChild.nodeValue;
					if(tposition != "NULL"){
						
					}
				}
			});
			$('hallOfFame').innerHTML.
			*/
		}
		return true;
	}else {
		return false;
	}	
}

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};



