/* <![CDATA[ */
//var http_request = null;
//var running = 0;

var _ct_counter_time = 0;
var _ct_counter_winner_id = 0;
var _ct_counter_winner_name = '';
var _ct_counter_price = 0;
var _ct_counter_status = 0;
var _ct_counter_status_changed = 0;

var _update_interval = 1000;
var _start_time = 0
var _cached_user_id = 0;

var _intv = '';
var _intv_index = '';
var _lock_ct_keys = Object();
var js_cd_speed_tics = new Object();
var _running_brw = new Object();
var _bid_history = new Array();
var _own_bid_history = new Array();
var _index_counter_prices = new Object();
var _bid_out_types = new Array();
var _wording_single_bid = 'Single bid';
var _wording_bid_agent = 'BidHelper';
var _wording_bid_phone = 'Telephone';
var _last_update_ct_time = 0;
var _cds_image_deleted = 0;
_bid_out_types[1] = _wording_single_bid;
_bid_out_types[2] = _wording_bid_agent;
_bid_out_types[3] = _wording_bid_agent;
_bid_out_types[4] = _wording_bid_agent;
_bid_out_types[5] = _wording_bid_agent;
_bid_out_types[6] = _wording_bid_phone;

var _counter_reset = 40;
var _default_bid_increment = 10;

var _auction_status_future = 30;
var _auction_status_future_wo_startdate = 31;
var _auction_status_finished = 20;
var _auction_status_paused = 10;
var _auction_status_live = 1;
var _overwrite_timeout = 1000;
var _overwrite_timeout_index = 1000;

var _counter_server_prefix = 'https://www.1bidshop.com/';

var _single_auction_verify = '';
var _single_auction_verify_id = 0;
var _multi_auction_verify = '';
var _multi_auction_verify_id = '';
var _last_bid_placed = 0;

/*
var _future_counter_running = 0;
var _future_counter_time = 0;
var _future_counter_changed = 0;
*/

var _last_bid_key = -1;

var _bid_history_blocked = 0;

var _counter_changed = 0

var _detail_request_running = 0;

var _last_action_div_displayed = 0;
var _auction_detail_last_update = 0;
var _index_auctions_js_last_update = 0;


var prefix;
var loc = new String(window.parent.document.location); 
if (loc.indexOf("https://")!= -1) 
	prefix = "https://"; 
else 
	prefix = "http://"; 


/**
 * void auction_detail_js(int, int, int)
 *   - init counter requests on detail pages
 */
function auction_detail_js(auction_id, auction_status, start_time, user_id, verify) {
 if (user_id == undefined || user_id == null) {
    user_id = 0;
  } else {
    if (user_id != 0) {
      _cached_user_id = user_id;
    }
  }

  if (verify == undefined || verify == null) {
    verify = '';
  }

  if (_cached_user_id != 0) {
    user_id = _cached_user_id;
  }

  if (auction_status == _auction_status_finished) {
    if (_intv != '' || _intv != null) {
		
      window.clearInterval(_intv);
    }
    return;
  }

  if (start_time > 0) {
    _start_time = start_time;
  }

  if (_lock_ct_keys['detail'] == undefined || _lock_ct_keys['detail'] == null) {
    _lock_ct_keys['detail'] = 0;
  }

  // 1. get counter data
  if (_lock_ct_keys['detail'] == 0) {  
    _auction_detail_last_update = (new Date()).getTime();
    _lock_ct_keys['detail'] = 1; 
	
	do_counter_request(auction_id, user_id, 'detail', verify);
  } else {
    if (((new Date()).getTime()-_auction_detail_last_update)>3000) {
       _lock_ct_keys['detail'] = 0;
    }
  }

  // 2. process auction status
  get_interval_by_status(auction_id);
  // 3. check_status_code
  check_status_code();

  // update start_time
  _start_time -= (_update_interval/1000);

  _single_auction_verify = verify;
  _single_auction_verify_id = auction_id;

  
  if (_overwrite_timeout != "") {
    _update_interval -= parseInt(_overwrite_timeout);
    if (_update_interval <= 250) {
      _update_interval = 1000;
    } else {
      _update_interval += 30;
    }
  }

  if (_intv != '' || _intv != null) {
    window.clearInterval(_intv);
  }
  _intv = setInterval('auction_detail_js(' + auction_id + ', ' + _ct_counter_status + ', ' + _start_time + ', ' + user_id + ', "' + verify + '")', _update_interval);
}

/**
 * void auction_index_js(int, int, int)
 *   - init counter requests on index pages
 */
