
/************************************************************
 * Watching Script v1.4                                     *
 *  by KnoedelDealer (http://knoedeldealer.de/) 2008/03/26  *
 *                                                          *
 *  licensed under the creative commons by-sa 3.0           *
 *  http://creativecommons.org/licenses/by-sa/3.0/          *
 ************************************************************/


/************************************************************
 * CONFIGURATION:                                           *
 ************************************************************/

// how often the head is shown, 0 = endless
var repeat = 3;

// URL linked by the image
var url = 'http://knoedeldealer.de/howto.html';



/************************************************************
 * CODE: (do not edit if you don't know what you're doing!) *
 ************************************************************/

var h = -150;
var h2 = 1;
var l = 100;
var ie = document.all && !window.opera;
var ie7 = (navigator.userAgent.indexOf('MSIE 7.0') > -1);
var count = 0;
var stop = false;

function watching() {
   if (document.cookie.indexOf('watching=false') > -1 || stop) return;
   var body = document.getElementsByTagName('body')[0];
   l = (body.offsetWidth - 140) * Math.random();
   var img = document.createElement('img');
   img.id = 'imgWatching';
   img.src = 'head.gif';
   img.title = 'i\'m watching you!';
   img.style.display = 'none';
   img.style.cursor = 'pointer';
   img.onclick = function() { document.location.href = url; }
   body.appendChild(img);
   if (!ie || ie7) {
      img.style.position = 'fixed';
   } else {
      img.style.position = 'absolute';
   }
   img.style.bottom = h + 'px';
   img.style.left = l + 'px';
   h2 = Math.random() / 3;
   window.setTimeout('moveUp();', 10000); /* Dauer bis zum ersten Schauen */
}

function moveUp() {
   if (document.getElementById('imgWatching')) {
      var img = document.getElementById('imgWatching');
      if (stop) {
         img.style.display = 'none';
         return;
      }
      if (img.style.display == 'none') {
         img.style.display = 'inline';
      }
      if (h < (0 - img.height * h2)) {
         h = h + 10;
         img.style.bottom = h + 'px';
         window.setTimeout('moveUp();', 50);
      } else {
         window.setTimeout('moveDown();', 2000); /* Dauer, die der Kopp oben is */
      }
   }
}

function moveDown() {
   if (document.getElementById('imgWatching') && (document.getElementsByTagName('body').length > 0)) {
      var body = document.getElementsByTagName('body')[0];
      var img = document.getElementById('imgWatching');
      if (h > (0 - img.height)) {
         h = h - 10;
         img.style.bottom = h + 'px';
         window.setTimeout('moveDown();', 50);
      } else {
         img.style.display = 'none';
         l = (body.offsetWidth - 140) * Math.random();
         img.style.left = l + 'px';
         h2 = Math.random() / 3;
         count++;
         if (count < repeat) {
         	window.setTimeout('moveUp();', 50000 * Math.random());
         }
      }
   }
}

function stopWatching() {
	if (document.cookie.indexOf('watching=false') > -1) {
		setWatchingCookie('true');
		stop = false;
	} else {
		setWatchingCookie('false');
		stop = true;
	}
	watching();
}

function setWatchingCookie(active) {
	var expiration = new Date();
	expiration.setTime(expiration.getTime() + (7 * 24 * 3600000));
	document.cookie = 'watching=' + active + '; expires=' + expiration.toGMTString();
}

window.onload = watching;
