var ptime=0; 
APE.Controller = new Class({
Extends: APE.Client,
Implements: Options,
options:
{
	container: null
},
initialize: function(options)
{
	this.setOptions(options);
	this.container = $(this.options.container) || document.body;
	this.onRaw('postmsg', this.onMsg);
	this.addEvent('load',this.start);
},
start: function(core)
{
	this.core.start({'name': $time().toString()});
},
onMsg: function(raw)
{
	counter = decodeURIComponent(raw.data.message);
	aucdata = counter.split('|');
	postcheck = aucdata[0].split(':');
	if(postcheck[1] < ptime && postcheck[0]==1)
	{
	}
	else if(postcheck[0]==1)
	{
		var dat1 = (postcheck[1]) * 1000;
		var dat = dat1;
		var date = new Date(dat);
		var hours = date.getHours();
		var minutes = date.getMinutes();
		var seconds = date.getSeconds();
		if(seconds <=9)
		{seconds = '0' + seconds;}
		if(minutes <=9)
		{minutes = '0' + minutes;}
		var formattedTime = hours + ':' + minutes + ':' + seconds;
		if(document.getElementById('timeclock')){document.getElementById('timeclock').innerHTML = formattedTime;}		

		ptime = postcheck[1];
		document.getElementById('ptime2').innerHTML = 10;
	for (i = 1; i < aucdata.length; i++)
		{
		counter_data1 = aucdata[i].split(':');
		auction_id = counter_data1[0];
		if(document.getElementById('price_index_page_' + auction_id))
			{
			auction_time_raw = counter_data1[1];
			auction_pause = counter_data1[2];
			auction_price = counter_data1[3];
			auction_bidder_name = counter_data1[4];
			if(auction_time_raw >=1 && auction_pause==0)
				{
				diff1 = auction_time_raw - 2;
				if (diff1 >= 0)
					{
					days=Math.floor(diff1 / 86400)
					hours1=Math.floor(diff1 / 3600)
					minutes=Math.floor((diff1 / 3600 - hours1) * 60)
					seconds=Math.round((((diff1 / 3600 - hours1) * 60) - minutes) * 60)
					if(hours1 >= 24)
						{ 
						dayhr = days * 24;
						hours = hours1 - dayhr;
						}
					else
						{ 
						hours = hours1;
						}
					}
				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;
					}
				if(days >=1 && diff1>=1){ auction_time = days + "D " + hours + ":" + minutes + ":" + seconds;}
				if(days ==0 && diff1>=1){ auction_time = hours + ":" + minutes + ":" + seconds;}
				if(diff1 <= 0){ auction_time = "VALIDATING";}
				if(diff1<12)
					{
					document.getElementById('counter_index_page_' + auction_id).style.color = '#E80000';	
					}
				else
					{
					document.getElementById('counter_index_page_' + auction_id).style.color = 'Black';	
					}
				if(document.getElementById('product_bidder_' + auction_id).innerHTML != auction_bidder_name)
					{
					if(document.getElementById('price_index_page_' + auction_id)){document.getElementById('price_index_page_' + auction_id).style.backgroundColor = "#f79909";}
					if(document.getElementById('price_index_page_' + auction_id)){document.getElementById('price_index_page_' + auction_id).innerHTML = "&#0036;" + auction_price;}
					if(document.getElementById('product_bidder_' + auction_id)){document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;}
					if(document.getElementById('counter_index_page_' + auction_id)){document.getElementById('counter_index_page_' + auction_id).innerHTML = auction_time;}
					}
				else
					{
					if(document.getElementById('price_index_page_' + auction_id)){document.getElementById('price_index_page_' + auction_id).style.backgroundColor = "white";}
					if(document.getElementById('price_index_page_' + auction_id)){document.getElementById('price_index_page_' + auction_id).innerHTML = "&#0036;" + auction_price;}
					if(document.getElementById('product_bidder_' + auction_id)){document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;}
					if(document.getElementById('counter_index_page_' + auction_id)){document.getElementById('counter_index_page_' + auction_id).innerHTML = auction_time;}
					}
				}
			else if(auction_pause==1)
				{
				document.getElementById('counter_index_page_' + auction_id).innerHTML = 'Pause';					
				document.getElementById('image_main_' + auction_id).src = "images/bid_click_white_bid_hover.jpg";
				document.getElementById('image_main_' + auction_id).onclick="";
				document.getElementById('image_main_' + auction_id).onmouseover="";
				document.getElementById('image_main_' + auction_id).onmouseout="";
				}
			else if(auction_time_raw ==0 && auction_pause==0)
				{
				document.getElementById('counter_index_page_' + auction_id).innerHTML = 'Ended';					
				document.getElementById('image_main_' + auction_id).src = "images/but_sold2.jpg";
				document.getElementById('image_main_' + auction_id).onclick="";
				document.getElementById('image_main_' + auction_id).onmouseover="";
				document.getElementById('image_main_' + auction_id).onmouseout="";
				location.reload(true);
				}
			else if(auction_time_raw =="-1" && auction_pause==0)
				{
				if(document.getElementById('product_bidder_' + auction_id).innerHTML != auction_bidder_name)
					{
					if(document.getElementById('price_index_page_' + auction_id)){document.getElementById('price_index_page_' + auction_id).style.backgroundColor = "#f79909";}
					if(document.getElementById('price_index_page_' + auction_id)){document.getElementById('price_index_page_' + auction_id).innerHTML = "&#0036;" + auction_price;}
					if(document.getElementById('product_bidder_' + auction_id)){document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;}
					}
				else
					{
					if(document.getElementById('price_index_page_' + auction_id)){document.getElementById('price_index_page_' + auction_id).style.backgroundColor = "white";}
					if(document.getElementById('price_index_page_' + auction_id)){document.getElementById('price_index_page_' + auction_id).innerHTML = "&#0036;" + auction_price;}
					if(document.getElementById('product_bidder_' + auction_id)){document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;}
					}

				}
			else
				{
				}
 			}
		}
	}
	else if(postcheck[0]==2)
		{
		j = 0;
		for (i = 1; i < aucdata.length; i++)
			{
			history_data1 = aucdata[i].split(',');
			auction_id = history_data1[0];
			if(document.getElementById('thisistheauctionpage_' + auction_id))
				{
					bidder_name = history_data1[1];
					auction_price = history_data1[2];
					bid_type = history_data1[3];
					bid_time = history_data1[4];
					document.getElementById('bid_price_' + j).innerHTML = "&#0036;" + auction_price;
					document.getElementById('bid_user_name_' + j).innerHTML = bidder_name;
					if(bid_type=='s')
						{
						document.getElementById('bid_type_' + j).innerHTML = placesinglebid;
						}
					else if(bid_type=='b')
						{
						document.getElementById('bid_type_' + j).innerHTML = placebidbuddy;
						}
					else if(bid_type=='m')
						{
						document.getElementById('bid_type_' + j).innerHTML = placesmsbid;
						}
					else if(bid_type=='p')
						{
						document.getElementById('bid_type_' + j).innerHTML = placebidpbuddy;
						}
					else if(bid_type=='z')
						{
						document.getElementById('bid_type_' + j).innerHTML = placezapper;
						}
					else if(bid_type=='zr')
						{
						document.getElementById('bid_type_' + j).innerHTML = placezapped;
						}
					document.getElementById('bid_time_' + j).innerHTML = bid_time;
				j++;
				}
			
			}
		}
	else if(postcheck[0]==3)
		{
		location.reload(true);
		}
	else if(postcheck[0]==4)
		{
		j = 1;
		for (i = 1; i < aucdata.length; i++)
			{
			chatbox_data1 = aucdata[i].split(',');
			auction_id = chatbox_data1[0];
			if(document.getElementById('thisistheauctionpage_' + auction_id))
				{
					var username = chatbox_data1[1];
					var comment = chatbox_data1[2];
					if(document.getElementById('chatbox_' + auction_id + '_' + j)!=null){document.getElementById('chatbox_' + auction_id + '_' + j).innerHTML = username + ': ' + comment;}
					j++;
				}
			if(document.getElementById('thisistheauctionpagefc_' + auction_id))
				{
					var username = chatbox_data1[1];
					var comment = chatbox_data1[2];
					if(document.getElementById('chatbox_' + auction_id + '_' + j)!=null){document.getElementById('chatbox_' + auction_id + '_' + j).innerHTML = username + ': ' + comment;}
					j++;
				}
			}
		}
	else
		{
		}
}
});

