﻿$('html').addClass('js');

// Add support for opening new tabs/windows in xHTML Strict pages
function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}
window.onload = externalLinks;


$(document).ready(function() {

    // choose text for the show/hide link - can contain HTML (e.g. an image)
    var showText = 'More';
    var hideText = 'More';

    // initialise the visibility check
    var is_visible = false;

    // append show/hide links to the element directly preceding the element with a class of "toggle"
    $('.toggleLinkHolder').append(' <a href="#" class="toggleLink">' + showText + '</a>');

    // capture clicks on the toggle links
    $('div.toggleLinkHolder').click(function(){
        var link = $(this).find('a.toggleLink');                
        var module_expand = $(this).find("+ div.toggle");                        

        module_expand.slideToggle("fast", function() {                    
            if (link.hasClass('close')) {
                //if the module is CLOSING
                link.attr('class', 'toggleLink open');                                                           
            } else {
                //if the module is OPENING
                link.attr('class', 'toggleLink close');
                                
                if(module_expand.hasClass('toggle_with_sifr') && module_expand.data('sifr_ok') != 1){
                    //Re-generate Sifr when "sifr container" expandable modules open                        
                    includeScript("/scripts/sifr-config.js");    
                    module_expand.data('sifr_ok', 1);         
                }                                                      
            }
        });                        
        return false;
    });
    
    $('div.toggleLinkHolder').hover(function(){
        $(this).addClass('hover');
    }, function(){
        $(this).removeClass('hover');
    });            
});

function includeScript(scriptUrl) {
    // Change requests to be sent synchronous
    $.ajaxSetup({ async: false });

    // Loads and executes a local JavaScript file
    $.getScript(scriptUrl);

    // Restore requests to be sent asynchronous
    $.ajaxSetup({ async: true });
} 