function auction_index_js(auction_ids, user_id, verify) {
  if (auction_ids == '' || auction_ids == null) {
    return;
  }

  if (user_id == undefined || user_id == null) {
    user_id = 0;
  }

  if (verify == undefined || verify == null) {
    verify = '';
  }


  if (_lock_ct_keys['index'] == undefined || _lock_ct_keys['index'] == null) {
    _lock_ct_keys['index'] = 0;
  }

  if (_lock_ct_keys['index'] == 0) {
    _multi_auction_verify = verify;
    _multi_auction_verify_id = auction_ids;
    do_counter_request_index(auction_ids, user_id, 'index', verify);
    _index_auctions_js_last_update =  (new Date()).getTime();
  } else {
    if (((new Date()).getTime()-_index_auctions_js_last_update)>3000) {
       _lock_ct_keys['index'] = 0;
    }
  }



  update_interval_index = 1000;
  if (parseInt(_overwrite_timeout_index) > 500) {
    update_interval_index = _overwrite_timeout_index;
  }
  if (_intv_index != '') {
    window.clearTimeout(_intv_index);
  } 
 
  _intv_index = setTimeout('auction_index_js("' + auction_ids + '", "' + user_id + '", "' + verify + '")', update_interval_index);
}


function parse_counter_response_index(transport, plain) {
	
  if (plain != undefined && plain != null && plain == 1) {
    counters = transport.split('#');
  } else {
    counters = transport.responseText.split('#');
  }
  
  for (i = 0; i <= counters.length; i++) {
    if (counters[i] == null) continue;
    counter_data = counters[i].split(':');
    auction_id = counter_data[0];
    div_name_counter = 'counter_index_page_' + auction_id;
    div_price_name = 'price_index_page_' + auction_id;
    div_name_winner = 'winner_index_page_' + auction_id;
    div_button_name = 'button_index_page_' + auction_id;
	final_count_down_msg = 'final_count_down_msg_' + auction_id;
    div_button_name_finished = 'button_finished_index_page_' + auction_id;
	adminbid_index = 'adminbids_' +auction_id;
	admincredit_index = 'admincredits_' +auction_id;
	
	//alert(counter_data[1]);
	
    counter = counter_data[1].split('|');
    for (ii = 0; ii < counter.length; ii++) {
      data = counter[ii].split('=');
      if (data[0] == 'ct') {
        if (data[1] == '-') {
          _ct_time = '--:--:--';
        } else {
          _ct_time = calc_counter_from_time(data[1]);
        }
		$(final_count_down_msg).innerHTML = '';
        if ($(div_name_counter)) {
          $(div_name_counter).innerHTML = _ct_time;
          if (parseInt(data[1]) <= 10 && parseInt(data[1]) > 0) {
            $(div_name_counter).style.color = '#DD0000';
          } else {
            $(div_name_counter).style.color = '';
          }
        }
      } else if (data[0] == 'cs') {
		if (data[1] == 5) {
          $(div_name_counter).style.color = '#DD0000';
        }else if (data[1] == 2) {
          $(div_name_counter).style.color = '#DD0000';
           $(final_count_down_msg).style.color = '#DD0000';
 		$(final_count_down_msg).innerHTML = 'Final Countdown';
		}else if (data[1] == _auction_status_paused) {
          _cur_time = new Date();
          _cur_secs = (_cur_time.getTime()/1000);
          if (_cur_secs%4 <= 2 && $(div_name_counter)) {
            $(div_name_counter).innerHTML = 'Pause';
          }
        } else if (data[1] == _auction_status_finished) {
          // REMOVE FROM PULL TODO
          hidedisplay_show(div_button_name_finished);
          hidedisplay_dis(div_button_name);
          if ($(div_name_counter)) {
            $(div_name_counter).innerHTML = 'Ended';
          }
        } 
      } else if (data[0] == 'cw' && data[1] != '') {
        el = $(div_name_winner);
        if (el) {
          el.innerHTML = data[1];
        }
      } else if (data[0] == 'ab' && data[1] != '') {
		
      if ($(adminbid_index)) {
        $(adminbid_index).innerHTML = data[1];

      } 
	  
	 } else if (data[0] == 'cd' && data[1] != '') {
		
      if ($(admincredit_index)) {
        $(admincredit_index).innerHTML = data[1];

      } 
	  
    }else if (data[0] == 'cp') {
        if ($(div_price_name)) {
          $(div_price_name).innerHTML = format_raw_to_price(data[1]);
        }
        if (_index_counter_prices[auction_id] == null
            || _index_counter_prices[auction_id] == '') {
          _index_counter_prices[auction_id] = data[1];
        } else {
          if (_index_counter_prices[auction_id] != data[1]) {
            new Effect.Highlight(div_price_name, {duration:1,startcolor:'#ff0000', endcolor:'#FFFFFF', restorecolor:'#FFFFFF'});
            _index_counter_prices[auction_id] = data[1];
          }
        }
      }
    }
  }
}

