var cw = new Array();
var cw_utd = false;
var cw_time = 0;
var cw_timer_active = true;
var cw_id = 0;
var cw_focus = false;
var cw_clue_num = 0;

var CROSSWORD_WIDTH = 15;
var CROSSWORD_HEIGHT = 15;

function cw_timer() {
	if(cw_timer_active) {
		cw_time++;
		J('#cw_timer').html('Time: ' + cw_time);
		
		setTimeout("cw_timer()", 1000);
	}
}

function cw_highlight() {
	if(!cw_focus) return;
	
	J('.cw_shaded').removeClass('cw_shaded');

	tileX = J('.cursor').attr('cw_x');
	tileY = J('.cursor').attr('cw_y');
	
	if(cw_utd) {
		x = tileX;
		y = tileY;
		while(y < CROSSWORD_HEIGHT && !J('.cw_tile[cw_x="' + x + '"][cw_y="' + y + '"]').hasClass('cw_filled')) {
			J('.cw_tile[cw_x="' + x + '"][cw_y="' + y + '"]').addClass('cw_shaded');
			y++;
		}
		y = tileY;
		while(y >= 0 && !J('.cw_tile[cw_x="' + x + '"][cw_y="' + y + '"]').hasClass('cw_filled')) {
			J('.cw_tile[cw_x="' + x + '"][cw_y="' + y + '"]').addClass('cw_shaded');
			y--;
		}
		y++;
	} else {
		x = tileX;
		y = tileY;
		while(x < CROSSWORD_WIDTH && !J('.cw_tile[cw_x="' + x + '"][cw_y="' + y + '"]').hasClass('cw_filled')) {
			J('.cw_tile[cw_x="' + x + '"][cw_y="' + y + '"]').addClass('cw_shaded');
			x++;
		}
		x = tileX;
		while(x >= 0 && !J('.cw_tile[cw_x="' + x + '"][cw_y="' + y + '"]').hasClass('cw_filled')) {
			J('.cw_tile[cw_x="' + x + '"][cw_y="' + y + '"]').addClass('cw_shaded');
			x--;
		}
		x++;
	}
	 
	str = J('.cw_tile[cw_x="' + x + '"][cw_y="' + y + '"]').html();
	
	str = str.substring(str.indexOf('>')+1);
	cw_clue_num = parseInt(str.substring(0, str.indexOf('<')));
}

