$(document).ready(function(){

	hoverMenu("#topMenu li.children");
	hoverMenu("#topMenuContact li.children");
	hasCurrent("#topMenu li.current");
	selectRegion();
	
	enableRegion($('#regionSelector li.selected a').attr("id"));
	
	
	$('.keywordSearch').keydown(function(e){
		if (e.keyCode == "13") $('#filter_form').submit();
	});
	
		if ($('#hintGallery').length > 0)
		hintGallery("hintGallery");
	
	$('.resetButton').click(function(){
		$('.keywordSearch').val("");
		return false;
		
	});
	
});


function MM_jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

function enableRegion(name) {

	if (name == null) name = "default";

	var mapName = "large-map-" + name +".gif"; 
	
		$('#regionalMap').css({
		'background-image' : 'url(images-scapa-global/'+ mapName +')',	
		'background-repeat' : 'no-repeat'
		})
	
}


function selectRegion() {
	
	$('#regionSelector ul li a').hover(function() {											  
	
	var mapName = "large-map-" + $(this).attr('id')+".gif"; 
	
		$('#regionalMap').css({
		'background-image' : 'url(images-scapa-global/'+ mapName +')',	
		'background-repeat' : 'no-repeat'
		})
	
	}


);	
	
	$('#regionSelector ul li a').click(function() {
		
		//reset 
		$('#regionSelector ul li a.selected').removeClass("selected");
		$('#refineSearchContainer > div:not(#searchHeadings)').css("display", "none");

		$(this).addClass("selected");
	
		var formContainer = "#search_" + $(this).attr('id'); 
		
		$(formContainer).css("display","block");
		
	});
	
 
	$('#regionSelector ul li').mouseleave(
	
	function () {
		formContainerID = $("#regionSelector ul li a.selected").attr("id");
		formContainer = "#search_" +formContainerID; 
				
		if (!formContainerID == "") {
		
		var mapName = "large-map-" + formContainerID+".gif"; 
		
			$('#regionalMap').css({
			'background-image' : 'url(images-scapa-global/'+ mapName +')',	
			'background-repeat' : 'no-repeat'
			})
			
		}else{
			
			$('#regionalMap').css({
			'background-image' : 'url(images-scapa-global/large-map-default.gif)',	
			'background-repeat' : 'no-repeat'
			})
			
		}
		
		enableRegion($('#regionSelector li.selected a').attr("id"));
		
	}
);
	

}

function hoverMenu(element) {
	
	var leftPos = $(element).width();
	var lefPosInt = (parseInt(leftPos)+6); //- 2 ;		
	
	var leftPostIntA = lefPosInt - 20;
	
	if (element == "#topMenu li.children"){
		//$(element).css("width",lefPosInt+"px");
	}
	
$(element).hover(
			
		function () {
			
				$(this).addClass("selected");
			//	$(this).removeClass("current");
			
			if (element == "#topMenu li.children") {
							
				var leftPos = $(element).width();
				var lefPosInt = parseInt(leftPos); //- 2 ;		
			
				$(this).find("ul:first").css({
					'display': 'block',
					'background-image' : 'url(images/border-top-wide.gif)',
					'background-repeat' : 'no-repeat',
					'background-position' : leftPos+'px 0px'
				});
				
			}else{
				
				$(this).find("ul:first").css({
					'display': 'block'
				});
							
			}
			
		},
		
		function () {
			$(this).removeClass("selected");
		//	$(this).addClass("current");
			$(this).find("ul:first").css("display", "none");	
		}
		
	);	
}

function hasCurrent(element){

if ($(element).hasClass("children")){

$(element).hover(

function() {
	$(this).removeClass("current");
	},
function() {
	$(this).addClass("current");
}

)

}
}









	function hintGallery(container) {
		
		jQuery.fn.isChildOfElement = function(b){
			return (this.parents(b).length > 0);
		};

		$('#' + container + ' .hintDiv').each(function(index){
			$(this).attr("id", container+"Div_" + (index+1));
			$("#" + container + "Div_" + (index+1) + " *").attr("rel", container+"Div_" + (index+1));
		});
		
		$('#' + container + ' img').each(function(index){
			$(this).attr("rel", container+"Div_" + (index+1));
			$(this).addClass("hintImg");
			$("#"+container+"Div_" + (index+1) + " div.image").html("<img alt=\"" + $(this).attr("alt") + "\" src=\"" + $(this).attr("src") + "\" />");
		});
		

		$(document).mouseover(function(event){

				if ($(event.target).isChildOfElement('#'+container) == false) {
					$('.hintDiv').fadeOut("4000");
				} 
				
				else if ($(event.target).hasClass('hintImg')) {

					var imgOffset = $(event.target).offset();

					$("#" + $(event.target).attr("rel")).css("top", (imgOffset.top - 13) + "px");
	
					if ($("#" + $(event.target).attr("rel")).hasClass("alignLeft")) {
						$("#" + $(event.target).attr("rel")).css("left", (imgOffset.left - ($("#" + $(event.target).attr("rel")).outerWidth() / 2) - 29) + "px");
						
					} else {
						$("#" + $(event.target).attr("rel")).css("left", (imgOffset.left - 13) + "px");
					}

					if ($("#" + $(event.target).attr("rel")).css("display") != "block") {

						$('.hintDiv[id!="' + $(event.target).attr("rel") + '"]').fadeOut("4000");
						$("#" + $(event.target).attr("rel")).fadeIn("2000");

					}
					
				}

		});
		
	}
	
