/******** ===PRELOAD FUNCTIONS********/
document.write("<style type=text/css>.tabs-hide,.js-hdn {display:none} div.ms-select ul {z-index:20001!important;display:none;position:absolute;min-height:80px;max-height:200px;_height:200px;}.w10-head-t2 .h1 a span{display:none} .png-fix{_visibility:hidden}</style>"); getCookie = function(name) {
    var nameEQ = name + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); }
    return null;
}
setCookie = function(name, value, days) {
    if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); var expires = "; expires=" + date.toGMTString(); }
    else expires = ""; document.cookie = name + "=" + value + expires + "; path=/";
}
openSponsor = function() { $('#ss-ad-sponsor-expand').animate({ height: 308, opacity: 100 }, 300); }
closeSponsor = function() { $('#ss-ad-sponsor-expand').animate({ height: 115, opacity: 100 }, 300); }
function initSponsor(p, c) {
    if (!c) { c = "dropDownSponsor" }
    if (!p) { p = 8000 }
    else { p = p * 1000 }
    if (getCookie(c) != 'false') { $('#ss-ad-sponsor-expand').animate({ height: 308, opacity: 100 }, 1, function() { document.getElementById('connect-sponsorAd1').GotoFrame('2'); setTimeout("document.getElementById('connect-sponsorAd1').GotoFrame('3');$('#ss-ad-sponsor-expand').animate({height: 115, opacity: 100}, 300);", p); setCookie(c, 'false'); }); } 
}

/*


/*equalise height of 3rd shaded column*/
EqCol = function() {

    if ($('#w10-content')[0]) {

        var con = $('#w10-content');
        var col = $('#col2-height');

        var conheight = con.height();
        var conoffset = con.offset().top;
        var coloffset = col.offset().top;
        var diff = conoffset - coloffset;
        var colheight = (conheight + diff) - 24;

        $('#col2-height').css('min-height', colheight);
        if ($.browser.version == "6.0" && $.browser.msie == true) {
            $('#col2-height').css('height', colheight);

        }
        clearTimeout(window.w10EqCol)
    }

    else { window.w10EqCol = setTimeout("EqCol()"), 150 }


}
EqCol();

//* check for styleswitcher cookie *//
if (getCookie('w10style')) {

    document.write("<link title='w10style' rel='stylesheet' href='/skysports/styles/w10/" + getCookie('w10style') + ".css' type='text/css' media='all' />");
    $(function() {
        $('.nav-drop #w10-country-menu a[rel=' + getCookie('w10style') + '],.w10-section-header #w10-country-menu a[rel=' + getCookie('w10style') + ']').addClass('on')

    })


}

else {

    document.write("<link title='w10style' rel='stylesheet' href='/skysports/styles/w10/def.css' type='text/css' media='all' />");
    $(function() { $('.nav-drop #w10-country-menu a:eq(0)').addClass('on'); })

}


jQuery.ajaxSetup({ cache: true });

