	
/** Copyright (c) 2008 Zenex Solutions Ltd.
  *
  */
	
	
	/** 
	 * Arrays of image file names.
	 */
	var ims ;
		
	/**
	 * Arrays of the names of images that have been pre-loaded.
	 */
	var loaded_ims ;

	/** spin locks; initially all held
	  *
	  */
	var spinlock1 = false ;
	var spinlock2 = false ;
	var spinlock3 = false ;
		
	
	/** Sets the src of the img tag identified by its name attribute
	  *
	  * imageName - The value of the name attribute
	  */	
	function setRandomImage(imageName) {
	
		if(loaded_ims.length > 0) {
			var i = Math.round(Math.random() * 5000) ;
			
			i = i % loaded_ims.length ;
			
			var selectName = loaded_ims[i] ;
			
			//alert('Selected ' + selectName + '') ;
			
			var swapIn = $('#'+selectName).attr('src') ;
			
			document.images[imageName].src = swapIn ;
		}

	}

	/** 
	  *
	  */
	function rotateIms() {
	
		setRandomImage('firstimage') ;
	
		setRandomImage('secondimage') ;
	
		setTimeout("rotateIms()", 5000) ;
	}	
	
	/**
	  * Attempts to fade one image into another ; 
	  */
	function fadeImgOut() {
	
		// This is a value between 0 and 10.0 
		var value = 10.0 ;
		
		var imageName = 'firstimage' ;
		
		var testObj = document.images[imageName] ;
		
		//
		testObj.style.opacity = value / 10.0 ;
		testObj.style.filter = 'alpha(opacity=' + (value * 10.0) + ')';
		
	}
	
	/**
	  *	This function fades an image in and out
	  * flg = 1 to decrement by del, otherwise increment
	  */
	function fader(old_opA, flgA, old_opB, flgB, mx_op, del, tm) {
		
		// For image A
		if( old_opA >= 0 && old_opA <= mx_op ) {
			if ( flgA == 1 ) {
				new_opA = old_opA - del ;
			} else {
				new_opA = old_opA + del ;
			}
		}
		if( old_opA < 0 ) {
			new_opA = old_opA + del ;
			flgA = 0 ;
			
			//swap images (sneaky!!)
			setRandomImage('firstimage') ;
		}
		if( old_opA > mx_op ) {
			new_opA = old_opA - del ;
			flgA = 1 ;
		}
		
		// For image B
		if( old_opB >= 0 && old_opB <= mx_op ) {
			if ( flgB == 1 ) {
				new_opB = old_opB - del ;
			} else {
				new_opB = old_opB + del ;
			}
		}
		if( old_opB < 0 ) {
			new_opB = old_opB + del ;
			flgB = 0 ;
			
			//swap images (sneaky!!)
			setRandomImage('secondimage') ;
		}
		if( old_opB > mx_op ) {
			new_opB = old_opB - del ;
			flgB = 1 ;
		}


		//set new values
		document.images['firstimage'].style.opacity = new_opA / 10.0 ; 
		document.images['firstimage'].style.filter = 'alpha(opacity=' + (new_opA * 10.0) + ')' ; 
		document.images['secondimage'].style.opacity = new_opB / 10.0 ; 
		document.images['secondimage'].style.filter = 'alpha(opacity=' + (new_opB * 10.0) + ')' ; 

		// Iterate	
		setTimeout("fader(" + new_opA + "," + flgA + "," + new_opB + "," + flgB + "," + mx_op + "," 
									+ del + "," + tm + ")", tm) ;
	}
	
	/**
	  *	Return the maximum of two values
	  */
	function max(a,b) {
		return (a>b)?(a):(b) ;
	}

	/**
	  *	Return the minimum of two values
	  */
	function min(a,b) {
		return (a<b)?(a):(b) ;
	}

	/**
	  *	This function fades an image in and out on the splash screen
	  * flg = 1 to decrement by del, otherwise increment 
	  */
	function splash_fader(old_opA, flgA, old_opB, flgB, old_opC, flgC, mn_op, mn_cut_op, mx_cut_op, mx_op, del, tm) {
		
		spinlock3 = true ;
		
		if(!spinlock3) {
		
			setTimeout("splash_fader(" + old_opA + "," + flgA + "," 
									+ old_opB + "," + flgB + ","
									+ old_opC + "," + flgC + ","
									+ mn_op + ","
									+ mn_cut_op + ","
									+ mx_cut_op + ","
									+ mx_op + ","
									+ del + "," + tm + ")", 10.0) ;
			
		} else {

			// For image A
			if( old_opA >= mn_op && old_opA <= mx_op ) {
				if ( flgA == 1 ) {
					new_opA = old_opA - del ;
				} else {
					new_opA = old_opA + del ;
				}
			}
			if( old_opA < mn_op ) {
				new_opA = old_opA + del ;
				flgA = 0 ;
				
				//swap images (sneaky!!)
				setRandomImage('eleven_oclock') ;
			}
			if( old_opA > mx_op ) {
				new_opA = old_opA - del ;
				flgA = 1 ;
			}
			
			// For image B
			if( old_opB >= mn_op && old_opB <= mx_op ) {
				if ( flgB == 1 ) {
					new_opB = old_opB - del ;
				} else {
					new_opB = old_opB + del ;
				}
			}
			if( old_opB < mn_op ) {
				new_opB = old_opB + del ;
				flgB = 0 ;
				
				//swap images (sneaky!!)
				setRandomImage('nine_oclock') ;
			}
			if( old_opB > mx_op ) {
				new_opB = old_opB - del ;
				flgB = 1 ;
			}
	
			// For image C
			if( old_opC >= mn_op && old_opC <= mx_op ) {
				if ( flgC == 1 ) {
					new_opC = old_opC - del ;
				} else {
					new_opC = old_opC + del ;
				}
			}
			if( old_opC < mn_op ) {
				new_opC = old_opC + del ;
				flgC = 0 ;
				
				//swap images (sneaky!!)
				setRandomImage('eight_oclock') ;
			}
			if( old_opC > mx_op ) {
				new_opC = old_opC - del ;
				flgC = 1 ;
			}
			
	
			//set new values
			document.images['eleven_oclock'].style.opacity = min(mx_cut_op, max(new_opA, mn_cut_op)) / 10.0 ; 
			document.images['eleven_oclock'].style.filter = 'alpha(opacity=' + (new_opA * 10.0) + ')' ; 
			document.images['nine_oclock'].style.opacity = min(mx_cut_op, max(new_opB, mn_cut_op)) / 10.0 ; 
			document.images['nine_oclock'].style.filter = 'alpha(opacity=' + (new_opB * 10.0) + ')' ; 
			document.images['eight_oclock'].style.opacity = min(mx_cut_op, max(new_opC, mn_cut_op)) / 10.0 ; 
			document.images['eight_oclock'].style.filter = 'alpha(opacity=' + (new_opC * 10.0) + ')' ; 
	
			// Iterate	
			setTimeout("splash_fader(" + new_opA + "," + flgA + "," 
										+ new_opB + "," + flgB + ","
										+ new_opC + "," + flgC + ","
										+ mn_op + ","
										+ mn_cut_op + ","
										+ mx_cut_op + ","
										+ mx_op + ","
										+ del + "," + tm + ")", tm) ;
		}
	}
	  
	/**
	  *	This function resizes an image
	  */
	function resizer(old_wd, flg, mx_wd, mx_ht, del, tm) {
		
		if( old_wd >= 0 && old_wd <= mx_wd ) {
			if ( flg == 1 ) {
				new_wd = old_wd - del ;
			} else {
				new_wd = old_wd + del ;
			}
		}
		if( old_wd < 0 ) {
			new_wd = old_wd + del ;
			flg = 0 ;
		}
		if( old_wd > mx_wd ) {
			new_wd = old_wd - del ;
			flg = 1 ;
		}

		document.images['firstimage'].style.width = '' + new_wd + '%' ; 
		//document.images['firstimage'].style.left = '' + ((mx_wd - new_wd) / 2) + '%' ;
		document.images['firstimage'].style.top = '' + ((mx_ht * (mx_wd - new_wd)) /
															 (2 * mx_wd )) + '%' ;
	
		document.images['secondimage'].style.width = '' + new_wd + '%' ; 
		//document.images['secondimage'].style.left = '' + ((mx_wd - new_wd) / 2) + '%' ;
		document.images['secondimage'].style.top = '' + ((mx_ht * (mx_wd - new_wd)) /
															 (2 * mx_wd )) + '%' ;
															 
		setTimeout("resizer(" + new_wd + "," + flg + "," + mx_wd + "," + mx_ht + "," 
									+ del + "," + tm + ")", tm) ;
	}
	
	
	/** 
	  *
	  */
	function overimage(imgtosize) {
	
		document.images[imgtosize].style.width = '80%' ;
	}

	
	/** 
	  *
	  */
	function offimage(imgtosize) {
	
		document.images[imgtosize].style.width = '100%' ;		
	}
		
	
	/** Returns the computed pixel value
	  *	of 1em relative to the given element
	  */
	function getComputedEm(el){
	
		if (parseInt(navigator.appVersion)>3) {
			 if (navigator.appName=="Netscape") {
					var tdiv = document.createElement("div");
					tdiv.style.height = "1em";
					tdiv.style.position = "absolute";
					tdiv.style.backgroundColor = "#f00";
					el.appendChild(tdiv);
					var emValue = tdiv.offsetHeight;
					el.removeChild(tdiv);
			 }
			 if (navigator.appName.indexOf("Microsoft")!=-1) {
					var emValue = document.all['dummydiv'].offsetHeight ;
			 }
		}
	
		return emValue;
	}
	
	
	/** Takes an integer, and a javascript element. 
	  *
	  */
	function convertPixelsToEms(pixels, el){
		var emval = getComputedEm(el);
		var value = pixels/emval;
		return Math.round(value*100)/100;
	}
	
	/** Place a link around the circle at a specified angle, adjusting for 
	  * the number of lines of a specified hieght
	  */
	function place_link(link_prefix, span_prefix, link_id, line_no, emsize, pc_angle, inner_ring, link_text) {
	
		var link_name = link_prefix + link_id ;
		var span_name = span_prefix + link_id ;
		
		var winW = 630, winH = 460;

		if (parseInt(navigator.appVersion)>3) {
		 if (navigator.appName=="Netscape") {
		  winW = window.innerWidth;
		  winH = window.innerHeight;
		 }
		 if (navigator.appName.indexOf("Microsoft")!=-1) {
		  winW = document.body.offsetWidth;
		  winH = document.body.offsetHeight;
		 }
		}
		
		var ems_of_page = convertPixelsToEms( winW, document.anchors[link_name]) ;
		
		// FIXME: A slight hack 		
		var proportion_of_page = 0.5 ;
		
		var width_of_logo = (ems_of_page * proportion_of_page) ;

		document.images['nhc_logo'].style.width = '' + width_of_logo + 'em' ;
		
		var angle = (2.0 * Math.PI) * pc_angle ;
		var cos_theta = Math.cos( angle )  ;
		var sin_theta = Math.sin( angle )  ;
		
		var radius = (width_of_logo / 2) ;
		
		var move_up = emsize * line_no ;
		
		var lft = (-0.8 + radius + (cos_theta * (radius * inner_ring))) ;
		var tp = (0.05 - move_up - (radius + (sin_theta * (radius * inner_ring)))) ;
		
		// FIXME: A slight hack 
		if( pc_angle > 0.25 && pc_angle < 0.75) {
			var mv_txt = ((-link_text.length-2)/2) ;
		} else {
			var mv_txt = 0 ;
		}
		
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			document.all[link_id].style.left = '' + lft + 'em' ;
			document.all[link_id].style.top = '' + tp + 'em' ;
		} else {
			document.anchors[link_name].style.left = '' + lft + 'em' ;
			document.anchors[link_name].style.top = '' + tp + 'em' ;


			foo = document.getElementById(span_name) ;
			document.getElementById(span_name).style.left = '' + mv_txt + 'em' ;
			//document.getElementById(span_name).style.top = '' + 0 + 'em' ;
		}										
	}
	
	/** 
	  *
	  */
	function place_around_circle() {
	
		var start_angle = 0.07 ;
		var delta_ang = (1.0 - (2.0 * start_angle)) / 4.0 ;
		var inner_ring = 0.95 ;
		
		var link_prefix = 'first_' ;
		var span_prefix = 'sp_' ;
		
		// FIXME: should be able to 
		
		/*
		place_link(link_prefix, span_prefix, 'b', 1.0, 3.0, (start_angle + (delta_ang * 0.0)), inner_ring, 'About Us') ;
		place_link(link_prefix, span_prefix, 'c', 2.0, 3.0, (start_angle + (delta_ang * 1.0)), inner_ring, 'Events') ;
		place_link(link_prefix, span_prefix, 'd', 3.0, 3.0, (start_angle + (delta_ang * 2.0)), inner_ring, 'Sign Up') ;
		place_link(link_prefix, span_prefix, 'e', 4.0, 3.0, (start_angle + (delta_ang * 3.0)), inner_ring, 'Links') ;
		place_link(link_prefix, span_prefix, 'f', 5.0, 3.0, (start_angle + (delta_ang * 4.0)), inner_ring, 'Contact Us') ;
		*/

		//Placed by hand
		place_link(link_prefix, span_prefix, 'b', 1.0, 3.0, 0.125, inner_ring, 'About Us') ;
		place_link(link_prefix, span_prefix, 'c', 2.0, 3.0, 0.0625, inner_ring, 'Events') ;
		place_link(link_prefix, span_prefix, 'd', 3.0, 3.0, 0.0, inner_ring, 'Sign Up') ;
		place_link(link_prefix, span_prefix, 'e', 4.0, 3.0, 0.9375, inner_ring, 'Links') ;
		place_link(link_prefix, span_prefix, 'f', 5.0, 3.0, 0.875, inner_ring, 'Contact Us') ;
		
		spinlock1 = true ;
	}
	
	
	/** Fades in the NHC logo from transparent to opaque
	  *
	  */
	function fadeInNHCLogo(opacity) {

		if(!spinlock1) {
		
			setTimeout('fadeInNHCLogo('+opacity+')', 10.00) ;
			
		} else {


			if (opacity <= 100) {
				document.images['nhc_logo'].style.opacity = opacity / 10.0 ; 
				document.images['nhc_logo'].style.filter = 'alpha(opacity=' + (opacity * 10.0) + ')' ; 
				opacity += 0.5;
				window.setTimeout("fadeInNHCLogo("+opacity+")", 10.0);
			} else {
				//release spin lock
				spinlock2 = true ;
			}
		}
	}

	/** Collectively alter the style of a class
	  * http://twelvestone.com/forum_thread/view/31411
	  */
	function changeRule(theNumber,op) {
	
		var theRules = new Array();
	
		if (document.styleSheets[0].cssRules) {
			theRules = document.styleSheets[0].cssRules;
		} else if (document.styleSheets[0].rules) {
			theRules = document.styleSheets[0].rules;
		}
		
		var i;
		for(i = 0; i < theRules.length; i++) {
			if(theRules[i].selectorText == theNumber)
				break 
		}
		
		if(i != theRules.length) {
			theRules[i].style.opacity = op / 10.0 ; 
			theRules[i].style.filter = 'alpha(opacity=' + (op * 10.0) + ')' ; 
		}
	}

	/** Fades in the NHC link labels from transparent to opaque
	  *
	  */
	function fadeInLabels(opacity) {

		if(!spinlock2) {
		
			setTimeout('fadeInLabels(' +  opacity + ')', 10.0) ;
			
		} else {
	
			if (opacity <= 100) {
				changeRule('.linklabels',opacity) ;
				changeRule('.blt',opacity) ;
				opacity += 0.15;
				window.setTimeout("fadeInLabels("+opacity+")", 10.0);
			} else {
				//release spin lock
				spinlock3 = true ;
			}
		}	
	}
	
	
	/**
	 * Pre-load images.
	 */
	function populateImsArray(ims_select, set_select, load_array, name_prefix) {
		
		var kv_pairs ;
		if (set_select != null) {
			kv_pairs = {set : set_select} ;
		} else {
			kv_pairs = null ;
		}
		
		$.get("ajax_imgs.php", kv_pairs, function(xml) {
		
			//success				
			var aryLeng = ims_select.length ;

			//Results list
			$("image", $("images", xml)).each(function (i) {

				//Update list of images
				//alert($(this).text()) ; //for debug
				ims_select[aryLeng] = $(this).text() ;
				aryLeng += 1 ;
						
			});
			
			//pre-load images
			var counter = 0 ;
			
			$.each(ims_select, function(e) {
				jQuery("<img>").load(function() {
					
					ctr = load_array.length ;					
					
					//load_array[ctr] = name_prefix + ctr ;
					load_array[ctr] = ''+$(this).attr('id') ;

					//alert($(this).attr('id') + 'has loaded!'); //debug
					
				}).attr('src',this).attr('id', name_prefix+(counter++)).appendTo($("#imageshidden")) ;
			});				
			
			
		} );
	}
	
	/** AJAX - get the request object
	  *
	  */
	function getReqObj() {
	
		var result = null ;
		
		if( window.XMLHttpRequest ) {
			result = new XMLHttpRequest() ;
		} else {
			try{
				result = new ActiveXObject("Msxml2.XMLHTTP") ;				
			} catch(e) {
				result = null ;
			}
		}
		
		return result ;
	}

	/** AJAX - a global request object
	  *
	  */
	var req ;

	/** AJAX - setup to get the data from the server
	  *
	  */
	function getImagesFromServer() {
		
		req = getReqObj() ;
		
		if(req != null) {

			//endpoint = window.location.href ; // could be used to get absolute path
			endpoint = "ajax_imgs.php" ;
			
			req.open("GET", endpoint, true) ;
			req.onreadystatechange = ajxaProcessResponse ;
			req.send(null) ;
		
		}
		
	}
	
	/** AJAX - process response from server 
	  *
	  */
	function ajxaProcessResponse() {
		if(req.readyState == 4) {
			if(req.status == 200) {
				//success
				var data = req.responseXML ;
				var node = null ;
				
				if(data != null) {
					node = data.documentElement.firstChild ;
				}
			
				var result = new Array() ;
				var aryLeng = 0 ;
				
				//loop through all images 
				while(node != null) {
					var child = node.firstChild ;
					
					//Update list of images
					//alert( child.nodeValue ) ;	//for debug				
					result[aryLeng] = child.nodeValue ;
					
					aryLeng += 1 ;
					
					//Move on to next
					node = node.nextSibling ;
				}
				
				//Assign new array
				ims = result ;
				
			}
		}	
	}
	
	/**
	 * Could get rid of this ??
	 */
	/*function copyNode(fromNode, toNode) {
 		// Re-create the children, one at a time
 		for (var n=0; n<fromNode.childNodes.length; n++) {
   			var fromChild = fromNode.childNodes[n];
   			var toChild = toNode.appendChild(fromChild);
   			copyNode(toChild, fromChild);
 		}
	}*/
	
	
	/** Main entry point for the scripts.
	  *
	  */
	$(document).ready(function() {
	
		switch(page_type) {

			case 'm': 		
				
				//Do AJAX
				//pre-load image arrays				
				ims = new Array() ;
				loaded_ims = new Array() ;
				populateImsArray(ims, 			null, 		loaded_ims, 		'gen_im') ;
				//getImagesFromServer() ;  //Not now needed since we are using jQuery			

				// resizer(100, 1, 100, 100, 5, 1000);
				// rotateIms() ;
				// fadeImgOut() ;
				fader(10.0, 1, 0.0, 0, 10.0, 0.3, 150) ;
				
				break ;
			
			case 'lp':
			
				//Do AJAX
				//pre-load image arrays				
				ims = new Array() ;
				loaded_ims = new Array() ;
				populateImsArray(ims, 			null, 		loaded_ims, 		'gen_im') ;
				//getImagesFromServer() ;  //Not now needed since we are using jQuery			

				//	All four corners 'appear' together e.g. dissolve in, nothing else on page
				//Ensure photos are not visible at the start
				new_opA = 0.0 ;
				document.images['eleven_oclock'].style.opacity = new_opA / 10.0 ; 
				document.images['eleven_oclock'].style.filter = 'alpha(opacity=' + (new_opA * 10.0) + ')' ; 
				new_opB = 0.0 ;
				document.images['nine_oclock'].style.opacity = new_opB / 10.0 ; 
				document.images['nine_oclock'].style.filter = 'alpha(opacity=' + (new_opB * 10.0) + ')' ; 
				new_opC = 0.0 ;
				document.images['eight_oclock'].style.opacity = new_opC / 10.0 ; 
				document.images['eight_oclock'].style.filter = 'alpha(opacity=' + (new_opC * 10.0) + ')' ; 

				//	NHC logo zoom in from screen centre
				//	Labels and button appear in sequence - then at the same time
				//... all images dissolve in
				changeRule('.linklabels',0.0) ;
				changeRule('.blt',0.0) ;
				window.onresize = place_around_circle ;
				place_around_circle() ;
				fadeInNHCLogo(0.0) ;
				fadeInLabels(0.0) ;
				
				//	After 1 sec images fade in sequence as per other pages			
				new_opA = 20.0 ;
				document.images['eleven_oclock'].style.opacity = new_opA / 10.0 ; 
				document.images['eleven_oclock'].style.filter = 'alpha(opacity=' + (new_opA * 10.0) + ')' ; 
				new_opB = 6.66 ;
				document.images['nine_oclock'].style.opacity = new_opB / 10.0 ; 
				document.images['nine_oclock'].style.filter = 'alpha(opacity=' + (new_opB * 10.0) + ')' ; 
				new_opC = 6.66 ;
				document.images['eight_oclock'].style.opacity = new_opC / 10.0 ; 
				document.images['eight_oclock'].style.filter = 'alpha(opacity=' + (new_opC * 10.0) + ')' ; 
				
				splash_fader(new_opA, 1, new_opB, 1, new_opC, 0, 0.0, 0.0, 10.0, 20.0, 0.3, 125) ;
				
				break ;
								
			case 'd':
				
				alert('This is a preview screen only. Please use you browser back button to close this window to return to the editing screen. (Links on the menu at the top of the screen will not work.)') ;
			
				break ;
		
		}
	});	
	