//when page loads start standard functions
function initialize() {
	//replace external links with open in new window
	externalLinks();
}

function $import(src){
	var scriptElem = document.createElement('script');
  scriptElem.setAttribute('src',src);
	scriptElem.setAttribute('type','text/javascript');
	document.getElementsByTagName('head')[0].appendChild(scriptElem);
}


// import with a random query parameter to avoid caching
function $importNoCache(src){
	var ms = new Date().getTime().toString();
	var seed = "?" + ms; 
	$import(src + seed);
}


function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors .length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
			anchor.title = (anchor.title != "") ? anchor.title+" (opens in a new window)" : "opens in a new window";
			anchor.className = (anchor.className != '') ? anchor.className+' external' : 'external';
		}
	}
}


function loadMap() {
if (GBrowserIsCompatible()) {
	var icon = new GIcon();
	icon.image = "/images/mappointer.png";
	icon.shadow = "/images/mappointershadow.png";
	icon.iconSize = new GSize(74, 41);
	icon.shadowSize = new GSize(115, 41);
	icon.iconAnchor = new GPoint(37, 41);
	icon.infoWindowAnchor = new GPoint(37, 41);
	
	
	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(52.827957, 0.892000), 9);

	var marker = new GMarker(map.getCenter(), icon);
	map.addOverlay(marker);
/*
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<span style=\"font-family: 'Arial', Helvetica, sans-serif; font-size: 70%;\"><span style=\"color: #26488B; font-weight: bold; font-size: 120%;\">Pensthorpe,</span><br/>Fakenham, Norfolk,<br/>England. NR21 0LN</span>");
	});
*/
	GEvent.addListener(marker, "click", function() {
		map.setCenter(new GLatLng(52.827957, 0.892000), (map.getZoom()+1))
	});
  }
}


function prepareName()
{
	if(document.getElementById('name').value=='your name') document.getElementById('name').value='';
}

function prepareEmail()
{
	if(document.getElementById('email').value=='email address') document.getElementById('email').value='';
}

function fadeThumbnails(lastThumbnailDone,amount)
{
	$("#thumbnails li a").each( function() {
		$this = $(this);
		if (lastThumbnailDone != $this.attr("href"))
		{
			$(this).animate({opacity: amount}, 200);
		}
	});
}



$(document).ready(function() {

	initialize();

	// Reset Font Size
	var originalFontSize = $("html").css("font-size");
		$(".resetFont").click(function(){
		$("html").css("font-size", originalFontSize);
		$.cookie("cookieFontSize", "", { expires: -1 });
	});
	
	// Increase Font Size
	$(".enlarge-text").click(function(){
		var currentFontSize = $("html").css("font-size");
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*1.2;
		$("html").css("font-size", newFontSize);
		$.cookie("cookieFontSize", newFontSize, { expires: 7 });
		return false;
	});
	
	// Decrease Font Size
	$(".reduce-text").click(function(){
		var currentFontSize = $("html").css("font-size");
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*0.8;
		$("html").css("font-size", newFontSize);
		$.cookie("cookieFontSize", newFontSize, { expires: 7 });
		return false;
	});

	//cookie code
	if($.cookie("cookieFontSize")!="")
	{
		$("html").css("font-size", $.cookie("cookieFontSize"));
	}


	//menu initialiser
	$('#main-navigation-Level-1 > li').bind('mouseover', mmul_open);
	$('#main-navigation-Level-1 > li').bind('mouseout',  mmul_timer);

	if ( $("#map").length > 0 ) {
		loadMap();
	}

	//do not replace if editing
	if ( $(".editable-item").length < 1 ) {
		$("#sightings-board").html("");
		$("#sightings-board").flash(
			{ src: "/flash/sightings.swf",
			  width: 624,
			  height: 414,
			  wmode: 'transparent', 
			  flashvars: { domain: location.host }
			},
		{ version: 8 }
		);

	}

	$(".photo-gallery-photo").each( function() {
		document.getElementById($(this).attr("id")).width = width2;
//		document.getElementById($(this).attr("id")).height = height;
	});

	$("#main_picture #thumbnails .thumbnail img").mouseover(function(){

//		$("#main_picture #comments").stop().animate({opacity:"100"});
//		$("#main_picture #comments").css("display","block");

		$("#main_picture #main_image img").attr("src",$(this).attr("rel"));
		$("#main_picture #main_image img").attr("alt",$(this).attr("alt"));
		$("#main_picture #comments .inner_tube").html("<p>"+$(this).attr("longdesc")+"</p>");
		$("#main_picture #headline").html($(this).attr("alt"));

//		setTimeout(function() { $("#main_picture #comments").fadeOut(); }, 5000);

	});

	$("#more_content a").click(function(){
//		$("#main_content_area").height($("#main_content").height());
		$("#main_content_area").animate({ 
			height: $("#main_content").height()
		}, 1500 );

		
		$("#more_content").css("display","none");
	});


	//do not do if editing
	if ( ($(".editable-item").length < 1 ) && ($(".autoheight").length > 0 )) {

		if($("#main_content").height()>$("#main_content_area").height()) 
		{
			$("#more_content").css("display","block");
		} else {
			$("#more_content").css("display","none");
			$("#main_content_area").height($("#main_content").height()+200);
			$("#main_content_area").height("auto");
		}

	} else {
		$("#more_content").css("display","none");
		$("#main_content_area").height("auto");
	}

	//do not replace if editing
	if ( $(".editable-item").length < 1 ) {
		$(".register_link").colorbox({width:"600px", height:"460px", iframe:true});
		$(".add_comments_link").colorbox({width:"600px", height:"500px", iframe:true});
	}

	$(".text-section:contains(' ')").prepend("<div class=\"divider\"></div>");

	if ( $(".editable-item").length < 1 ) {
		$('#cyclepics').cycle({ 
			fx:    'fade', 
			speed:  2500 
		 });
	}

	if($("#further_links a").length < 1) {
		$("#further_links").css("display","none");
	}

	$("a.display_comments_link").click(function(){
		$("#"+$(this).attr("rel")).slideToggle("slow");
	});


	//add section home to main navigation:

	$(".a-level-1").each( function() {
		$parent = $(this).parent();
		var html = $("span", $(this)).html();
		$a = $("a", $parent);
		var href = $a.attr('href');
		var title = $a.attr('title');
		var html = $("span", $a).html();
		var classAttr = $a.attr('class');
		var current = false;
		if (classAttr.search("current")>-1) current = true;
		if($("ul", $parent).length >0) {
			var htmlToAdd = "<li";
			if (current) htmlToAdd += ' class="li-current"'
			htmlToAdd += '><a class="';
			if (current) htmlToAdd += 'current '
			htmlToAdd += 'a-level-2" title="'+title+'" href="'+href+'"><span';
			if (current) htmlToAdd += ' class="current-span"'
			htmlToAdd += '>'+html+'</span></a></li>';
			$(htmlToAdd).prependTo($("ul", $parent));
		}
	});

});