/*
thunderbirdit()
e-mail constructor	
*/
function thunderbirdit()
{
	var domain = "@alecgalloway.co.uk";
	var myurl = "ma" + "ilto:" + "ag" + domain;
	var displaytext = '<p><img id="postbox" src="images/winopener/mailbox.jpg"><br /><a href="'+myurl+'">e-mail alec galloway</a></p><p id="mailwintext">mobile: 07590296606';
	var thunderbirdwin=dhtmlwindow.open('contactwin', 'inline',	displaytext,'Contact Details',
																			'width=425px,height=350px,center=1,resize=1,scrolling=1'); 
}

/*

	creates navigator mouseover and mouseout actions on nav images
	
	loads corresponding over-state image when nav image is hovered
	and restores mormal-state image on mouse out.
	
	called by onmouseover handler of navigator a element eg
	<a href="home.php" onclick="navListener('home', 'over');return false;" ><img src="images/text/home.png" /></a>

	each nav link must have two images named according to link  eg home.png and home_over.png or about.png and about_over.png
	id on nav image must be set to "nav"+ the image name set above eg navhome or navabout
	
	the paths and ext on nav images are hard coded into routine
	
	*/
	
function navListener(imagename, mouseaction)
{
		// get the nav image and source using the id constructed from the image name
		var navImg = document.getElementById("nav"+imagename);
		var navImgPath = 'images/text/';
	  var imgExt = '.png';
		
		if (mouseaction=='over')
		{
			// mouse over so get corresponding hover state image
			navImg.src = navImgPath+imagename+"_over"+imgExt;
		}
		else
		{
			// mouse out so restore the normal state omage
			navImg.src = navImgPath+imagename+imgExt;
		}

}

