var Cookie = {

   set : function(name, value, days) {
      // Default to a 1 year cookie
      if (days == undefined) { days = 365; }

      // Format date string
      var date = new Date();
      date.setTime(date.getTime() + (days * 86400000));

      // Set cookie name, value, and expiration date
      document.cookie = name + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
   },

   get : function(name) {
      // Find the cookie's value in the document cookie string
      var results = document.cookie.match(
         new RegExp("(?:^|; )" + name + "=" + "(.*?)(?:$|;)")
      );

      // Return the value if a match was found, undefined otherwise
      if (results && results.length > 1) return results[1];
      return undefined;
   },

   clear : function(name) {
      // Erase a cookie
      setCookie(name, "", -1);
   }

};
DisplayFormat = "%%D%% %%H%% %%M%% %%S%%"; //// display format
function calcage(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
// if (s.length < 2)
 // s = "0" + s;
 return s;
}

function CountBack(secs) {
  var DisplayStr="";

  if (calcage(secs,86400,100000)>0) {
	  DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000) + ' Tage');
  } else {
	  DisplayStr = DisplayFormat.replace(/%%D%%/g, '');
  };
  
  if (calcage(secs,3600,24)>0) {
	  DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24) + ' Stunden');
  } else {
	  DisplayStr = DisplayStr.replace(/%%H%%/g, '');
  };
  
  if (calcage(secs,60,60)>0) {
	  DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60) + ' Minuten');
  } else {
	  DisplayStr = DisplayStr.replace(/%%M%%/g, '');
  };
  
  DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60) + ' Sekunden');
    OTO.layerObject.innerHTML = DisplayStr + "";
	document.getElementById('otoCounter1').innerHTML = DisplayStr + "";
    document.getElementById('otoCounter2').innerHTML = DisplayStr + "";
	document.getElementById('otoCounter3').innerHTML = DisplayStr + "";
}

var OTO = {
   

   layerObject : null,
   timer : null,

   start : function(layer) {
      
	  OTO.layerObject = document.getElementById(layer);

      var value = parseInt(OTO.layerObject.innerHTML);
      if (!isNaN(value) && value > 0) {
         OTO.seconds = value;
       CountBack(OTO.seconds);
	  }
	  
	  

      OTO.timer = setInterval("OTO.go()", 1000);
   },

   go : function() {
      OTO.seconds--;
  
CountBack(OTO.seconds);

	  
      // OTO.layerObject.innerHTML = OTO.seconds + "";

	  // Subtract 1 from seconds


      if (OTO.seconds <= 0) {
         clearInterval(OTO.timer);
         if (OTO.onEnd != null) {
            OTO.onEnd();
         }
      }
   },

   // bind this to a JavaScript function on your own
   onEnd : null

};
