var maxZ = 500;
var bin_gedrueckt = "nein";

Event.observe(window, 'load', function(){
	var movables = $A(document.getElementsByClassName('icon_start'));
	movables.each(function(b){
		// Mausverhalten
		b.onmouseover = function(){
			if (bin_gedrueckt != "ja") {
				$(b.id+'-projektname').style.display = 'block'
			}
		};
		b.onmousedown = function(){
			b.addClassName('movable');
			bin_gedrueckt = "ja"
		};
		b.onmouseup = function(){
			b.removeClassName('movable');
			$(b.id+'-projektname').style.left = b.style.left;
			$(b.id+'-projektname').style.top = b.style.top;
			$(b.id+'-projektname').style.display = 'block';
			bin_gedrueckt = "nein";
		};
		b.onmouseout = function(){
			$(b.id+'-projektname').style.display = 'none'
		};
		// Draggen
		new Draggable(b, {
			onStart : function(){
				$(b.id+'-projektname').style.display = 'none';
				// $A(document.links).each(function(l){l.onclick = function(){l.onclick = null; return false}})
				$(b.id+'-link').onclick = function(){$(b.id+'-link').onclick = null; return false}
			},
			onEnd : function(){
				var p = Position.positionedOffset(b);
				var pair = b.id.split('-');
				new Ajax.Request('/ajax/iconPosition.php?' + $H({id: pair[1], x:p[0], y:p[1]}).toQueryString(), {
					onSuccess: function(t){
						// new Effect.Pulsate(b, {pulses: 3, duration: 0.7});
						// alert(t.responseText)
						b.style.zIndex = maxZ++;
					}
				});
			},
			snap: function(x,y){
				// return [Math.max(0, 4*Math.round(x/4)), Math.max(0, 2*Math.round(y/2))];
				return [Math.max(0, 2*Math.round(x/2)), Math.max(0, Math.round(y))];
			}
		});
	});
});
