// JavaScript Document
window['auto'] = true;
window['manual'] = false;
window['timeout'] = 1000;
window['gtimeout'] = 1000;
function dubbleletterchallenge(e) {
	var key;
	var isCtrl;
	var d = el('dubbleletters');
	if (window.event){
		k = window.event.keyCode;
		if(window.event.ctrlKey){
			isCtrl = true;
		}else{
			isCtrl = false;
		}
	} else if (e){
		k = e.which;
		if(e.ctrlKey){
			isCtrl = true;
		}else{
			isCtrl = false;
		}
	}
	if(isCtrl&&k==86){
		return false;
		void(0);
	}else{
		if(k == 13){
			s = d.value;
			d.value = s.replace(/\r\n|\r|\n/g,' ');
		}
		c = el('dubbleletters').value.split(" ");
		if(c.length <= 1000){
			window['auto'] = true;
			window['manual'] = false;
		}
		if(window['auto'] == true){
			var k;
			var d = el('dubbleletters');
			if (window.event){
				k = window.event.keyCode;
			} else if (e){
				k = e.which;
			}
			if(k == 13){
				s = d.value;
				d.value = s.replace(/\r\n|\r|\n/g,' ');
			}
			if(k == 13 || k == 32 || k == 46 || k == 8){
				if(window['loggedIn'] == true){
					if(window['utime']){
						window.clearTimeout(window['utime']);
					}
					doUpdate();
				} else {
					if(window['gtime']){
						window.clearTimeout(window['gtime']);
					}
					doGUpdate();
				}
			} else {
				if(window['loggedIn'] == true){
					if(window['utime']){
						window.clearTimeout(window['utime']);
					}
					window['utime'] = window.setTimeout("doUpdate()",window['timeout']);
				} else {
					if(window['gtime']){
						window.clearTimeout(window['gtime']);
					}
					window['gtime'] = window.setTimeout("doGUpdate()",window['gtimeout']);
				}
			}
		}
	}
}
function mandubbleletter(){
	$('.congrat').slideUp("fast");
	if(window['loggedIn'] == true){
		doUpdate();
	} else {
		doGUpdate();
	}
}
doUpdate = function(){
	$.post('../_rsc/game/updScore'+window['user50']+'.php', {e: el('dubbleletters').value}, function(data){
		 el('playHeader').innerHTML = data;
         d = data.split(": ");
   	     $("span.score").html(d[1]);
				 if(parseInt(d[1]) > 1000){
				 	window['manual'] = true;
					window['auto'] = false;
					$('#manupd').slideDown("fast");
				 } else {
				 	window['manual'] = false;
					window['auto'] = true;
					$('#manupd').slideUp("fast");
				 }
         fixPanel();
	 });
}
doGUpdate = function(){
	$.post('../_rsc/game/updGuest'+window['guest50']+'.php', {e: el('dubbleletters').value}, function(data){
		 el('playHeader').innerHTML = data;
         d = data.split(": ");
   	     $("span.score").html(d[1]);
				 if(parseInt(d[1]) > 1000){
				 	window['manual'] = true;
					window['auto'] = false;
					$('#manupd').slideDown("fast");
				 } else {
				 	window['manual'] = false;
					window['auto'] = true;
					$('#manupd').slideUp("fast");
				 }
         fixPanel();
	 });
}
function beginchallenge(){
	$('.challenge h3:last').animate({'opacity':'0'},300,function(){
		$('#dubbleletters').val('').css({'opacity':'0','display':'block'}).attr('disabled','disabled').animate({'opacity':'1'},3000);
		$('.challenge h3:last').replaceWith('<h3 align="center">your cha<span class="dubble">ll</span>enge will begin in <span class="timer">3</span> seconds</h3>');
		$('.timer').animate({'opacity':'0'},1000,function(){
			$('.timer').html('2').css('opacity','1').animate({'opacity':'0'},1000,function(){
				$('.timer').html('1').css('opacity','1').animate({'opacity':'0'},1000,function(){
					$('.challenge h3:last').html('<span class="timer">GO!</span>').css('opacity','1').animate({'opacity':'0'},1000,function(){;
						startTimer();
					});
					$('#dubbleletters').removeAttr('disabled').focus();
				});
			});
		});
	});
};
function startTimer(){
	$('.challenge h3:last').attr('align','left').html('<span class="score">0</span> double le<span class="dubble">tt</span>ered words<span style="float:right;">seconds remaining: <span class="timer">59</span></span>').css('opacity','1');
	$('.timer').animate({'opacity':'1'},1000,function(){;
		doTimer();
	});
};
function doTimer(){
	$('.timer').html((parseInt($('.timer').html())-1)+'').css('opacity','1').animate({'opacity':'1'},1000,function(){
		if($('.timer').html()>0){
			doTimer();
		}else{
			score();
		}
	});
};
function score(){
	var words=$('#dubbleletters').val();
	$('#dubbleletters').attr('disabled','disabled');
	$('#dubbleletters').replaceWith('<div id="dubbleletters" style="display:block;"></div>');
	if(parseInt($('span.score').html())<parseInt($('.results td:last').html()&&$('.results tr').length>=10)){
		d='<h3 align="center">your final score was <span class="score">'+$('span.score').html()+'</span></h3><p align="center">not quite high enough to make it onto our top ten leaderboard - why not <a onclick="tryagain();">try again<a>?</p>';
	}else{
		d='<h3 align="center">congratulations!</h3><p align="center">you made it into our top 10 leaderboard - please enter your name:</p><p align="center"><input type="text" /><br /><input type="button" class="button" value="save!" onclick="savescore();" /></p>';
	}
	$('#dubbleletters').html(d);
};
function savescore(){
	$.post('../_rsc/challenge_score.php',{a:$('span.score').html(),b:$('#dubbleletters input[type="text"]').val()},function(data){
		$('#dubbleletters').html('<h3 align="center">su<span class="dubble">cc</span>e<span class="dubble">ss</span>fu<span class="dubble">ll</span>y saved</h3><p align="center"><a onclick="tryagain();">play again</a></p>');
	},'html');
};
function tryagain(){
	$('#dubbleletters').animate({'opacity':'0'},300,function(){
		$('#dubbleletters').replaceWith('<textarea id="dubbleletters" name="dubbleletters" onkeyup="return dubbleletterchallenge(event)" onkeydown="return dubbleletterchallenge(event)" disabled="disabled"></textarea>');
		$('#dubbleletters').val('').css({'opacity':'0','display':'block'}).attr('disabled','disabled').animate({'opacity':'1'},3000);
		$('.challenge h3:last').replaceWith('<h3 align="center">your cha<span class="dubble">ll</span>enge will begin in <span class="timer">3</span> seconds</h3>');
		$('.timer').animate({'opacity':'0'},1000,function(){
			$('.timer').html('2').css('opacity','1').animate({'opacity':'0'},1000,function(){
				$('.timer').html('1').css('opacity','1').animate({'opacity':'0'},1000,function(){
					$('.challenge h3:last').html('<span class="timer">GO!</span>').css('opacity','1').animate({'opacity':'0'},1000,function(){;
						startTimer();
					});
					$('#dubbleletters').removeAttr('disabled').focus();
				});
			});
		});
	});
};