
function writeRebus(filename){

    loadNew(filename);
    
	table = document.getElementById('rebus-grid');
	table.oncontextmenu = function(){
		return false;
	};

    $('#rebus-title').text("");
    deleteTableContent(document.getElementById('integrame-grid'));
	//$("#integrame").hide();
	$("#int-adds").hide();
	//$("#rebus-container").show();
    $(table).removeClass("integrame-table").addClass("rebus-table");

	$.ajax({
		url:"data/careuri/" + filename + ".xml",
		success: rebusLoadCallback
	});

    $("#fb-like-panel").html('<div id="fb-root"></div>' +
            '<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>' +
            '<fb:like href="http://rebus-integrame.ro/?rebus=' + filename +
            '" send="true" width="450" show_faces="false" font=""></fb:like>');

	Logger.log(filename, 2);
}

function rebusLoadCallback(response){

	var xml = $(response);
	var i, j;
	
	n = xml.find("lungime:first").text();
	solutie = xml.find("solutie").text();
	
	m = table.rows.length;
	for (i = 0; i < m; i++) {
		table.deleteRow(0);
	}
	activeCell = null;
	m = n;
	
	var row = table.insertRow(0);
	cell = row.insertCell(0);
	cell.className = "header";
	for (j = 1; j <= n; j++) {
		cell = row.insertCell(j);
		cell.className = "header";
		cell.innerHTML = j + "&nbsp;";
	}
	
	for (i = 1; i <= n; i++) {
		row = table.insertRow(i);
		
		cell = row.insertCell(0);
		cell.className = "header";
		cell.innerHTML = i + "&nbsp;";
		
		for (j = 1; j <= n; j++) {
			cell = row.insertCell(j);
			cell.className = "editable-cell rebus-editable-cell";
			cell.innerHTML = "&nbsp;";
			cell.unselectable = "on";
		}
		
	}
	
	matVert = new Array(n + 1);
	matOriz = new Array(n + 1);
	for (i = 0; i <= n; i++) {
		matVert[i] = new Array(n + 1);
		matOriz[i] = new Array(n + 1);
		
		for (j = 0; j <= n; j++) 
			matVert[i][j] = matOriz[i][j] = 0;
	}
	
	xml.find('punct').each(function(i){
		var x = parseInt($(this).find('x').text());
		var y = parseInt($(this).find('y').text());
		table.rows[x].cells[y].className = "block";
		matVert[x][y] = matOriz[x][y] = -1;
	});
	
	for (i = 1; i <= n; i++) {
		var index = 1;
		j = 1;
		if (matOriz[i][j] == -1) 
			j = 2;
		
		while (j < n) {
			if (matOriz[i][j + 1] != -1) {
				matOriz[i][j] = index;
				while (j < n && matOriz[i][j + 1] != -1) {
					matOriz[i][j + 1] = index;
					j++;
				}
				index++;
			}
			
			j += 2;
		}
	}
	
	for (j = 1; j <= n; j++) {
		var index = 1;
		i = 1;
		if (matVert[i][j] == -1) 
			i = 2;
		while (i < n) {
			if (matVert[i + 1][j] != -1) {
				matVert[i][j] += index;
				while (i < n && matVert[i + 1][j] != -1) {
					matVert[i + 1][j] += index;
					i++;
				}
				index++;
			}
			
			i += 2;
		}
	}

	var td = document.getElementById('hdef');
	
	$('#rebus-title').text(xml.find("nume").text());
	
	td.innerHTML = "<h3>ORIZONTAL</h3>";
	
	xml.find('orizontal definitie').each(function(i){
		var def = $(this).text();
		var subdef = def.split(" - ");
		td.innerHTML += " <b>" + (i + 1) + "</b>) <span id='h" + (i + 1) + "1'>" + subdef[0] + "</span>";
		for (j = 1; j < subdef.length; j++) 
			td.innerHTML += " - <span id='h" + (i + 1) + (j + 1) + "'>" + subdef[j] + "</span>";
	});

	var td = document.getElementById('vdef');
	td.innerHTML = "<h3>VERTICAL</h3>";
	
	xml.find('vertical definitie').each(function(i){
		var def = $(this).text();
		var subdef = def.split(" - ");
		td.innerHTML += " <b>" + (i + 1) + "</b>) <span id='v" + (i + 1) + "1'>" + subdef[0] + "</span>";
		for (j = 1; j < subdef.length; j++) 
			td.innerHTML += " - <span id='v" + (i + 1) + (j + 1) + "'>" + subdef[j] + "</span>";
	});
	


	xml.find("dictionar").each(function(){
		td.innerHTML += "<br><br><b>Dictionar : </b>" + $(this).text();
	});		
	
	td.innerHTML += "<br><p class='rebus-author'><b>" + xml.find("autor").text() + "</b></p>";
	

	solution = xml.find("solutie").text();
	if (solution && solution.length > 0) {
		$("#check-button").show();
		$("#solve-button").show();
	}
	else{
		$("#check-button").hide();
		$("#solve-button").hide();
	}
	
	$(".editable-cell").bind("mousedown", mouseClickHandler);
	setupHighlighting({
		highlightAll: rebusHighlightAll
	});
}

function rebusHighlightAll(){
    var x = this.parentNode.rowIndex;
    var y = this.cellIndex;

	if (matOriz[x][y] > 0) 		document.getElementById('h' + x + matOriz[x][y]).className = "definition-highlighted";
	if (matVert[x][y] > 0) 
		document.getElementById('v' + y + matVert[x][y]).className = "definition-highlighted";

	highlightAll.apply(this, arguments);
}


//-->


