Array.prototype.remove=function(s){
	for(i=0;i<this .length;i++){
		if(s==this[i]) this.splice(i, 1);
	}
}

/*
Object.prototype.isArray = function(){
	return this.constructor == Array;
}

Object.prototype.toArray = function(){
	return Array(this);
}
*/
function get_color(value){
if (value>60) return '#a6f8aa';//2min
if (value>30) return '#edf8a6';//1min
if (value>10) return '#f7ab26';//30s
if (value>5)  return '#ff5842';//10s
if (value>3)  return '#f33218';
}

var updater = function(){
	aid = ids.join("|");
	var localtime = new Date();
	var updater_interval = 750;

	var current_member = $("#current_member").text();
	
	$.getJSON("/latest/" + aid + ".js&localtime=" + localtime.getTime(), function(auctions){
		$.each(auctions, function(i, auction) {
			$("#status_" + auction.id).text(auction.status);
            if (auction.latest_bidder.length > 12) {
			    $("#latest_bidder_" + auction.id).text(auction.latest_bidder.substring(0, 12) + "...");
            } else {
			    $("#latest_bidder_" + auction.id).text(auction.latest_bidder);
            }
			current = parseInt($("#seconds_left_" + auction.id).text())
			next = parseInt(auction.time_left)
			if (next > current){
			    $('#'+auction.id).effect("highlight", {color:get_color(next)}, 1000); //vh5
			}
			$("#seconds_left_" + auction.id).text(auction.time_left);
			$("#latest_price_" + auction.id).text('$'+parseFloat(auction.latest_price).toFixed(2));

			if (auction.latest_bidder == current_member){
				if ( $("#highbidder").css("display") != 'block' ){
					$("#highbidder").show("fast");
					$("#placebid").hide("fast");
				}
			} else {
				if ( $("#highbidder").css("display") == 'block' ){
					$("#highbidder").hide("fast");
					$("#placebid").show("fast");
				}
			}
			$("#biddingtime_item_" + auction.id).html(showTime(auction.bidding_time));//vh5

			if (auction.status == "p") {
				//nothing
			} else if(auction.status == "s"){
				$("#time_left_" + auction.id).text("Auctions are paused for maintenance");
			} else {
				$("#time_left_" + auction.id).text("This auction has ended");
				if (ids.length == 1){
					updater_interval = -1;	// will stop the query				
				} else {
					// if we are on front page and there is an item expired we will reload
					if (auction.reload) {
						window.location = "/";
					}
				}
			}
		});
		if (updater_interval > 0) window.setTimeout("updater()", updater_interval);
		else return void(0);
	});

}
var updateTime = function(){
	var updater_interval = 1000;
	$.each(ids, function(i, aid) {
		if ($("#status_" + aid).text() == 'p') {
			t = parseInt($("#seconds_left_" + aid).text());
			if(parseFloat($("#latest_price_" + aid).text()) > 0){
				$("#seconds_left_" + aid).text(--t);
			}
			$("#time_left_" + aid).html(showTime(t));
		} else {
			if (ids.length == 1) {
				updater_interval = -1;
			}			
		}
	});

	if (updater_interval > 0) window.setTimeout("updateTime()", updater_interval);
	else return void(0);
}

var showTime = function(seconds){
	if (isNaN(seconds)) {
		return seconds;
	} else if (seconds >= 0) {
		hour = Math.floor(seconds / 3600);
		min = Math.floor((seconds - hour*3600) / 60);
		sec = seconds % 60;
		
		timeString = "";
		if (hour) 
			timeString += hour + "<span class=\"time_text\"> Hour" + unit_plural(hour) + " </span>";
		if (min) 
			timeString += min + "<span class=\"time_text\"> Minute" + unit_plural(min) + " </span>";
		if (sec) 
			timeString += sec + "<span class=\"time_text\"> Second" + unit_plural(sec) + " </span>";
		return timeString;
	} else {
		return "";
	}
}

var next_interval = function(n){
	i = 1;
	if (n > 240){
		i = 8;
	} else if (n>120 && n<=240){
		i = 6;
	} else if (n>60 && n<=120){
		i = 4;
	} else if (n>30 && n<=60){
		i = 3;
	} else if (n>15 && n<=30){
		i = 2;
	} else if (n<=15){
		i = 1;
	}
	return i*1000;
}


var unit_plural = function(n){
	return n==1?"":"s";
}

var ids = new Array();

$(document).ready(function(){
	$("div.pic_holder, div.item_info").click(function(){
    	window.location=$(this).find("a").attr("href");
		return false;
	});
	$("a.ajaxmsg").click(function(){
		msg_item = $(this).parents("div.msg_item");
		$.get(this.href, function(result){
			msg_item.hide();
			msg_item.html(result);
			msg_item.fadeIn("slow");
		});
		return false;
	});
	var i = 0;
	$.each($(".auction_info"), function(i, auction_info) {
		ids[i] = auction_info.id;
		t = parseInt($("#bidding_time_" + ids[i]).text());
		$("#bidding_time_" + ids[i]).html(showTime(t));

		bt = parseInt($("#biddingtime_item_" + ids[i]).text());
		$("#biddingtime_item_" + ids[i]).html(showTime(bt));

		p = parseFloat($("#latest_price_" + ids[i]).text());
		$("#latest_price_" + ids[i]).html(p.toFixed(2));
	});
	
	updater();
	updateTime();
});
