﻿
var hideLiTimer;
var hideDropTimer;
var nonTopNavTimer;

$(document).ready(function() {
    setTextSize();

    $('html').click(function() {
        hideDropdowns();
    });

    $('#video-links a:not(#moreLink a)').click(function(e) {
        e.preventDefault();
        $(".active").removeClass("active");
        $(this).addClass("active");
        DoAjax($(this).attr("href"));
    });

    // slider bind
    if ($('#slides').length > 0) {
        $('#slides').slides();
    }

    //Drop nav
    $('.top_nav ul li').mouseenter(function() {
        clearTimers();
        if ($(this).hasClass('top_nav_1')) {
            nonTopNavTimer = setTimeout(function() { hideDropdowns() }, 300);
        }
        if ($(this).hasClass('top_nav_2')) {
            $('.drop_nav').hide();
            $('.drop_nav_2').show();
        }
        if ($(this).hasClass('top_nav_3')) {
            $('.drop_nav').hide();
            $('.drop_nav_3').show();
        }
        if ($(this).hasClass('top_nav_4')) {
            nonTopNavTimer = setTimeout(function() { hideDropdowns() }, 300);
        }
        if ($(this).hasClass('top_nav_5')) {
            nonTopNavTimer = setTimeout(function() { hideDropdowns() }, 300);
        }

    });

    $('.drop_nav').mouseenter(function() {
        clearTimers();
    });

    $('.drop_nav').mouseleave(function() {
        hideDropTimer = setTimeout(function() { hideDropdowns() }, 300);
    });

    $('.top_nav ul li').mouseleave(function() {
        hideLiTimer = setTimeout(function() { hideDropdowns() }, 300);
    });
    //End drop nav

    //Search bar
    $('.search_box').click(function() {
        //alert("running");
        $(this).addClass("search_selected");
        $(this).removeClass("search_box");
        $(this).parent().removeClass("small").addClass("large");
        $(this).parent().parent().addClass("large");
    });

    $('.search_box').focusout(function() {
        //alert("running");
        $(this).addClass("search_box");
        $(this).removeClass("search_selected");
        $(this).parent().addClass("small").removeClass("large");
        $(this).parent().parent().removeClass("large");
        $(this).val("");

    });
    //End search bar

    //Accordians
    $(".accordion_toggle a").click(function(event) {
        //alert("running");
        event.preventDefault();
        if ($(this).parent().hasClass("on")) {
            $(this).parent().removeClass("on");
        } else {
            $(this).parent().addClass("on");
        }
        var accordionScroll = "#" + $(this).parent().attr("id");
        $(this).parent().next(".accordion_content").slideToggle('slow', function() {

            $.scrollTo(accordionScroll, 800);
        });
    });
    //End accordions

    //Header quick links drop
    $(".sites_drop_toggle a").click(function() {
        $(".quick_links_container").toggle();
    });

    //Footer slide up
    $(".quick_links_drop_toggle a").click(function(event) {

        var animateToHeight
        var Show;

        if ($(this).parent().hasClass("open")) {
            $(this).parent().removeClass("open");
            animateToHeight = 41;
            Show = false;
            $(".footer_container").hide();
        }
        else {
            $(this).parent().addClass("open");
            animateToHeight = 205;
            Show = true;
        }

        $('.footer_nav').animate({
            height: animateToHeight + 'px'
        }, 150, function() {

            if (Show) {
                $(".footer_container").show();
                $.scrollTo(".quick_links_drop_toggle", 200);
            }

        });

    });

    //Board overlay open
    $(".board_member a").click(function() {
        var memberId = $(this).attr("id");
        var memberNoArray = memberId.split("_");
        var memberNo = memberNoArray[1];
        $(".content").hide();
        $("#content_" + memberNo).show();
        $(".overlay_container .thumbnails a").removeClass("selected");
        $(".overlay_container .thumbnails #thumb_" + memberNo).addClass("selected");
        $(".overlay_container, .overlay_background").show();

        //sifr
        sIFR.replace(univers_std_cond, {
            selector: '.text_content .name',
            wmode: 'transparent',
            css: '.sIFR-root {color:#cc0033}'
        });

        sIFR.replace(univers_std_cond, {
            selector: '.text_content .title',
            wmode: 'transparent',
            offsetTop: '4'
        });

        sIFR.replace(univers_std_cond, {
            selector: '.text_content .age',
            wmode: 'transparent',
            css: '.sIFR-root {color:#666666}',
            offsetTop: '4'
        });
    });

    //Board overlay close
    $(".close_button a").click(function() {
        $(".overlay_container, .overlay_background").hide();
    });

    //Overlay switch profile
    $(".overlay_container .thumbnails a").click(function() {
        var memberId = $(this).attr("id");
        var memberNoArray = memberId.split("_");
        var memberNo = memberNoArray[1];
        $(".content").hide();
        $("#content_" + memberNo).show();
        $(".overlay_container .thumbnails a").removeClass("selected");
        $(".overlay_container .thumbnails #thumb_" + memberNo).addClass("selected");

        //sifr
        sIFR.replace(univers_std_cond, {
            selector: '.text_content .name',
            wmode: 'transparent',
            css: '.sIFR-root {color:#cc0033}'
        });

        sIFR.replace(univers_std_cond, {
            selector: '.text_content .title',
            wmode: 'transparent',
            offsetTop: '4'
        });

        sIFR.replace(univers_std_cond, {
            selector: '.text_content .age',
            wmode: 'transparent',
            css: '.sIFR-root {color:#666666}',
            offsetTop: '4'
        });
    });

    $(".print_resize a").click(function() {
        if ($(this).hasClass("option_1")) {
            removeResize();
        }

        if ($(this).hasClass("option_2")) {
            removeResize();
            resizeLarge();
            $.cookie('sizeCookie', 'large', { expires: 7, domain: 'www.abfreports.co.uk', path: '/' });
        }
        if ($(this).hasClass("option_3")) {
            removeResize();
            resizeLarger();
            $.cookie('sizeCookie', 'larger', { expires: 7, domain: 'www.abfreports.co.uk', path: '/' });
        }
    });

    //Downloads check / uncheck all

    $(".all_container .check_all").click(function() {
        if ($(this).attr("checked") == "checked") {
            $("#" + $(this).attr("name") + " input:not(.check_all)").each(function(index, value) {
                $(this).attr('checked', true);
            });
        }
        else {
            $("#" + $(this).attr("name") + " input:not(.check_all)").each(function(index, value) {
                $(this).attr('checked', false);
            });
        }
    });

    $('input#pageNum').keydown(function(e) {
        if (e.keyCode == 13) {
            $(this).parents('form').submit();
            return false;
        }
    });

});

