function DESemail(emailname,emailserver) {
	document.write("<a href='mailto:" + emailname + "@" + emailserver +"'>");
	document.write(emailname + "@" + emailserver); 
	document.write("</a>"); 
}

// Accessible Pop Ups
function DESopen(url,width,height,toolbar,scroll) {
	window.open(url, "", "scrollbars="+scroll+",toolbar="+toolbar+",height="+ height +",width="+width);
	return false;
}


// sets the select box values to 0 (called onload
function setFilterDefaults() {
	document.getElementById("parent_id").selectedIndex=0;
}


$(document).ready(function(){
	
	// Submit link change category selector
	if (document.getElementById("link_submit")) { 
		$("#parent_id").change(
			function() {
				var formNum = $(this).val();
				$("#link_submit fieldset.link_hidden").html(); // clear all fieldset html
				$("#link_submit fieldset.link_hidden").hide(); // hide all fieldsets
				if (formNum != 0) {
					// populate desired fieldset
					$("#fieldset_"+formNum+"").load("/incs/ahah/link_form.php",
					  {id: formNum},
					  function() { $("#fieldset_"+formNum+"").show(); } // show fieldset
					);
				}
			}
		);
	}
	
	// remove intro from Contact Form
	$("#message").focus( function() {
		var textMessage = $(this).text();
		if (textMessage=='Your message...') $(this).text("");
	});
	
  // date selectors for contact form
	if (document.getElementById("arrive")) { $("#arrive").datepicker(); }
	if (document.getElementById("depart")) { $("#depart").datepicker(); }
			
	
	//  Photo Gallery
	$("ul.thumbs a").click(function() {

		$("#image img").fadeOut('slow').remove();
		$("#preloading").css("display","block");
		var randomnumber=Math.floor(Math.random()*150)
		var newSrc = $(this).get(0).href;
		/*
			Add a random number to image to make browser load again
			Ensures .onload works in IE/Opera
			Rubbish hack, would like to improve
		*/

		var newSrc = ''+newSrc+'?ran='+randomnumber+'';
		var nextImg = new Image();
		nextImg.src = newSrc;
		var nextImgHTML = '<img src="'+newSrc+'" />';
		nextImg.onload = function()
		{
			$("#preloading").css("display","none");
			// Safari didn't like append, don't know why I used it anyways!!
			//$("#image").append(nextImg);
			$("#masterPic").html(nextImgHTML);
			$("#masterPic img").fadeIn('slow');
		}

		//if (nextImg.complete) {
		//	nextImg.onload();
		//}

		$("ul.thumbs a").removeClass("current");
		$(this).addClass("current");
		
		return false;
	});


	// Jump Links
	$("select.jump_links").change(
		function() {
			var theLink = $(this).val();
			window.location=theLink;
		}
	);
	
	// Browse Sites Hover
	$(".site").hover(
		function() { $(this).addClass("hovered");},
		function() { $(this).removeClass("hovered");}
	);
	// Browse Sites Link
	$(".site").click( function() {
		var pageLink = $(this).find("td.link a").attr("href");
		window.location = pageLink;
		return false;
	});
	
	// Image Map
	var rel_to_region_map = 
	{
		"ni" : "i1",
		"cornwall" : "e11",
		"somerset" : "e10",
		"sussex" : "e9",
		"oxford" : "e8",
		"london" : "e7",
		"norfolk" : "e6",
		"derbyshire" : "e5",
		"shropshire" : "e4",
		"powys" : "w3",
		"pembrokeshire" : "w2",
		"anglesey" : "w1",
		"manchester" : "e3",
		"yorkshire" : "e2",
		"durham" : "e1",
		"borders" : "s6",
		"glasgow" : "s5",
		"stirling" : "s4",
		"perth" : "s3",
		"aberdeen" : "s2",
		"highlands" : "s1"	
	};
	
	var rel_to_title_map = 
	{
		"ni" : "Northern Ireland",
		"cornwall" : "Cornwall, Devon",
		"somerset" : "Somerset, Dorset, Wiltshire",
		"sussex" : "Surrey, West Sussex, East Sussex, Kent",
		"oxford" : "Buckinghamshire, Oxfordshire, Berkshire, Hampshire, Isle of Wight",
		"london" : "Bedfordshire, Hertfordshire, Essex, London",
		"norfolk" : "Lincolnshire, Norfolk, Suffolk, Cambridgeshire",
		"derbyshire" : "Derbyshire, Nottinghamshire, Leicestershire, Rutland, Northamptonshire, Warwickshire",
		"shropshire" : "Shropshire, Staffordshire, Herefordshire, Worcestershire, Gloucestershire, West Midlands",
		"powys" : "Powys, Monmouthshire, South Wales",
		"pembrokeshire" : "Ceredigion, Pembrokeshire, Carmarthenshire",
		"anglesey" : "Anglesey, Gwynedd, Conwy, Flintshire,Denbighshire, Flintshire, Wrexham",
		"manchester" : "Cheshire, Greater Manchester, Lancashire, Merseyside",
		"yorkshire" : "North Yorkshire, East Yorkshire, West Yorkshire, South Yorkshire",
		"durham" : "Durham, Northumberland, Tyne and Wear, Cumbria",
		"borders" : "Dumfries and Galloway, Borders, Lothian, Edinburgh",
		"glasgow" : "Ayrshire, Glasgow, Dunbartonshire, Lanarkshire, Renfrewshire, Inverclyde",
		"stirling" : "Argyll & Bute, Stirling",
		"perth" : "Angus, Perth & Kinross, Fife, Clackmannanshire",
		"aberdeen" : "Aberdeenshire, Moray",
		"highlands" : "Highlands, Western Isles"	
	};
	

	if (document.getElementById("search_map_img")) {
		
		// Image map hidden value update
		$("#search_map area").click( function() {
			
			// get code, and title from the REL attribute
			var regionID = $(this).attr("id");
			var regionCode = rel_to_region_map[regionID];
			var regionTitle = rel_to_title_map[regionID];
			
			//alert(regionRel);
			//alert(regionID);
			
			$("#region").val(regionCode); // update the hidden input with code
			$("#region_alts").html(regionTitle); // update the fake textarea with title
			
			// animate background color
			$("#region_alts").animate({ backgroundColor: "#FFFF91" }, 250);
			$("#region_alts").animate({ backgroundColor: "#fff" }, 500);
			
			return false;
		});
		
		// Highlight Image map on hover
		$.fn.maphilight.defaults = {
			fill: true,
			fillColor: 'ffffff',
			fillOpacity: 0.5,
			stroke: false,
			strokeColor: 'ff0000',
			strokeOpacity: 1,
			strokeWidth: 1,
			fade: true,
			alwaysOn: false
		}
		$('#search_map_img').maphilight();
	
	}
	
	
}); // End jQuery