$(document).ready(function(){


 setTimeout('$("#chubakka").animate({opacity: "hide"},"slow");',4000);

 clicked = false;
 elmove = 1;
 point = $("<img class=point src='/images/circle.png'>");
 errormess = 'Тут такое дело, пока ты ничего не напишешь, на кнопку бесполезно нажимать :)';

 $('img.point').Draggable({
	helper: 'clone',
	opacity: 0.5,
	onStop: function(){
	  var pid = $(this).attr('pointid');
	  var posX = $(this).css('left');
	  var posY = $(this).css('top');
	  $.ajax({
   	    data: "id="+pid+"&posx="+posX+"&posy="+posY,
   	    url:  "/core/backends/backends.save.new.pos.php",
   	    type: "POST"
   	  });

	}
 });

   function str_replace(search, replace, subject) {
    return subject.split(search).join(replace);
    }



   $("#hide-all").toggle(
     function(){
     	$("img.point").animate({opacity: 0},"fast");
     	$(this).text('Показать всё');
     	$("#chubakka").css({display: "block"});},
     function(){
     	$("img.point").animate({opacity: 1},"fast");
     	$(this).text('Спрятать всё');
     	$("#chubakka").css({display: "none"});}
   );

   $("img.point").live('mouseover',
     function(){
      if(clicked != true && elmove != 0){
      if(!$("#container").is(":visible")){
        var mytext = $(this).attr("content");
        var container = $("<div id=container></div>").text(mytext).css({left: $(this).css("left"), top: $(this).css("top") });
        container.prependTo('body');
       }
      }
    });


    $("img.point").live('mouseout',function(){
    	$("#container").remove();
    });

   $("#write").click(function(){
   	$("html").css({cursor: "crosshair"});
   	if($("#chubakka").is(":visible")){
     	$("#chubakka").css({display: "none"});
     	$("img.point").animate({opacity: 1},"fast");
     	$("#hide-all").text('Спрятать всё');
   	}
   	clicked = true;
   	return false;
   });


   $("html").click(function(e){
    var point_block = $('<div class=point-block></div>').css({top: (e.clientY - 10) + "px", left: (e.clientX - 10)+ "px"});
    point_block.html("<a href=\"#close\" class=close title=Закрыть>&nbsp;</a><textarea name=text></textarea><input type=hidden name=x value='"+(e.clientX - 10)+"'><input type=hidden name=y value='"+(e.clientY - 10)+"'><p class=ok><input type=button value=Yarrr! id=ok></p>");
     if(clicked){
     	point_block.prependTo('body');
     	clicked = false;
     	$("html").css({cursor: "auto"});
     }


   });

   $("a.close").live('click',function(){
   	$(this).parent().remove();
   	return false;
   });

   $("#ok").live('click',function(){
   	var posX = $("input[name=x]").val();
   	var posY = $("input[name=y]").val();
   	var newtext = $("textarea[name=text]").val();
   	if(newtext != '' && newtext != errormess){
   	 $(this).attr("disabled","disabled").val('Сохраняю');
   	 newtext = str_replace('ш', 'Ш', newtext);
   	   $.ajax({
   	    data: "text="+newtext+"&posx="+posX+"&posy="+posY,
   	    url:  "/core/backends/backends.save.mess.php",
   	    type: "POST",
   	    error: function(){$("#ok").val('Ошибка сохранения').addClass('error');},
   	    success: function(data){
   	    	if(data == 0){
   	          point.clone().css({left: posX + "px", top: posY + "px" }).prependTo('body').attr("content", newtext);
   	          $("div.point-block").remove();
   	    	}
   		}
   	});
    } else {
      $("textarea[name=text]").text(errormess)
    }
   });

});