/*
	setimageto(imagename as string):	eg setimageto('mylargeimage')
	
	loads corresponding main image when a thumbnail is clicked
	called by onclick handler of img element that points to thumbnail eg
	<img src="images/gallery/thumbs/african2.jpg" onclick="setimageto('african2');return true;"/>

	An img element on page must have an id = "mainimage" which is a placeholder for large image
	All images in database must have a title and text description otherwise crashes
	The current image fades out and is replaced by the new image which fades in
	The timer settings for fade out and fade in are optimised to reduce flicker
	
	*/
	function setimageto(imagepath, imgtext)
	{
			//alert("image text is "+imgtext);
			var imgname = imagepath.substring((imagepath.lastIndexOf("/")+1), imagepath.length-4)
			//alert("image name is "+imgname);
			//alert("image text is "+imgtext);
			// get the path to the large image currently being displayed
			var currentPath = document.getElementById("mainimage").src;
			// 	strip the image name out of the path - assumes ext is 4 characters		
			var currentImage = currentPath.substring((currentPath.lastIndexOf("/")+1), currentPath.length-4)
			// if click was on the thumbnail of large image currently being displayed do nothing - avoid flicker
			if (currentImage==imgname)
				return;
			// new thumnail clicked - change the large image
			// fade the current image out
			FadeOpacity("mainimage", 99.99, 0, 600, 12);
			FadeOpacity("imgtext",99.99, 0, 600, 12);
			// change the source only after fade out is complete
			setTimeout("changeSource('"+imgname+"')", 1200);
			setTimeout("changeText('"+imgtext+"')", 1200);
			
	}

	function dbsetimageto(imagepath, imgtitle, imgtext)
	{
			//alert("image text is "+imgtext);
			var imgname = imagepath.substring((imagepath.lastIndexOf("/")+1), imagepath.length-4)
			//alert("image name is "+imgname);
			//alert("image text is "+imgtext);
			// get the path to the large image currently being displayed
			var currentPath = document.getElementById("mainimage").src;
			// 	strip the image name out of the path - assumes ext is 4 characters		
			var currentImage = currentPath.substring((currentPath.lastIndexOf("/")+1), currentPath.length-4)
			// if click was on the thumbnail of large image currently being displayed do nothing - avoid flicker
			if (currentImage==imgname)
				return;
			// new thumnail clicked - change the large image
			// fade the current image out
			FadeOpacity("mainimage", 99.99, 0, 600, 12);
			FadeOpacity("imgtext",99.99, 0, 600, 12);
			FadeOpacity("imgtitle",99.99, 0, 600, 12);
			// change the source only after fade out is complete
			setTimeout("changeSource('"+imgname+"')", 1200);
			setTimeout("dbchangeText('"+imgtitle+"','"+imgtext+"')", 1200);
	}




	function changeSource(imagename)
	{
			// get the img element on page that has an id of mainimage. It is a placeholder for the large image
			var imageHolder = document.getElementById("mainimage");
			// 	get the path to the new large image		
			var imageSource = 'images/gallery/fullsize/'+imagename+'.jpg';
			// switch source
			imageHolder.src = imageSource;
			// restore opacity on imageholder
			FadeOpacity("mainimage", 0, 99.99, 1200, 12);
	}

	function changeText(imagetext)
	{
			//alert("changing text");
			// get the td element on page that has an id of imgtext. It is a placeholder for the image description
			var textHolder = document.getElementById("imgtext");
			// change the text to new description
			textHolder.firstChild.nodeValue=imagetext;
			// restore opacity on textHolder
			FadeOpacity("imgtext", 0, 99.99, 1200, 12);
	}

	function dbchangeText(imagetitle, imagetext)
	{
			//alert("changing text");
			// get the td element on page that has an id of imgtext. It is a placeholder for the image description
			var textHolder = document.getElementById("imgtext");
			var titleHolder = document.getElementById("imgtitle");
			// change the title to new title
			titleHolder.firstChild.nodeValue=imagetitle;
			// change the text to new description
			textHolder.firstChild.nodeValue=imagetext;
			// restore opacity on textHolder
			FadeOpacity("imgtext", 0, 99.99, 1200, 12);
			FadeOpacity("imgtitle", 0, 99.99, 1200, 12);
	}


	function FadeOpacity(elemId, fromOpacity, toOpacity, time, fps)
	{
		var steps = Math.ceil(fps * (time / 1000));
		var delta = (toOpacity - fromOpacity) / steps;
		FadeOpacityStep(elemId, 0, steps, fromOpacity, delta, (time / steps));
	}

	function FadeOpacityStep(elemId, stepNum, steps, fromOpacity, delta, timePerStep)
	{
		SetOpacity(document.getElementById(elemId), Math.round(parseInt(fromOpacity) + (delta * stepNum)));
		if (stepNum < steps)
			setTimeout("FadeOpacityStep('" + elemId + "', " + (stepNum+1)	+ ", " + steps + ", " + fromOpacity + ", " + delta + ", " + timePerStep + ");", 	timePerStep);
	}
	
	function SetOpacity(elem, opacityAsInt)
	{
		var opacityAsDecimal = opacityAsInt;
   
		if (opacityAsInt > 100)
			opacityAsInt = opacityAsDecimal = 100; 
		else if (opacityAsInt < 0)
			opacityAsInt = opacityAsDecimal = 0; 
    
		opacityAsDecimal /= 100;
		if (opacityAsInt < 1)
			opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0
		elem.style.opacity = (opacityAsDecimal);
		elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
	}
	

	/*
	AI_preloadImages()
	Loads images into the browser's cache for later use.
	
	Example: <body onLoad="AI_preloadImages('header.gif','nav1.gif','foo.gif','scoob.gif');">
	
	*/

	function AI_preloadImages() 
	{


		// Don't bother if there's no document.images
		if (document.images) 
		{
			if (typeof(document.AI) == 'undefined')
			{
				document.AI = new Object();
			}
			document.AI.loadedImages = new Array();
			// Loop through all the arguments.
			var argLength = AI_preloadImages.arguments.length;
			// alert("arg length is "+argLength);
			for(arg=0;arg<argLength;arg++) 
			{
				// For each arg, create a new image.
				document.AI.loadedImages[arg] = new Image();
				// Then set the source of that image to the current argument.
				document.AI.loadedImages[arg].src = AI_preloadImages.arguments[arg];
				//alert("loaded image at "+AI_preloadImages.arguments[arg]);
			}
		}
	}	
	
	
