//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();

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

	//replace thumbnail links with local anchor links
	var lastThumbnailDone="";
	$("#thumbnails li a").each( function() {
		$this = $(this);
		$this.attr("href","#"+$this.attr("href"));
		if(lastThumbnailDone=="") {
			lastThumbnailDone = $this.attr("href");
			fadeThumbnails(lastThumbnailDone,0.6);
		}
	});

	var lastThumbnailDone = "";
	$("#thumbnails li a").click(function(){
		$this = $(this);
		var dimensions = $this.attr("rel").split("|");

		var html = $this.attr("href");
		html = html.replace("#","");
		html ="<img src=\""+html+"\" width=\""+dimensions[0]+"\" height=\""+dimensions[1]+"\" alt=\""+$this.attr("title")+"\"/>"+"<div id=\"picture-title\">"+"<p class=\"title\">"+$this.attr("title")+"</p>";
		if($("img",$this).attr("longDesc")) html += "<p class=\"description\">"+$("img",$this).attr("longDesc")+"</p>";
		html += "</div>";


		$("#main-picture-area").html(html);
		
		lastThumbnailDone = $this.attr("href");
		fadeThumbnails(lastThumbnailDone,0.6);
	});

	$("#thumbnails a").mouseover(function(){
		$(this).animate({opacity: 1.0}, 200);
	});

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

	$("#picture-title").mouseover(function(){
		 $(this).hide("slide", { direction: "down" }, 1000);
	});
	
	$("#news").newsTicker();

	//replace images with flash images
	$("#main-content .zoomImage").addClass("thickbox").attr("rel", "gallery");

	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;

	//do not run on the location page
	if ( $("#map").length < 1 ) {
		$("#main-content-area #main-content img, #landing-page #main-content img, #spring-watch-landing-page #main-content img").flash(
			{ src: '/flash/image_border_rotate.swf'},
			{ version: 8 },
			function(htmlOptions) {
				var rotation = 0;
				$this = $(this);
				htmlOptions.height = $this.attr("height");
				htmlOptions.width = $this.attr("width");
				htmlOptions.flashvars.imageUrl = $this.attr("src");

				//parmas should be set up as colour | float | amount | direction
				var params = $this.attr("class").split(" ");
				if(params[0]=="birds") {
					fillColor = "0x9EB8F3";
				} else if (params[0]=="gardens") {
					fillColor = "0xF6B9F8";
				} else if (params[0]=="visit-us") {
					fillColor = "0xB7A1F1";
				} else if (params[0]=="conservation") {
					fillColor = "0xA6B888";
				} else if (params[0]=="discovery") {
					fillColor = "0xF6F599";
				} else if (params[0]=="family-fun") {
					fillColor = "0xF9C423";
				} else if (params[0]=="shopping-dinning") {
					fillColor = "0xF355AA";
				} else if (params[0]=="events") {
	//				fillColor = "0x26E0F1";
					fillColor = "0x172784";
				} else if (params[0]=="news") {
	//				fillColor = "0xE7CB69";
					fillColor = "0x172784";
				} else if (params[0]=="spring-watch") {
					fillColor = "0x9CF879";
				} else {
					fillColor = "0xC9EA71";
				}

				if(params[2]=="small") {
					rotation = 5;
				} else if (params[2]=="medium") {
					rotation = 10;
				} else if (params[2]=="large") {
					rotation = 15;
				} else if (params[2]=="none") {
					rotation = 0;
				} else {
					rotation = Math.floor((6-1)*Math.random()) + 2;
				}

	/*
				IMAGES ARE NO LONGER ROTATED AND SET VERTICALLY BY DEFAULT
				if(params[3]=="anticlockwise") {
					rotation = rotation * -1;
				} else if(params[3]=="clockwise") {
					rotation = rotation;
				} else if(params[3]=="none") {
					rotation = rotation;
				} else {
					var tempRandNo = Math.floor(10*Math.random())
					if (tempRandNo>4)
					{
						rotation = rotation * -1;
					}
				}
	*/
				rotation = 0;

				htmlOptions.flashvars.rotation = rotation;
				if($this.parent())
				{
					htmlOptions.flashvars.linkUrl = $this.parent().attr("href");
					htmlOptions.flashvars.gallery = $this.parent().attr("rel");
				}
				htmlOptions.flashvars.captionText = $this.attr("alt");
				htmlOptions.flashvars.fillColor = fillColor;
				htmlOptions.flashvars.domain = location.host;

				$this.before("<div class=\"flash-wrap-"+params[1]+"\" style=\"width:"+$this.attr("width")+";\">"+$.fn.flash.transform(htmlOptions)+"</div>").remove(); 
	//			$this.before($.fn.flash.transform(htmlOptions));
	//			$this.addClass('flash-replaced');
			}
		);
	}

	//do not replace if editing
	if ( $(".editable-item").length < 1 ) {

		$("#flash-homepage-banner").flash(
			{ src: "/flash/rotate_home.swf",
			  width: 730,
			  height: 365,
			  flashvars: { domain: location.host }
			},
			{ version: 8 }
		);

		$("#sightings-board").flash(
			{ src: "/flash/sightings.swf",
			  width: 624,
			  height: 414,
			  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;
	});


	//tidy up the styling
	$(".further-info-header").each( function() {
			$this = $(this);
			var temp = $this.html();
			temp = temp.replace(" ","");
			temp = temp.replace(/\t/g,"");
			temp = temp.replace(/\n/g,"");
			if(temp=="")
			{
				$this.parent().css("visibility", "hidden");
			}
	});

	$(".cell .text .inner-tube").each( function() {
			$this = $(this);
			var temp = $this.html();
			temp = temp.replace(" ","");
			temp = temp.replace(/\t/g,"");
			temp = temp.replace(/\n/g,"");
			if(temp!="")
			{
				$this.parent().parent().css("border-bottom", "1px solid #CBC6A3");
			}

	});

	if ( $("#side-navigation .level-1").length < 1 ) {
		$("#secondary-navigation").css("height", "3px");
	}

});
