﻿$(document).ready(function () {

    $('.topmenuoverlay').click(function (e) {


        var x = 1023 - (e.pageX - $(this).offset().left);
        var y = e.pageY - $(this).offset().top;

        // 56 in between
        // offset 2

        // first subtract 1.66y from x

        x = x - (1.66 * y);

        // next divide x by 56 and floor

        var iMenuTab = Math.floor(x / 56);

        // make sure all tabs are valid by setting the max tab to match the max entry in the database

        if (iMenuTab >= $('.topmenu ul li').size()) iMenuTab = $('.topmenu ul li').size() - 1;

        // go to the specific page

        window.location = $('.topmenu ul li:nth-child(' + (iMenuTab + 1) + ') .menuitem').attr("data-url");

    });

    var _hoverMenuItem;

    $('.topmenuoverlay').mouseout(function (e) {

        _hoverMenuItem.css({ 'background-position': '0px 0px' });
        _hoverMenuItem = null;

    });


    $('.topmenuoverlay').mousemove(function (e) {

        try {

            var x = 1023 - (e.pageX - $(this).offset().left);
            var y = e.pageY - $(this).offset().top;

            // 56 in between
            // offset 2

            // first subtract 1.66y from x

            x = x - (1.66 * y);

            // next divide x by 56 and floor

            var iMenuTab = Math.floor(x / 56);

            if (iMenuTab >= $('.topmenu ul li').size()) iMenuTab = $('.topmenu ul li').size() - 1;

            var oldHoverMenuItem = _hoverMenuItem;

            _hoverMenuItem = $('.topmenu ul li:nth-child(' + (iMenuTab + 1) + ')').children('.menuitem');

            if (_hoverMenuItem == oldHoverMenuItem) return;

            if (_hoverMenuItem != null) oldHoverMenuItem.css({ 'background-position': '0px 0px' });
            _hoverMenuItem.css({ 'background-position': '2px -104px' });

            //var li = $('.topmenu li').get(iMenuTab);

            //document.writeln($(li).attr('style'));

            //if ($(_hoverMenuItem) != $('.menuitem', li))
            //    $(_hoverMenuItem).css({ 'background-color': 'navy' });

            //$(li).children('.menuitem').css({ 'background-position': '5px' });

            //_hoverMenuItem = $('.menuitem', li);

        } catch (e) { }

    });

    $('.pictureRewardsImg, .ticket-issued-white-background .RewardImg').click(function (e) {

        // Find position of mouse in reward

        var x = e.pageX - $(this).offset().left;
        var y = e.pageY - $(this).offset().top;

        // If the user clicks on the print button, go to the print page.

        if (((x >= 275) && (x <= 352)) && ((y >= 154) && (y <= 185))) {
            window.open($(this).attr("PrintLink"));
        } 
        else {
            window.open($(this).attr("RedeemLink"));
        }

        // Use the following code if "Print" links to one place, "Redeem" links to another, and the rest of the image links to a third.

        /* if (((x >= 275) && (x <= 352)) && ((y >= 154) && (y <= 185))) {
            window.location = $(this).attr("PrintLink");
        } else if (((x >= 356) && (x <= 433)) && ((y >= 154) && (y <= 185))) {
            window.location = $(this).attr("RedeemLink");
        } else {
            window.location = $(this).attr("GeneralLink");
        } */

    });


});