/* Expando Image Script ©2008 John Davenport Scheuer
   as first seen in http://www.dynamicdrive.com/forums/
   username: jscheuer1 - This Notice Must Remain for Legal Use
   */

if (document.images){
 (function(){
  var cos, a = /Apple/.test(navigator.vendor), times = a? 20 : 40, speed = a? 40 : 20;
  var expConIm = function(im){
   im = im || window.event;
   if (!expConIm.r.test (im.className))
    im = im.target || im.srcElement || null;
   if (!im || !expConIm.r.test (im.className))
    return;
   var e = expConIm,
   widthHeight = function(dim){
    return dim[0] * cos + dim[1] + 'px';
   },
   resize = function(){
    cos = (1 - Math.cos((e.ims[i].jump / times) * Math.PI)) / 2;
    im.style.width = widthHeight (e.ims[i].w);
    im.style.height = widthHeight (e.ims[i].h);
    if (e.ims[i].d && times > e.ims[i].jump){
     ++e.ims[i].jump;
     e.ims[i].timer = setTimeout(resize, speed);
    } else if (!e.ims[i].d && e.ims[i].jump > 0){
     --e.ims[i].jump;
     e.ims[i].timer = setTimeout(resize, speed);
    }
   }, d = document.images, i = d.length - 1;
   for (i; i > -1; --i)
    if(d[i] == im) break;
   i = i + im.src;
   if (!e.ims[i]){
    im.title = '';
    e.ims[i] = {im : new Image(), jump : 0};
    e.ims[i].im.onload = function(){
     e.ims[i].w = [e.ims[i].im.width - im.width, im.width];
     e.ims[i].h = [e.ims[i].im.height - im.height, im.height];
     e (im);
    };
    e.ims[i].im.src = im.src;
    return;
    }
   if (e.ims[i].timer) clearTimeout(e.ims[i].timer);
   e.ims[i].d = !e.ims[i].d;
   resize ();
  };

  expConIm.ims = {};

  expConIm.r = new RegExp('\\bexpando\\b');

  if (document.addEventListener){
   document.addEventListener('mouseover', expConIm, false);
   document.addEventListener('mouseout', expConIm, false);
  }
  else if (document.attachEvent){
   document.attachEvent('onmouseover', expConIm);
   document.attachEvent('onmouseout', expConIm);
  }
 })();
}


// Handles display/hide of text on page if more than one page exists

	// set to ensure page 1 is displayed at first call 	
	var currentpageno=100
	function SetTextOn(newpageno)
	{
			// skip all if page not changed
			if (newpageno!==currentpageno)
			{
				//window.alert("In SetTextOn and current page number is "+currentpageno);
				// switch off current page
				if (document.getElementById("textpage"+currentpageno))
				{
					var oldpage = document.getElementById("textpage"+currentpageno);
					oldpage.style.display = "none";
					oldpage.style.color="2f2f2f";
				}
				// switch on new page 
				currentpageno=newpageno;
				var newpage = document.getElementById("textpage"+newpageno);
				newpage.style.color="2f2f2f";
				newpage.style.display = "inline";
				fadepagetext(newpageno);
			}
	}

var hex=20
function fadepagetext(pageno)
{
		var newpage = document.getElementById("textpage"+pageno);
		// window.alert("hex is "+hex); 
		if(hex<200) 
		{ //Set to required final text color - must be shade of gray
			hex+=10; // increase color darkness in each call 
			newpage.style.color="rgb("+hex+","+hex+","+hex+")";
			setTimeout("fadepagetext("+pageno+")",50); 
		}
		else 
		{
		//window.alert("final hex was "+hex);
			hex=20;
		}
}
