function htmlentities(s){
	var div = document.createElement('div');
	var text = document.createTextNode(s);
	div.appendChild(text);
	return div.innerHTML;
}

$(function()
{
	
	$("div.container").mousewheel(function(event, delta) {
	   this.scrollLeft -= (delta * 100);
	   event.preventDefault();
	});

	$("div.container").droppable({
		accept: "div.new",
		drop: function( event, ui ) {

		$.ajax({
		   type: "POST",          
		   url: "/sticker-resources/controller.php",
		   data: "id="+$(".ui-draggable #idnumber").text()+"&left="+$(".ui-draggable").position().left+"&top="+$(".ui-draggable").position().top,
		   success: function(msg){
		      $(".new").draggable("destroy");
		      $(".new").removeClass("new");
		   }
		});
	
	}
	});

});


	$(function() {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog:ui-dialog" ).dialog( "destroy" );

	
		var fieldbig = $( "#fieldbig" ),
			fieldsmall = $( "#fieldsmall" ),
			allFields = $( [] ).add( fieldbig ).add( fieldsmall ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( "Length of " + n + " must be between " +
					min + " and " + max + "." );
				return false;
			} else {
				return true;
			}
		}
		                                          	
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			height: 350,
			width: 350,
			modal: true,
			buttons: {
				"Написать!": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( fieldbig, "Заголовок", 0, 12 );
					bValid = bValid && checkLength( fieldsmall, "Мелкий текст", 5, 128 );

					if ( bValid ) {

						var style = $("#colors input:radio:checked").val();

						$.ajax({
						   type: "POST",          
						   url: "/sticker-resources/controller.php",
						   data: "title="+fieldbig.val()+"&comment="+fieldsmall.val()+"&style="+style,
						   success: function(msg){


						   	var newstick = "<div class='sticker "+htmlentities(style)+" new' style='top:200px;left:600px;'>"+
									"<a href='#'><h2>"+htmlentities(fieldbig.val())+"</h2><p>"+htmlentities(fieldsmall.val())+"</p>"+
									"<span id='editable'></span><span id='idnumber'>"+msg+"</span></a></div>";

						   	$("div.container").append(newstick);
						   	$(".new").draggable();
  							$("#addsticker").html("<p>Записки можно добавлять не чаще одного раза в час.</p>");
  							$("#dialog-form").dialog( "close" );

  							var d = new Date();
							var t = Math.round(d.getTime()/1000);
  							
  							$.cookie('lastpost', t, { expires: 1 });


							$( "#okay" ).dialog({
								height: 220,
								width: 320,
								modal: true,
								buttons: {
									"Ща перетащу!": function() {
									$( this ).dialog( "close" );
									}
								}
							});

						   }
						});
					}
				},
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});

		$( "#create-sticker" )
			.click(function() {
				$( "#dialog-form" ).dialog( "open" );
			});
	});


