﻿//
//  File:       sds_NearestCentre.js
//  Created:    16/02/2010
//  Notes:      - requires jQuery 1.4
//              - This snippet of code needs to be added to the <head> of each page below the other script references:
//
//                <script type="text/javascript">$("html").addClass("js");</script>        
//


  ///////////////////////////////////////
 // Nearest Centre Search help popups //
///////////////////////////////////////

$(function() {

    // Z-index fix for IE
    // - puts decreasing z-index on each relative-positioned container for popup
    var onTop = 10000;
    $(".formHelpContent").each(function() {
        $(this).css("z-index", onTop);
        onTop--;
    });
    // End of z-index fix for IE
    

    // Create Help Title anchors to allow accessible control of popups
    $(".formHelpContent .control").each(function() {
        var helpTitleText = $(this).text();
        $(this).empty();
        var helpAnchor = document.createElement("a");
        $(helpAnchor).attr("title", "Open help information").attr("href", "#");
        $(helpAnchor).append(helpTitleText);
        $(this).append(helpAnchor);
    });

    // Add "Close" link to popup and set click events 
    $(".formHelpContent .content").each(function() {
        var closeButton = document.createElement("a");
        $(closeButton).addClass("close").text("close").attr("title", "Close this popup").attr("href", "#");
        $(this).append(closeButton);
        $(closeButton).click(function() {
            var helpSection = $(this).parents(".formHelpContent").eq(0);
            hideAllPopups();
        });
        $(this).click(function() {
            event.stopPropagation();
        });
    });

    // Set events and actions for "Help" button
    $(".formHelpContent .control").click(function() {
        // If 
        if ($(this).parent().find(".content:hidden").length) {
            hideAllPopups();
            var helpSection = $(this).parent();
            $(this).addClass("open");
            $(this).parent().find(".content").eq(0).fadeIn();
            $(document).click(function(event) {
                if (!$(event.target).closest(".formHelpContent .content").length) {
                    hideAllPopups();
                    $(this).unbind('click');
                }
            });
            return false;
        }
    });
});

function hideAllPopups() {
    $(".formHelpContent .control").removeClass("open");
    $(".formHelpContent .content").fadeOut();
}

  //////////////////////////////////////////////
 // End of Nearest Centre Search help popups //
//////////////////////////////////////////////
