$(document).ready(function() {
    // Prepare glossary linked words
    prepare_glossary_tips();

    // Prepare admin linked words
    prepare_admin_tips();

    // Register global error handler
    window.onerror = function(message, uri, line) {
        // An unplanned exception occured, it should not stop
        // the form working however, as we've caught it here.  We know
        // that typically it is a cosmetic UI error in JQuery on IE8.
        // Return true to prevent the browser from raising an exception.
        return true
    }
});

function prepare_glossary_tips() {
    // Iterate over each glossary item
    $("span[class='glossary_tip']").each(function(index) {
        // Get the tooltip text
        tip_text = $(this).attr("title");

        // Remove the title element to prevent ei and chrome showing two tips
        $(this).attr("title", "");

        // Add the tool tip, using text passed via the class attribute
        $(this).qtip({
            content: tip_text,
            show: { when: { event: 'click' }, effect: { type: 'fade', length: 300} },
            hide: { when: { event: 'mouseout' }, effect: { type: 'fade', length: 300} },
            position: {
                corner: {
                    target: 'topMiddle',
                    tooltip: 'bottomMiddle'
                }
            },
            style: {
                border: {
                    width: 5,
                    radius: 10
                },
                padding: 10,
                textAlign: 'center',
                tip: true, // Give it a speech bubble tip with automatic corner detection
                name: 'dark' // Style it according to the preset 'cream' style
            }
        });
    });
}

function prepare_admin_tips() {
    // Iterate over each glossary item
    $("span[class='admin_tip']").each(function(index) {
        // Get the tooltip text
        tip_text = $(this).attr("title");

        // Remove the title element to prevent ei and chrome showing two tips
        $(this).attr("title", "");

        // Add the tool tip, using text passed via the class attribute
        $(this).qtip({
            content: tip_text,
            show: { when: { event: 'mouseover' }, effect: { type: 'fade', length: 300} },
            hide: { when: { event: 'mouseout' }, effect: { type: 'fade', length: 300} },
            position: {
                corner: {
                    target: 'topMiddle',
                    tooltip: 'bottomMiddle'
                }
            },
            style: {
                width: 400,
                border: {
                    width: 1,
                    radius: 1
                },
                padding: 10,
                textAlign: 'center',
                tip: true, // Give it a speech bubble tip with automatic corner detection
                name: 'green' // Style it according to the preset 'cream' style
            }
        });
    });
}

function onclick_livesupport(){
    // Open new window for live support
    window.open('https://livechat.boldchat.com/aid/3426580264338971474/bc.chat?cwdid=4228439601009322029&url=' + escape(document.location.href), 'Chat1523280939266028415', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=640,height=800'); 
    return false;    
}

function faq_toggle(id){
    $("p[id$='i_A_" + id + "']").toggle("slow");
    
    // Prevent anchor being followed
    return(false);
}

function toggle(id) {
    $("[id$='" + id + "']").toggle("slow");

    // Prevent anchor being followed
    return (false);
}

function hide(id) {
    $("[id$='" + id + "']").hide();

    // Prevent anchor being followed
    return (false);
}