var mlastNow = Object();


function refreshDetails(mUpdateURL, script_id) {
  if (mlastNow[script_id] == null || mlastNow[script_id] == undefined) {
    mlastNow[script_id] = 0;
  }

  if (((new Date()).getTime()-mlastNow[script_id])<300) return;

  mlastNow[script_id] = (new Date()).getTime();
  var script = document.createElement('script');

  script.type = 'text/javascript';
  script.src = mUpdateURL + "&now="+(new Date()).getTime();
  script.id = script_id + '_refresh_js' + '2';

  // remove old script-node (if there is one..)
  el = document.getElementById(script.id);
  if (el) {
    document.getElementsByTagName('head')[0].removeChild( el );
  }

  // set new script node
  document.getElementsByTagName('head')[0].appendChild(script);
}


var single_updates = new Object();
function force_single_bid_update(auction_id, user_id) {
  if (single_updates[auction_id] == undefined || single_updates[auction_id] == null) {
    single_updates[auction_id] = 0;
  }

  if (single_updates[auction_id] == 1) {
    return;
  }

  script_id = auction_id + '_single';

  url = _counter_server_prefix + '/counter/' + auction_id + '.html?plain=1&ext=1';
  if (user_id != undefined && user_id != null && user_id != 0) {
    url += '&uid=' + user_id;
  }

  refreshDetails(url, script_id);
}



/**
 * void do_counter_request(int)
 *   - do counter request and call success function
 */
function do_counter_request(auction_id, user_id, lock_key, verify) {
	
  url = _counter_server_prefix + '/updater_mrd.php?auction_id=' + auction_id + '&plain=1';
  //alert(url);
  if (user_id != undefined && user_id != null && user_id != 0) {
    url += '&uid=' + user_id;
  }
  
  if (verify != undefined && verify != null) {
    url += '&val=' + verify;
  }

  if (lock_key == undefined || lock_key == null || lock_key == 0) {
    lock_key = 0;
  }

  if (_last_bid_key > -1) {
    url += '&lbp=' + _last_bid_key;
  }
  
  refreshDetails(url, lock_key);

  /*options = {
    onSuccess: function(transport) {
		
                  parse_counter_response(transport, auction_id);
                }
  }
 
  if (lock_key != 0) {
    if (_lock_ct_keys[lock_key] == undefined || _lock_ct_keys[lock_key] == null || _lock_ct_keys[lock_key] == 0) {
      _lock_ct_keys[lock_key] = 1;
    }
    options = {
      onSuccess: function(transport) {
		  //change_browse_page("countertime", "ajexphp/counter.php?auction_id="+auction_id); //vikas
		  //alert(transport);
                    parse_counter_response(transport, auction_id);
                  },
      onComplete: function(transport) {
                    _lock_ct_keys[lock_key] = 0;
                  }
    }
  }

  new Ajax.Request(url, options);*/

  
}


function _ev_detail(transport, auction_id, rt, restet_counter_time) {
	//alert(transport);
  if (restet_counter_time != null && restet_counter_time != undefined
      && restet_counter_time != '' && restet_counter_time != 0) {
    //_overwrite_timeout = restet_counter_time;
    _overwrite_timeout = (1000-restet_counter_time);
  }
 
  //alert(rt);
 // $('CDUMST_IMG').style.color = '#000000';
 //$('CDUMST_IMG').innerHTML = rtt;
 //alert(rt);
  parse_counter_response(transport, auction_id, 1);

//  if ($('error_debug3')) {
//    $('error_debug3').innerHTML = "rct: " + restet_counter_time;
//  }
  _lock_ct_keys['detail'] = 0;
  _ev_reset_time(rt);
}
function _ev_reset_time(restet_time) {
  //alert(rt);
  $('CDUMST_IMG').style.color = '#FFFFFF';
   $('CDUMST_IMG').innerHTML = '<p style="padding-top:5px;padding-left:1px;font: Trebuchet MS; font-size:16px;  color:#FFFFFF; font-weight:bold;"> '+ restet_time+ '</p>';
 //alert(restet_time);
  //parse_counter_response(transport, auction_id, 1);

//  if ($('error_debug3')) {
//    $('error_debug3').innerHTML = "rct: " + restet_counter_time;
//  }
  //_lock_ct_keys['detail'] = 0;
}

