﻿//Координаты клика
x = 0; y = 0;


$(document).ready(function(){
	if (window.Event) {
		document.captureEvents(Event.MOUSEMOVE);
	}
	if($("#wordsPanel div").length < 35) $('#wordsPanel').click(getXY);
	
	$("#next").click(function(){
		$("#capIMG").html('');
		r = new Date();
		$("#capIMG").append("<img src=\"CaptchaImage.php?"+r.getTime()+"\" alt=\"\" />");
		$(".message2").fadeOut(500);
		$(".message").fadeIn(500);
		$("#captcha").focus();
	});
	
	$("#add").click(function(){
		$.post('checkCaptcha.php',{captcha: $("#captcha").val()},captcha);
	});
});


function getXY(e) {
	x = (window.Event) ? e.pageX : event.clientX;
	y = ((window.Event) ? e.pageY : event.clientY)-80;
	pos = 'left: '+x+'px; top: '+(y-80)+'px;';
	$("#panel").fadeOut(250);
	$('.message2').fadeIn(250);
	$('#word').focus();
}


function postsend(data, textStatus){
	if((textStatus != 'success') || (data == 'filed')) { 
	    alert('Извините, сейчас записать невозможно. Попробуйте позже.');
		exit;
	}
	$("#wordsPanel").append("<div style=\"position: absolute; top: "+y+"px; left: "+x+"px;\">"+data+"</div>");
	$('.message').fadeOut(250);
	$("#panel").fadeIn(250);
	$('#word').val('');
	$('#captcha').val('');
	if($("#wordsPanel div").length > 33) document.location = 'index.php';
}


function captcha(data, textStatus){
	if(data == 'NO') { 
		$("#capIMG").html('');
		r = new Date();
		$("#capIMG").append("<img src=\"CaptchaImage.php?"+r.getTime()+"\" alt=\"\" />");
		$('#captcha').val('');
	    alert("Код введён не верно");
	}
	if(data == 'YES'){
		$.post('index.php?mode=new',{word: $("#word").val(), x: x, y: y},postsend);
	}
}

