var counter = 0;

function enableBidding (sale, val) {
    if (val) {
        $('#bidimg_'+sale).attr('src', $('#imgButtonBidOn').text());
        $('#bidimg_'+sale).click(function(){ bid(sale); });
        $('#bidimg_'+sale).hover(
            function(){ $('#bidimg_'+sale).fadeTo(  1, 1.0); },
            function(){ $('#bidimg_'+sale).fadeTo(800, 0.6); }
            );
        $('#bid_'+sale).click(function(){ bid(sale); });
    	$('#bid_'+sale).removeAttr('disabled');
    }
    else {
        $('#bidimg_'+sale).unbind();
        $('#bid_'+sale).unbind();
    	$('#bid_'+sale).attr('disabled', 'disabled');
        $('#bidimg_'+sale).attr('src', $('#imgButtonBidOff').text());
        $('#bidimg_'+sale).fadeTo(1,1);
    }
}

function testAllowBidding (sale, left) {
	if (left < 0) {
        enableBidding (sale, false);
    }
    else {
	    var disabled = !$('#currUser').text() || ($('#currUser').text() == $('#winner_'+sale).html());
        enableBidding (sale, !disabled);
    }
}

function pad2 (n) {
    return n < 10 ? '0'+n : n;
}

function moneyStr (n) {
    var i = Math.floor(n / 100);
    var d = pad2(n % 100);
    return '&euro; '+ i +','+ d;
}

function timeLeftStr (i, cents, h, m, s) {
    var time = ((h * 60 + m) * 60 + s);
    if (time < 0) {
        if (cents < 0+$('#bottom_'+i).text()) {
            return $('#msg_bonus').html();
        }
        else {
            return $('#msg_sold').html();
        }
    }
    var str = '';
    if (h > 23) {
        var d = Math.floor(h/24);
        h %= 24;
        str = '' + d + 'd ';
    }
    str += (time < 60) ? ('' + s +' sec !!') : (h + 'u ' + pad2(m)+'m ' + pad2(s)+'s') ; 
    var color = (time < 1800) ? 'rgb('+(255 - Math.floor(time/7))+',0,0)' : 'black';
    return '<b style="color:'+color+'">'+str+'</b>';
}

function update (data, state) {
    if (++counter > 900) {
        location.replace(location.pathname);
    }
	$.each (data, function(i, item) {
			var oldWinner = $('#winner_'+i).html();

			$('#left_'+i).html(timeLeftStr(i, item.c, item.lh, item.lm, item.ls));

			var newPrice = moneyStr(item.c);
			var oldPrice = $('#price_'+i).html();
			if (oldPrice != newPrice) {
                if (item.c < $('#bottom_'+i).text()) {
				    $('#price_'+i).html(newPrice + ' <a class="reserve" title="'+$('#msg_reserve_price').text()+'" href="freeflipz.php">&lt; '+moneyStr($('#bottom_'+i).text())+'</a>');
                }
                else {
				    $('#price_'+i).html(newPrice);
                }
			}

			if (oldWinner != item.u) {
				$('#winner_'+i).html(item.u).animate({ letterSpacing:"-10px"}, 100).animate({letterSpacing:"0px"}, 100);
			    testAllowBidding(i, item.ls);
			}
            else if (item.ls < 4) {
			    testAllowBidding(i, item.ls);
            }

		}
	);
}

function tick () {
	$.getJSON('http://89.255.12.70?callback=?', update);
	setTimeout(tick, 1000);
}

function bid (sale) {
    enableBidding(sale, false);
	$.get('/ajax.php?bid='+sale, function(data) {data = data.replace(/"/g, '').split(':')[0]; if (data == '0') { infoBuyNow(); }; $('#credits').html(data)});
}

function toggleExtraInfo (sale) {
        $('#extra_'+sale).toggle();
}

function follow() {
    var top  = $(this).position().top;
    var left = $(this).next('span').position().left;
    $('#info').show();
    $('#info').animate({'left':left, 'top':top}, 200);
    $('#infoText').html($(this).next('span').next('span').html());
}
function infoBuyNow() {
    var el   = $('#buyButton');
    $('#infoText').html(el.next('span').html());
    var top  = $('#buyButton').position().top;
    var left = $('#buyButton').position().left - $('#info').width();
    $('#info').show();
    $('#info').animate({'left':left, 'top':top}, 200);
}

function fbs_click(u, t) {
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}

$(function() {
        $.ajaxSetup({cache:false});
        $('.bidimg').hover(
            function(){ $(this).fadeTo(  1, 1.0); },
            function(){ $(this).fadeTo(800, 0.6); }
            );
        $('#focus').focus();
        tick();
    })