function _ev_index(transport, auction_id, restet_counter_time) {
	//alert(transport);
  if (restet_counter_time != null && restet_counter_time != undefined
      && restet_counter_time != '' && restet_counter_time != 0) {
    _overwrite_timeout_index = (1000-restet_counter_time);
    //_overwrite_timeout_index = (restet_counter_time-1000);
  }
 
  parse_counter_response_index(transport, 1);
  _lock_ct_keys['index'] = 0;
}

function _ev_ext(transport, auction_id, restet_counter_time) {
  if (restet_counter_time != null && restet_counter_time != undefined
      && restet_counter_time != '' && restet_counter_time != 0) {
    _overwrite_timeout_index = restet_counter_time;
  }

  parse_counter_response_ext(transport, auction_id);
}


function do_counter_request_index(auction_ids, user_id, lock_key, verify) {
  if (auction_ids == '' || auction_ids == null) {
    return;
  }

  url = _counter_server_prefix + '/updater_mrd.php?aids=' + auction_ids;
  if (user_id != undefined && user_id != null && user_id != 0) {
    url += '&uid=' + user_id;
  }
  if (verify != undefined && verify != null) {
    url += '&val=' + verify;
  }
  if (lock_key == undefined || lock_key == null || lock_key == 0) {
    lock_key = 0;
  }
  if (lock_key != 0) {
    if (_lock_ct_keys[lock_key] == undefined || _lock_ct_keys[lock_key] == null || _lock_ct_keys[lock_key] == 0) {
      _lock_ct_keys[lock_key] = 1;

      refreshDetails(url, 'index'); 
    }
  }



//alert(auction_id);
/*
  options = {
    onSuccess: function(transport) {
                  parse_counter_response_index(transport);
                }
  }

  if (lock_key != 0) {
    if (_lock_ct_keys[lock_key] == undefined || _lock_ct_keys[lock_key] == null || _lock_ct_keys[lock_key] == 0) {
      _lock_ct_keys[lock_key] = 1;
    }
    options = {
      onSuccess: function(transport) {
                    parse_counter_response_index(transport);
                  },
      onComplete: function(transport) {
                    _lock_ct_keys[lock_key] = 0;
                  }
    }
  }

  new Ajax.Request(url, options);
*/
}


/**
 * void check_status_code(void)
 *   - get status code specific HTML
 */
function check_status_code() {
  if (_ct_counter_status == _auction_status_finished) {
    if ($('gebotsbutton')) {
      $('gebotsbutton').innerHTML = '<strong>Auction ended!</strong>';
    }
    if ($('bietbutler')) {
      $('bietbutler').innerHTML = '';
    }
    window.location.reload();
  }
}



/**
 * void get_future_auction_counter(int, int)
 *   - display future auction counter
 */
/*
function get_future_auction_counter(auction_id, start_time) {
  if (_future_counter_time > 0 && _future_counter_changed == 1) {
    start_time = _future_counter_time;
    _future_counter_changed = 0;
  }
  if (start_time < 0 || _ct_counter_status != _auction_status_future) {
    return;
  }

  time_string = calc_counter_from_time(start_time);

  $('countertime').innerHTML = time_string;

  _intv2 = setTimeout('get_future_auction_counter(' + auction_id + ', ' + --start_time + ')', 1000);
}
*/


/**
 * void get_interval_by_status(int)
 *   - check for counter status and adapt update interval
 */
function get_interval_by_status(auction_id) {

  // set interval
  if (_ct_counter_status_changed == 1
      || _ct_counter_status == _auction_status_future) {
    if (_ct_counter_status != 0) {
      if (_ct_counter_status == _auction_status_future) {
        // future auction
        _update_interval = 1000;
        if (_start_time > 0) {
          _update_interval = (_start_time*1000);
          if (_start_time < 10) {
            _update_interval = 1000;
          } else if (_update_interval > 5000 || _update_interval < 0) {
            _update_interval = 1000;
          }
        }
      } else if (_ct_counter_status == _auction_status_paused) {
        _update_interval = 2000;
      } else if (_ct_counter_status == _auction_status_live) {
        _update_interval = 1000;
      } 
    }


    if (_ct_counter_status_changed == 1) {
      //alert('status changed - ' + _update_interval + ' - TODO print it');
    }

    if (_ct_counter_status == _auction_status_live
        || _ct_counter_status == _auction_status_future) {

      if (parseInt(_overwrite_timeout) > 500) {
        _update_interval = _overwrite_timeout;
      }
    }

    _ct_counter_status_changed = 0;
    //alert('status changed - ' + _update_interval);
    //auction_detail_js(auction_id, _ct_counter_status, 0);
  }
}



