var hoverMenu = 0;
var currentMenuItem = '';

$(document).ready(function(){
	$('a[rel="print"]').click( function() {
	 	TheNewWin = window.open($(this).attr('href'),'mypop','height=600,width=555,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no');
	  return false;
	});
	
	if($.browser.msie){
		initMenu_down();
	} else {
		initMenu();
	}
	
	/* Makes all images larger than 350 px in the right column without margin */
	$("#wrapper-right img.border").each(function(){
		var thisObject = $(this);
		if(thisObject.width() > 350){
			thisObject.css({
				'margin' : '0'
			});
		}
	});
	
	/* Makes all "/ladda-hem"-files open in a new window (ladda hem == download) */
	$('a[href^=/ladda-hem/]').click(function(ev) {
		ev.preventDefault();
		void(window.open($(this).attr('href'),'',''));
	});
	
	/* Makes all "http://wm.crossnet.net/furuhojdskyrkan/"-files open in a new window (streaming media) */
	$('a[href^=http://wm.crossnet.net/furuhojdskyrkan/]').click(function(ev) {
		ev.preventDefault();
		void(window.open($(this).attr('href'),'',''));
	});
	
});

function initMenu(){
	/* Makes main-items switch on hover */
	$("#menu > a").hover(function(){
		var menuItem = $(this);
		var tmpSrc = menuItem.find('img').attr('src');
		menuItem.find('img').attr('src', tmpSrc.substring(0, tmpSrc.indexOf('.png')) + '_hover.png');
	}, function(){
		var menuItem = $(this);
		var tmpSrc = menuItem.find('img').attr('src');
		tmpSrc = tmpSrc.replace('_hover', '');
		menuItem.find('img').attr('src', tmpSrc);
	});
	
	//Simple how to tell if the menu is hovered or not
	$("#menu").mouseenter(function(){
		hoverMenu = 1;
	});
	
	/*
		When leaving the menu, wait and restore
		The wait is useful for two reasons, (1) it looks better, (2) it avoids accidently menu-up-and-down-loops
	*/
	$("#menu").mouseleave(function(){
		hoverMenu = 0;
		var t = setTimeout('restoreMenu();', 1250);
	});
	
	/* Make the menu animate upwards only for those items with a submenu */
	$("#menu .submenu").each(function(){
		var tmpObject = $(this);
		var tmpId = filterString(tmpObject.attr('id'), 'submenu_');
		
		$("#menu_" + tmpId).mouseenter(function(){
			if(parseInt($("#menu").css('marginTop')) > 160){
				$("div.submenu").hide();
				$("#submenu_" + tmpId).show();
				$("#menu").animate({
					marginTop : '160px'
				}, 500, function(){
					
				});
			} else {
				if(currentMenuItem != 'menu_' + tmpId){
					$("#menu").stop(true);
					$("#menu").animate({
						marginTop : '187px'
					}, 500, function(){
						$("div.submenu").hide();
						$("#submenu_" + tmpId).show();
					}).animate({
							marginTop : '160px'
						}, 500, function(){
							
						});
				}
			}
			currentMenuItem = 'menu_' + tmpId;
		});
	});
	
	/* If a main-item is set to active, then animate up the menu */
	if($("#menu a.active").length > 0){
		var tmpId = filterString($("#menu a.active").attr('id'), 'menu_');
		
		if($("#submenu_" + tmpId).is(':hidden')){
			$("#menu").animate({
				marginTop : '187px'
			}, 500, function(){
				$("div.submenu").hide();
				$("#submenu_" + tmpId).show();
			}).animate({
					marginTop : '160px'
				}, 500, function(){
					
				});
		}
		currentMenuItem = 'menu_' + tmpId;
	}
}

