var Countdown = {

	initializeForLotItems: function()
	{
		$("input.date-end").each(Countdown.initializeCountdownForLotItem);
	},

	initializeCountdownForLotItem:function(idx, element)
	{
		var _id = element.id.split("_")[3];
		if(!_id){return;}
		Countdown.add({
			id: _id,
			update_progress_left: ("#lot_progress_left_" + _id),
			update_progress: ("#lot_progress_" + _id),
			date_start: $("#lot_date_start_" + _id).attr('value'),
			date_end: $("#lot_date_end_" + _id).attr('value'),
			update_price: "#lot_price_update_" + _id,
			price_step:   $("#lot_price_step_" + _id).attr('value'),
			price_start:  $("#lot_price_start_" + _id).attr('value'),
			price_end:    $("#lot_price_end_" + _id).attr('value'),
			state:    $("#lot_state_" + _id).attr('value'),
			round: 2
		});
	},

	add: function(options)
	{
		options.update_progress_left = $(options.update_progress_left);
		options.update_progress = $(options.update_progress);
		options.date_start = new Date(options.date_start * 1000);
		options.date_end = new Date(options.date_end * 1000);
		options.update_price = $(options.update_price);
		if(options.state != 'sold'){
			Countdown.update(options);
		}
	},

  update: function(options){
    var stateHash = Countdown.getCurrentStateHash(options);
    var countdown_element_pattern = '#lot_' + stateHash.next_state + '_time_' + options.id;
    var countdown_until = stateHash.until;
    Countdown.updateLotElement('#lot_' + options.id, options, null, stateHash);
    Countdown.updateLotElement('.lot_' + options.id, options, null, stateHash);
    if(countdown_element_pattern && countdown_until){
      $(countdown_element_pattern).countdown({
          until: countdown_until,
          onExpiry:  function() {
						Cowndown.onLotStateExpiry(options, stateHash);
					},
          onTick: function() {
						Countdown.onLotTick(options, stateHash);
					},
          layout: '{h10}{h1}:{m10}{m1}:{s10}{s1}'
      });
    }
  },

  onLotStateExpiry: function(options, stateHash){
    if(options.reload_page_on_state_expiry){
      history.go(0);
    }else{
      Countdown.updateLotElement('#lot_' + options.id, options, stateHash, null);
      Countdown.updateLotElement('.lot_' + options.id, options, stateHash, null);
      Countdown.update(options);
    }
  },

  updateLotElement:function(element_id, options, stateHash, newStateHash){
    var element = $(element_id);
    if(element && (element.length > 0) && stateHash && stateHash.color){
      element.removeClass(stateHash.color);
    }
    if(element && (element.length > 0) && newStateHash && newStateHash.color){
      element.addClass(newStateHash.color);
    }
  },

  onLotTick: function(options, stateHash){
    var countdown_element_pattern = '#lot_' + stateHash.next_state + '_time_' + options.id;
    Countdown.update_price(options, stateHash);
    Countdown.update_progress(options, stateHash);
    Countdown.update_progress_left(options, stateHash);
    Countdown.update_time(countdown_element_pattern, stateHash);
  },
	
	update_time: function(element_pattern, options)
	{
		//window.console.log('update_time');
    var value = $(element_pattern).html();
    var diff_in_milliseconds = (new Date() - options.until);
    var date_hash = ItemDate.secondsToTimeHash(diff_in_milliseconds / 1000);
    var days = 0 - (date_hash.days + 1);
    var days_string = (days < 1) ? '' : ('' + days + ' ' + ItemDate.wordsListHashRus.days[ItemDate.numberKindRus(days)]);
    $(element_pattern).html( days_string + ' ' + value);
	},

  getCurrentStateHash: function(options){
    var current_time = new Date();
    var date_start = options.date_start;
    var startDateHash = ItemDate.getRightTimeHash(date_start);
    var date_fall = ItemDate.dateObjectFromTimeHash(ItemDate.sumTwoTimeHash(startDateHash, ItemDutch.startStateDuration));
    var date_end = options.date_end;
    var endDateHash = ItemDate.getRightTimeHash(date_end);
    var date_close = ItemDate.dateObjectFromTimeHash(ItemDate.subTimeHash(endDateHash, ItemDutch.closeStateDuration));
		var current_progress = 0;
    var current_state = null;
    var current_color = null;
    var date_until = null;
    var lot_next_state = null;
    if(current_time < date_start){
		 current_progress = 100.0 * 0;
     current_state = 'active';
     current_color = 'green';
     date_until = date_start;
     lot_next_state = 'start';
    }else if(current_time < date_fall){
		 current_progress = 100.0 * (current_time - date_start) / (date_fall - date_start);
     current_state = 'start';
     current_color = 'green';
     date_until = date_fall;
     lot_next_state = 'fall';
    }else if(current_time < date_close){
		 current_progress = 100.0 * (current_time - date_fall) / (date_close - date_fall);
     current_state = 'fall';
     current_color = 'yellow';
     date_until = date_close;
     lot_next_state = 'close';
    }else if(current_time < date_end){
		 current_progress = 100.0 * (current_time - date_close) / (date_end - date_close);
     current_state = 'close';
     current_color = 'red';
     date_until = date_end;
     lot_next_state = 'end';
    }else {
		 current_progress = 100 * 1;
     current_state = 'end';
     current_color = 'grey';
    }
    return {
      now: current_time,
      state: current_state,
      color: current_color,
      until: date_until,
      next_state: lot_next_state,
			progress: parseInt(current_progress),
      start: date_start,
      fall: date_fall,
      close: date_close,
      end: date_end
    };
  },

	update_price: function(options, stateHash)
	{
    var current_time = new Date();
		if (options.update_price)
		{
			var current = (current_time <= stateHash.fall) ? options.price_start :
				(current_time >= stateHash.close) ? options.price_end :
				(options.price_start - parseInt((current_time - stateHash.fall)/1000) * options.price_step);
			var current_number = new Number(current);
			var rounded = current_number.toFixed(options.round);
			options.update_price.attr('value', rounded);
			options.update_price.text(rounded);	
		}
	},

	update_progress: function(options, stateHash)
	{
		if (options.update_progress)
		{
			$(options.update_progress).css("width", stateHash.progress + "%");
		}
	},

	update_progress_left: function(options, stateHash)
	{
		if (options.update_progress_left)
		{
			$(options.update_progress_left).css("width", (100 - stateHash.progress) + "%");
		}
	}
	
};