function parse_counter_response_ext(transport, auction_id) {
  if (transport == undefined || transport == null) {
    return;
  }
  
  result_array = transport.split("|");

  div_counter_name = 'countertime';  
  div_winner = 'a_current_winner';
  div_price_name = 'a_current_price';

  div_counter_name_index = 'counter_index_page_' + auction_id;
  div_price_index = 'price_index_page_' + auction_id;
  div_winner_index = 'winner_index_page_' + auction_id;
	

  for (i = 0; i < result_array.length; i++) {
    if ('' == result_array[i] || null == result_array[i]) {
      continue;
    }
	
    data = result_array[i].split("=");
    if (data.length < 2) {
      continue;
    }
    if (data[0] == 'ct' && data[1] != '') {
      _ct_counter_time = data[1];
      if (_ct_counter_time != undefined && _ct_counter_time != null && _ct_counter_time != '') {
        _ct_counter_time = calc_counter_from_time(_ct_counter_time);
      } else {
        _ct_counter_time = '';
      }
      if (_ct_counter_time != '' && _ct_counter_time != '-') {
        if ($(div_counter_name)) {
          $(div_counter_name).innerHTML = _ct_counter_time;
        } else if ($(div_counter_name_index)) {
          $(div_counter_name_index).innerHTML = _ct_counter_time;
        }
      }
    }  else if (data[0] == 'cw' && data[1] != '') {
      if ($(div_winner_index)) {
        $(div_winner_index).innerHTML = data[1];
      } else if ($(div_winner)) {
        $(div_winner).innerHTML = data[1];
      }
    }  else if (data[0] == 'cp' && data[1] != '') {
		
      if ($(div_price_index)) {
        $(div_price_index).innerHTML = data[1];
//        new Effect.Highlight(div_price_index, {duration:1,startcolor:'#ff0000',endcolor:'#ffffff',restorecolor:'#FFFFFF'});
      } else if ($(div_price_name)) {
		
        $(div_price_name).innerHTML = data[1];
		
//        new Effect.Highlight(div_price_name, {duration:1,startcolor:'#ff0000',endcolor:'#ffffff',restorecolor:'#FFFFFF'});
      }
    }
  }
  return;
}



/**
 * void parse_counter_response(object int)
 *   - parse request from counter server and adapt html
 */
