$(function() {
    var mainMenuAct = getMainMenuAct();
    var baseTitle='IntelliTree Solutions';
    // hide all sub menu points but the one belonging to the active main menu:
    $(".submenu").not("[@id='"+mainMenuAct+"']").hide();
    // disable permanent dotted borders around links:
    $('a').click(function() { this.blur(); });
    // no white separator line after the last menu point:
    $('#mainmenu li:last-child a').css('padding-right', '0px');
    // initializing the menu point images:
    $('#mainmenu a').each(function(){
        var sel = isActive($(this), 'main');
        $(this).html('<img src="/img/' + $(this).attr('name') + sel + '.gif" border="0">');
    });  
    // creating menu hover effect: replacing images:
    $('#mainmenu a img').hover(function(){
        $(this).attr('src', '/img/'+$(this).parent().attr('name')+'-over.gif');                       
    },function() {
        var sel = isActive($(this).parent(), 'main');
        $(this).attr('src', '/img/'+$(this).parent().attr('name') + sel +'.gif');                       
    });
    // Adding circle images:
    $(".submenu > li > a").prepend("<img src='/img/anim0.gif' class='circle'>");
	// Searching for an active sub-sub menu point:
	var collapsibleLis = $(".submenu > li[ul]");
    collapsibleLis.each(function(){
        $(this).find(" > ul > li > a").each(function(){
            if( isActive($(this), 'sub') )
            {
                $(this).parent().addClass('active'); // highlighting if active
            }
        });
    });                      
    // Searching for an active sub- menu point: Non-collapsible li-s:
    $(".submenu > li").not(' > ul').each(function(){
        if( isActive($(this).find(" > a")) )  // highlighting if active
        {
            $(this).addClass('active');
            $(this).find(" > a > img").attr("src", "/img/circle_active.gif");
        }
    });
	// Initial expanding/collapsing of the sub-sub menupoints of the visible sub menu:
	$(".submenu[@id='"+mainMenuAct+"'] > li[ul]").each(function(i){
        if( isExpanded(i, $(this).attr('name')) )
        {
            $(this).addClass("expanded").find(" > a + ul").slideDown(1);
        }
        else $(this).addClass("collapsed").find(" > a + ul").slideUp(1);
		// sorszamozzuk a lenyithato submenupontokat:
		$(this).find(" > a").attr('id', 'sub_'+i);
	});
    // if a main menu point was clicked:
    $("#mainmenu").click(function(event) {
        var newMainMenuAct = $(this).attr('name');  // the new acive main menu point
        // storing that in cookie:
        $.cookie('mainMenuAct', newMainMenuAct);
		resetExpanded();
    });    
    // Expand or collapse:
    collapsibleLis.find(" > a").click(function(event) {
        // replacing plus and minus signs:                         
        $(this).parent().toggleClass("collapsed").toggleClass("expanded");
		var i = $(this).attr('id').split('_')[1];
        // animation effect on the embedded ul-s:
        $(this).find("+ ul").each(function() {
			var state = $(this).is(":hidden") ? "show" : "hide";
			$(this).animate({height: state}, 'slow', 'bounceout');
			setExpanded(i, state);
        });    
        event.preventDefault();
        return false;
    });
});  

// which: 'main' or 'sub'
// 'main': returns the selected image tag ('-sel') if the link is just the active page:
// 'sub':  returns true if the sub menu link is just the active page, false otherwise
function isActive(link, which)
{
    if( which=='main' ) return link.attr('name')==getMainMenuAct() ? '-sel' : '';
    else return (document.location.toString().indexOf(link.attr('href'))>-1);
}

function getMainMenuAct()
{
    var result = /:\/\/[^\/]+\/([^\/]+)/.exec(document.location.toString());
    if( result ) return result[1];
    else return 'home';
}

function setExpanded(i, state)
{
	$.cookie('expandedState_'+i, state, {path: '/'});
}

function isExpanded(i, subMenuPointName)
{
	// Ez egy http://intree/products/it_support/managedit jellegu URL-bol kiszedi
	// az 'it_support'-ot:
    var subPageName = /:\/\/[^\/]+\/[^\/]+\/([^\/]+)\/[^\/]+/.exec(document.location.toString());
	// Ha ilyen jellegu az URL, akkor eppen az '$infoTextSupport' sub menu point lesz
	// expanded, fugegtlenul attol, hogy a kukiban mi van:
	if( subPageName && subPageName[1]==subMenuPointName )
	{
		setExpanded(i, 'show');
		return true;
	}
	var state = $.cookie('expandedState_'+i);
	if( state==null || state=='' || state=='hide' ) return false;
	return true;
}

function resetExpanded()
{
	var state;
	for( var i=0; i<100; i++ )
	{
		state = $.cookie('expandedState_'+i);
		if( state!=null && state!='' ) $.cookie('expandedState_'+i, '', {path: '/'});
	}
}

function dumpExpanded()
{
	var state;
	var s="";
	for( var i=0; i<10; i++ )
	{
		state = $.cookie('expandedState_'+i);
		if( state!=null && state!='' ) s += i+": "+state+"\n";
	}
	alert(s);
}

