Array.prototype.count=function(){return this.length;}
function refershData()
{
	var a= new Array();
	jQuery("div.auction,tr.auction_line,div.auction-description,div.right-panel-auction-top").each(function(i){
		var id =jQuery(this).attr("id"); 
		if (!id.match("end"))
			a[i]=id;
	});

	if (a.count())
	{
		jQuery.ajax({
			url:"http://jofi.pl/auction.php",type:"POST",dataType:"json",
			cache:false, 
			data:{'aid[]':a},
			success:auctionResponse
		});
	}
	
}
function auctionResponse(result)
{
	jQuery(result.auction).each(function(i,a){
		if (a.t!=jQuery('#auction_time_'+a.i).html())
		{
			if (a.w && a.t<8)
			{
				if (a.t<5)
					jQuery('#auction_time_'+a.i).addClass("redbg");
				else
					jQuery('#auction_time_'+a.i).addClass("red");
			}
			else
			{
				jQuery('#auction_time_'+a.i).removeClass("redbg");
				jQuery('#auction_time_'+a.i).removeClass("red");
			}
			switch(a.s)
			{
			case 0:
				jQuery('#auction_time_'+a.i).html(a.T);
				break;
			case 2:
				jQuery('#auction_time_'+a.i).html('Wkrótce');
				jQuery('#auction_time_'+a.i).removeClass("redbg");
				break;
			case 1:
				jQuery('#auction_time_'+a.i).html('Koniec');
				jQuery('#auction_time_'+a.i).removeClass("redbg");
				jQuery('#auction_bid_'+a.i).attr('disabled', 'disabled');
				break;
			case 3:
				jQuery('#auction_time_'+a.i).html('Pauza');
				jQuery('#auction_time_'+a.i).removeClass("redbg");
				break;
			}
			if (a.w && a.w!=jQuery('#auction_winner_'+a.i).html())
			{
				jQuery('#auction_price_'+a.i).html(a.p+" zł");
				jQuery('#auction_winner_'+a.i).html(a.w);
				jQuery(document).everyTime(500,function(i){blinkbg('#auction_price_'+a.i,i)},2);
			}
			jQuery('#auction_hits_'+a.i).html(a.hit);
		}
	});
}
function hitAction(id)
{
	jQuery.ajax({
		url:"/auction.php",type:"POST",dataType:"json",cache:false, 
		data:{'hid':id},
		success:hitResponse
	});
}
function hitResponse(result)
{
	if (result.s==302)
	{
		window.location.href='/logowanie.html?redir='+encodeURIComponent(window.location.href);
	}
	else
	{ 
		if (result.status)
			alert(result.status);
	}
}
function creditResponse(result)
{
	jQuery("#credit").html(result.credit);
}
function creditAction()
{
	jQuery.ajax({url:"/auction.php",type:"POST",dataType:"json",cache:false,data:{'credit':'1'},success:creditResponse});
}
function automatResponse(result)
{
	jQuery("div.add_auction_automat span").html(result.automat);
	if (result.show)
		jQuery("div.info_auction_automat").show("slow");
	else
		jQuery("div.info_auction_automat").hide("fast");
}
jQuery(document).ready(function()
{
	jQuery(document).everyTime(900, function(i) {refershData()}, 0);
	jQuery(".auction_bid a").each(function(){ jQuery(this).bind("click",function(){hitAction(jQuery(this).attr("id"));} );});
	if (jQuery("#credit")[0])
	{
		creditAction();
		jQuery(document).everyTime(6000, function(i){creditAction()},0);
	}
	if (jQuery("div.add_auction_automat").length>0)
	{
		var id=jQuery("div.auction-description").attr("id");
		if (!id.match('end'))
		{
			jQuery(document).everyTime(3000, function(i)
			{
			jQuery.ajax({url:"/auction.php",type:"POST",dataType:"json",cache:false, 
				data:{'auto':id },
				success:automatResponse});
			},0);
		}
	}
});
/*
 * jQuery Blink
 * Author: WonderGroup, Jordan Thomas
 * URL: http://labs.wondergroup.com/demos/mini-ui/index.html
 * License: MIT (http://en.wikipedia.org/wiki/MIT_License)
 * */
jQuery.fn.blink=function(o){var d={speed:200,blinks:3,callback:null};var o=jQuery.extend(d,o);return this.each(function(){var calls=0;for(i=1;i<=o.blinks;i++){$(this).animate({opacity:0},o.speed).animate({opacity:1},o.speed,function(){calls++;if(calls==o.blinks&&jQuery.isFunction(o.callback)){o.callback();}});}});};
function blinkbg(id,i){ if (i%2)jQuery(id).css("background-color","#FFDF4E");else jQuery(id).css("background-color","#fff");}