function parse_counter_response(transport, auction_id, plain) {
	//alert(transport);
	update_savings_details(auction_id);
  if (plain != undefined && plain != null && plain == 1) {
    result_array = transport.split("|");
  } else {
    result_array = transport.responseText.split("|");
  }

  div_counter_name = 'countertime';

  for (i = 0; i < result_array.length; i++) {
    if ('' == result_array[i] || null == result_array[i]) {
      continue;
    }
    data = result_array[i].split("=");
    if (data.length < 2) {
      continue;
    }

    if (_last_bid_key != null) {
      if (js_cd_speed_tics != undefined && js_cd_speed_tics != null) {
        _last_cs_speed_matched = 0;
        _current_speed = 20;
        for (var ii in js_cd_speed_tics) {
          if (_last_cs_speed_matched == 1) {
            _update_counter_speed_image(ii, js_cd_speed_tics[ii], _current_speed);  //Vikas
            _last_cs_speed_matched = 0;
          }
          if (parseInt(_last_bid_key) >= parseInt(js_cd_speed_tics[ii])) {
            _last_cs_speed_matched = 1;
            _current_speed = ii;
          }
        }
        if (_last_cs_speed_matched == 1) {
          _update_counter_speed_image(_current_speed, 1000, _current_speed);
          _delete_counter_speed_image(_current_speed);
          _last_cs_speed_matched = 0;
        }
      }
    }


    if (data[0] == 'ct') {
      _ct_counter_time = data[1];

//      if (_ct_counter_status != _auction_status_future) {
      if (_ct_counter_status == _auction_status_future_wo_startdate) {
        _ct_counter_time = '-- : -- : -- ';
      } else {
        if (_ct_counter_time == '-') {
          _ct_counter_time = '-- : -- : --';
        } else if (_ct_counter_status == _auction_status_live
                   && (_ct_counter_time == '0' || _ct_counter_time == '')) {
          _ct_counter_time = '';
        } else {
          _ct_counter_time = calc_counter_from_time(_ct_counter_time);
        }
      }

      if ($(div_counter_name) && _ct_counter_time != '') {
        $(div_counter_name).innerHTML = _ct_counter_time;
		var a=0;
        if (parseInt(data[1]) <=10 && parseInt(data[1]) > 0) {
			
          $(div_counter_name).style.color = '#DD0000';
          //$('countertime').style.textDecoration = 'blink';
          $('last_actions_div').innerHTML = '<img src="' + prefix + 'www.1bidshop.com/images/blink.gif">'; 
		 //$('last_actions_div').innerHTML = 'Just A Few Secont To Go'; 
         $('last_actions_div').style.display = ''; 
			

        } else {
          if (_last_action_div_displayed == 0) {
            $('countertime').style.color = '';
            //$('countertime').style.textDecoration = 'none';
            //$('last_actions_div').innerHTML = ''; 
            //$('last_actions_div').style.display = 'none'; 
          }
        }  
      } else if ($('counter_index_page_' + auction_id)) {
        $('counter_index_page_' + auction_id).innerHTML = _ct_counter_time;
      }

     
//      } else {
//        _future_counter_time =
//          (parseInt(_ct_counter_time) + parseInt(_start_time));
//        _ct_counter_time = calc_counter_from_time(_future_counter_time);
//         $('countertime').innerHTML = _ct_counter_time;
//        _future_counter_changed = 1;
//      }
    } else if (data[0] == 'cw' && data[1] != '') {
      _ct_counter_winner_name = data[1];
      if ($('a_current_winner')) {
        $('a_current_winner').innerHTML = _ct_counter_winner_name;
      }
      if ($('winner_index_page_' + auction_id)) {
        $('winner_index_page_' + auction_id).innerHTML = _ct_counter_winner_name;
      }
    } else if (data[0] == 'cp') {
		
		_ct_counter_price = data[1];
      if ($('a_current_price')) {
        $('a_current_price').innerHTML = format_raw_to_price(_ct_counter_price);
      }
      if ($('a_current_price2')) {
        $('a_current_price2').innerHTML = '<b>'+ format_raw_to_price(_ct_counter_price) +'</b>';
      }
      if ($('price_index_page_' + auction_id)) {
        $('price_index_page_' + auction_id).innerHTML = format_raw_to_price(_ct_counter_price);
      }

      if (_ct_counter_price != _counter_changed) {
		 
        new Effect.Highlight('a_current_winner', {duration:1,startcolor:'#ff0000',endcolor:'#ffffff',restorecolor:'#FFFFFF'}); //vikas 
        if (_last_bid_key != null && _last_bid_key != undefined && _last_bid_key > 0) {
          if ($('a_current_price')) {
             $('a_current_price').style.backgroundColor='#FFFFFF';
             new Effect.Highlight('a_current_price', {duration:1,startcolor:'#ff0000',endcolor:'#ffffff',restorecolor:'#FFFFFF'});
          } else if ($('price_index_page_' + auction_id)) {
            new Effect.Highlight('price_index_page_' + auction_id, {duration:1,startcolor:'#ff0000',endcolor:'#ffffff',restorecolor:'#FFFFFF'});
          }
        } else if ($('price_index_page_' + auction_id)) {
          new Effect.Highlight('price_index_page_' + auction_id, {duration:1,startcolor:'#ff0000',endcolor:'#ffffff',restorecolor:'#FFFFFF'});
        }
      }

      if (_last_bid_key != -1) {
        // update savings
       _calc_savings_detail_by_params();
      }
      // counter changed 
      _counter_changed = _ct_counter_price;

    } else if (data[0] == 'cs') {
      if (data[1] != _ct_counter_status) {
        _ct_counter_status_changed = 1;
      }
      _ct_counter_status = data[1];
	  if(_ct_counter_status == 5){
 $(div_counter_name).style.color = '#DD0000';
	  }else if(_ct_counter_status == 2){
 $(div_counter_name).style.color = '#DD0000';
 $('final_count_down_msg').style.color = '#DD0000';
 $('final_count_down_msg').innerHTML = 'Final Countdown';
	  }else{
	$('final_count_down_msg').innerHTML = '';
		  }
      // display puased if needed
      if (_ct_counter_status == _auction_status_paused) {
        _cur_time = new Date();
        _cur_secs = _cur_time.getTime();
        if (_cur_time%5 <= 2) {
          $(div_counter_name).innerHTML = 'Pause';
        }
      }
    } else if (data[0] == 'cwi') {
      _ct_counter_winner_id = data[1];
    } else if (data[0] == 'bh' && data[1] != '') {
	parse_bid_history(data[1], auction_id);
    } else if (data[0] == 'lui') {
      parse_last_update_info(data[1]);
    }
  }
  _detail_request_running = 0;
}


