$(document).ready(function(){
	var oBids = $('#cntBids');
	var cntBids = oBids.text();
	autoBid();
	$('.lot_button').click(function(e){
		e.preventDefault();
		var button = $(this);
		var lotid = button.attr('id').replace('bid_','');
		$.post(
			'/bid/'+lotid,
			function(data, textStatus){
				if(data == 'ok'){
					oBids.text(cntBids--);
					cls = 'ok';
					updateBidHisory(lotid);
				}
		});
	});
	(function(){
		var aFunction = function(){
			$.getScript('/tmp/update.js',function(data,textStatus){
				window.setTimeout(aFunction, 700);    
			});
		};
		aFunction();
	})();
	$('#autobid_save').click(function(){
		var data = {};
		$('#autobid input').each(function(){
			data[this.name] = this.value;
		});
		$.post(
			'/autobid',
			data,
			function(result, txtR) {
				if(!result.match(/class='error'/)){
					$('#autobid').hide();
					$('#autobid_info').fadeOut('fast',function(){
						$('#autobid_info').html(result).show();
					});
					autoBid();
				}
			},
			'html'
		);
	});  
	$('.loadimg').each(function(e){
		var i =this.src.indexOf('load');
		var type = this.src.substr(i-1,1);
		this.src= "http://top-lot.ru/img/" + this.id.replace('imglot_','') + "_" + type +"small.jpg";
		this.className = 'lot_image';
	});
});


function autoBid(){
	var idLot = $('.lot_number').attr('id').replace('lot_','');
	$('#autobid_delete').click(function(){
		$.post('/autobid/delete/'+idLot,function(){
			$('#autobid_info').fadeOut('fast',function(){
					$('#autobid').show()
			});
		})
	});
	$('#autobid_edit').click(function(){
	    $('#autobid_info').fadeOut('fast',function(){
	        $('#autobid').show(); 
	    });
	}) 
}

function updateBidHisory(lotid){
	if($.cache.get('#bid_history .table').length>0){
		$.get(
			'/tmp/bidlog-'+lotid + '.html',
			function(data, txtR){
				$.cache.get('#bid_history .table').html(data);  
			},
			'html'
		)
	}  
}

function up(lot, time, price, leader){
	if($.cache.get('#lot_'+lot).length>0){
		timediv = $.cache.get('#lot_'+lot+' .lot_time');
		pricevalue = $.cache.get('#lot_'+lot+' .lot_price');
		var flash = parseFloat(pricevalue.html()) != price;
		$.cache.get('#lot_'+lot+' .lot_username').html(leader);
		if(time > 0){
			h = Math.floor(time/3600);
			m = Math.floor((time - h*3600)/60);
			s = time - h*3600 - m*60;
			timediv.html((h>9?h:'0'+h)+':'+(m>9?m:'0'+m)+':'+(s>9?s:'0'+s));
			if(time<=15)
				timediv.removeClass('blue').addClass('red');
			else
				timediv.removeClass('red').addClass('blue');
		}
		else
			timediv.removeClass('blue').addClass('red').html('завершён');
		if(flash) {
			pricevalue.html(price + ' руб.').animate({fontSize: '20pt'}, 100, 'linear', function(){
				$(this).animate({fontSize: '18pt'}, 'slow', 'linear'); 
			});
			if($.cache.get('#economy').length>0){
        		$.cache.get('#economy').html(parseFloat($.cache.get('#lot_' + lot +' .lot_oldprice span').html()) - price + ' руб.');
      		}
			if(time==0)
				$('#autobid').hide('slow'); 
			updateBidHisory(lot);
		}
	}
	else
		return function(time, price, leader) {};
}