/*------------------------------------------------------------------------------------
* Filename: common.js
* Version: 1.0.0 (2010-06-20)
* Website: www.fairwork.gov.au
* Author: Tanvir Ahmad (Ronty) & Dhruv Raniga
* Description: Handles site layout, fonts and animations.
------------------------------------------------------------------------------------*/
$(document).ready(function() {// Document Ready Start

    //  If Bowser is IE6 then call IE6_Menu Function.
    if ($.browser.msie && $.browser.version == "6.0") {
        IE6_Menu()// Menu Work on IE6
    }


    if ($.browser.msie) {
        KeyAccess(); // IF Access Key
    }

});  // Document Ready Start




/*-----------------------------------------------------------------------------------------------------------------------------
* Function: IE6_Menu
* Description: This Function Will make the Main Menu working on IE6
* Author: Tanvir Ahmad Ronty
* Date: 20100623
---------------------------------------------------------------------------------------------------------------------------*/
function IE6_Menu() {// IE6_Menu Funciton Menu Start

    menu_Hover = function(id) {
        var menu_LI = document.getElementById(id).getElementsByTagName("li");
        for (var i = 0; i < menu_LI.length; i++) {
            menu_LI[i].onmouseover = function() {
                this.className += " hover";
            }
            menu_LI[i].onmouseout = function() {
                this.className = this.className.replace(new RegExp(" hover\\b"), "");
            }
        }
    }
    if (window.attachEvent) {
        menu_Hover("menu-wrapper");

    }
} // IE6_Menu Funciton Menu End


/*--------------------------------------------------KeyAccess ---------------------------------------------------------------------------
* Function: KeyAccess
* Description: This Function Will make the css menu access by tab key
* Author: Tanvir Ahmad Ronty
* Date: 20100623
---------------------------------------------------------------------------------------------------------------------------*/

function KeyAccess() {
    var tabActrive = false; // it Tab key use
    // if Tab key press then Active tab true 
    $(document).keydown(function(e) {
        if (e.keyCode == 9) {
            tabActrive = true;
        }
        else {

        }
    });

    // if esc key press hide top and main menu
    $(document).keypress(function(e) {
        if (e.which == 27) {
            tabActrive = false;

            $("div.main-menu-column").css("left", "-9999px");
            $(".top-menu ul").css("left", "-9999px");
        }  // esc   
    });


    // Top Menu Start

    /*
    If no tab press then this function will not active and will use stander css menu
    If Tab press then it will activate and show hide sub menu by javascirpt
    If Tab and Mouse both use then this function will work
		
		Mouseover and Mouseout function for mouse movement
    focus and blur for tab press
    */

    // find All to li menu
    $("ul.top-menu li").each(function(index) { // Each Start
        // Get Active link
        var Link = $(this).children("a");

        // Keyboard
        // check the link is Focus set
        $(Link).focus(function() { // link focus Start

            // if focus link has sub ul then
            if ($(Link).next("ul").length) {

                // Hide all others sub menu
                $(".top-menu ul").css("left", "-9999px");

                // show current sub menu
                $(Link).next("ul").css("left", "0").css("top", "auto").css("display", "block");

            }
            else { // if focus link has no ul then

                // check is the focus link under top menu or sub menu
                if ($(Link).parent("li").parent("ul").attr("class") == "top-menu") {
                    //Hide all others sub menu
                    $(".top-menu ul").css("left", "-9999px");
                }
            }
        }); // link focus End


        // Mouse 
        $(this).mouseover(function() { // On Mouse Over Start
            // if Tab key Activate then else use old CSS Menu Funciton no JS
            if (tabActrive) {	// Get Active link
                var Link = $(this).children("a");
                // if Active link has sub ul then
                if ($(Link).next("ul").length) {
                    //Hide all others sub menu
                    $(".top-menu ul").css("left", "-9999px");
                    // show current sub menu
                    $(Link).next("ul").css("left", "0").css("top", "auto");
                }

            } // Active
        }); // On Mouse Over End


        $(this).mouseout(function() {// On Mouse Out Start
            // it Tab use
            if (tabActrive) {
                // Get Active link
                var Link = $(this).children("a");

                // check is the focus link under top menu or sub menu
                if ($(Link).parent("li").parent("ul").attr("class") == "top-menu") {
                    //Hide all others sub menu
                    $(".top-menu ul").css("left", "-9999px");
                }
            }
        }); // On Mouse Out End

    });     // Each End


    // Top Menu End

    // Main Menu Start
    /*
    If no tab press then this function will not active and will use stander css menu
    If Tab press then it will activate and show hide sub menu by javascirpt
    If Tab and Mouse both use then this function will work
	
	Mouseover and Mouseout function for mouse movement
    focus and blur for tab press
    */

    $("li.main-menu-title a.main-menu-title-text").each(function(index) {
        // for each Main manu link--Start
        $(this).focus(function() { // link focus Start
            // Remove focus css from parent menu
            $("li.main-menu-title a.main-menu-title-text").removeClass("focus");

            // hide all sub menu
            $("div.main-menu-column").css("left", "-9999px");
            // current sub menu show
            $(this).parent("li").children("div.main-menu-column").css("left", "auto").css("display", "block");
            // Set focus css from parent menu
            $(this).addClass("focus");

        }); // link focus End



        $(this).mouseover(function() { // On Mouse Over Start
            // if Tab key Activate then else use old CSS Menu Funciton no JS
            if (tabActrive) {	 // Remove focus css from parent menu
                $("li.main-menu-title a.main-menu-title-text").removeClass("focus");
                // hide all sub menu
                $("div.main-menu-column").css("left", "-9999px");
                // current sub menu show
                $(this).parent("li").children("div.main-menu-column").css("left", "auto").css("display", "block");
                // Set focus css from parent menu
                $(this).addClass("focus");
            } // Active
        }); // On Mouse Over End
    }); // for each Main manu link--End


    // for Mouse Out
    // To hide Sub menu
    // If Mouse on content 
    $("#content").mouseover(function() {// On Mouse Out Start
        // it Tab use
        if (tabActrive) {
            $("li.main-menu-title a.main-menu-title-text").removeClass("focus");
            $("div.main-menu-column").css("left", "-9999px");
        }
    }); // On Mouse Out End

    // If Mouse on header
    $("#header").mouseover(function() {// On Mouse Out Start
        // it Tab use
        if (tabActrive) {
            $("li.main-menu-title a.main-menu-title-text").removeClass("focus");
            $("div.main-menu-column").css("left", "-9999px");
        }
    }); // On Mouse Out End


    // for Tab
    // Find the last Menu item 
    $("#main-menu-wrapper li.last a").each(function(index) {
        // if Focus out from last menu item 
        $(this).blur(function() { // link focus Start
            //Find the div.main-menu-column
            parentDivColumn = $(this).parent("li").parent("ul").parent("div").parent("div");
            // hide current sub menu
            $(parentDivColumn).css("left", "-9999px");
            // Remove focus css from parent menu
            $("li.main-menu-title a.main-menu-title-text").removeClass("focus");

        }); // link focus End
    });

    // Main Menu End

}
/* ---------------------------------------------------- KeyAccess -----------------------------------------------------------------------*/
