var TimerLeft = Class.create();

var AllTimers = $A();
var GlobalTimerStarting = false;
var ConnectivityProblemCounter = 0;
var auction_ids = '';
var use_study_auctions='N';
var Timer_Shift = 500;

var TimeUpdateRun = false;
var GlobalAuctionDataUpdateTime = null;
var GlobalAuctionData = $A(new Array());
var GlobalAuctionDataLoading = false;
var NowTimer = (new Date().getTime() + Timer_Shift) / 1000;

function GlobalTimer() {
	NowTimer = (new Date().getTime() + Timer_Shift) / 1000;
	for (index = 0; index < AllTimers.length; index++) {
		AllTimers[index].DecrementSecond();
	}
	setTimeout('GlobalTimer()', 500);
}

TimerLeft.prototype = {
	initialize : function(DivId, PriceDivId, AuctionId) {
		this.div_id = DivId;
		this.price_div_id = PriceDivId;
		this.auction_id = AuctionId;
                this.auction_item = 'auction-item-'+AuctionId;
		if (auction_ids != '') {
			auction_ids = auction_ids + ',';
		}
		auction_ids = auction_ids + AuctionId;
		this.time_all_seconds = $(this.div_id).innerHTML * 1 + NowTimer;
		this.EvaluateTime();
		this.auction_is_finished = false;
		if (!TimeUpdateRun) {
			TimeUpdateRun = true;
			LoadAllAuctionData();
		}
		this.update_time = null;
		this.checking_auction = false;
		AllTimers[AllTimers.length] = this;
	},
	EvaluateTime : function() {
		all_seconds = all_seconds = Math
				.round(this.time_all_seconds - NowTimer);
		if (all_seconds >= 0) {
			all_minutes = Math.floor(all_seconds / 60);
			all_hours = Math.floor(all_minutes / 60);
			all_days = Math.floor(all_hours / 24);
			this.seconds = all_seconds - all_minutes * 60;
			this.minutes = all_minutes - all_hours * 60;
			this.hours = all_hours - all_days * 24;
			this.days = all_days;
		} else {
			this.seconds = 0;
			this.minutes = 0;
			this.hours = 0;
			this.days = 0;
			GetTimerAndPrice(this);
		}
	},

	DecrementSecond : function() {
		this.EvaluateTime();

		var str = '';

		if (this.days > 0) {
			str += this.days + 'd ';
		}
        	if (this.hours > 0) {
	            str += this.hours + ':';
        	}
		str += ((this.minutes > 9) ? this.minutes : ("0" + this.minutes))
				+ ':'
				+ ((this.seconds > 9) ? this.seconds : ("0" + this.seconds));
		if (this.days==0 && this.hours==0 && this.minutes==0 && this.seconds==0) {
			str = 'Going';
		}
		if (ConnectivityProblemCounter > 3) {
			str = 'Connectivity problem';
		}
        
        if (this.days==0 && this.hours==0 && this.minutes==0 && this.seconds<=10) {
            $(this.div_id).setStyle({'color':'Red'});
        }
        else {
            $(this.div_id).setStyle({'color':'#313639'});
        }

		if (!this.auction_is_finished) {
			$(this.div_id).innerHTML = str;
			GetTimerAndPrice(this);
		} else {
			$(this.div_id).innerHTML = finished_phrase;
                        if($(this.auction_item)){
                            $(this.auction_item).removeClassName('open');
                            $(this.auction_item).addClassName('finished');
                        }
			if($('bid_btn_'+this.auction_id)) {
			    $('bid_btn_'+this.auction_id).hide();
			}
		}
		if (!GlobalTimerStarting) {
			GlobalTimerStarting = true;
			GlobalTimer();
		}
	}
}

