﻿/*****
Main menu navigation used in Bingo and Casino
Requires libraries: jQuery, hoverIntent
*****/
$(document).ready(function() {

    //Hover functions
    //menu hover on (menu open)
    var makeTall = function() {
        var menuItem = $('#menuItem');
        $(this).append(menuItem);

        //There is id collision so had to add 'li_' to the menu items
        var id = this.id.substring(this.id.indexOf('_') + 1);
        $('#menuCategoryList div').hide();
        $('#menuCategoryList span').removeClass('mainNavArrowLeft');
        var categoryListId = $('#menuCategoryList div.' + id);
        categoryListId.show();
        categoryListId.find('span:first').addClass('mainNavArrowLeft');

        $('#menuGameList div').hide();
        $('#menuGameList div.' + id).show();
        //Initializes the games content
        $('#menuGameList div a').show();
        $('#menuGameList div h1').hide();
        $('#menuGameList div h1.all').show();

        $('#mainNav > li').removeClass('mainNavArrowUp');
        $(this).addClass('mainNavArrowUp');
        menuItem.css('left', -1 * $('#mainNav').position().left + 5);
        menuItem.stop(false, true).slideDown('200');
    }

    //menu hover off (close menu)
    var makeShort = function() {
        var obj = $(this).find('#menuItem');
        var objParent = $(obj).parent();
        obj.slideUp('200', function() {
           $(objParent).removeClass('mainNavArrowUp');
        });
        $('#menuGameInfo').hide();
    }

    //hoverIntent configuration object
    var config = {
        sensitivity: 7,
        interval: 400,
        over: makeTall,
        timeout: 500,
        out: makeShort
    };

    //only elements with 'id' get a submenu
    $('#mainNav > li[id]').hoverIntent(config);

    //close menuItem: close all elements
    $('#menuItem .close').click(function() {
        $('#menuGameInfo').hide();
        $('#menuItem').hide();
        $('#mainNav > li').removeClass('mainNavArrowUp');
    });

    //Filter games by categories
    $('#menuCategoryList a').click(function() {
        //adds the arrow pointing the category
        $('#menuCategoryList span').removeClass('mainNavArrowLeft');
        $(this).parent().addClass('mainNavArrowLeft');
        //displays the list of items in the category
        $('#menuGameInfo').hide();
        $('#menuGameList h1').hide();
        var css = $(this).attr('class');
        if (css.length == 0) {
            $('#menuGameList div a').show();
            $('#menuGameList div h1.all').show();
        }
        else {
            $('#menuGameList div a').hide();
            $('#menuGameList div a[class*=_' + css + '_]').show();
            $('#menuGameList div h1.' + css).show();
        }

    });
    //open menuGameInfo
    $('#menuGameList a').click(function() {
        //initialize the object
        var gameInfo = $('#menuGameInfo');
        gameInfo.find('#gameImg').attr('src', '');
        //Obtain the gameCode
        var css = $(this).attr('class');
        var gameCode = css.substring(0, css.indexOf('_'));
        //call to retrieve info
        $.ajax({
            type: "POST",
            data: '{gameCode: "' + gameCode + '"}',
            url: "/feeds/GameInfo.asmx/GetGame",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(response) {
                var data = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
                //all elements when click go to play
                gameInfo.find('.gameCash').unbind('click').click(function() { LaunchGame('' + data.GameCode + '', 'cashplay') });
                gameInfo.find('#infoTitle').html(data.GameDescription);
                gameInfo.find('#gameImg').attr('src', '/cmsdocs/games/' + data.GameCode + '-88x88.gif');
                gameInfo.find('#gameDesc').html(data.LongGameDescription);
                gameInfo.find('#gameLink').hide();
                //If in Casino show details page otherwise nothing
                if (window.location.pathname.toUpperCase().startsWith('/CASINO/')) {
                    gameInfo.find('#gameLink').show().attr('href', '/casino/gamedetails.aspx?gamecode=' + data.GameCode);
                    gameInfo.find('#gameFree').hide();
                    if (data.AllowFreePlay) {
                        gameInfo.find('#gameFree').unbind('click').show().click(function() { LaunchGame('' + data.GameCode + '', 'freeplay') });
                    }
                }
            },
            error: function(xhr) {
                //gameInfo.find('#gameDesc').html('Error: ' + xhr.status + ' ' + xhr.statusText);
            }
        });
        gameInfo.css('left', $(this).position().left);
        gameInfo.css('top', $(this).position().top + 25);
        gameInfo.fadeIn('200');
    });

    //double click on the game link opens the game
    $('#menuGameList a').dblclick(function() {
        var css = $(this).attr('class');
        var gameCode = css.substring(0, css.indexOf('_'));
        LaunchGame('' + gameCode + '', 'cashplay');
    });

    //close menuGameInfo
    $('#menuGameInfo .menuGameClose').click(function() {
        $('#menuGameInfo').hide();
    });
});
