var sActiveFold = false;

$(document).ready( function() {

		
	$('a.foldoutbtn').click( function() {
		var sTarget = this.id.replace('foldouttoggle_','foldout_');
		if($('#'+sTarget).hasClass('visible')) {
			hideFold(sTarget,this);
		} else {
			showFold(sTarget,this);
		}
		
	});

	
	$('li.level-1').hoverIntent({
		over: function() {
			var sTarget = this.id.replace('level_','children_');
			showChildren(sTarget,this);		
		},
		timeout: 200,
		sensitivity: 100,
		out: function() {
			var sTarget = this.id.replace('level_','children_');
			hideChildren(sTarget,this);		
		}
	
	});
	
	/*
	$('li.level-1').hover( function() {
		var sTarget = this.id.replace('level_','children_');
		showChildren(sTarget,this);
	}, function() {
		var sTarget = this.id.replace('level_','children_');
		hideChildren(sTarget,this);		
	});
	*/
	
	
	$('div.boxhome').click( function() {
		var sTarget = this.id.replace('box_','foldout_');
		var sPosition = this.id.replace('box_','foldouttoggle_');
		var oPosition = $('#'+sPosition);
		
		showFold(sTarget,oPosition);
	});
	
	
	
	$('ul.folder').hoverIntent({
		over: function() {},
		timeout: 200,
		sensitivity: 100,
		out: function() {
			$(this).parent().fadeOut('fast');
		}
	
	});
	
	
});


function showChildren(sTarget,oSource) {
	// Hide others
	$('ul.children').hide();

	/*
	iHeight = $('#'+sTarget).parent().height();
	$('#'+sTarget).css({bottom: iHeight});
	*/
	
	$('#'+sTarget).show();
	$.post(LINKROOT+'/do/frontend/setlastactive',{cat: 'toggle_'+sTarget.replace('children_','')});
}

function hideChildren(sTarget,oSource) {
	$('#'+sTarget).hide();
}

function showFold(sTarget,oSource) {
	// Hide others
	$('div.visible-foldout').fadeOut('fast').removeClass('visible-foldout').hide();
	
	// Position target
	oPosition = $(oSource).position();	
	iLeft = oPosition.left;
	iTop = oPosition.top;
	
	
	if($('#'+sTarget.replace('foldout_','box_')).hasClass('boxhome-2')) {
		iLeft = iLeft-208;
	} else {
		iLeft = iLeft+208;	
	}
	
	
	iTop = iTop + 29;
	//iLeft = iLeft + 5;
	iTop = iTop - $('#'+sTarget).height();
	

	
	
	
	$('#'+sTarget).css({left: iLeft, top: iTop}).fadeIn('fast').addClass('visible-foldout');
	$.post(LINKROOT+'/do/frontend/setlastactive',{cat: 'group_'+sTarget.replace('foldout_','')});
}

function hideFold(sTarget,oSource) {
	// hide 
	$('#'+sTarget).fadeOut('fast').removeClass('visible-foldout');
}


