var ab = { delay:500,topoffset:25,minwidth:55 };

$(function() {
  ab.graphwidth = ($('#graph ul li:first em').width() - ab.minwidth);

  if (ab.graphwidth > 0) {
		resetgraph(6);
		$(window).bind('load',function() {
			setTimeout("growgraph(0);",1050); // don't use callback in case overgraphads doesn't exist (rare)
			setTimeout("growgraph(1);",1300);
		});
	}
});

function growgraph(i) {
  if (i < graphs.length) {
    if (graphs[i] < 100) { ab.delay = (ab.delay == 500)?375:250; }
    var w = ((ab.graphwidth * graphs[i] / 100) + ab.minwidth);
    $('#bar'+i+' span:first').animate({width: w},ab.delay,function() { growgraph(i+2) });
  }
}

function resetgraph(reqbars) {
  var barcnt = 2;
  var $overgraphads = $('#overgraphads');
  if ($overgraphads.length > 0) {
    var adpos = $overgraphads.position().left;
    var leftoffset = parseInt($('#graph em').css('left'));
    if (isNaN(leftoffset)) { leftoffset = 0; }
    var btmoffset = parseInt($('#graph ul>li').css('marginBottom'));
		var i;
    for (i = 0; i < graphs.length; i++) {
      var barwidth = ((ab.graphwidth * graphs[i] / 100) + ab.minwidth + leftoffset + 7);
      if (barwidth >= adpos) {
        ab.topoffset += (barcnt*(40+btmoffset));
        barcnt = 1;
      }
      else {
        if (barcnt++ == reqbars) { break; }
      }
    }
		if (i == (reqbars - 2)) { $overgraphads.css({ top:'14px'}); } // put the ad a little farther up if it fits right away
		else { $overgraphads.css({ top: ab.topoffset }); }
  }
  $('#graph span.bar').css({ width:'auto' });
}
