// $Header: /web/src/rui/sfh/js/groupNav.js,v 1.1 2007/11/13 06:48:11 bperera Exp $
//desc   The RUI namespace.
var RUI = window.RUI || {};

//desc   Controls the Group Navigation dropdown menu.
//note   This is self-contained and must not rely on external
//       code so that it can be distributed to all REA Group sites
//       without any dependencies.
RUI.GroupNavigation = function() {
    // HTML id attributes
    var ID = {
        CONTAINER : 'groupNavBorder',    // Group Nav Border
        GROUP_NAV : 'groupNav',          // Group Nav
        DROPDOWN  : 'groupNavCountries', // Dropdown menu UD
        TRIGGER   : 'groupNavTrigger'    // Trigger anchor ID
    };

    var CLASS = {
        EXPANDED  : 'expanded',           // Expanding navigation
        CONTRACTED: 'contracted'          // Contract navigation
    };

    //method [static] createShim
    //desc   Create invisible iframe to handle the shim effect in IE. This is
    //       used to cover Select Box, ActiveX object, Iframes etc.
    //param  [HTMLElement] element.
    //note   Only create shim if the browser is IE Version >= 5.5 && < 7.
    function createShim(element) {

        var ieVersion = parseFloat(navigator.appVersion.split('MSIE')[1]);
        if (ieVersion >= 5.5 && ieVersion < 7) {
            // To handle the shim effect create an iframe
            var groupNav        = document.getElementById(ID.GROUP_NAV);
            var shim            = document.createElement('iframe');
            shim.scrolling      = 'no';        //no scrolling
            shim.frameBorder    = '0';         //no frame border on iframe
            shim.style.position = 'absolute';  //position iframe absolute
            shim.style.top      = '0px';
            shim.style.left     = '0px';
            shim.style.display  = 'none';
            shim.style.filter   =
                'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0,'
                + 'sizingMethod="crop")';

            // display the shim
            var width = (
                element.offsetWidth === 0
                    ? element.renderedWidth
                    : element.offsetWidth
            );
            var height = (
                element.offsetHeight === 0
                    ? element.renderedHeight
                    : element.offsetHeight
            );

            if (!element.style.zIndex) {
                element.style.zIndex = 999;
            }
            shim.style.width   = width + 'px';
            shim.style.height  = height + 'px';
            shim.style.top     = element.style.top - groupNav.offsetHeight;
            shim.style.left    = element.style.left - groupNav.offsetWidth;
            shim.style.zIndex  = element.style.zIndex - 1;
            shim.style.display = 'block';
            element.appendChild(shim);
        }
    }

    // method [static] removeShim.
    // desc   Remove the created iframe, the first iframe attached to the
    //        element.
    //param  [HTMLElement] element.
    function removeShim(element) {
        if(element && element.getElementsByTagName('iframe')[0]) {
            var iframe = element.getElementsByTagName('iframe')[0];
            element.removeChild(iframe);
        }
    }

    var dropdown = document.getElementById(ID.DROPDOWN);
    var trigger = document.getElementById(ID.TRIGGER);
    var container = document.getElementById(ID.CONTAINER);

    trigger.style.outline = 'none'; // Remove outline from FF

    // Removes the display style attribute so that the dropdown is shown
    function show() {
        var ieVersion = parseFloat(navigator.appVersion.split('MSIE')[1]);
        dropdown.style.display = '';
        trigger.className = CLASS.CONTRACTED;
        createShim(container);

if (ieVersion >= 5.5 && ieVersion < 7) {
document.getElementById('area').style.display='none';
document.getElementById('district_select').style.display='none';
}

    }

    // Sets the display style attribute to 'none' so that the dropdown is
    // hidden
    function hide() {
        var ieVersion = parseFloat(navigator.appVersion.split('MSIE')[1]);
        if (visible()) {
            dropdown.style.display = 'none';
            trigger.className = '';
            removeShim(container);
        }

if (ieVersion >= 5.5 && ieVersion < 7) {
document.getElementById('area').style.display='';
document.getElementById('district_select').style.display='';
}

    }

    // Toggles the style display attribute of the dropdown
    function toggle() {
        visible() ? hide() : show();
    }

    // Returns true if the dropdown is visible
    function visible() {
        return !(dropdown.style.display === 'none');
    }

    // document onclick handler for hiding the dropdown
    function documentOnclick(e) {
        // Hide if dropdown is visible and the click occured anywhere
        // except on the trigger.
        var target = window.event? window.event.srcElement : e.target;
        if (visible() && target !== trigger) {
            hide();
        }
    }

    // Toggle display of the dropdown onclick
    trigger.onclick = function() {
        toggle();
        return false;
    };

    // Add the document onclick handler
    if (document.onclick) {
        var oldOnclick = document.onclick; // Be nice to others
        document.onclick = function(e) {
            oldOnclick(e);
            documentOnclick(e);
        };
    }
    else {
        document.onclick = documentOnclick;
    }
};
