var activeCell = null;
var n;
var m;
var table;
var solution;
var current;
var cache = {};


var crosswords = [['scriitori', 'Scriitori romani'],
        ['actori','Actori celebri'],
        ['pe_ascuns','Pe ascuns'],
        ['regasiri','Regasiri'],
        ['pe_nume','Pe nume'],
        ['neologisme','Neologisme'],
        ['cu_sinceritate','Cu sinceritate'],
        ['pamantesti','Pamantesti'],
        ['selectii','Selectii'],
        ['1','Integrama 1'],
        ['2','Integrama 2'],
        ['3','Integrama 3'],
        ['4','Integrama 4'],
        ['5','Integrama 5'],
        ['6','Integrama 6'],
        ['7','Integrama 7'],
        ['8','Integrama 8'],
        ['9','Integrama 9'],
        ['10','Integrama 10'],
        ['11','Integrama 11'],
        ['12','Integrama 12'],
        ['anagrama1','Anagrama 1'],
        ['anagrama2','Anagrama 2'],
        ['rute','Rute']];

var randomCrossword = Math.floor(Math.random() * 23) + 1;

var currentCrossword = {
    filename: crosswords[randomCrossword][0],
    title: crosswords[randomCrossword][1],
    method: 'writeTable'
}

$(document).ready(function(){
	$(document).bind("keydown", keyHandler);
    window[currentCrossword.method](currentCrossword.filename, currentCrossword.title);
	
	$('.toggle-button').click(function(){
		if($(this).next().is(":hidden")){

			$(this).siblings('.toggle-content').slideUp();
			$(this).next().slideDown();
		}
		else{
			$(this).next().slideUp();
		}
	});
	
	$('.toggle-button').hover(function(){
		$(this).addClass("toggle-button-hover");
	},
	function(){
		$(this).removeClass("toggle-button-hover");		
	});
	
	$("#sidebar li a").bind("click", function(){
		$(this).parent().addClass("content-loading"); 
	});
});




function keyHandler(event){

	if (!activeCell) 
		return;
	
	var cell = $(activeCell);
	var charCode = event.which;
	var key;
	
	if (charCode == 32 || charCode == 8 || charCode == 46) { //SPACE || BACKSPACE || DEL
		cell.html("&nbsp");
        event.preventDefault();
		return;
	}
	else {
		key = String.fromCharCode(charCode)
		
		key = key.toUpperCase();
		charCode = key.charCodeAt(0);
		
		if (charCode < 65 || charCode > 90) 
			return;
		
		
		var cssClass, nextCell;
		cell.html(key);
		
		var x = activeCell.parentNode.rowIndex;
		var y = activeCell.cellIndex;
		
		if (cell.hasClass("active-cell-down")) {
			cssClass = "active-cell-down"
			if (x + 1 < table.rows.length) 
				nextCell = table.rows[x + 1].cells[y];
		}
		else if (cell.hasClass("active-cell-right")) {
			cssClass = "active-cell-right";
			nextCell = table.rows[x].cells[y + 1];
		}
		
		if (nextCell != null) {
			var nc = $(nextCell);
			if (nc.hasClass("editable-cell")) {
				cell.removeClass(cssClass).removeClass("active-cell");
				nc.addClass(cssClass).addClass("active-cell");
				activeCell = nextCell;
			}
		}
	}
}



function highlightAll(){
    var x = this.parentNode.rowIndex;
    var y = this.cellIndex;
    var n = table.rows.length;
    var m = table.rows[0].cells.length;
    
    $(this).addClass("cell-hover");
    
    var i = x - 1, j = y;
    
    while (i >= 0 && $(table.rows[i].cells[j]).hasClass('editable-cell')) {
        $(table.rows[i].cells[j]).addClass('cell-hover');
        i--;
    }
    
    i = x + 1;
    while (i < n && $(table.rows[i].cells[j]).hasClass('editable-cell')) {
        $(table.rows[i].cells[j]).addClass('cell-hover');
        i++;
    }
    
    i = x;
    j = y - 1;
    while (j >= 0 && $(table.rows[i].cells[j]).hasClass('editable-cell')) {
        $(table.rows[i].cells[j]).addClass('cell-hover');
        j--;
    }
    
    i = x;
    j = y + 1;
    while (j < m && $(table.rows[i].cells[j]).hasClass('editable-cell')) {
        $(table.rows[i].cells[j]).addClass('cell-hover');
        j++;
    }
}

function setupHighlighting(config)
{	
	var onEnter = config ? config.highlightAll || highlightAll : highlightAll;
	
	$(".editable-cell").hover(onEnter, 
		function () {
			$(".cell-hover").removeClass("cell-hover");
			$(".definition-highlighted").removeClass("definition-highlighted");
		});	

    if(cache[current]){
        loadContent(cache[current]);
    }

	$(".content-loading").each(function(){
		$(this).removeClass("content-loading");
	});
}




function mouseClickHandler(event){

    $(".active-cell").removeClass("active-cell").removeClass("active-cell-down").removeClass("active-cell-right");
    $(this).addClass(event.which == 1 ? "active-cell-right" : "active-cell-down").addClass("active-cell");
    
    activeCell = event.target;
}




function contact(){
	window1 = window.open("despre.htm", "window1", "HEIGHT=300,  WIDTH=320, screenX=100, screenY=100")
}

function instructiuni(){
	window1 = window.open("instructiuni.htm", "window1", "HEIGHT=300,  WIDTH=300, screenX=100, screenY=100")
}


function deleteTableContent(table){
	var i, m;
    
    m = table.rows.length;
    
	for (i = 0; i < m; i++) {
		table.deleteRow(0);
	}
}

var log = function(){
	if (window.console) {
		console.log.apply(this,arguments);
	}
}

var Logger = {

	log: function(value, type){
		/*$.ajax({
			type: "POST",
			url: "php/log.php",
			data: "rebus=" + value + "&type=" + type
		});		
		*/
	}

}


function solve(){

	if (solution && solution.length > 0) {
        solution = solution.replace(/[ ]/g, ""); 
        loadContent(solution);
	}
}

function loadContent(content){
    $('.editable-cell').each(function(index){
        $(this).html(content.charAt(index));  
    });
}

function check(){
        solution = solution.replace(/[ ]/g, "");
	alert( solution === $('.editable-cell').text() ? 'Felicitari! Careul a fost completat corect!' : 'Careul NU este completat corect!')
}


function loadNew(id){
    
	$("#rebus-comments").html("");
	
    if(current){
        cache[current] = $('.editable-cell').text();
    }
    
    current = id;
}


