var KEYENUM = {a:97,b:98,c:99,d:100,e:101,f:102,g:103,h:104,i:105,j:106,k:107,l:108,m:109,n:110,o:111,p:112,q:113,r:114,s:115,t:116,u:117,v:118,w:119,x:120,y:121,z:122,ENTER:13};
var currEntryString = "";
var cfcPath = "";
var sharedPath = "";

$(document).ready(function() {
	//Document is loaded, grab the CFC path
	cfcPath = $('div#tpFoot').attr('rel').split('-')[0];
	sharedPath = $('div#tpFoot').attr('rel').split('-')[1];

	//Binds the keypress, or keydown function to the document
	$(document).bind(($.browser.opera ? "keypress" : "keydown"), function(event) {
		//If we hit enter, we want to check the current string. Otherwise append to it.
		if(event.keyCode == KEYENUM.ENTER) {
			//If they type debugOff, empty the debugger out
			if(currEntryString.toLowerCase() == 'debugoff')
				$('div#tpFoot').html('');
			else {
				//Check and see if the entered string is the string necessary to open the debugger
				$.post(cfcPath + 'debugger.cfc?method=validateEntryString', {currString: currEntryString}, function(data) {
					if(data.indexOf('true') != -1)
						$('div#tpFoot').load(sharedPath + 'debugger.cfm?debugContent=pwReq');
				});
			}
			//Clear out our entry string
			currEntryString = '';
		} else currEntryString += String.fromCharCode(event.keyCode);
			
	});
	
	//Bind the forms to post via ajax
	$('div#tpFoot form').livequery(function() {
		$(this).submit(function() {
			//post the form
			theForm = this;
			//Each form has a different effect
			switch($(theForm).attr('id')) {
				case "pwReq":
					$('div#tpFoot').load(sharedPath + 'debugger.cfm', $(theForm).serialize(), function() {
						//If we submitted the password form, check the hidden span.
						if($('span#isPasswordValid').html().indexOf("true") != -1) {
							//If it was valid, load the choices up
							$('div#tpFoot').load(sharedPath + 'debugger.cfm?debugContent=choices');
						}
						else {
							//It was invalid, they can try again I suppose.
							$('div#tpFoot').load(sharedPath + 'debugger.cfm?debugContent=pwReq', '', function() {$('label#passwordLabel').html('Password - Incorrect');});
						}
					});
					break;
				case "choices":
					//Post to the debugger file to set the variables in the session CFC, then load the dumps in
					$.post(sharedPath + 'debugger.cfm', $(this).serialize(), function(data) {
						$('div#tpFoot').load(sharedPath + 'debugger.cfm?debugContent=dumps');
					});
					break;
			}
			//We don't want the form post to do anything
			return false;
		});
	});
});