function restoreMenu(){
	if(hoverMenu == 1){
		
	} else {
		//Check if a main-menu-item is set to active, then restore to that one
		if($("#menu a.active").length > 0){
			var tmpId = filterString($("#menu a.active").attr('id'), 'menu_');
			
			if($("#submenu_" + tmpId).length > 0){
				if($("#submenu_" + tmpId).is(':hidden')){
					$("#menu").animate({
						marginTop : '187px'
					}, 500, function(){
						$("div.submenu").hide();
						$("#submenu_" + tmpId).show();
					}).animate({
							marginTop : '160px'
						}, 500, function(){
							
						});
				}
				currentMenuItem = 'menu_' + tmpId;
			} else {		
				$("#menu").animate({
					marginTop : '187px'
				}, 1000);
			}
		} else {
			$("#menu").animate({
					marginTop : '187px'
				}, 1000);
		}
	}
}

function initMenu_down(){
	/* Makes main-items switch on hover */
	$("#menu > a").hover(function(){
		var menuItem = $(this);
		var tmpSrc = menuItem.find('img').attr('src');
		menuItem.find('img').attr('src', tmpSrc.substring(0, tmpSrc.indexOf('.png')) + '_hover.png');
	}, function(){
		var menuItem = $(this);
		var tmpSrc = menuItem.find('img').attr('src');
		tmpSrc = tmpSrc.replace('_hover', '');
		menuItem.find('img').attr('src', tmpSrc);
	});
	
	//Simple how to tell if the menu is hovered or not
	$("#menu").mouseenter(function(){
		hoverMenu = 1;
	});
	
	/*
		When leaving the menu, wait and restore
		The wait is useful for two reasons, (1) it looks better, (2) it avoids accidently menu-up-and-down-loops
	*/
	$("#menu").mouseleave(function(){
		hoverMenu = 0;
		var t = setTimeout('restoreMenu_down();', 1250);
	});
	
	/* Make the menu animate upwards only for those items with a submenu */
	$("#menu .submenu").each(function(){
		var tmpObject = $(this);
		var tmpId = filterString(tmpObject.attr('id'), 'submenu_');
		
		$("#menu_" + tmpId).mouseenter(function(){
			if(parseInt($("#top-and-menu").css('height')) < 245){
				$("div.submenu").hide();
				$("#submenu_" + tmpId).show();
				$("#top-and-menu").animate({
					height : '245px'
				}, 500, function(){
					
				});
			} else {
				if(currentMenuItem != 'menu_' + tmpId){
					$("#top-and-menu").stop(true);
					$("#top-and-menu").animate({
						height : '219px'
					}, 500, function(){
						$("div.submenu").hide();
						$("#submenu_" + tmpId).show();
					}).animate({
							height : '245px'
						}, 500, function(){
							
						});
				}
			}
			currentMenuItem = 'menu_' + tmpId;
		});
	});
	
	/* If a main-item is set to active, then animate up the menu */
	if($("#menu a.active").length > 0){
		var tmpId = filterString($("#menu a.active").attr('id'), 'menu_');
		
		if($("#submenu_" + tmpId).is(':hidden')){
			$("#top-and-menu").animate({
				height : '219px'
			}, 500, function(){
				$("div.submenu").hide();
				$("#submenu_" + tmpId).show();
			}).animate({
					height : '245px'
				}, 500, function(){
					
				});
		}
		currentMenuItem = 'menu_' + tmpId;
	}
}

function restoreMenu_down(){
	if(hoverMenu == 1){
		
	} else {
		//Check if a main-menu-item is set to active, then restore to that one
		if($("#menu a.active").length > 0){
			var tmpId = filterString($("#menu a.active").attr('id'), 'menu_');
			
			if($("#submenu_" + tmpId).length > 0){
				if($("#submenu_" + tmpId).is(':hidden')){
					$("#top-and-menu").animate({
						height : '219px'
					}, 500, function(){
						$("div.submenu").hide();
						$("#submenu_" + tmpId).show();
					}).animate({
							height : '245px'
						}, 500, function(){
							
						});
				}
				currentMenuItem = 'menu_' + tmpId;
			} else {		
				$("#top-and-menu").animate({
					height : '219px'
				}, 1000);
			}
		} else {
			$("#top-and-menu").animate({
					height : '219px'
				}, 1000);
		}
	}
}
