/**
 * Time helper
 *
 * @author heinz
 * $Rev: 0.2
 */
 
  var L10N_WEEK_DAYS = new Array("Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag");

  /**
   * AJAX-Aufruf erfolgt alle _UPDATE_TIME Sekunden.
   * Einmal jede Minute wird ein 'erweiterter' Aufruf (Kommentare usw.) ausgefuehrt.
   *
   * Deshalb 0 < _UPDATE_TIME < 59.
   */
  var _UPDATE_TIME = 10; 

  /**
   * Formats actual time in format (EEEEE, dd.MM.yyyy, HH:mm)
   **/
  function getActualTimeString() {
    var date = new Date();
    var dateString = "";
    // add name of day 
    dateString += getLocalizedDay(date)+", ";
    // add day as day of month
    dateString += leadingZero(date.getDate(), 2)+".";
    // add month
    dateString += leadingZero(date.getMonth()+1, 2)+".";
    dateString += getYear(date.getYear())+", ";
    dateString += leadingZero(date.getHours(), 2)+":";
    dateString += leadingZero(date.getMinutes(), 2);
    return dateString;
  }
  
  function getLocalizedDay(date) {
    return L10N_WEEK_DAYS[date.getDay()];
  }

  /**
   * Adds leading zeros to given number, till length is reached
   **/  
  function leadingZero(number, length) {
    while(number.toString().length < length) {
      number="0"+number;
    }
    return number;
  }
  
  /**
   * Checks if year is greater 1900, if not 1900 will be added (necessary for Opera)
   **/
  function getYear(year) {
    if ( year < 1900 ) {
      year += 1900;
    }
    return year;
  }

  /**
   * Set couter
   **/
  function setCounter(id, hours, minutes, seconds) {
	try {
	  $("#second_"+id).html(seconds);
	  $("#minute_"+id).html(minutes);
	  $("#hour_"+id).html(hours);
	} catch(e){}
  }
  
  /**
   * Set percent value for Zack-O-Meter
   **/
  function setPercent(id, percent) {
    var position = Math.floor(percent*(330/100));
    var positionSmall = Math.floor(percent*(210/100));
    ( percent == 100 ) ? $(".zack_"+id).addClass("p100") : $(".zack_"+id).removeClass("p100");
    $("#percent_"+id).html(""+percent);
    $("#detailContainer .percentPosition_"+id).css("left", position+"px");
    $(".highlight .percentPosition_"+id).css("left", positionSmall+"px");
    ( percent == 0 ) ? $(".zack_"+id).addClass("p0") : $(".zack_"+id).removeClass("p0");
  }
  
  /**
   * Update all timers
   **/
  function updateTimer(url, ids, counterItemId) {
    for (i=0; i<ids.length; i++) {
      if (ids[i] != 0) {
	    var _second = Math.floor($("#second_"+ids[i]).html())-1;
	    if ( _second < 0 ) {
	      _second = 59;
	      // change minute
          var _minute = Math.floor($("#minute_"+ids[i]).html())-1;
	      if ( _minute < 0 ) {
	        _minute = 59;
	        // change hour
	        var _hour = Math.floor($("#hour_"+ids[i]).html())-1;
	        if ( _hour < 0 ) {
	          _hour = 0;
	          _second = 0;
	          _minute = 0;
	          ended(ids[i],"soldOutEnded", counterItemId);
	        } 
	        $("#hour_"+ids[i]).html(leadingZero(_hour,2));
	      }
	      $("#minute_"+ids[i]).html(leadingZero(_minute,2));
        }
        $("#second_"+ids[i]).html(leadingZero(_second,2));
	  }
    }
    times++;
    if ( times % _UPDATE_TIME == 0 ) {
      times = 0;
      if (_second > (59 - _UPDATE_TIME)) {
        (url.indexOf("?") == -1) ? url += "?" : url += "&";
        url += "cmd=getCount&counterItemId="+counterItemId;
      }
      $.getScript(url);
    }
  }
  
  function itemEnded(id) {
    ended(id,"soldOutEnded");
  }
  
  function ended(id,cssClass, counterItemId) {
    $(".zack_"+id).addClass(cssClass);
    if ( counterItemId== null || id == counterItemId ) {
      $("#timeInfoCounterContainer").hide();
    }
  }
  
  function updateTime(url, ids, counterItemId) {
    $("#actualDate").html(getActualTimeString());
    updateTimer(url, ids, counterItemId);
  }
  
  /**
   * Helper Methods, to reduce eval script traffic.
   */
  
  function ie(id) {
    itemEnded(id);
  }
  
  function sp(id, percent) {
    setPercent(id, percent);
  }
  
  function sc(id, hours, minutes, seconds) {
    setCounter(id, hours, minutes, seconds);
  }
  
  function rc(count) {
    $('.theRatingsCount').html(count);
  }
  
  function ce(key) {
    $("#checkoutEntry_"+key).addClass("timedOut");
  }