GCKPUZZLEANTISPAM = {
   snapDistance: 10,
   hightlightColor : "#fff890",
   boundingBox : null,
   smallBox : null,
   bigBox : null,
   targetBox : null,
   textBox : null,
   oldDocMouseMove : null,
   
   normalizedPositionFor : function (el) {
      var pos = bytefx.$position(el);
      var bboxOrigin = bytefx.$position(this.boundingBox);
      pos.x -= bboxOrigin.x;
      pos.y -= bboxOrigin.y;
      
      return pos;
   },
   
   distanceFromTarget : function () {
      var targetPos = this.normalizedPositionFor(this.targetBox);
      var targetSize = bytefx.$size(this.targetBox);
      var boxPos = this.normalizedPositionFor(this.smallBox);
      var boxSize = bytefx.$size(this.smallBox);
      
      var targetCenter =
         { x : targetPos.x/2 + targetSize.width/2, y : targetPos.y/2 + targetSize.height/2};
      var boxCenter =
         { x : boxPos.x/2 + boxSize.width/2, y : boxPos.y/2 + boxSize.height/2};
      var diff =
         { x : boxCenter.x - targetCenter.x, y : boxCenter.y - targetCenter.y};
            
      return Math.sqrt(diff.x*diff.x + diff.y*diff.y);
   },
   
   snapToTarget : function () {
      var dist = this.distanceFromTarget();
      
      if (dist <= this.snapDistance) {
         var targetPos = this.normalizedPositionFor(this.targetBox);
         
         jQuery(this.smallBox).animate({top : targetPos.y, left: targetPos.x}, 500, 'swing', function () {
            var bboxSize = bytefx.$size(GCKPUZZLEANTISPAM.boundingBox);
            var boxSize = bytefx.$size(GCKPUZZLEANTISPAM.smallBox);
            
            var rPos = bboxSize.width - targetPos.x - boxSize.width;
            jQuery(GCKPUZZLEANTISPAM.smallBox).css('left', 'auto');
            jQuery(GCKPUZZLEANTISPAM.smallBox).css('right', rPos + 'px');
         });
                  
         this.smallBox.onmousedown = function () { return false; };
         this.smallBox.onmouseup = function () { return false; };
         document.onmousemove = this.oldDocMouseMove;
         
         setTimeout(function() { GCKPUZZLEANTISPAM.puzzleSolved(); }, 500);
      }  
   },
   
   puzzleSolved : function () {
      var zauberwort = jQuery("input#tdomf_zauberwort").attr("value");
      jQuery("input#content-zauberwort").attr("value", zauberwort);
      setSubmitEnabled(true);
       
      var t = this.textBox;
      bytefx.fade(t, 100, 0, 1.8, function () {
         jQuery(t).html("Danke sehr!");
         bytefx.fade(t, 0, 100, 1.3, function () {
            var color = jQuery(t).css('background-color');
            jQuery(t).animate({ backgroundColor: GCKPUZZLEANTISPAM.hightlightColor }, 200, 'swing');
            jQuery(t).animate({ backgroundColor: color }, 600, 'swing');
         });   
      });
   },
   
   setup : function () {
      var boundingBox = jQuery(".gckpuzzleantispam");
      if (!boundingBox || !boundingBox[0])
         return;
      
      GCKPUZZLEANTISPAM.boundingBox = boundingBox[0];
      
      var textBox = jQuery(".gckpuzzleantispam .gckpuzzleantispam-textbox");
      if (!textBox || !textBox[0])
         return;
      
      GCKPUZZLEANTISPAM.textBox = textBox[0];
      
      var smallBox = jQuery(".gckpuzzleantispam .gckpuzzleantispam-smallbox");
      if (!smallBox || !smallBox[0])
         return;
      
      GCKPUZZLEANTISPAM.smallBox = smallBox[0];
         
      var bigBox = jQuery(".gckpuzzleantispam .gckpuzzleantispam-bigbox");
      if (!bigBox || !bigBox[0])
         return;
      
      GCKPUZZLEANTISPAM.bigBox = bigBox[0];
      
      var targetBox = jQuery(".gckpuzzleantispam .gckpuzzleantispam-targetbox");
      if (!targetBox || !targetBox[0])
         return;
      
      GCKPUZZLEANTISPAM.targetBox = targetBox[0];
            
      var bboxSize = bytefx.$size(GCKPUZZLEANTISPAM.boundingBox);
      var sBSize = bytefx.$size(GCKPUZZLEANTISPAM.smallBox);
      
      this.oldDocMouseMove = document.onmousemove;
                
      bytefx.drag(
         GCKPUZZLEANTISPAM.smallBox,
         null,
      	null,
      	function() {
            GCKPUZZLEANTISPAM.snapToTarget();
      	},
      	{
      		$x:0,
      		x$:bboxSize.width-sBSize.width,
      		$y:0,
      		y$:bboxSize.height-sBSize.height
      	}
      );
   }
};

jQuery(document).ready(GCKPUZZLEANTISPAM.setup);