﻿sportLoadMenu = {
    loadedMenu: [],
    tagLoadToId: 'bannerMenu',
    firstLoad: false,
    currentSportId: -1,
    currentSportName: '',
    menuUrl: '',
    registeredClose: false,
    isAMyAccountPage: true,
    url_Allsports: '',
    //    addHeight: false,
    registerClose: function() {
        if (this.registeredClose) {
            return;
        }
        j$(document).ready(function() {
            j$(document.body).bind('click',
                function(event) {
                    if (sportLoadMenu.isSelect && !isEffectByEvent(event, 'sportMenuBlock')) {
                        sportLoadMenu.showSelectSport();
                    }
                }
            );
        });
        this.registeredClose = true;
    },
    load: function(sportId, sportName) {
        if (sportId == this.currentSportId) {
            return;
        }
        this.currentSportId = sportId;
        this.currentSportName = sportName;
        if (!this.firstLoad) {
            j$.get(this.menuUrl + '?sportId=' + sportId, function(data) {
                sportLoadMenu.loadedMenu[sportId] = data;
                sportLoadMenu.afterLoad(data);
                sportLoadMenu.firstLoad = true;
            });
        }
        else {
            if (this.loadedMenu[sportId] != null) {
                j$.get(this.menuUrl + '?sportId=' + this.currentSportId + '&recache=1&t=' + (new Date()).getTime(), function(data) { sportLoadMenu.afterLoad(sportLoadMenu.loadedMenu[sportId]); });

            }
            else {
                j$.get(this.menuUrl + '?sportId=' + sportId, function(data) {
                    sportLoadMenu.loadedMenu[sportId] = data;
                    sportLoadMenu.afterLoad(data);
                });
            }
        }

        this.setSportName(sportName);
        this.showSelectSport();
    },
    load_Allsports: function() {
        
        j$.get(this.url_Allsports, function(data) {
        sportLoadMenu.afterLoad(data);
        sportLoadMenu.firstLoad = true;
        });
        this.showSelectSport();
    },
    sifrChanged: function() {
        //        setTimeout("sIFR.replacements['#liButtonClose span'][0].getFlashElement().width = parseInt(sIFR.replacements['#liButtonClose span'][0].getFlashElement().width, 10) + 2;", 100);
    },
    setSportName: function(sportName) {
        if (this.isAMyAccountPage) {
            this.onReplaceSifr = function(fi) {
                //                sIFR.replacements['#liButtonClose span'][0].replaceText(sportName);
                //                sIFR.replacements['#liButtonClose span'][0].resize();
                sportLoadMenu.sifrChanged();
            }
            //            if (sIFR.replacements['#liButtonClose span'] == null || sIFR.replacements['#liButtonClose span'][0] == null) {

            //                sIFR.replace(selectAsport, { selector: '#liButtonClose span', css: { '.sIFR-root': { 'color': '#ffffff', 'leading': -2} }, wmode: 'transparent', fitExactly: true, forceSingleLine: true, onReplacement: sportLoadMenu.onReplaceSifr
            //                });
            //            }
            //            else {
            this.onReplaceSifr();
            //            }
        }
        else {
            j$('#liButtonClose span').html(sportName);
        }

        j$('#liButtonClose').css('width', 'auto');
    },
    runIntellimatch: function() {
        if (window.intelSelectSport) {
            var haveSport = false;
            j$('#connections_sport option').each(function() {
                if (haveSport) {
                    return;
                }
                if (j$(this).val() == this.currentSportId) {
                    haveSport = true;
                }
            });

            intelSelectSport(this.currentSportId);
        }
    },
    runMyNews: function() {
        if (window.MyNews && window.MyNews.loadNews) {
            j$('#divDashboardMyNewsEdit input[type=checkbox][value!=' + this.currentSportId + ']').parent().hide();
            j$('#divDashboardMyNewsEdit input[type=checkbox][value=' + this.currentSportId + ']').attr('checked', 'checked').attr('disabled', 'disabled').parent().show();
            j$('#hidDashboardMyNewsSports').val(this.currentSportId + '_' + j$('#cboDashboardMyNewsNoTitle').val());
            MyNews.loadNews(true);

            j$('#MyNewsApply').unbind('click').click(function() {
                MyNews.setSportsValue();
                MyNews.loadNews(true);
            });
        }
    },
    setStaticMenu: function(sportId) {
        j$('#' + this.tagLoadToId + ' script').each(function() {
            eval(j$(this).html());
        });
        j$('#bannerSearchbar script').each(function() {
            eval(j$(this).html());
        });
        var text = '<response><menu><![CDATA[\n' + j$('#' + this.tagLoadToId).html() + '\n]]></menu><searchbar><![CDATA[\n' + j$('#bannerSearchbar').html() + '\n]]></searchbar></response>';
        var xmlDoc;
        if (window.DOMParser) {
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(text, "text/xml");
        }
        else // Internet Explorer
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
            xmlDoc.loadXML(text);
        }
        sportLoadMenu.loadedMenu[sportId] = xmlDoc;
        j$('#' + this.tagLoadToId).parent().height(j$('#' + this.tagLoadToId).parent().height() + 20);
        j$('#' + this.tagLoadToId).show();
        j$('#bannerSearchbar').show();
        j$('.bgheaderNoSport').css('border', 'none');
        this.firstLoad = true;
        this.runMyNews();
        this.runIntellimatch();
    },
    changeStyle_BgHeaderNoSport: function() {
        j$('#' + this.tagLoadToId).parent().height(j$('#' + this.tagLoadToId).parent().height() + 20);
        j$('.bgheaderNoSport').css('border', 'none');
    },
    afterLoad: function(data) {
        jQuery('#div_MySportNavigations').hide();
        j$('.bgheaderNoSport').css('border', 'none');
        var xmlContent = j$(data);
        if (!this.firstLoad) {
            j$('#' + sportLoadMenu.tagLoadToId).css('filter', 'alpha(opacity=1)');
            j$('#' + sportLoadMenu.tagLoadToId).css('opacity', '0.01');

            j$('#bannerSearchbar').css('filter', 'alpha(opacity=1)');
            j$('#bannerSearchbar').css('opacity', '0.01');

            j$('#' + this.tagLoadToId).html(xmlContent.find('menu').text());
            j$('#bannerSearchbar').html(xmlContent.find('searchbar').text());

            j$('#' + this.tagLoadToId).parent().animate({ height: (j$('#' + this.tagLoadToId).parent().height() + 20) }, 'slow', function() {
                j$('#' + sportLoadMenu.tagLoadToId).css('display', 'none');
                j$('#' + sportLoadMenu.tagLoadToId).css('filter', '');
                j$('#' + sportLoadMenu.tagLoadToId).css('opacity', '');
                j$('#' + sportLoadMenu.tagLoadToId).fadeIn('slow');
            });
            j$('#bannerSearchbar').slideDown('slow', function() {
                j$('#bannerSearchbar').css('display', 'none');
                j$('#bannerSearchbar').css('filter', '');
                j$('#bannerSearchbar').css('opacity', '');
                j$('#bannerSearchbar').fadeIn('slow');
            });
            this.isFirstLoad = true;
        }
        else {
            j$('#' + this.tagLoadToId).fadeOut('fast', function() {
                j$('#' + sportLoadMenu.tagLoadToId).html(xmlContent.find('menu').text());
                j$('#' + sportLoadMenu.tagLoadToId + ' script').each(function() {
                    eval(j$(this).html());
                });
                j$('#' + sportLoadMenu.tagLoadToId).fadeIn('slow');
            });

            j$('#bannerSearchbar').fadeOut('fast', function() {
                j$('#bannerSearchbar').html(xmlContent.find('searchbar').text());
                j$('#bannerSearchbar script').each(function() {
                    eval(j$(this).html());
                });
                j$('#bannerSearchbar').fadeIn('slow');
            });
        }
        j$('#myAccountSportName').html(this.currentSportName);
        this.runMyNews();
        this.runIntellimatch();
    },
    isSelect: false,
    onReplaceSifr: function(fi) {
        if (sportLoadMenu.isSelect) {
            //            sIFR.replacements['#liButtonClose span'][0].changeCSS('.sIFR-root {color:#E4E4E4;}');
            sportLoadMenu.setWidth();
        }
        sportLoadMenu.sifrChanged();
    },
    showSelectSport: function() {
        if (this.isSelect == false) {
            j$('#menuSportButton').attr('class', 'sp_option sp_open');
            this.onReplaceSifr = function(fi) {
                //                sIFR.replacements['#liButtonClose span'][0].changeCSS('.sIFR-root {color:#E4E4E4;}');
                sportLoadMenu.sifrChanged();
                sportLoadMenu.setWidth();
            }
            //            if (this.isAMyAccountPage) {
            //                if (sIFR.replacements['#liButtonClose span'] == null || sIFR.replacements['#liButtonClose span'][0] == null) {

            //                    sIFR.replace(selectAsport, { selector: '#liButtonClose span', css: { '.sIFR-root': { 'color': '#ffffff', 'leading': -2} }, wmode: 'transparent', fitExactly: true, forceSingleLine: true,
            //                        onReplacement: sportLoadMenu.onReplaceSifr
            //                    });
            //                }
            //                else {
            this.onReplaceSifr();
            //                }
            //            }
            this.setWidth();
            j$('#menuSportItems').show();

            this.setLineHeight();
            this.isSelect = true;
        }
        else {
            //            if (this.isAMyAccountPage) {
            //                this.onReplaceSifr = function(fi) {
            //                    sIFR.replacements['#liButtonClose span'][0].changeCSS('.sIFR-root {color:#ffffff;}');
            //                    sportLoadMenu.sifrChanged();
            //                }
            //                if (sIFR.replacements['#liButtonClose span'] == null || sIFR.replacements['#liButtonClose span'][0] == null) {

            //                    sIFR.replace(selectAsport, { selector: '#liButtonClose span', css: { '.sIFR-root': { 'color': '#ffffff', 'leading': -2} }, wmode: 'transparent', fitExactly: true, forceSingleLine: true,
            //                        onReplacement: sportLoadMenu.onReplaceSifr
            //                    });
            //                }
            //                else {
            //                this.onReplaceSifr();
            //                }
            this.setWidthAuto();
            //            }
            j$('#menuSportButton').attr('class', 'sp_option sp_close');
            j$('#menuSportItems').hide();

            this.isSelect = false;
        }
    },
    setSelectSport: function(sportName) {
        j$('#menuSportButton').innerHTML = sportName;
    },
    setWidthAuto: function() {
        j$('#liButtonClose').css('width', 'auto');
        j$('#leftSportList').css('width', 'auto');
    },
    setWidth: function() {
        if (!this.isAMyAccountPage) {
            //return;
        }
        j$('#sportMenuBlock ul').attr('id', '');
        if (document.getElementById('sportMenuLine') == null) {
            j$('#liButtonClose').css('width', 'auto');
            j$('#leftSportList').css('width', '150px');
            j$('#sportMenuBlock ul').attr('id', 'ulMenuButton');
            return;
        }
        var isHidden = (j$('#menuSportItems').css('display') == 'none');
        if (isHidden) {
            j$('#menuSportItems').css('height', '1px');
            j$('#menuSportItems').css('filter', 'alpha(opacity=1)');
            j$('#menuSportItems').css('opacity', '0.01');
            j$('#menuSportItems').css('display', 'block');
        }
        this.setWidthAuto();

        var buttonWidth = j$('#liButtonClose').width();
        var leftMenuWidth = j$('#leftSportList').width();
        leftMenuWidth += 11;
        if (buttonWidth < leftMenuWidth) {
            j$('#liButtonClose').width(leftMenuWidth);
        }
        if (leftMenuWidth < buttonWidth) {
            j$('#leftSportList').width(buttonWidth - 11);
        }
        if (isHidden) {
            j$('#menuSportItems').css('height', '');
            j$('#menuSportItems').css('filter', '');
            j$('#menuSportItems').css('opacity', '');
            j$('#menuSportItems').css('display', 'none');
        }
    },
    setLineHeight: function() {
        //        if (this.isAMyAccountPage) {
        //            j$('#sportMenuLine').height(j$('#menuBody').height() + 35);
        //        }
        //        else {
        j$('#sportMenuLine').height(j$('#menuBody').height() + 41);
        //        }
    }
}

// Login
function setLoginInfo() {
    var txtEmails = document.getElementsByName("s_Email");
    var txtPasss = document.getElementsByName("s_Password");
    var chckRemembers = document.getElementsByName("b_RememberMe");

    var cookieAcccount = qtek_dhtml.readCookie("S_LOGIN_ACCOUNT_NAME");
    var cookiePass = qtek_dhtml.readCookie("S_LOGIN_ACCOUNT_PASS");
    var cookieAutoLogin = qtek_dhtml.readCookie("S_AUTOLOGIN");
    var cookieRemember = qtek_dhtml.readCookie("S_REMEMBER_ME");

    if ((cookieRemember == 'True') && (cookieAutoLogin == 'True')) {
        j$('input[name=s_Email]').each(function() { j$(this).val(cookieAcccount); });
        j$('input[name=s_Password]').each(function() { j$(this).val(cookiePass); });
        j$('input[name=b_RememberMe]').each(function() { j$(this).attribute('checked', true); });
    }
    else {
        j$('input[name=s_Email]').each(function() { j$(this).val(''); });
        j$('input[name=s_Password]').each(function() { j$(this).val(''); });
        j$('input[name=b_RememberMe]').each(function() { j$(this).attr('checked', false); });
    }
}