﻿var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;
var animateSpeed = 250;
var origWidth = 0;
var origHeight = 0;

function menu_open()
{
    menu_canceltimer();
	//alert("hello");
	if ($(this)[0].id != null && ($(this)[0].id == 'sector-menu' || $(this)[0].parent.id == 'sector-menu'))
    {
//        var top = $(this).offset().top + 10;
//        var right = $(this).parent().width() + $(this).offset().left - 20;
//        var margin = $(this).css('margin-left');
//        var height = 0;
    
        ddmenuitem = $('.sector_items');
        if (ddmenuitem.find('li') != null)
        {
        	
//	        ddmenuitem.css('top', top);
//	        ddmenuitem.css('left', right);
//	        ddmenuitem.css('margin-left', margin);
//	        ddmenuitem.find('li').css('visibility','visible');
//	        ddmenuitem.css('visibility','visible');
//	        ddmenuitem.stop().animate({height:origHeight,padding:'5px',width:origWidth, borderWidth:'10px'},animateSpeed);
            ddmenuitem.slideDown();
            
	    }
	}

}

function get_menu_width()
{
    var result = 0;

    for(var i = 0; i < $('.sector_items li').length; i++)
    {
        var test = measure_string($('.sector_items li')[i].innerHTML);
        
        if (test > result)
        {
            result = test;
        }
    }
    
    return result;
}

function measure_string(test)
{
    return test.offsetWidth;
}

function menu_close()
{
    if (ddmenuitem)
    {
//        ddmenuitem.find('li').css('visibility','hidden');
//        ddmenuitem.stop().animate({height:'0px',padding:'0px',width:'0px', borderWidth:'0px'},animateSpeed);	
           //ddmenuitem.slideUp();
    }   
}

function menu_timer()
{
    closetimer = window.setTimeout(menu_close, timeout);
}

function menu_canceltimer()
{
    if(closetimer)
	{
	    window.clearTimeout(closetimer);
		closetimer = null;
}
}

$(document).ready(function()
{
    origWidth = $('.sector_items').width();//get the "natural" width of the menu before we set it to 0
    origHeight = $('.sector_items').height();//get the "natural" height of the menu before we set it to 0
    
	$('.left-menu > dl').bind('mouseover', menu_close);
	
	$('#sector-menu').bind('mouseover', menu_open);
	$('#sector-menu').bind('mouseout', menu_timer);
	
	$('.sector_items > li').bind('mouseover', function(){menu_open();});//direct call to menu_open() will cause javascript error due to value of this
	$('.sector_items > li').bind('mouseout', menu_timer);
	
	$(document).bind('mouseover', menu_close);
	
	ddmenuitem = $('.sector_items');
	menu_close();
}
);