//resize functions
function removeResize() {
    $("h6, p, .accordion_content p, .coloured_text, .quick_links_drop_toggle, .footer_links a, .footer_links, .copyright_message, .left_nav_container ul a, .t3_main_content ul").removeClass("large");
    $("h6, p, .accordion_content p, .coloured_text, .quick_links_drop_toggle, .footer_links a, .copyright_message, .left_nav_container ul a, .t3_main_content ul").removeClass("larger");
    $.cookie('sizeCookie', null);
}

function resizeLarge() {
    $("h6, p, .accordion_content p, .coloured_text, .quick_links_drop_toggle, .footer_links a, .footer_links, .copyright_message, .left_nav_container ul a, .t3_main_content ul").addClass("large");
}

function resizeLarger() {
    $("h6, p, .accordion_content p, .coloured_text, .quick_links_drop_toggle, .footer_links a, .footer_links, .copyright_message, .left_nav_container ul a, .t3_main_content ul").addClass("larger");
}

function setTextSize() {
    var textSize = $.cookie('sizeCookie');
    if (textSize != null) {
        switch (textSize) {
            case "large":
                resizeLarge();
                break;
            case "larger":
                resizeLarger();
                break;
        }
    }
}

//Drop nav functions
function hideDropdowns() {
    $('.drop_nav').hide();
}

function clearTimers() {
    clearTimeout(hideLiTimer);
    clearTimeout(hideDropTimer);
    clearTimeout(nonTopNavTimer);
}
//End drop nav functions


// Generate the PDF
function generatePDF() {
    
    $("#error").html("");

    var valArray = $('input.chkPDF:checkbox').serializeArray();
    var PDFstr = "";
    jQuery.each(valArray, function(i, field){
        if (PDFstr.length > 0) {
            PDFstr = PDFstr + ",";
        }
        PDFstr = PDFstr + field.name;
    });
    
    if (PDFstr == "") {
        $("#error").html("<p class=\"form_text\" style=\"color: #FF0000; font-weight: bold\"><strong>Please select at least one section</p>");
    }
    else {
        window.open("/outputPDF.aspx?data=" + PDFstr);
    }
    
}

var debug = true;

    function DoAjax(URL) {
        $.ajax({
            url: "/homepage/" + URL,
            cache: false,
            success: function(data) {
                $('#video-content').html(data);
            },
            error: function(e) {
                if (debug) {
                    alert(e.responseText);
                }
            }
        });
    }
   