function cf_crossword(id) {
	cw_id = id;
	var cw_tmp = new Object;
	
	cw_tmp.name = '';
	cw_tmp.setup_data = '';

	cw_tmp.getScores = function(id) {
		J.post(
			'http://rockrivertimes.com/crossword/get_scores.php',
/*			'get_scores.php',*/
			{
				cw_id:				cw_id
			},
			function(data) {
				J('.cw_scores_wrapper').html(data);
			}
		);
	}

	cw_tmp.generate = function() {
		cw_html = '';
		for(y = 0; y < CROSSWORD_HEIGHT; y++) {
			cw_html += '<div class="cw_row">';
			for(x = 	0; x < CROSSWORD_WIDTH; x++) {
				cw_tile_class = '';
				if(x == 0) cw_tile_class += ' cw_first_col';
				if(x == (CROSSWORD_WIDTH - 1)) cw_tile_class += ' cw_last_col';
				if(y == 0) cw_tile_class += ' cw_first_row';
				if(y == (CROSSWORD_HEIGHT - 1)) cw_tile_class += ' cw_bottom_row';
				
				if(cw[y * CROSSWORD_WIDTH + x] == -1)
					cw_tile_class += ' cw_filled';
				
				var cw_clue = '';
				if(cw[y * CROSSWORD_WIDTH + x] > 0) {
					cw_clue = '<div class="cw_clue">' + cw[y * CROSSWORD_WIDTH + x] + '</div>';
				}
				
				cw_html += '<div class="cw_tile' + cw_tile_class + '" cw_x="' + x + '" cw_y="' + y + '" clue="' + cw[y * CROSSWORD_WIDTH + x] + '">' + cw_clue + '</div>';
			}
			cw_html += '</div>';
		}
		
		J('#cw_crossword').html(cw_html);
		

		
		J(document).keydown(function(event) {
			var unicode = event.charCode ? event.charCode : event.keyCode;
			
			if(!cw_focus && unicode==8) return false;
			if(!cw_focus) return;

			x = parseInt(J('.cursor').attr('cw_x'));
			y = parseInt(J('.cursor').attr('cw_y'));
			prevX = x;
			prevY = y;
			
			switch(unicode) {
				case 38: // up
					while(y != 0 && cw[--y * CROSSWORD_WIDTH + x] == -1);			
					event.preventDefault();
					break;
					
				case 40: // down
					while(y != (CROSSWORD_HEIGHT-1) && cw[++y * CROSSWORD_WIDTH + x] == -1);
					event.preventDefault();
					break;
					
				case 37: // left
					while(x != 0 && cw[y * CROSSWORD_WIDTH + --x] == -1);
					event.preventDefault();
					break;
					
				case 39: // right
					while(x != (CROSSWORD_WIDTH-1) && cw[y * CROSSWORD_WIDTH + ++x] == -1);
					event.preventDefault();
					break;
					
				case 8: // backspace
					var cw_clue = '';
					if(cw[y * CROSSWORD_WIDTH + x] > 0)
						cw_clue = '<div class="cw_clue">' + cw[y * CROSSWORD_WIDTH + x] + '</div>';

					
					J('.cursor').html(cw_clue);

					if(cw_utd) {
						if(y > 0) y--;	
					} else {
						if(x > 0) x--;
					}

					event.preventDefault();
					break;
			}

			if(cw[y * CROSSWORD_WIDTH + x] == -1)
			{
				x = prevX;
				y = prevY;
			}
				
			if(cw_utd) {
				J('.cursor').removeClass('cursor utd');
				J('.cw_tile[cw_x="' + x + '"][cw_y="' + y + '"]').addClass('cursor utd');
			} else {
				J('.cursor').removeClass('cursor utd');
				J('.cw_tile[cw_x="' + x + '"][cw_y="' + y + '"]').addClass('cursor');
			}

			cw_highlight();
			
			J('.cw_clue_sel').removeClass('cw_clue_sel');
			
			if(cw_utd)
				J('#cw_down li[clue="' + cw_clue_num + '"]').addClass('cw_clue_sel');
			else
				J('#cw_across li[clue="' + cw_clue_num + '"]').addClass('cw_clue_sel');
			
//			alert(event.keyCode);
		});

		J(document).keypress(function(event) {
			if(!cw_focus) return;
			
			var unicode = event.charCode ? event.charCode : event.keyCode;
			if(unicode==8) {
				event.preventDefault();
				return false;
			}

			if(unicode < 65 || unicode > 122) return false;

			x = parseInt(J('.cursor').attr('cw_x'));
			y = parseInt(J('.cursor').attr('cw_y'));

			var cw_clue = '';
			if(cw[y * CROSSWORD_WIDTH + x] > 0)
				cw_clue = '<div class="cw_clue">' + cw[y * CROSSWORD_WIDTH + x] + '</div>';

			J('.cursor').html(cw_clue + String.fromCharCode(unicode).toUpperCase());
			
			if(cw_utd) {
				if(y < (CROSSWORD_HEIGHT-1)) {
					if(cw[(y+1) * CROSSWORD_WIDTH + x] != -1) {
						J('.cursor').removeClass('cursor utd');
						J('.cw_tile[cw_x="' + x + '"][cw_y="' + (y+1) + '"]').addClass('cursor utd');
					}
				}
			} else {
				if(x < (CROSSWORD_WIDTH-1)) {
					if(cw[y * CROSSWORD_WIDTH + (x+1)] != -1) {
						J('.cursor').removeClass('cursor');
						J('.cw_tile[cw_x="' + (x+1) + '"][cw_y="' + y + '"]').addClass('cursor');
					}
				}
			}
		});
		
		J('#cw_submit').click(function(event) {
			cw_data = '';
			
			for(y = 0; y < CROSSWORD_HEIGHT; y++) {
				for(x = 0; x < CROSSWORD_WIDTH; x++) {
					tile = J('.cw_tile[cw_x="' + x + '"][cw_y="' + y + '"]');
					str = tile.html();
					
					if(!str && !tile.hasClass('cw_filled'))
						str = '  ';
					
					cw_data += str.charAt(str.length - 1);
				}
			}
			
			J.post(
				'http://rockrivertimes.com/crossword/check_crossword.php',
/*				'check_crossword.php',*/
				{
					cw_id:				cw_id,
					cw_user_data:		cw_data
				},
				function(data) {
					cw_complete = Boolean(parseInt(data.substring(0,1)));
					data = data.substring(1);
					
					if(cw_complete) {
						J('#cw_ajax').html(data);
						
//						alert(data + ' in ' + cw_time + ' seconds!');
						J('.cursor').removeClass('cursor');
						J('#cw_not_finished').slideUp('fast');
						J('#cw_send_score').slideDown('fast');
						
						cw_focus = false;
						cw_timer_active = false;
						J('#cw_submit').hide();
					} else {
						J('#cw_not_finished').hide();
						J('#cw_not_finished').html(data);
						J('#cw_not_finished').slideDown('fast');
					}
				}
			);
		});

		// clicking on clue puts cursor at first letter of word
		J('#cw_across li').click(function(event) {
			cw_clue_str = J(event.target).html();
			cw_clue_num = cw_clue_str.substring(0, cw_clue_str.indexOf(' '));
			
			J('.cursor').removeClass('cursor utd');
			J(".cw_tile[clue='" + cw_clue_num + "']").addClass('cursor');
			cw_focus = true;
			cw_utd = false;
			
			J('.cw_clue_sel').removeClass('cw_clue_sel');
			J(event.target).addClass('cw_clue_sel');
			
			cw_highlight();

		});
		
		J('#cw_down li').click(function(event) {
			cw_clue_str = J(event.target).html();
			cw_clue_num = cw_clue_str.substring(0, cw_clue_str.indexOf(' '));
			
			J('.cursor').removeClass('cursor utd');
			J(".cw_tile[clue='" + cw_clue_num + "']").addClass('cursor utd');
			cw_focus = true;
			cw_utd = true;
			
			J('.cw_clue_sel').removeClass('cw_clue_sel');
			J(event.target).addClass('cw_clue_sel');
			
			cw_highlight();
		});
		

		J('.cw_tile').click(function(event) {
			if( J(event.target).hasClass('cw_clue') ) {
				J(event.target).parent().trigger('click');
				return false;
			}
			
			if( !J(event.target).hasClass('cw_filled') ) {
				if( J(event.target).hasClass('cursor') ) {
					if(cw_utd) {
						J('li').removeClass('cw_clue_sel');
						J(event.target).removeClass('utd cursor');
						cw_focus = false;
						J('.cw_shaded').removeClass('cw_shaded');
					}
					else {
						J(event.target).addClass('utd');
					}
					
					cw_utd = J('.cursor').hasClass('utd');
				} else {
					if(cw_utd) {
						J('.cursor').removeClass('cursor utd');
						J(event.target).addClass('cursor utd');
					} else {
						J('.cursor').removeClass('cursor');
						J(event.target).addClass('cursor');
					}
					
					cw_focus = true;
				}
				
				cw_highlight();
				
				// select which clue
				J('.cw_clue_sel').removeClass('cw_clue_sel');
				
				if(cw_utd)
					J('#cw_down li[clue="' + cw_clue_num + '"]').addClass('cw_clue_sel');
				else
					J('#cw_across li[clue="' + cw_clue_num + '"]').addClass('cw_clue_sel');
			}
		});
		
	
		cw_timer();
	};


	// **************** properties/functions are all defined ******************
	J.ajax({
		type: "POST",
		url: "http://rockrivertimes.com/crossword/get_crossword.php",
/*		url: "get_crossword.php",*/
		data: ({
			cw_id: id
		}),
		success: function(data) {  // received crossword data
			// retrieve name
			var cw_data = new Array();
			cw_data = data.split('^^');
			
			cw_tmp.name = cw_data[0];
			
			J('#cw_name').html(cw_tmp.name);

			cw = cw_data[1].split(',');

			var cw_across = String(cw_data[2]).split('\n');
			var cw_down = String(cw_data[3]).split('\n');
			
			// setup clue data
			cw_across_str =  '<li style="font-weight:bold;">Across</li>';
			for(i = 0; i < cw_across.length; i++) {
				cw_across_str += '<li clue="' + cw_across[i].substring(0, cw_across[i].indexOf(' ')) + '">' + cw_across[i] + '</li>';
			}
			cw_across_str += '</li>';
			
			cw_down_str = '<li style="font-weight:bold;">Down</li>';
			for(i = 0; i < cw_down.length; i++) {
				cw_down_str += '<li clue="' + cw_down[i].substring(0, cw_down[i].indexOf(' ')) + '">' + cw_down[i] + '</li>';
			}
			cw_down_str += '</li>';
			
			J('#cw_across').html(cw_across_str);
			J('#cw_down').html(cw_down_str);
			
			
			cw_tmp.generate();
		},
		error: function(x, str) {
			alert(str);
		}	
	});
	
	J('#cw_send_score_btn').click(function() {
		J.post(
			'http://rockrivertimes.com/crossword/save_score.php',
/*			'save_score.php',*/
			{
				cw_id:	cw_id,
				cw_name:	J('#cw_send_score_name').val(),
				cw_time:	cw_time
			},
			function(data) {
//				alert(data);
				J('#cw_send_score').slideUp('fast');
				
				cf_cw.getScores(cw_id);
			}
		);
	});

	return cw_tmp;
}
