
var bkpsearch="";
var initpos=0;

$(document).ready(function() {

	// setup the search field.
	try {
		bkpsearch = $("#search").val();
		$("#search").focus(function() {
									if ($(this).val() == bkpsearch) {
										$(this).val("");
									}}	);
		$("#search").blur( function() {
									if ($(this).val() == "") {
										$(this).val(bkpsearch);
									}
									});
		
		$("#searchform").submit(function() { return !($("#search").val() == bkpsearch); });
	} catch(ex){}
				 
	 
	// back to top
	initpos = getY($("#backtotop").get(0));
	$(window).scroll(function() {
				set_backtotop();
							  });
	set_backtotop();
	
	
	
	// setup accordeons
	$(".level1 a:not(.noacc)").click(function() {
			if ($(this).parents(".level1").hasClass("opened")) {
				$(this).parents(".level1").removeClass("opened");
				$(this).parents(".level1").siblings(".level2").slideUp();
			} else {
				$(this).parents(".level1").addClass("opened");
				$(this).parents(".level1").siblings(".level2").slideDown();
			}
			
								  });
	//$(".level2:not(.noacc)").hide();

/*
	$(".lebanon .level1").addClass("opened");
	$(".lebanon .level2").show();
*/			
			
	// setup level 3 acc
	/*$(".level3:not(.noacc)").hide();*/
	$(".level2 a:not(.noacc)").click(function() {
			
			if ($(this).parent("li").hasClass("opened2")) {
				$(this).parent("li").removeClass("opened2");
				$(this).siblings(".level3").slideUp();
			} else {
				$(this).parent("li").addClass("opened2");
				$(this).siblings(".level3").slideDown();
				
				$(this).siblings(".level3").children("li").removeClass("opened2");
				
			}
			
								  });
	
	
	// careers
	//$(".careerbody").hide();
	$(".careertitle a:not(.noacc)").click(function() {
			
			if ($(this).hasClass("op")) {
				$(this).removeClass("op");
				$(this).parent().siblings(".careerbody").slideUp();
			} else {
				$(this).addClass("op");
				$(this).parent().siblings(".careerbody").slideDown();
			}
			
								  });
	
});


function set_backtotop() {
	
	if (!$("#sidebar").get(0)) return;
	
	var pos = $(window).height() + $(window).scrollTop() - 25;
	if (pos < initpos) pos = initpos;	
	var maxpos = getY($("#sidebar").get(0)) + $("#sidebar").get(0).offsetHeight - 20;
	if (pos > maxpos) pos = maxpos;
	
	$("#backtotop").css("top", pos + "px");
	if ($(window).scrollTop() == 0) {
		$("#backtotop").fadeOut();
	} else {
		$("#backtotop").fadeIn();
	}
}


// note: replace by some jQuery function
function getY(obj) {
	var tmp = obj;
	var y = 0;
	if (!tmp) return y;
	do {
		y += tmp.offsetTop;
		tmp = tmp.offsetParent;		
	} while (tmp);
	return y;
}
