(function($) {

    $.fn.pricewise = function() {
        return this.each(function() {

            // Need these later
            var me = this;

            // Split event and outcome descriptors
            descriptor = $(this).attr('rel').split('|');
            outcome    = descriptor.pop().replace(/[^a-z0-9-]/g, '');
            event_desc = descriptor.join('|');

            // Set a unique ID
            $(this).attr("id", "pwBet_" + $.data(this)); 

            // Define the URL            
            var pwURL = 'http://pricewise.gg.com/v3/?bookmaker=-1&tok=' + tok + '&descriptor=' + event_desc + '&runner=' + outcome + '&jsonp=?';
            $.getJSON(pwURL, null, function(data) {

                // Check for error state
                if (data.Error) return;
                if (!data.Selections.length) return;
                if (!data.Selections[0].Price) return;

                try {

                    // Update the title
                    $(me).find('H3').html(data.Selections[0].EventName.replace(/ /g, '&nbsp;') + ': ' + data.Selections[0].MarketName.replace(/ /g, '&nbsp;'));
                    
                    // Grab a reference to the container element
                    var container = $(me).find('.bet_row');

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

                        // Clone a container element
                        var $local = $(container).clone().appendTo(me);

                        // Update the outcome name
                        $local.find('.outcome').text(data.Selections[i].OutcomeName);
                        
                        // Update the price
                        $local.find('.price').text(data.Selections[i].Price[0].DecimalPrice);
        
                        // Update the bookie image
                        $local.find('.bookmaker img').attr({
                            'src': vimagedir + '/' + data.Selections[i].Price[0].BookmakerID + '.png',
                            'alt': data.Selections[i].Price[0].BookmakerName,
                            'title': data.Selections[i].Price[0].BookmakerName
                        });
                        
                        // Update the bookie link
                        $local.find('.bookmaker').attr({
                            'href': 'http://pricewise.gg.com/v3/signup.php?partner=gg.com&bookmaker=' + data.Selections[i].Price[0].BookmakerID
                        });
        
                        // Update the bet button         
                        $local.find('.bet_link A').attr({
                            'href': data.Selections[i].Price[0].URL
                        });
                        
                        // Bind a click to the bet button
                        if (data.Selections[i].Price[0].SlipWidth > 0) {
                            $local.find('.bet_link A').click(function() {
                                popup(this.href, data.Selections[i].Price[0].SlipWidth, data.Selections[i].Price[0].SlipHeight, true);
                                return false;
                            });
                        };
                    });
                    
                    // Remove the template container
                    $(container).remove();
    
                    // Display the price
                    $(me).show();

                } catch (err) {};
            });
        });
    };    

})(jQuery);
