﻿if (typeof (SB) == "undefined" || SB == null ) {
    var SB = new Object();
}

if (typeof (SB.leftSportNavigation) == "undefined" || SB.leftSportNavigation == null) {
    SB.leftSportNavigation = new Object();
}


SB.leftSportNavigation.initHierarchy = function(targetDiv, ajaxresult) {
    $(targetDiv).html(ajaxresult);
    //Hack ids for Multiples and TotePools
    $('#modLayoutLeftSport0').attr('id', 'modLayoutLeftSportMultiples');
    $('#modLayoutLeftSport99999').attr('id', 'totePools');

    // Sport level
    SB.leftSportNavigation.bindClickForSports("li.SportItem > a");
    //Group level
    SB.leftSportNavigation.bindClickForGroups("li.Group > a");
    //Parent level
    SB.leftSportNavigation.bindClickForParents("li.Parent > a");

    SB.leftSportNavigation.openCurrentSport();
    $('#loaderDiv').hide();
    $(targetDiv).show();
}

SB.leftSportNavigation.bindClickForSports = function(selector) {
    $(selector).click(function(event) {
        var self = this;
        SB.leftSportNavigation.clickForSport(self, event);
    }).before("<span></span>"); // span is used for the arrow icon
}

SB.leftSportNavigation.clickForSport = function(item, event) {
    event.preventDefault(); //do not navigate to landing page - story#98
    SB.leftSportNavigation.closeMySiblings(item);
    SB.leftSportNavigation.sportNavLevelHide("Child"); //hide Child menu level
    SB.leftSportNavigation.sportNavLevelHide("Parent"); // hide Parent menu level
    SB.leftSportNavigation.toggleMe(item);
    return false;
}

SB.leftSportNavigation.toggleOpenedClass = function(liItem) {
    if($(liItem).hasClass('opened'))
    {
        $(liItem).removeClass('opened');
    }
    else
    {
        $(liItem).addClass('opened');
    }
}

SB.leftSportNavigation.bindClickForParents = function(selector) {
    $(selector).each(function() {
        $(this).click(function() {
            var upperParent = $(this).parent("li").parent("ul").parent("li");
            if (!upperParent.hasClass("Group")) {
                SB.leftSportNavigation.sportNavLevelHide("Parent");
            }

            SB.leftSportNavigation.closeMySiblings(this);
            SB.leftSportNavigation.toggleMe(this);
            return false;
        }).before("<span></span>"); // span is used for the arrow icon
    });
}

SB.leftSportNavigation.bindClickForGroups = function(selector) {
    $(selector).each(function() {
        $(this).click(function() {
            SB.leftSportNavigation.clickForGroup(this);
        }).before("<span></span>"); // span is used for the arrow icon
    });
}

SB.leftSportNavigation.clickForGroup = function(item) {
    SB.leftSportNavigation.closeMySiblings(item);
    SB.leftSportNavigation.sportNavLevelHide("Child"); // hide Child menu level

    SB.leftSportNavigation.toggleMe(item);
    return false;
}

SB.leftSportNavigation.closeMySiblings = function(linkItem) {
    var itemsToClose = $(linkItem).parent("li").siblings("li");
    itemsToClose.children("ul").hide();
    itemsToClose.removeClass("opened");
}

SB.leftSportNavigation.toggleMe = function(linkItem) {
    SB.leftSportNavigation.toggleOpenedClass($(linkItem).parent("li"));
    $(linkItem).parent("li").children("ul").slideToggle("fast");
}

// Sport navigation hide menu level
SB.leftSportNavigation.sportNavLevelHide = function(classToHide) {
    var ulsToHide = $("ul.childList").children("." + classToHide).parent("ul");
    ulsToHide.hide();
    ulsToHide.parent("li").removeClass("opened");
}

SB.leftSportNavigation.openCurrentSport = function() {
    //open current sport
    var sportIds = $('#sportGroupIds').val();
    if (sportIds == undefined || sportIds == '') sportIds = $('#sportNo').val();
    if (sportIds !== undefined) {
        var correctedSportIds = sportIds.replace(/,/g, '-');
        $("#m" + correctedSportIds + ">a").click();

        //open current eventclass
        var ecId = $('#ecNo').val();
        var ecLink = $("#ec" + ecId).parent("ul").parent("li").children("a");
        SB.leftSportNavigation.forceOpenEventClass(ecLink);
        return false;
    }
}

SB.leftSportNavigation.forceOpenEventClass = function(ecLink) {
    $(ecLink).parents("li.Group").children("a").click();
    $(ecLink).click();
}

SB.leftSportNavigation.asyncReplaceDiv = function(contentUrl, selector) {
    var options = {
        type: "GET",
        url: contentUrl,
        traditional: true,
        cache: true,
        success: function(result) {
            $(selector).html(result);
        }
    };    
    $.ajax(options);
}

SB.leftSportNavigation.asyncDownloadLeftNav = function(contentUrl, selector) {
    var options = {
        type: "GET",
        url: contentUrl,
        traditional: true,
        cache: true,
        success: function(result) {
            SB.leftSportNavigation.initHierarchy(selector, result);            
        }
    };
    $.ajax(options);
}


$(document).ready(function() {
    SB.leftSportNavigation.asyncDownloadLeftNav('/services/LeftSportNav.mvc/Display','#sportsPlaceHolder');
});


