function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days2460601000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function saveBroken() {
broke++;
currentBid = getCurrentBid();
createCookie($("title").text()+'broke', broke, 3);
if ($(".bidBot").find(".brokeV").length)
{
$(".bidBot").find(".brokeV").text(broke);
}
else
{
var text = '<tr><td class="broke"><h5>Broke</h5></td><td class="brokeV"><h5></h5></td></tr>';
$('body').append(text);
$(".bidBot").find(".brokeV").text(broke);
}
needSave = 0;
}

function editTable(name) {
var price = parseFloat($("#bhp_1").text().slice(1));
if (readCookie(name) != null)
{
var nameBid = parseFloat(readCookie(name));
createCookie(name, Math.round((nameBid + .60)100)/100, 3);
if ($(".bidBot").find("td:contains('"+name+"')").length > 0)
{
var cloneTable = $(".bidBot").find("td:contains('"+name+"')").eq(0).parent("tr").clone();
$(".bidBot").find("td:contains('"+name+"')").each(function() {
$(this).parent("tr").remove();
});
$(cloneTable).find("td:contains('"+name+"')").next("td").text(Math.round((nameBid + price + .60)100)/100);
$(cloneTable).find("td:contains('"+name+"')").parent("tr").removeClass();
$(cloneTable).find("td:contains('"+name+"')").parent("tr").addClass("20");
$(".bidBot").find("table").prepend(cloneTable);
}
else
{
$(".bidBot").find("table").prepend('<tr class="20"><td class="name">'+name+'</td><td class="deep">'+Math.round((price + .60)100)/100+'</td></tr>');
}
}
else
{
createCookie(name, Math.round((0.60)100)/100, 3);
$(".bidBot").find("table").prepend('<tr class="20"><td class="name">'+name+'</td><td class="deep">'+Math.round((price + .60)100)/100+'</td></tr>');
}
needSave--;
currentTable = $(".bidBot").clone().html();
createCookie($("title").text()+'currentTable', currentTable, 3);
}

function bidderRank()
{
$(".bidBot").find("tr").each(function() {
var value = parseFloat($(this).attr("class"));
if (value < 10)
{
if (value == 0)
{
$(this).attr("style", "background-color:silver;");
}
else
{
$(this).attr("style", "background-color:yellow;");
value--;
}
}
else
{
$(this).removeAttr("style");
value--;
}
$(this).attr("class", value);
});
}

function main() {
var i = 0;

if (needSave > 5)
{
saveBroken();
}
if (timer)
{
timer = 0;
var formatBid = (Math.round((currentBid + .01)100)/100).toFixed(2);
var bid = Math.round((currentBid + .01)*100)/100;
if ($("#bidding-history").find("td:contains('$"+formatBid+"')").length != 0)
{
needSave++;
uName = $("#bidding-history").find("td:contains('$"+formatBid+"')").next(".underlineMeHover").text();
if (uName.length < 17)
{
if ($(".bidBot").find(".name:contains('"+uName+"')").length)
{
$(".bidBot").find(".name:contains('"+uName+"')").each(function() {
if ($(this).text() == uName)
{
editTable(uName);
currentBid = bid;
createCookie($("title").text()+'currentBid', currentBid, 3);
bidderRank();
}
// Probably should add some indicator for this later
});
}
else
{
editTable(uName);
currentBid = bid;
createCookie($("title").text()+'currentBid', currentBid, 3);
bidderRank();
}

}
}
timer = 1;
}

}

var style = '<style type="text/css">'+
'td.name, td.broke {'+
'text-align:left;'+
'}'+
'td.deep, td.brokeV {'+
'text-align:right;'+
'}'+
'</style>';

if (readCookie($("title").text()+'currentTable') != null)
{
var html = '<div class="bidBot" style="z-index:4000;position:fixed;top:0;left:20px;background-color:white;border-style:solid;border-width:1px;border-color:black;">'+
readCookie($("title").text()+'currentTable')+'</div>';
}
else
{
var html = '<div class="bidBot" style="z-index:4000;position:fixed;top:0;left:20px;background-color:white;border-style:solid;border-width:1px;border-color:black;">'+
'<table>'+
'<tr><td class="broke"><h5>Broke</h5></td><td class="brokeV"><h5></h5></td></tr>'+
'</table>'+
'</div>';
}

$("body").prepend(style+html);

if (readCookie($("title").text()+'broke') != null)
{
broke = parseFloat(readCookie($("title").text()+'broke'));
}
else
{
broke = 0;
}

if ($(".broke").length == 0)
{
$(".bidBot").find("table").append('<tr><td class="broke"><h5>Broke</h5></td><td class="brokeV"><h5>'+broke+'</h5></td></tr>');
}

needSave = 0;

var timer = 1;
bidders = new Array;

function getCurrentBid() {
var cookieBid = 0;
if (readCookie($("title").text()+'currentBid') != null)
{
cookieBid = parseFloat(readCookie($("title").text()+'currentBid'));
}
for (var i=9; i>0; i--)
{
if ($("#bhp_"+i+":contains('$')").length)
{
currentBid = parseFloat($('#bhp_'+i).text().slice(1));
if (currentBid <= cookieBid)
{
return cookieBid;
}
else
{
return currentBid;
}

}
}

}

currentBid = getCurrentBid();
setInterval("main()",10);