if (typeof(oPriceManager) == "undefined") {

    var oPriceManager = {

        // Set price refresh rate
        iRefresh: 60000,

        // Store timeout
        iTimeoutID: null,

        // Setup the buttons to have no betting
        init: function() {
            $('.betButton').click(function() {
                alert('Betting is not currently available on this horse');
                return false;
            });
        },

        // loadPrice - loads betting data for runner(s)
        loadPrice: function(sEvent, sPrefix, iRunner, iRunnerID) {

            // Clear the timeout
            clearTimeout(oPriceManager.iTimeoutID);

            // Define the lookup URL
            sURL = vpricedir + '/v3/?tok=' + tok + '&descriptor=' + sEvent + '&bookmaker=' + iOddsBookie + (iRunner ? ('&runner=' + iRunner) : '') + '&jsonp=?';

            // Load JSON data from this URL
            $.getJSON(sURL, function(data) {
                
                if (data.Selections.Error && !iRunner) {

                    // Show an error graphic
                    $('.bookieLogo').attr({
                        'src'   : vimagedir + '/bookmakers/grid/error.png',
                        'alt'   : 'No Price'
                    }).parent().attr({
                        'title' : null,
                        'href'  : '#',
                        'target': '_self'
                    });
                    return false;
                };

                // Loop through returned selections
                $.each(data.Selections, function(i, oItem){

                    // Define the selection to work with
                    iSel = iRunnerID ? iRunnerID : oItem.RunnerNum;

                    // Update this button
                    oPriceManager._updateButton('#' + sPrefix + iSel, oItem);
                });

                // Set a timer to reload later
                oPriceManager.iTimeoutID = setTimeout('oPriceManager.loadPrice("' + sEvent + '", "' + sPrefix + '", ' + iRunner + ', ' + iRunnerID + ');', oPriceManager.iRefresh);
            });
        },

        // Update buttons with betting links
        _updateButton: function(oRunner, oItem) {

            // Find the relevant stuff
            var $price  = $(oRunner).find('.price');
            var $button = $(oRunner).find('.betButton');
            var $logo   = $(oRunner).find('.bookieLogo');

            // Check we have all the data we need
            if (oItem.Error || !oItem.Price) {

                // Show an error graphic
                $logo.attr({
                    'src'   : vimagedir + '/bookmakers/grid/error.png',
                    'alt'   : 'No Price'
                }).parent().attr({
                    'title' : null,
                    'href'  : '#',
                    'target': '_self'
                });
                return false;
            };

            // Update the bookie logo
            $logo.attr({
                'src'   : vimagedir + '/bookmakers/grid/' + oItem.Price[0].BookmakerID + '.png',
                'alt'   : oItem.Price[0].BookmakerName
            }).parent().attr({
                'title' : 'Open account with ' + oItem.Price[0].BookmakerName,
                'href'  : vhttpdir + '/bookiesignup.php?bookie=' + oItem.Price[0].BookmakerID,
                'target': '_blank'
            });

            // If numerator is 0, then set SP
            if (oItem.Price[0].Numerator == 0) {
                $price.html('SP');

            // Use decimal format
            } else if (iOddsFormat == 0) {
                $price.html(oItem.Price[0].DecimalPrice);

            // Use decimal format
            } else if (iOddsFormat == 2) {
                $price.html(oItem.Price[0].AmericanPrice);

            // Use fractional format, checking for evens
            } else if(oItem.Price[0].Numerator == 1 && oItem.Price[0].Denominator == 1) {
                $price.html('EVENS');

            } else {
                $price.html(oItem.Price[0].Numerator + '-' + oItem.Price[0].Denominator);
            }

            // Construct the click URL and store on the button
            $button.attr({
                'href'   : oItem.Price[0].URL,
                'target' : '_blank'
            });

            // Unbind any previous click events on this link
            $button.unbind('click');

            // Bind a new click event, if required
            if (oItem.Price[0].SlipWidth > 0) {
                $button.click(function() {
                    oSlip = window.open(this.href, '_blank', 'resizable=no,scrollbars=yes,status=no,menubar=no,toolbar=no,location=no,directories=no,width=200,height=200,scrollbars=no');
                    return false;
                });
            }
        }
    }

    $(document).ready(oPriceManager.init);
};