var _lui_running = '';
function parse_last_update_info(lui) {
  lui_data = lui.split('#');
  lui_out = '';
  lui_div = 'last_actions_div';
  if (null == bid_increment || undefined == bid_increment) {
    bid_increment = _default_bid_increment;
  }

  if (lui_data.length == 4) {
    if (lui_data[0] != null && lui_data[0] != undefined
        && lui_data[1] != null && lui_data[1] != undefined) {
      if (lui_data[1] != 0) {
        lui_out += lui_data[1] + ' ';
        lui_out += _wording_single_bid;
        if (lui_data[1] > 1) {
          lui_out += 's';
        }
        lui_out += ' + ' + format_raw_to_price(parseInt(bid_increment)*parseInt(lui_data[1]));
		if (lui_data[0] != 0){
		
        lui_out += ' + ' + calc_counter_from_time(lui_data[0]);
	  }
        lui_out += '<br />';
      }
    }

    if (lui_data[2] != null && lui_data[2] != undefined
        && lui_data[3] != null && lui_data[3] != undefined) {
      if (lui_data[3] != 0) { 
        lui_out += lui_data[3] + ' ';
        lui_out += _wording_bid_agent;
        lui_out += ' + ' + format_raw_to_price(parseInt(bid_increment)*parseInt(lui_data[3]));
		if (lui_data[2] != 0){
        lui_out += ' + ' + calc_counter_from_time(lui_data[2]);
	  }
      }
    }
  }
//lui_running = window.setTimeout('new Effect.BlindUp(\'' + lui_div + '\', {duration:1}); _last_action_div_displayed = 0',3000);
  if (lui_out != "") {
    el = $(lui_div);
    if (el) {
      if (_lui_running) {
        window.clearTimeout(_lui_running);
      }
      el.innerHTML = lui_out;
      _last_action_div_displayed = 1;
      new Effect.BlindDown(el);
      _lui_running = window.setTimeout('new Effect.BlindUp(\'' + lui_div + '\', {duration:1}); _last_action_div_displayed = 0',1000);
    }
  }
}

function parse_bid_history(bh, auction_id) {
	//alert(bh);
  bh_data = bh.split('#');
  history_out = "";
  own_history_out = "";
  tmp_bh = new Array();
  tmp_own_bh = new Array();
  y = 0;
  z = 0;
  tmp_min_bid_key = 9999999;
  div_stats_all = 'stats_test';
  div_stats_own = 'my_placed_bids';
  if (!document.getElementById(div_stats_all)) {
    return;
  }
  //alert(bh_data.length);
  for (x = 0; x < bh_data.length; x++) {
    if (null != bh_data[x]) {
      single_entry = bh_data[x].split(':');

      if (null == single_entry || single_entry.length == 0) {
        continue;
      }

      if (single_entry[0] == null || single_entry[0] == "") {
        continue;
      }

      

      tmp_bh[y] = new Object();
      tmp_bh[y]["bid_key"] = parseInt(single_entry[0]);
      tmp_bh[y]["user"] = single_entry[1];
      tmp_bh[y]["type"] = single_entry[2];
      tmp_bh[y]["price"] = format_raw_to_price(single_entry[3]);
      tmp_bh[y]["your_bid"] = 0;
      if (undefined != single_entry[4]) {
        tmp_bh[y]["your_bid"] = single_entry[4];
      }
      if (tmp_bh[y]["your_bid"] == 1) {
        //tmp_bh[y]["user"] = "<b>" + tmp_bh[y]["user"] + "</b>";
		tmp_bh[y]["user"] = tmp_bh[y]["user"];
        tmp_own_bh[z] = tmp_bh[y];
        z++;
      }
      y++;

      if (single_entry[0] != "" && tmp_min_bid_key >= single_entry[0]) {
        tmp_min_bid_key = parseInt(single_entry[0]);
      }
    }
  }

  if (tmp_bh != null) {
    if (_bid_history != null && _bid_history.length > 0) {
      for (x = 0; x < _bid_history.length; x++) { 
        if (y >= 10) continue;
 
        if (_bid_history[x]["bid_key"] < tmp_min_bid_key) {
          tmp_bh[y] = _bid_history[x];
          y++;
        }
      }
      _bid_history = tmp_bh;
    } else {
      _bid_history = tmp_bh;
    }
  }

  //get_my_placed_bids(div_stats_own, auction_id);

/*
 *
  if (tmp_own_bh != null) {
    if (_own_bid_history != null && _own_bid_history.length > 0) {
      for (x = 0; x < _own_bid_history.length; x++) { 
        if (z >= 10) continue;
 
        if (_own_bid_history[x]["bid_key"] < tmp_min_bid_key) {
          tmp_own_bh[z] = _own_bid_history[x];
          z++;
        }
      }
      _own_bid_history = tmp_own_bh;
    } else {
      _own_bid_history = tmp_own_bh;
    }
  }
*/
  if (_bid_history != null) {
    history_out += '<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">';
    history_out += '<tr> ' +
                   '  <td align="center" valign="top" ></td>' +
                   '  <td align="center" valign="top" ></TD>' +
                   '</tr>';

    for (x = 0; x < _bid_history.length; x++) {
      if (_bid_out_types[_bid_history[x]["type"]] != null) {
        _type = _bid_out_types[_bid_history[x]["type"]];
      } else {
        _type = 'n/a';
      }
      history_out += "<tr>";
      history_out += "<td valign=\"top\" align=\"center\">" + _bid_history[x]["price"] + "</td>";
      history_out += "<td valign=\"top\" align=\"center\">" + _bid_history[x]["user"] + "</td>";
     history_out += "</tr>";
/*
      history_out += "type: " + _bid_history[x]["type"];
      history_out += " <br> ";
*/
    }
    history_out += '</table>';
  }

  if (history_out != "") {
    $(div_stats_all).innerHTML = history_out;
  }


  return;
}