function GetTimerAndPrice(Obj) {

	if (GlobalAuctionData[Obj.auction_id]) {
		if (GlobalAuctionDataUpdateTime != Obj.update_time) {

			Obj.update_time = GlobalAuctionDataUpdateTime;
			Obj.time_all_seconds = GlobalAuctionData[Obj.auction_id][1];
			Obj.EvaluateTime();
			old_price = $(Obj.price_div_id).innerHTML;
            $(Obj.price_div_id).update(currency_fromat.replace('^^^^^',GlobalAuctionData[Obj.auction_id][2].toString()));                    
            if($('from_price') && !$('from_price').dontchange) {
                $('from_price').value=GlobalAuctionData[Obj.auction_id][2].toString();
            }
			$(Obj.user_div_id).innerHTML = GlobalAuctionData[Obj.auction_id][3]
					.toString();
if(Obj.user_avatar_div_id)
			{
				if(GlobalAuctionData[Obj.auction_id][5].toString()=='D')
				$(Obj.user_avatar_div_id).src = default_avatar;
				else
				$(Obj.user_avatar_div_id).src = GlobalAuctionData[Obj.auction_id][5].toString();
			}
            if (GlobalAuctionData[Obj.auction_id][4] == 'finished') {
                Obj.auction_is_finished = true;
            }
            if($('top_bidder_'+Obj.auction_id)) {
                    if (GlobalAuctionData[Obj.auction_id][6] == 'Y') {
                            $('top_bidder_'+Obj.auction_id).addClassName('active');
                            $('bid_btn_'+Obj.auction_id).addClassName('disabled');
                    }
                else {
                    $('top_bidder_'+Obj.auction_id).removeClassName('active');
                    $('bid_btn_'+Obj.auction_id).removeClassName('disabled');
                }
            }
			if (old_price != $(Obj.price_div_id).innerHTML) {
				new Effect.Highlight($(Obj.div_id), {
					startcolor : '#27c7ff',
					endcolor : '#FFFFFF',
					restorecolor : '#FFFFFF',
					duration : 0.5
				});
				new Effect.Highlight($(Obj.price_div_id), {
					startcolor : '#FF0000',
					endcolor : '#FFFFFF',
					restorecolor : '#FFFFFF',
					duration : 1.5
				});
				if(Obj.user_avatar_div_id)
				{
					new Effect.Highlight($(Obj.user_avatar_div_id), {
						startcolor : '#27c7ff',
						endcolor : '#FFFFFF',
						//restorecolor : '#FFFFFF',
						duration : 0.5
					});
				}
			}
		}
	}
}

TimerLeft.methods = {

}
function LoadAllAuctionData() {
	if (!GlobalAuctionDataLoading) {
		GlobalAuctionDataLoading = true;
		var start_loading = (new Date().getTime()) / 1000;
		new Ajax.Request(
				'/ajax_get_auction_timer_data.php',
				{
					method : 'get',
					parameters : {
						auction_ids : encodeURIComponent(auction_ids),
						unique_string : Math.random(),
                        is_study : use_study_auctions
					},
					onComplete : function(transport) {
						var tempAuctionData = $A(new Array());
						var data = $A(transport.responseText.split('^!%'));
						var str = '';
						var end_loading = new Date().getTime() / 1000;
						var loading_time = (end_loading - start_loading);
						if (loading_time > 1) {
							ConnectivityProblemCounter++;
						} else {
							ConnectivityProblemCounter = 0;
							for ( var index = 1; index < data.length; index++) {
								var item = data[index];
								var item_data = $A(item.split('|'));
								var auction_id = item_data[0];
								tempAuctionData[auction_id] = $A(item_data);
								tempAuctionData[auction_id][1] = tempAuctionData[auction_id][1]
										* 1
										+ start_loading
										+ (loading_time / 2);
							}
							GlobalAuctionData = $A(tempAuctionData);
							GlobalAuctionDataUpdateTime = end_loading;

						}
						GlobalAuctionDataLoading = false;
						setTimeout('LoadAllAuctionData()', 1000);

					}
				});
	}
}

