﻿var SBROdds_Plugin;if (typeof (jQuery) != 'undefined') {    (function ($) {
        $(document).ready(function () {
            $('div#sbrodds-plugin-output').parent().addClass('sbrodds-plugin-widget');
            $('div.sbrodds-plugin-dates strong a').live('click', function (e) {
                $(this).parent().parent().find('strong').removeClass('sbrodds-plugin-dates-item');
                $(this).parent().addClass('sbrodds-plugin-dates-item');
                $('.sbrodds-plugin-group', '#sbrodds-plugin-content').hide();
                $('#' + $(this).attr('rel'), '#sbrodds-plugin-content').show();
            });
            $('div.sbrodds-plugin-event').live({
                mouseenter: function (e) {
                    var $event = $(this);
                    $('.sbrodds-plugin-side-consensus', $event).show().each(function (i) {
                        $(this).stop().animate({ width: $(this).attr('rel') + '%' }, 200, 'linear');
                    });
                },
                mouseleave: function (e) {
                    var $event = $(this);
                    $('.sbrodds-plugin-side-consensus', $event).each(function (i) {
                        $(this).stop().animate({ width: '0%' }, 200, 'linear', function (i) { $(this).hide(); });
                    });
                }
            });

            var sbrodds_onLoad = function () {
                $('div.sbrodds-plugin-group-events-over').each(function (i) {
                    var $this = $(this);
                    var labelHeight = $('h5', $this).outerHeight();
                    $this.css({
                        height: $this.height() + 'px'
                    }).attr('start-height', $this.height());
                    var inner = $('<div />').addClass('sbrodds-plugin-group-inner').css({
                        top: labelHeight + 'px'
                    });
                    $this.append(inner);
                    $('div.sbrodds-plugin-status-group', $this).appendTo(inner);
                    $('div.sbrodds-plugin-event').show();
                });
            };
            sbrodds_onLoad();
            $('div.sbrodds-plugin-more-events a').live('click', function (e) {
                var $this = $(this);
                var $holder = $this.parents('.sbrodds-plugin-holder');
                var $inner = $('div.sbrodds-plugin-group-inner', $holder);
                var $outer = $('div.sbrodds-plugin-group-events-over', $holder);
                var expanded = $this.is('.sbrodds-plugin-expanded');
                var height = expanded ? $outer.attr('start-height') : $inner.height() + $inner.position().top;				
                $outer.animate({ height: height + 'px' }, 200, function () {
                    if (!expanded)
                        $this.addClass('sbrodds-plugin-expanded').html('Show Less Games');
                    else
                        $this.removeClass('sbrodds-plugin-expanded').html('Show More Games');
                });
            });
            if (typeof (sbrodds_params) == 'undefined' || sbrodds_params == null) {
                return;
            }
            SBROdds_Plugin = SBROdds_Plugin || {
                offset: 10,
                callTimeout: 0,
                sendingAjax: false,
                ordinal: function (num) {
                    var mod1 = num % 100;
                    var mod2 = num % 10;
                    var ord;
                    if ((mod1 - mod2) == 10) {
                        ord = "th";
                    } else {
                        switch (mod2) {
                            case 1: ord = "st"; break;
                            case 2: ord = "nd"; break;
                            case 3: ord = "rd"; break;
                            default: ord = "th"; break;
                        }
                    }
                    return num + ord;
                },
                init: function () {
                    if (sbrodds_params == null) return false;
                    this.getData(sbrodds_params);
                },
                getData: function (args) {
                    $.getJSON((odds_base_url ? odds_base_url : '/wp-content/plugins/sbrodds-widget/') + 'ajax.php?a=update', args, function (data) {
                        var t = data['sbr-odds'];
                        SBROdds_Plugin.updateParams({
                            timestamp: t.timestamp,
                            key: t.key
                        });
                        // Ajax stuff
                        SBROdds_Plugin.findEvents(t);
                        // Set timer for next call
                        SBROdds_Plugin.callTimeout = setTimeout(function () {
                            SBROdds_Plugin.getData(sbrodds_params);
                        }, SBROdds_Plugin.offset * 1000);
                    });
                },
                findEvents: function (data) {
                    $(data.sports).each(function (i, sports) {
                        $(this.sport).each(function (j, sport) {
                            $(this.leagues).each(function (k, leagues) {
                                $(this.league).each(function (l, league) {
                                    $(this['event-groups']).each(function (m, event_groups) {
                                        $(this['event-group']).each(function (n, event_group) {
                                            $(this.events).each(function (o, events) {
                                                $(this.event).each(function (p, event) {
                                                    SBROdds_Plugin.updateEvent(event);
                                                });
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    });
                },
                doScores: function (event, $event) {
                    $(event['event-sides']).each(function (i, event_sides) {
                        $(event_sides['event-side']).each(function (j, event_side) {
                            $(event_side['side-scores']).each(function (k, side_scores) {
                                SBROdds_Plugin.updateScore($event, event_side.id, side_scores['score-total']);
                            });
                        });
                    });
                },
                updateScore: function ($event, sideid, score) {
                    var selector = '#sbrodds-side-' + sideid;
                    var $score = $(selector + ' span.sbrodds-plugin-side-score');
                    if ($score.length == 0) return;
                    if ($score.html() == score) return;
                    $score.html(score);
                },
                doLines: function (event, $event) {
                    // Update side lines
                    $(event['event-sides']).each(function (i, event_sides) {
                        $(event_sides['event-side']).each(function (j, event_side) {
                            $(event_side['side-lines']).each(function (k, side_lines) {
                                $(side_lines['line-type']).each(function (l, line_type) {
                                    $(line_type.lines).each(function (m, lines) {
                                        SBROdds_Plugin.updateLine($event, line_type.id, lines.line, event_side.id);
                                    });
                                });
                            });
                        });
                    });
                    // Update totals
                    $(event['event-lines']).each(function (i, event_lines) {
                        $(event_lines['line-type']).each(function (l, line_type) {
                            $(line_type.lines).each(function (m, lines) {
                                SBROdds_Plugin.updateLine($event, line_type.id, lines.line);
                            });
                        });
                    });
                },
                updateLine: function ($event, linetype, newline, sideid) {
                    $(newline).each(function (i, line) {
                        var selector = '#sbrodds-linetype-' + linetype + '-' + (linetype == 3 ? line.side : sideid);
                        var $line = $(selector, $event);
                        if ($line.length == 0) return;
                        var p = '';
                        // For spreads and totals show adjust
                        if (line.adjust && (linetype == 1)) {
                            p += (line.adjust > 0 ? '+' : '') + line.adjust + ' ';
                        }
                        // For moneylines
                        if (line.price && (linetype == 2)) {
                            p += (line.price > 0 ? '+' : '') + line.price;
                        }
                        // For totals
                        if (line.adjust && (linetype == 3)) {
                            p += line.adjust;
                        }
                        if ($line.html() == p) return;
                        $line.html(p).addClass('sbrodds-plugin-change');
                        if ($line.length > 0) {
                            clearTimeout($line[0].to);
                            $line[0].to = setTimeout(function () {
                                $line.removeClass('sbrodds-plugin-change');
                            }, 25 * 1000);
                        }
                    });
                },
                doStatus: function (event, $event) {
                    var status = event['event-status'];
                    if (!$event.parent().is('.sbrodds-plugin-' + status)) {
                        $('.sbrodds-plugin-' + status).append($event);
                    }
                    var $event_stats = $('.sbrodds-plugin-status', $event);
                    event_stats = event['event-stats'];
                    if ($event_stats.length == 0 || !event_stats) return;
                    $event_stats.html(event_stats['event-stat']);
                },
                doConsensus: function (event, $event) {
                    // Update consensus
                    $(event['event-sides']).each(function (i, event_sides) {
                        $(event_sides['event-side']).each(function (j, event_side) {
                            $(event_side['side-details']).each(function (k, side_consensus) {
                                SBROdds_Plugin.updateConsensus($event, side_consensus.consensus, event_side.id);
                            });
                        });
                    });
                },
                updateConsensus: function ($event, consensus, side_id) {
                    $side = $('#sbrodds-side-' + side_id, $event);
                    $consensus = $('.sbrodds-plugin-consensus', $side);
                    if ($consensus.length == 0 || consensus == 0)
                        return;
                    consensus = parseInt((parseFloat(consensus) * 100) * 10) / 10;
                    $('.sbrodds-plugin-side-consensus', $side).attr('rel', consensus);
                    $consensus.html(consensus + '%');
                },
                updateEvent: function (event) {
                    var $event = $('#sbrodds-event-' + event.id);
                    if ($event.length == 0) return;
                    SBROdds_Plugin.doLines(event, $event);
                    SBROdds_Plugin.doStatus(event, $event);
                    SBROdds_Plugin.doScores(event, $event);
                    SBROdds_Plugin.doConsensus(event, $event);
                },
                updateParams: function (obj) {
                    $.extend(sbrodds_params, obj);
                },
                getNewLeague: function (obj, leagueId) {
                    if (SBROdds_Plugin.sendingAjax)
                        return false;
                    SBROdds_Plugin.sendingAjax = true;
                    var args = $.extend(sbrodds_params, {});
                    args.leagueId = [leagueId];
                    $holder = $(obj).parents('.sbrodds-plugin-holder');
                    $.get((odds_base_url ? odds_base_url : '/wp-content/plugins/sbrodds-widget/') + 'ajax.php?a=get', args, function (html) {
                        $holder.html(html);
                        sbrodds_onLoad();
                        SBROdds_Plugin.sendingAjax = false;
                    });
                }
            }
            if (SBROdds_Plugin.init()) {
            }
        });
    })(jQuery);};
