﻿Cufon.replace('.cta h3');

$(document).ready(function () {
    $('#menu ul li').hover(
        function () {
            //show its submenu
            $(this).find('a:first-child').css('background-color', '#6e1941')
            $('ul', this).slideDown(100);

        },
        function () {
            //hide its submenu
            $(this).find('a:first-child').css('background-color', '')
            $('ul', this).slideUp(100);
        }
    );

    $('.newsitem').last().css('border', 'none');

    if ($('#sidebar').height() < $('.content').first().height()) {
        $('#sidebar').height($('.content').first().height() + 46);
    }


    /* NEWS PAGE ITEM FILTERING */

    /* Determine how many pages are required - based on five news items per page */
    var iTotalItems = $(".newsitem").length;
    var iTotalPages = Math.ceil(iTotalItems / 5);

    /* Limit total number of pages to 10 */
    if (iTotalPages > 10) { iTotalPages = 10 }

    /* Add page numbers to page */
    for (i = 1; i <= iTotalPages; i++) {

        if (i == 1) { // Set page 1 as the active page initially
            $('.pagination').append('<li><a href="#" class="onstate">' + i + '</a></li>');
        } else {
            $('.pagination').append('<li><a href="#">' + i + '</a></li>');
        }

        // Show only first five items on initial page load
        $('.newsitem').each(function (index) {
            if (index > 4) { $(this).hide(); }
        });

        /* Resize height of right hand side bar inline with left content */
        if ($('#sidebar').height() > $('.content').first().height()) {
            $('#sidebar').height($('.content').first().height() + 46);
        }
    }
    $('.pagination').append('<li>1 to 5 of ' + iTotalItems + '</li>');

    $(".pagination li a").click(function () {

        // Highlight the selected page
        $(".pagination li a").removeClass("onstate");

        var iPageNo = $(this).text();

        $('.pagination li a').each(function (index) {
            if ($(this).text() == iPageNo) {
                $(this).addClass("onstate");
            }
        });

        // Show only the items relevant to the selected page
        var iStartItem = 0;
        var iTotalItems = $(".newsitem").length;

        if (iPageNo > 1) {
            var iStartItem = ((iPageNo - 1) * 5)
        }

        $('.newsitem').each(function (index) {
            if (index >= iStartItem && index < iStartItem + 5) {
                $(this).show();
            } else {
                $(this).hide();
            }
        });

        /* Resize height of right hand side bar inline with left content */
        if ($('#sidebar').height() > $('.content').first().height()) {
            $('#sidebar').height($('.content').first().height() + 46);
        }

        if ($('#sidebar').height() < $('.content').first().height()) {
            $('#sidebar').height($('.content').first().height() + 46);
        }

        var iStart = iStartItem + 1;
        var iEnd = iStartItem + 5;

        if (iEnd > iTotalItems) { iEnd = iTotalItems; }

        $('.pagination').each(function (index) {

            $(this).find("li").last().text(iStart + ' to ' + iEnd + ' of ' + iTotalItems);
        });

    });
    /* END NEWS PAGE ITEM FILTERING */


    $(".bottom-sidebar.grad").click(function () {
        window.location = "http://www.tesco-graduates.com/"
    });

    $(".bottom-sidebar.events").click(function () {
        window.location = "/Events/Events.aspx"
    });

    $(".bottom-sidebar.mentor").click(function () {
        window.location = "https://www.tescoacademy.com/"
    });

    $(".bottom-sidebar.comms").click(function () {
        window.location = "Contact-us/Contact-Us.aspx"
    });
});
