//Script must be referenced on page after any targeted buttons are loaded, and before Hitbox script (thus before doc load)

//Set Hitbox link IDs by default in "name" attribute on buttons--apparently required by Hitbox base code
$("a.button").each(function() {
    var linkName = $(this).attr("name");
    linkName = (typeof linkName == "undefined" || linkName.length == 0)? "&lid="+$(this).text() : linkName;
    $(this).attr("name", linkName);
    var position = getPosition.apply(this);
    //Create a list of dummy links to hook into Hitbox page view routine
    $("#wrapper").before("<a href='javascript:noop()' name='"+linkName+"' style='position:absolute;z-index:1;top:"+position.top+"px;left:"+position.left+"px;width:"+position.width+"px;height:"+position.height+"px'></a>");
});

function noop() {}

function getPosition() {
    var offset = {};
    $(this).offset({scroll: false, margin: false}, offset);
    if (offset.top == 0 && $(this).parents(".styleSelector").length == 1) {
        return getPosition.apply($(".button",".styleSelector.selected").get(0));
    }
    else {
        var returnee = {top: offset.top, left: offset.left, width: $(this).innerWidth(), height: $(this).innerHeight()};
        returnee.toString = function() {return this.top+","+this.left+","+this.width+","+this.height};
        return returnee;
    }
}

function doHitboxSend() {
    _hbLink($(this).attr("name").substr(5), '', '');
}

$("div.buttonContainer").filter(function() {
    return $(this).children(".button").length == 1;
}).mousedown(function() { doHitboxSend.apply($(this).children(".button").get(0)); });

$("a.button").filter(function() {
    return $(this).parent(".buttonContainer").length == 0;
}).mousedown(function() { doHitboxSend.apply(this); });