var spell_words_str = '';
var spell_words = new Array(1000);
var spell_obj;
var spell_name='';
var spell_html='';
var spell_is_menu=false;


function spell_start(name) {
	spell_is_menu=true;
	w = $('#'+name).width();
	h = $('#'+name).height();
	$('#'+name).before('<div id="div_spell_menu" style="display:none;border:1px solid #666;position:absolute;background-color:#fff;padding:2px;"></div><div id="div_spell_'+name+'"><a href="#" onclick="spell_it(\''+name+'\');return false;" class="k10">Sprawdź pisownię</a></div>')
	.before('<div id="div_'+name+'" style="text-align:left;border-style:solid;border-width:0px;border-color:#666;">');
	$('#div_spell_menu').width(220);
	$('#div_'+name).width(w).height(h).hide();
}

function spell(obj) {
	spell_obj=obj;
	scrolly=0;
	foo=$(obj).html();
	id=($(obj).attr('id').split('_'))[1];
	words=spell_words_str.split(';');
	words_ok='';
	if(spell_words[id]=='' || spell_words[id]==null || spell_words[id]==undefined) {
		for(i=0; i<words.length; i++) {
			words2=words[i].split(':');
			if(words2[0]==foo) {
				words_ok=words2[1];
				break;
			}
		}
		spell_words[id]=words_ok;
	} else {
		words_ok=spell_words[id];
	}
	words=words_ok.split(',');
	html='<div style="float:right;"><a href="javascript:void(0)" onclick="javascript:$(\'#div_spell_menu\').hide();" style="text-decoration:none;" title="zamknij"><b>&nbsp;X&nbsp;</b></a></div>';
	if(words.length > 0 && words[0].length > 0) {
		for(i=0; i<words.length && i < 10; i++) {
			html+='<a href="javascript:void(0)" onclick="spell_spelling(\''+words[i]+'\');return false;" class="k10">'+words[i]+'</a><br />';
		}
	} else {
		html+='<a href="javascript:void(0)" onclick="javascript:$(\'#div_spell_menu\').hide();" class="k10">(brak)</a><br />';
	}
	$('#div_spell_menu').html(html).css({
		left : x+"px",
		top : (y+5)+"px"
	}).show();
}

function spell_spelling(word) {
	$('#'+spell_obj.id).html(word).css({ color : '#77cc00' });
	$('#div_spell_menu').hide();
}

function spell_off() {
	var html='';
	if(spell_name.length > 0) {
		$('#'+spell_name).show();
		$('#div_spell_'+spell_name).html('<a href="javascript:void(0)" onclick="spell_it(\''+spell_name+'\');return false;" class="k10">Sprawdź pisownię</a>');
		$('#div_'+spell_name).css({
			overflow : 'hidden',
			padding : '0px',
			margin : '2px 0 0 0',
			borderWidth : '0px',
			backgroundColor : 'transparent'
		});
		html=$('#div_'+spell_name).html();
		if(html.length > 0) {
			while(html.indexOf('<!--^^^br^^^-->')>=0) {
				html=html.replace('<!--^^^br^^^-->',"\n");
			}
			html=html.replace(/(<([^>]+)>)/ig,"");
			while(html.indexOf('&lt;')>=0) {
				html=html.replace('&lt;','<');
			}
			while(html.indexOf('&gt;')>=0) {
				html=html.replace('&gt;','>');
			}
		}
		$('#div_'+spell_name).html(spell_html).hide();
		$('#div_spell_menu').hide();

		$('#'+spell_name).val(html);
		spell_words_str='';
		spell_words=new Array(1000);
		spell_name='';
		spell_html='';
	}
}

function spell_check_end(foo) {
	$('#div_'+spell_name).show();
	if (foo[1]) {
		text=foo[1];
	} else {
		text='';
	}
	i=1;
	while(text.indexOf('^^^111^^^')>=0) {
		text=text.replace('^^^111^^^','<a href="javascript:void(0)" onclick="spell(this); return false;" style="color: #ee0000;" id="spell_'+(i++)+'">');
	}
	while(text.indexOf('^^^222^^^')>=0) {
		text=text.replace('^^^222^^^','</a>');
	}
	while(text.indexOf('\\u000a')>=0) {
		text=text.replace('\\u000a','<br><!--^^^br^^^-->');
	}
	while(text.indexOf('\\u000d')>=0) {
		text=text.replace('\\u000d','');
	}
	while(text.indexOf('\\u003c')>=0) {
		text=text.replace('\\u003c','&lt;');
	}
	while(text.indexOf('\\u003e')>=0) {
		text=text.replace('\\u003e','&gt;');
	}
	$('#'+spell_name).css({ display : 'none' });
	w=parseInt($('#'+spell_name).width());
	h=parseInt($('#'+spell_name).height());
	$('#div_'+spell_name).html(text).width(w).height(h).css({
		overflow : 'auto',
		padding : '1px',
		borderWidth : '1px',
		backgroundColor : '#f5f5f5'
	});

	if (foo[0]=='') {
		$('#div_spell_'+spell_name).html('<span style="background-color:#ffff00;" onclick="spell_off()"><b>Brak błędów</b></span>');
		setTimeout('spell_no_errors()',1000);
		return false;
	}
	spell_words_str=foo[0];
	$('#div_spell_'+spell_name).html('<a href="javascript:void(0)" onclick="spell_off()" class="k10">Wróć do edycji</a>');
}

function spell_no_errors() {
	clearTimeout(); //?
	spell_off();
	clearTimeout(); //?
}


function spell_it(name) {
	if(spell_name=='' || spell_name==null || spell_name==undefined) {
		spell_name=name;
	} else {
		spell_off();
		spell_name=name;
	}
	$('#div_spell_'+spell_name).html('Sprawdzam...');
	foo=$('#'+spell_name).val();
	spell_html=$('#div_'+spell_name).html();
	spell_ajax('/ajax/spell.2.php',foo);
	return false;
}

function spell_ajax(plik_php,param) {
	var param=encodeURI(JSON.stringify(param));
	$.ajax({
		url : plik_php,
		type : 'post',
		dataType : 'json',
		data : 'val='+param,
		success : function (response) {
			eval('spell_check_end(response);');
		},
		error : function (response, exception) {
			eval('spell_failure(response);');
		}
	});
}

function spell_failure(dane) { }


var x = 0, y = 0;
$(document).mousemove(function (e) {
	x = e.pageX;
	y = e.pageY;
});