var WC10 = {

    accordian: {

        bind: function() {
            $('.accordian-enable').each(function() {

                var thisaccordian = $(this);

                var Aspeed = 500;
                var Aremember = 'no';
                var Amulti = 'no';
                var Adelay = 'yes';
                var Aopen = 'no';

                if ($(this).attr('options')) {

                    var options = $(this).attr('options');
                    options = options.split("&");

                    $(options).each(function() {

                        var opts = this.split('=');
                        if (opts[0] == 'speed') { Aspeed = parseInt(opts[1]) }
                        if (opts[0] == 'remember') { Aremember = opts[1] }
                        if (opts[0] == 'delay') { Adelay = opts[1] }
                        if (opts[0] == 'multi') { Amulti = opts[1] }
                        if (opts[0] == 'open') { Aopen = parseInt(opts[1]) }
                    });

                }

                if (Amulti == 'yes') {

                    thisaccordian.children('ul').children('.accordian-item').children('.accordian-head').click(function() {

                        var thisitem = $(this);
                        thisitem.closest('.accordian-item').children('.accordian-section:visible').slideUp(Aspeed, function() { thisitem.removeClass('on') });
                        thisitem.closest('.accordian-item').children('.accordian-section:hidden').slideDown(Aspeed, function() { thisitem.addClass('on') })

                        WC10.accordian.remember({ con: thisaccordian, id: Aremember });
                    })
                }

                else {

                    if (Adelay == 'yes') {

                        thisaccordian.children('ul').children('.accordian-item').children('.accordian-head').click(function() {

                            var thisitem = $(this);

                            if (thisitem.closest('.accordian-item').children('.accordian-section:visible')[0]) {

                                thisitem.closest('.accordian-item').children('.accordian-section:visible').slideUp(Aspeed, function() {

                                    thisitem.removeClass('on');
                                    WC10.accordian.remember({ con: thisaccordian, id: Aremember });

                                })


                            }



                            else {
                                thisaccordian.children('ul').children('.accordian-item').children('.accordian-head').removeClass('on');
                                thisitem.addClass('on');
                                thisaccordian.children('ul').children('.accordian-item').children('.accordian-section:visible').slideUp(Aspeed, function() {

                                    thisitem.closest('.accordian-item').children('.accordian-section:hidden').slideDown(Aspeed, function() {
                                        WC10.accordian.remember({ con: thisaccordian, id: Aremember });


                                    });



                                });

                            }

                            if (!thisaccordian.children('ul').children('.accordian-item').children('.accordian-section:visible')[0]) {

                                thisitem.closest('.accordian-item').children('.accordian-section:hidden').slideDown(Aspeed, function() {
                                    WC10.accordian.remember({ con: thisaccordian, id: Aremember });

                                });
                                thisitem.addClass('on');

                            }



                        })

                    }


                    else {

                        thisaccordian.children('ul').children('.accordian-item').children('.accordian-head').click(function() {

                            thisaccordian.children('ul').children('.accordian-item').children('.accordian-section').slideUp(Aspeed)

                            $(this).closest('.accordian-item').children('.accordian-section:hidden').slideDown(Aspeed, function() {
                                WC10.accordian.remember({ con: thisaccordian, id: Aremember });

                            })

                        });




                    }

                }

                if (getCookie(Aremember) && Aopen == 'no') {
                    thisaccordian.find('.accordian-section').hide();
                    thisaccordian.find('.accordian-head').removeClass('on');
                    thisaccordian.find('.accordian-item:eq(' + getCookie(Aremember) + ')').find('.accordian-section').show();
                    thisaccordian.find('.accordian-item:eq(' + getCookie(Aremember) + ')').find('.accordian-head').addClass('on');

                }

                if (Aopen != 'no') {
                    thisaccordian.find('.accordian-section').hide();
                    thisaccordian.find('.accordian-head').removeClass('on');
                    thisaccordian.find('.accordian-item:eq(' + (Aopen - 1) + ')').find('.accordian-section').show();
                    thisaccordian.find('.accordian-item:eq(' + (Aopen - 1) + ')').find('.accordian-head').addClass('on');

                }

            })



        },

        remember: function(p) {
            if (p.id != 'no') {
                var index = p.con.find('.accordian-head').index(p.con.find('.accordian-head.on'));
                setCookie(p.id, index);
            }


        }
    }
}

$(document).ready(function() {
    $.ajaxSetup({ cache: true });
    prepareAccordion();

    $.getJSON('http://live.skysports.com/Betting/Football/JSON/prem_outrights.json?callback=?');


    //makes sure all boxes are filled - any not get a default skybet link
    timer = setTimeout("setDefaultLinks()", 1000);

    if ($('#ac1_sub').is(':visible') == false) {
        $('#ac1_head').trigger('click');
    }
});