function get_my_placed_bids(div_stats_own, auction_id) {
  if ($(div_stats_own)) {
	  //alert(div_stats_own);
	  //alert(auction_id);
    new Ajax.Updater(div_stats_own, prefix + 'www.1bidshop.com/ajexphp/my_placed_bids.php?aid=' + auction_id+'&bidtype=1');
  }
}

var _last_savings_update = 0;
function update_savings_details(auction_id) {
	
  if (auction_id == undefined || auction_id == null) {
    return;
  }

  if (((new Date()).getTime()-_last_savings_update)<5000) {
    return;
  }
 
  _last_savings_update = (new Date()).getTime();

  url = prefix + 'www.1bidshop.com/ajexphp/savings.php?aid=' + auction_id;
  
  new Ajax.Request(url, {onSuccess: function(transport) {
										
                                      parse_savings_detail(transport);
                                    }
                        }); 
}

function _calc_savings_detail_by_params() {
  div_name_savings = 'user_savings';

  _savings = parseInt(_savings_ajp_price_sale);

  if (_savings_ajp_is_fixed == 1) {
    __current_price = _savings_ajp_current_price;
  } else {
    __current_price = _savings_ajp_current_price;
	
    if (_ct_counter_price != undefined && _ct_counter_price != null && _ct_counter_price != '') {
      __current_price = parseInt(_ct_counter_price);
    }
  }
  _savings -= __current_price;

  if (_savings_ajp_own_bids_num > 0) {
    _savings -= (_savings_ajp_own_bids_num*_bids_cost);
  } 

  if (_savings < 0) {
    _savings = 0;
  }
//alert(_savings_ajp_own_bids_num*_bids_cost);
  if ($(div_name_savings)) {
    $(div_name_savings).innerHTML = format_raw_to_price(_savings);
  }
}


function parse_savings_detail(transport) {
	//alert(transport);
  result_array = transport.responseText.split("|");
  div_name_own_bids = 'a_bid_amount_placed';
  div_name_savings = 'user_savings';
  div_name_savings_num = 'user_savings_num_bids';
  if (result_array.length == 3) {
    el = $(div_name_own_bids);
    el2 = $(div_name_savings);
    el3 = $(div_name_savings_num);
    if (el) {
      el.innerHTML = result_array[2];
    }
    if (el2) {
      el2.innerHTML = result_array[0];
    }
    if (el3) {
      _savings_ajp_own_bids_num = result_array[1];
      el3.innerHTML = result_array[1];
    }
  } else if (result_array.length == 1) {
    el2 = $(div_name_savings);
    if (el2) {
      el2.innerHTML = result_array[0];
    }
  }
}


var updated_to_cds = 0;
function _update_counter_speed_image(_sec, _num_bids, _current_sec) {
  if (undefined == bid_increment || null == bid_increment) {
    bid_increment = _default_bid_increment;
  }

  if (_last_update_ct_time == _current_sec) {
//    return;
  }
  updated_to_cds = _current_sec;

  _last_update_ct_time = bid_increment;

  _price = format_raw_to_price(parseInt(_num_bids)*parseInt(bid_increment));
  _text = '' + _sec + ' second countdown<br/>starts at ' + _price;

  _img = _current_sec ;
  el = $('CDUMST');
  if (el) {
    el.innerHTML = _text;
  }
  el = $('CDUMST_IMG');
  if (el) {
    el.innerHTML = "15";
	//el.src = _img;
  }
}

function _delete_counter_speed_image(_current_sec) {
  el = $('CDUMST');
  if (el) {
    el.parentNode.removeChild(el);
  }

  if (_cds_image_deleted == 1) {
    return;
  }
  _cds_image_deleted = 1;
  _img =  _current_sec ;
  //_img = 'http://192.168.0.100/bolibol/images/uhr' + _current_sec + '.png';
  el = $('CDUMST_IMG');
  if (el) {
    //el.src = _img;
	el.innerHTML = "";
  }
}
