﻿/* Scripts for Skogsindustrierna */

var ActiveMenuItem = null;
var ActiveMenuItemHideTimeout = null;
  
function InitTopMenu()
{
    $("div[id^='topMenuItem_']").each( 
        function(){ 
            var id = GetElementID(this);
            $(this).children("a").each(function(){
                    $(this).attr("id", "topMenuItemLink_" + id);
                    $(this).mouseover(function(){ ShowSubMenu(this); }); 
                    $(this).mouseout(function(){ HideSubMenu(this); }); 
                }
            );
        }
    );
}

function HideSubMenu()
{
    ActiveMenuItemHideTimeout = setTimeout( 
        function(){  
            var id = GetElementID(ActiveMenuItem);
            $("#topMenuSub_" + id).hide();
            ActiveMenuItem = null;
        }, 10
   );
}

function ShowSubMenu(parent)
{
    if(ActiveMenuItem != null)
    {
        var id = GetElementID(ActiveMenuItem);
        $("#topMenuSub_" + id).hide();
    }
    
    clearTimeout(ActiveMenuItemHideTimeout);

    // Save current menu item, used when hoveringe other menu items to hide previouse
    ActiveMenuItem = parent;
    
    var offset = $(parent).offset();
    var id = GetElementID(parent);
    var subMenu = $("#topMenuSub_" + id);
    $(subMenu).show();
    $(subMenu).css({"top": (offset.top + $(parent).height()), "left":offset.left - 14});
    
    $(subMenu).mouseover(function() { SubMenuMouseOver(); });    
    $(subMenu).mouseout(function() { SubMenuMouseOut(); });
}

function SubMenuMouseOver()
{
    // Stop timer so menu item wont be hidden
    clearTimeout(ActiveMenuItemHideTimeout);
}

function SubMenuMouseOut()
{
    if(ActiveMenuItem != null)
    {
        clearTimeout(ActiveMenuItemHideTimeout);
        ActiveMenuItemHideTimeout = setTimeout( 
            function(){  
                if(ActiveMenuItem != null)
                {
                    var id = GetElementID(ActiveMenuItem);
                    $("#topMenuSub_" + id).hide();
                    ActiveMenuItem = null;
                }
            }, 10
        );
    }
}

function GetElementID(element)
{
    if(element != null)
    {
        var idChunks = $(element).attr("id").split("_");
        if(idChunks.length == 2)
        {
             return idChunks[1];
        }
    }
    return null;
}

//Inits an Accordion menu. Clicked item should have class LinkItem and its content to show
//should have class LinkBox
function InitAccordionMenu()
{
	// When clicking Linkitem...
    $("#Linklist1 h2.LinkItem").click(function() {
        if($(this).hasClass('Selected')) {
            close($(this));
        }
        else {
            open($(this));
        }
    });

	// Show first LinkBox in Linklist
	$("#Linklist1").children("div.LinkBox").hide();
	$("#Linklist1").children("div.LinkBox:first").show();
	$("#Linklist1").children("div.LinkBox:first").prev("h2.LinkItem").addClass('Selected');

	var open = function(element) {
	    // Change background image to selected for Linkitem
	    element.addClass('Selected');

	    // Slide down current LinkBox and slide upp all other LinkBoxes
	    element.next("div.LinkBox").slideDown("slow").siblings("div.LinkBox").slideUp("slow");

	    // Change background image to not selected for all other Linkitems
	    element.siblings().removeClass('Selected');
	};

	var close = function(element) {
	    // Change background image to selected for Linkitem
	    element.removeClass('Selected');

	    // Slide down current LinkBox and slide upp all other LinkBoxes
	    element.next("div.LinkBox").slideUp("slow").siblings("div.LinkBox").slideUp("slow");

	    // Change background image to not selected for all other Linkitems
	    element.siblings().removeClass('Selected');
	};
}