function callback(data) {

    var eventList = data.xml[0].prem_outrights;

    //get the team_name if its a team or player page and create string to add to titles

    $(eventList).each(function() {
        var event = this.event;

        $(event).each(function(h) {
            var regor = /^Barclays Premier League Winner/;
            var reggs = /^Barclays Premier League Top Goalscorer/;

            var event_name = new String(event[2].description);

            if (h > 3) {
                var market = event[h].market;

                var market_name = new String(market[1].description);
                $(market).each(function(i) {
                    if (i > 4 && i < 8) {
                        var j = i - 4;
                        var selection = this.selection;
                        var selection_id = selection[0].selection_id;
                        var selection_name = selection[1].description;
                        var selection_price = new String(selection[5].price);

                        var splitPrice = selection_price.split("/");
                        var lp_num = splitPrice[0];
                        var lp_den = splitPrice[1];


                        if (event_name.match(regor)) {
                            if (j == 1) {
                                $('#ac1_head').html("Premier League Winner");
                                $('#ac1_sub').html("<strong>Winner Outright</strong>");
                                $('#ac1_gen').empty();
                                $('#ac1_gen').html("Full Premier League betting");
                            }
                            $('#ac1_name' + j).empty();
                            $('#ac1_name' + j).html(" " + selection_name + " <strong>" + selection_price + "</strong>");
                            $('#ac1_link' + j).val("https://www.skybet.com/secure/rbs?action=go_gmulti&ev_oc_id=" + selection_id + "&lp_num" + lp_num + "&lp_den" + lp_den + "&AFF_ID=4829&SS_FF_Accordian");
                            $('#ac1_link' + j).show();
                            $('#ac1_button').show();
                            $('#ac1_link' + j).css({ visibility: 'visible'});
                            $('#ac1_button').css({ visibility: 'visible' });

                        }
                        else if (event_name.match(reggs)) {
                            if (j == 1) {
                                $('#ac2_head').html("Top Goalscorer");
                                $('#ac2_sub').html("<strong>Top Goalscorer</strong>");
                                $('#ac2_gen').empty();
                                $('#ac2_gen').html("Full Premier League betting");
                            }
                            $('#ac2_name' + j).empty();
                            $('#ac2_name' + j).html(" " + selection_name + " <strong>" + selection_price + "</strong>");
                            $('#ac2_link' + j).val("https://www.skybet.com/secure/rbs?action=go_gmulti&ev_oc_id=" + selection_id + "&lp_num" + lp_num + "&lp_den" + lp_den + "&AFF_ID=4829&SS_FF_Accordian");
                            $('#ac2_link' + j).show();
                            $('#ac2_button').show();
                            $('#ac2_link' + j).css({ visibility: 'visible' });
                            $('#ac2_button').css({ visibility : 'visible'});
                        }

                    }
                });
            }
        });
    });
}

function launchBetslip(nameVal) {

    var ch_val = $("input[name=" + nameVal + "]:checked").val();

    var left = (screen.width / 2) - 320;
    var top = (screen.height / 2) - 250;

    if (ch_val != "" && ch_val != null) {
        window.open(ch_val, '_blank', 'width=1020,height=580,left=' + left + ',top=' + top);
    }
}

//fills in any blank boxes with a default link
function setDefaultLinks() {

    if ($('#ac1_link1').val() == "") {
        $('#ac1_head').html("Premier League Winner");
        $('#ac1_gen').html("<strong>Click here for Full Premier League betting</strong><br> </br>");
    }
    if ($('#ac2_link1').val() == "") {
        $('#ac2_head').html("Top Goalscorer");
        $('#ac2_gen').html("<strong>Click here for Full Premier League betting</strong><br> </br>");
    }

}

//sets all the buttons and check boxes to hidden ready for populating
function prepareAccordion() {

    $('#ac1_link1').hide();
    $('#ac1_link2').hide();
    $('#ac1_link3').hide();
    $('#ac1_button').hide();

    $('#ac2_link1').hide();
    $('#ac2_link2').hide();
    $('#ac2_link3').hide();
    $('#ac2_button').hide();
}
