// Projekt: Nordstadtateliers 08              */
// (C) 05/2008 Heil-Computertechnik, Dortmund */
//     Alle Rechte vorbehalten                */


      function getRandom(min, max) {
        if (min > max) {
          return(-1);
        }
        if (min == max) {
          return (min);
        }
        return min + parseInt(Math.random() * (max-min+1))
      }

      Function.prototype.Timer = function (interval, calls, onend) {
        var count = 0;
        var payloadFunction = this;
        var startTime = new Date();
        var callbackFunction = function () {
          return payloadFunction(startTime, count);
        };
        var endFunction = function () {
          if (onend) {
            onend(startTime, count, calls);
          }
        };
        var timerFunction =  function () {
          count++;
          if (count < calls && callbackFunction() != false) {
            window.setTimeout(timerFunction, interval);
          } else {
            endFunction();
          }
        };
        timerFunction();
      };

      function leadingzero (number) {
          return (number < 10) ? '0' + number : number;
      }

      function callreadjustcontent() {
        readjustcontent('content');
        return state('resized'); }

      function eventstate(val) {
        readjustcontent("content");
        return state(val); }

      function state (val) {
        var oldval;
        if (!state.val) {
          oldval = 'resized'; 
          state.val = 'resized'; }
        else {
          oldval = state.val; }
        if (!val) {} else {
          state.val = val; }
        return oldval;
      }
      function readjustcontent (elementId) {
        var element;
        var elementHeight, wrkHeight, elementTop;
        element=document.getElementById(elementId);
        elementTop=element.offsetTop;
        elementHeight=element.offsetHeight;
        if (!readjustcontent.initialTop) {
          readjustcontent.initialTop=elementTop;
          readjustcontent.initialHeight=elementHeight;
          element.style.height=(elementHeight-20)+'px'; }
        // alert(element.id+' ('+element.offsetLeft+'/'+elementTop+')'+' overflow:'+element.style.overflow+' height:'+elementHeight);
        if (elementTop<8) {
          if (!element.style.overflow=='auto') {
            element.style.overflow='auto';}
          wrkHeight=(elementHeight-20+elementTop);
          // alert('wrkHeight: '+wrkHeight);
          if (wrkHeight>32) {
            element.style.height=(wrkHeight-8)+'px'; } 
          else {
            element.style.height='32px';} }
        else {
          // alert(elementHeight+'<'+readjustcontent.initialHeight+'?');
          if (elementHeight<readjustcontent.initialHeight) {
            // alert('yep');
            wrkHeight=(elementHeight-20+elementTop);
            element.style.height=(wrkHeight-8)+'px';
            if (!element.style.overflow=='auto') {
              element.style.overflow='auto';}}
          else {
            element.style.height=(readjustcontent.initialHeight-20)+'px';
            if (!element.style.overflow=='hidden') {
              element.style.overflow='hidden';}}}
      }

      function togglepictures (seconds, targetId, maxIndex) {
        // var element = document.getElementById(targetId);
        var calculateAndShow = function () {
          if (seconds > 0) {
            var h = Math.floor(seconds / 3600);
            var m = Math.floor((seconds % 3600) / 60);
            var s = seconds % 60;
            seconds--;
            if (!togglepictures.maxvisible) { 
              readjustcontent("content");
              state('resized'); }
            if (state()=='resized') { 
              togglepictures.maxvisible=1;
              var top;
              var index=1;
              top=document.getElementById(targetId+index).offsetTop; 
              while (index<maxIndex) {
                index++;
                if (top == document.getElementById(targetId+index).offsetTop) {
                  togglepictures.maxvisible++; }
                else { 
                  index=maxIndex; } 
               }
            // alert(togglepictures.maxvisible); 
              state('not resized'); }

         // 2 Bilder tauschen zwischen sichtbaren und unsichtbaren Elementen   
            var tg1 = document.getElementById(targetId+getRandom(1,togglepictures.maxvisible));
            // alert(tg1.id+' ('+tg1.offsetLeft+'/'+tg1.offsetTop+')');
            var tg2 = document.getElementById(targetId+getRandom(togglepictures.maxvisible+1,maxIndex));
            // alert(tg2.id+' ('+tg2.offsetLeft+'/'+tg2.offsetTop+')');
            srcbuffer=tg1.src;
            altbuffer=tg1.alt;
            tg1.src=tg2.src;
            tg1.alt=tg2.alt;
            tg2.src=srcbuffer;
            tg2.alt=altbuffer;
          } else {
            return false;
          }
        };
        var completed = function () {
//          alert('ready');
        };
        calculateAndShow.Timer(2000, Infinity, completed);
      }

      function CountDown() {
        var eventdate = new Date("October 30, 2010 00:00:00 GMT");
        d=new Date();
        count=Math.floor((eventdate.getTime()-d.getTime())/1000);
        count=Math.floor(count/(60*60*24)+1);
        if (count > 1) {
          document.write('Noch '+count+' Tage bis zum Start der NordstadtAteliers 2010. Näheres unter <span ><a class="pfooterlink" href="http://www.offenenordstadtateliers.de" target="_blank">http://www.offenenordstadtateliers.de</a>.</span>')};
        if (count == 1) {
          document.write('Noch '+count+' Tag bis zum Start der NordstadtAteliers 2010. Näheres unter <span ><a class="pfooterlink" href="http://www.offenenordstadtateliers.de" target="_blank">http://www.offenenordstadtateliers.de</a>.</span>')};
        if (count <= 0 && count >= -1) {
          document.write('Es ist soweit. Näheres unter <span ><a class="pfooterlink" href="http://www.offenenordstadtateliers.de" target="_blank">http://www.offenenordstadtateliers.de</a>.</span>')};
        if (count < -1) {
          document.write('Wir freuen uns auf ein Wiedersehen beim nächsten Mal.')};
      }

