var _lock_ct_keys = 0;
var _counter_server_prefix = basepath;
var _index_counter_prices = new Object();
function auction_index_js(auction_ids) 
{
  if (auction_ids == '' || auction_ids == null) 
  {
    return;
  }
  do_counter_request_index(auction_ids); 

  setTimeout('auction_index_js("' + auction_ids + '")', 500);
}

function parse_counter_response_index(transport) 
{
  content = transport.split('@@');
  $("#user_current_bids").html(content[0]); 
  counters = content[1].split('#'); 
  for (i = 0; i <= counters.length; i++) 
  {
    if (counters[i] == null) continue;	
	
    counter_data = counters[i].split('!@!');
    auction_id = counter_data[0];
	
	div_price_name = '#price_index_page_' + auction_id;
	div_high_price = '#highest_price_' + auction_id;
	div_name_winner = '#winner_index_page_' + auction_id; 
	div_button_name_finished = '#button_finished_index_page_' + auction_id;
	div_name_req_credit = '#required_credit_' + auction_id;
	div_bid_detail = '#bid_detail_' + auction_id;	
    div_name_counter = '#counter_index_page_' + auction_id; 
	 div_product_counter = '#productcounter_' + auction_id;
	 div_auction_type = '#auction_type_' + auction_id;
	//blinkdiv_counter ='#blinkdiv_' + auction_id;   	
	
	counter = counter_data[1].split('|');
    for (ii = 0; ii < counter.length; ii++) {
      data = counter[ii].split('=');
      
	  
	  if (data[0] == 'cgt') 
	  {      
          gray_clock = data[1]; 
      }	 
	  else if (data[0] == 'cty') 
	  {      
         auc_type = data[1]; 
      }	 
	  else if (data[0] == 'cr') 
	  {      
         auc_round = data[1]; 
      }	 
	  else if (data[0] == 'ct') 
	  {
        diff_value = parseInt(data[1]);
		
	   if(auc_type == 1 || (auc_type == 2 && (auc_round == 3 || auc_round == 4)) || (auc_type == 3 && auc_round != 2))
	   { 
	   	 if((diff_value >= 1 && auc_type == 1)|| (diff_value >= 1 && (gray_clock == '' || gray_clock >= 1) && auc_type == 2))
	  	 {	  
	  	  _ct_time = calc_counter_from_time(data[1]);
		  $(div_product_counter).show();
		  $(div_name_counter).html(_ct_time);
	  	 }
	 	 else if ((diff_value < 1 && auc_type == 1 ) || ((diff_value < 1 ||gray_clock < 1) && (auc_type == 2 && auc_round == 4 )))
	 	 {
	  	  bid_ended(auction_id);			
	  	 }
	   }	
		
          if (parseInt(data[1]) <= 10 ) 
		  {
            $(div_name_counter).css("color", "#FF0000");
			//$(blinkdiv_counter).show();			
          } else 
		  {
            $(div_name_counter).css("color", "#0002F9");
			//$(blinkdiv_counter).hide();	
          }     
      }
	  else if (data[0] == 'cbi' && data[1] != '') 
	  {       
          $(div_price_name).css("background", "#FF0000");
		  $(div_bid_detail).html(data[1]); 
		  setTimeout("hide_bidinfo("+auction_id+")",2000); 
      } 	  
	  else if (data[0] == 'cp') 
	  {      
          $(div_price_name).html(data[1]); 
      }
	  else if (data[0] == 'chp') 
	  {      
          $(div_high_price).html(data[1]); 
      }
	  else if (data[0] == 'cw' && data[1] != '') 
	  {       
          $(div_name_winner).html(data[1]); 
      } 
	  else if (data[0] == 'cbp' && data[1] != '') 
	  {       
          $(div_button_name_finished).html(data[1]); 
      } 
	  else if (data[0] == 'ccr' && data[1] != '') 
	  {       
          $(div_name_req_credit).html(data[1]+" Credits"); 
      } 
	  else if (data[0] == 'cba') 
	  {      
         if(auc_type == 1)
		 $(div_auction_type).html('<div class="penny_img">1&cent;</div>');
		 else if(auc_type == 2 && auc_round < 3)
		 $(div_auction_type).html('<img src="eb_image/hour.jpg" />');
		 else if(auc_type == 2 && auc_round > 2)
		 $(div_auction_type).html('<div class="penny_img">'+data[1]+'</div>');		
      }
    }
  }
}

function do_counter_request_index(auction_ids) 
{
  $.ajax({
	   type: "POST",
	   url: _counter_server_prefix + '/ajax_home_detail_new.php?pids=' + auction_ids ,	  
	   dataType: "html",
		   success: function(responseText)
			{		
				eval(responseText);
			}		  
	 });
	return false;
}

function _ev_index(transport) 
{ 
  parse_counter_response_index(transport); 
}
function hide_bidinfo(divid)
{
	price_name = '#price_index_page_' + divid;
	bid_detail = '#bid_detail_' + divid;	
	$(bid_detail).html("&nbsp;");
	$(price_name).css("background", "");
}
function popupBox(str,pid)
{
refreshURL = _counter_server_prefix + "/product_details.php?prodid=" + pid;
window.location.href = refreshURL;
}

function calc_counter_from_time(diff) 
{
  if (diff > 0) {
    hours=Math.floor(diff / 3600)

    minutes=Math.floor((diff / 3600 - hours) * 60)

    seconds=Math.round((((diff / 3600 - hours) * 60) - minutes) * 60)
  } else {
    hours = 0;
    minutes = 0;
    seconds = 0;
  }

  if (seconds == 60) {
    seconds = 0;
  }

  if (minutes < 10) {
    if (minutes < 0) {
      minutes = 0;
    }
    minutes = '0' + minutes;
  }
  if (seconds < 10) {
    if (seconds < 0) {
      seconds = 0;
    }
    seconds = '0' + seconds;
  }
  if (hours < 10) {
    if (hours < 0) {
      hours = 0;
    }
    hours = '0' + hours;
  }
  return hours + ":" + minutes + ":" + seconds;
}

