var rotator1Current = 1;
var currentImageNumber = 0;
var nextImageNumber;
var previousImageNumber;
var currentImage;
var nextImage;
var previousImage;
var availableImages;
var photoSlider;



function scroll(direction) {
	var bodyPageCurrent = parseInt($('bodyPageCurrent').innerHTML);
	var bodyPageCount = parseInt($('bodyPageCount').innerHTML);
	
	Element.writeAttribute('box_scroller_up', 'onClick', '');
	Element.writeAttribute('box_scroller_down', 'onClick', '');
								
				if (direction == 1)
				{
				// Scrolling Up
		
					if (bodyPageCurrent >> 1)
					{
						new  Effect.Move ('box_content',{ x: 0, y: 390, mode: 'relative', duration: 0.8, fps:100, afterFinish: endEffectCallback});
						$('bodyPageCurrent').update(bodyPageCurrent - 1);
						
				
					}
				}
				
				if (direction == 2)
				{
				// Scrolling Down
					if (bodyPageCurrent < bodyPageCount)
					{
						new  Effect.Move ('box_content',{ x: 0, y: -390, mode: 'relative', duration: 0.8, fps:100, afterFinish: endEffectCallback});
						$('bodyPageCurrent').update(bodyPageCurrent + 1);
						
					}
					
				}
			
			bodyPageCurrent = parseInt($('bodyPageCurrent').innerHTML);
			
			if (bodyPageCurrent == 1) {
			$('box_scroller_up').hide();
			$('box_scroller_up_off').show();
			}
			else {
			$('box_scroller_up').show();
			$('box_scroller_up_off').hide();
			}
			
			if (bodyPageCurrent == bodyPageCount) {
			$('box_scroller_down').hide();
			$('box_scroller_down_off').show();
			}
			else {
			$('box_scroller_down').show();
			$('box_scroller_down_off').hide();
			}
			
				
	
				
			}
			
			function endEffectCallback(obj)
			{
				Element.writeAttribute('box_scroller_up', 'onClick', 'scroll(1)');
				Element.writeAttribute('box_scroller_down', 'onClick', 'scroll(2)');
			}
			
			
			function openMenu(pageId, menuId, aux)
			{
				var pageOpen = parseInt($('page_open').innerHTML);
		
				//$('box_navigation').hide();
				$('box_navigation').setStyle({left: '430px'});
				// Potentially cancel the queue here to stop double click opening
				
				if (Element.visible('box_content') == 1) { 
					
					$('box_content').hide();
					
					
					if (pageOpen != 0) {
						var bodyPageCount = parseInt($('bodyPageCount').innerHTML);
		//alert('test');
						
							
								//perform reset here.
								$('box_navigation').show();
								$('box_content').setStyle('top: 0px');
								$('box_scroller_up').hide();
								$('box_scroller_up_off').show();
								$('box_scroller_down').show();
								$('box_scroller_down_off').hide();	
							
						

							//alert($('box_navigation').getStyle('left'));
							//if ($('box_navigation').getStyle('left') <= '380px') new Effect.Move('box_navigation', {x: 50, duration: 0.5, mode:'relative', queue: {position:'end', scope: 'scrollerscope'}});
						
					}
					
					// Move the box back up if its been scrolled
					//
				}
				new Effect.Appear('box_loading', {duration: 0.3});
				
				if (pageOpen == 0)
				{
					new Effect.Parallel([new Effect.Move ('model',{sync: true, x: 320, y: 182, mode: 'relative', fps:80}), new Effect.Scale('model', 60, {sync: true, direction: 'top-right', fps: 80})], { duration: 1.0, fps: 80, queue: {position:'end', scope: 'modelmove'} });
					$('page_open').update(pageId);	
					new Effect.Fade('welcomeText2', {duration: 0.2, fps: 80});
					new Effect.Fade('frontSponsors', {duration: 0.2, fps: 80});
					new Effect.Appear('model_resize', {duration:0.1, queue: {position:'end', scope: 'modelmove'}});
					new Effect.Appear('box', {delay:0.3});
					
				}
	
				var theDate = new Date();
				//alert();
	
				var url = 'getcontent.php?postid=' + pageId + '&time=' + theDate.getTime() + '&aux=' + aux;
				//new Ajax.Request(url, {method: 'get', onSuccess: showBox(transport)});
				new Ajax.Request(url, {method: 'get', onSuccess: function(transport) { 
					
					$('box').setStyle({backgroundImage: 'url(\'images/' + menuId + '_bg.jpg\')'});
					$('box_content').update(transport.responseText); 
					
					var bodyPageCount = parseInt($('bodyPageCount').innerHTML);
					if (bodyPageCount > 1) $('box_navigation').show();
					
					new Effect.Fade('box_loading', {duration: 0.2, fps: 80});
					new Effect.Appear('box_content', {duration: 1, queue: {position:'end', scope: 'scrollerscope'}});
				
							
							if (bodyPageCount > 1) new Effect.Move('box_navigation', {x: -50, duration: 0.3, mode: 'relative', queue: {position:'end', scope: 'scrollerscope'}});
						
			
					// Note update of the page has to occur after the above page open check or navigation bar may move in too far
					
				} });	
			}
			
		
			
			function menuMouseOver(id)
			{
				
				// Check to see if another menu item is hovered before opening this one
				if (id != 'nav1_over') { if (Element.visible('nav1_over') == 1) { new Effect.Fade('nav1_over', {duration: 0.2, fps: 80}); } }
				if (id != 'nav2_over') { if (Element.visible('nav2_over') == 1) { new Effect.Fade('nav2_over', {duration: 0.2, fps: 80}); } }
				if (id != 'nav3_over') { if (Element.visible('nav3_over') == 1) { new Effect.Fade('nav3_over', {duration: 0.2, fps: 80}); } }
				if (id != 'nav4_over') { if (Element.visible('nav4_over') == 1) { new Effect.Fade('nav4_over', {duration: 0.2, fps: 80}); } }
				if (id != 'nav5_over') { if (Element.visible('nav5_over') == 1) { new Effect.Fade('nav5_over', {duration: 0.2, fps: 80}); } }
				if (id != 'nav6_over') { if (Element.visible('nav6_over') == 1) { new Effect.Fade('nav6_over', {duration: 0.2, fps: 80}); } }
				
				Effect.Appear(id, {duration: 0.3});
				
				var isIE = document.all?true:false;
//if (!isIE) document.captureEvents(Event.MOUSEMOVE);
				//alert(Event.pointerX(Event.MOUSEMOVE));
				
				//window.setTimeout("checkOver('" + id + "')", 10000);
			}
			
			function checkOver(id)
			{
				if (Element.visible(id) == 1) { new Effect.Fade(id, {duration: 0.2, fps: 80}); }
	
			}
			
			function menuMouseOut(id)
			{
				//id.hide();
				Effect.Fade(id, {duration: 0.2, fps: 80});
			}
			
			function menuCleanup()
			{
				//if (Element.visible('nav1_over') == 1) { new Effect.Fade('nav1_over', {duration: 0.2, fps: 80}); }
				//if (Element.visible('nav2_over') == 1) { new Effect.Fade('nav2_over', {duration: 0.2, fps: 80}); }
				//if (Element.visible('nav3_over') == 1) { new Effect.Fade('nav3_over', {duration: 0.2, fps: 80}); }
				//if (Element.visible('nav4_over') == 1) { new Effect.Fade('nav4_over', {duration: 0.2, fps: 80}); }
				//if (Element.visible('nav5_over') == 1) { new Effect.Fade('nav5_over', {duration: 0.2, fps: 80}); }
				//if (Element.visible('nav6_over') == 1) { new Effect.Fade('nav6_over', {duration: 0.2, fps: 80}); }
			}
			
			
			function doRotator()
	{
		
		var rotator = $('rotatorOne');
		var count = rotator.readAttribute('title');
		//var current = parseInt($('currentCount').innerHTML);
		var current = rotator1Current;
	
		if (current == count) var next = 1;
		else var next = current + 1;
		
		var imgName = "rotimg" + current;
		var newImgName = "rotimg" + next;
		
		Effect.Fade(imgName);
		Effect.Appear(newImgName);
	
		
		//$('currentCount').update(next);
		rotator1Current = next;
		
		setTimeout('doRotator()',7000)
		
		
	}
	
	
		function openVideo()
		{
		$('newVideoContainer').show();
		//new Effect.Appear('videoContainer', {delay:0.3, to:0.8, queue: {position:'end', scope: 'vidscope'}});
	
		  
   
   
   
		
		}
		
		function hideVideo()
		{
			new Effect.Fade('newVideoContainer', {delay:0.3, queue: {position:'end', scope: 'vidscope'}});
		}
		
		function showContactBox()
		{
			new Effect.Appear('contactus', {duration:1.0, to:0.8});
		}
		
		
		function hideContactBox()
		{
			new Effect.Fade('contactus', {duration:1.0});
		}
		
		function showPhotoBox()
		{
			new Effect.Parallel([new Effect.Appear('overlayPhoto', {fps:80, sync: true, to:0.7})  ], { duration: 1.0, fps: 80, queue: {position:'end', scope: 'photoopen'} });
		
			if(!Object.isArray(availableImages)) {
				
				var theDate = new Date();
				var url = 'getPhotoBox.php?time=' + theDate.getTime();

				new Ajax.Request(url, {method: 'get', onSuccess: function(transport) { 
					//test2 = transport.responseText.evalScripts();
					availableImages = transport.responseText.split(",");
					imageCount = (availableImages.length - 2);
					currentImage = new Image();
					nextImage = new Image();
					previousImage = new Image();
					currentImage.onload = function(){showImage();};
					currentImageNumber = 0;
					new Effect.Opacity('photoControlBG', { from: 1.0, to: 0.5, duration: 0.1 });
					new Effect.Opacity('photoForwardButton', { from: 1.0, to: 0.5, duration: 0.1 });			
					new Effect.Opacity('photoBackButton', { from: 1.0, to: 0.5, duration: 0.1 });			
					new Effect.Appear('photobox', {duration: 0.5,queue:'end',afterFinish: function(){ initSlider(); goToImage(1);}});
						//new Effect.Fade('box_loading', {duration: 0.2, fps: 80});

						//	$('galleryPositionCurent').update(currentPhoto + 1);
						//	$('galleryPositionTotal').update(photoCount);			
					} });
					
					
					
					
				
			} else {
				// Gallery has already been loaded once, just return to the previous position!
				new Effect.Appear('photobox', {duration: 0.5,queue:'end',afterFinish:function(){enablePhotoKeyboard();}});
				
			}
		
			
		}
		
		function initSlider()
		{
				imageCount = (availableImages.length - 2);

				
			photoSlider = new Control.Slider('photoSliderHandle', 'photoSliderRunner', {range: $R(1,imageCount), alignX: -5, alignY: 0});

					// Setting the callbacks later on
				photoSlider.options.onSlide = function(value) {
					$('galleryPositionCurent').update(Math.round(value));
				};

				photoSlider.options.onChange = function(value) {

					  goToImage(Math.round(value));
				};
					
			
		
		}
		
		function showImage()
		{
			//alert('running');
			$('viewerPhoto').update(currentImage);
			currentWidth = $('photobox').getWidth();
			currentHeight = $('photobox').getHeight();

			newWidth = ((currentImage.width + 2) / currentWidth) * 100;
			newHeight = ((currentImage.height + 2) / currentHeight) * 100;
			//newWidth = Math.floor(newWidth);
			//newHeight = Math.floor(newHeight);
			//alert(newHeight);

			
			if (newWidth < 97 || newWidth > 105 || newHeight < 97 || newHeight > 105) {
				new Effect.Parallel([
				  	new Effect.Scale('photobox', newWidth, {scaleY:false, scaleContent:false, sync: true}),
					new Effect.Scale('photobox', newHeight, {scaleX:false, scaleContent:false, sync: true})
				], { 
				  duration: 0.5,
				queue:'end'
				});
			
			
			}
				
			
			new Effect.Appear('viewerPhoto', {duration:0.3, queue:'end', beforeStart: function(){$('photoLoading').hide();}, afterFinish:function(){enablePhotoKeyboard();}});
			updatePhotoNavDetails();
			
			

			



		}

		function navigationWrapper(picID){	
			photoSlider.setValue(picID);
		}
		
		
		
		
		function goToImage(picId)
		{
			disablePhotoKeyboard();
			
			//alert(availableImages[picId]);
			if($('viewerPhoto').visible()) {
						//fadeit = Effect.Fade('viewerPhoto', {afterFinish:internalChangeImage(picId) });
						$('viewerPhoto').hide();
						$('photoLoading').show();
				//fadeit.afterFinish = ();
				}
			else {
				//alert('running');
				
			}
			
			currentImage.onload = function(){showImage();};
			currentImage.src = availableImages[picId];

			if (picId == (availableImages.length - 2)) nextImageNumber = 1;
			else nextImageNumber = picId + 1;
			
			if (picId == 1) previousImageNumber = (availableImages.length - 2) // Note subtracting 2 instead of 1 because of trailing ',' on php genned array
			else previousImageNumber = picId - 1;
			
			if (picId != availableImages.length - 1) nextImage.src = availableImages[nextImageNumber];
			if (picId != 0) previousImage.src = availableImages[previousImageNumber];
			currentImageNumber = picId;



		}
		
		function photoKeyboardEvent(event) {
			var keyPressed = event.keyCode;
			var key = String.fromCharCode(keyPressed).toLowerCase();
			//alert(keyPressed);
			if (keyPressed == 37) {
				
				//alert('test');
				navigationWrapper(previousImageNumber);
				
			}
			if (keyPressed == 39) {
				//disablePhotoKeyboard(); // Moved to goToImage
				//alert('test');
				navigationWrapper(nextImageNumber);
				
			}
			
			if(keyPressed == Event.KEY_ESC) {
				hidePhotoBox();	
			
			}
			

			
		}
		
		function enablePhotoKeyboard() {
			
			$('photoForwardButton').show();
			$('photoBackButton').show();
			Event.observe(document, 'keypress', photoKeyboardEvent);
		}
		
		function disablePhotoKeyboard() {
			
				$('photoForwardButton').hide();
				$('photoBackButton').hide();
			Event.stopObserving(document, 'keypress', photoKeyboardEvent);
		}
		
		function hidePhotoBox()
		{
			new Effect.Fade('photobox', {duration:1.0});
			new Effect.Fade('overlayPhoto', {duration:0.5});
			disablePhotoKeyboard();
		}
		
		function updatePhotoNavDetails() {
			if (!$('galleryPosition').visible()) $('galleryPosition').show();
			$('galleryPositionCurent').update(currentImageNumber);
			$('galleryPositionTotal').update(availableImages.length - 2);
			//photoSlider.setValue(currentImageNumber);
			
		}
		
		
		function showSurvey()
		{
			new Effect.Parallel([new Effect.Appear('overlay', {fps:80, sync: true, to:0.7})  ], { duration: 1.0, fps: 80, queue: {position:'end', scope: 'surveyopen'} });
			var theDate = new Date();
			var url = 'form.html?time=' + theDate.getTime();
			
			new Ajax.Request(url, {method: 'get', onSuccess: function(transport) { 
			$('surveycontent').update(transport.responseText); 
					//new Effect.Fade('box_loading', {duration: 0.2, fps: 80});
					new Effect.Appear('surveybox', {duration: 1, queue: {position:'end', scope: 'surveyopen'}});
		
			
					// Note update of the page has to occur after the above page open check or navigation bar may move in too far
					
				} });	
		}
		
		
		function hideSurvey()
		{
			new Effect.Fade('surveybox', {duration:1.0});
			new Effect.Fade('overlay', {duration:0.5});
		}
		
		function submitSurvey()
		{
			$('surveybutton').update('Please Wait...');
			$('atsesurvey').request({
 						 method: 'get',
 						
 						 onComplete: function(transport){ 
						 //alert(transport.responseText);
						 //$('box_content').update("<h1>Event</H1><H2>Registration</H2><p id='afterTitle'></p><br>Thank you! Your registration of interest in the upcoming Extreme Science Experience has been successfully registered.<div id='bodyPageCount' style='display:none;'>1</div><div id='bodyPageCurrent' style='display:none;'>1</div>");
						
						 hideSurvey();
						 if (transport.responseText != 0) {
							$('box_content').update(transport.responseText);
						}
						else {
							$('box_content').update("<h1>Event</H1><H2>Registration</H2><p id='afterTitle'></p><br>An error has occured.<div id='bodyPageCount' style='display:none;'>1</div><div id='bodyPageCurrent' style='display:none;'>1</div>");
						
						}
						// setTimeout('openMenu(4,2)',5000);
						 }
					})
		}
		
		function doform2()
		{
			var formfields = Form.getElements('atseform2');
			var error = 0;
			
				if ($('student1').getValue() != "" && $('workshop1').getValue() == "Select Workshop") {
					error = 1;
					$('errorbox').update("<b>Error:</b> You must select a workshop for student 1");
					
				}
				
				if ($('student2').getValue() != "" && $('workshop2').getValue() == "Select Workshop") {
					error = 1;
					$('errorbox').update("<b>Error:</b> You must select a workshop for student 2");
					
				}
				if ($('student3').getValue() != "" && $('workshop3').getValue() == "Select Workshop") {
						error = 1;
						$('errorbox').update("<b>Error:</b> You must select a workshop for student 3");

				}
				if ($('student4').getValue() != "" && $('workshop4').getValue() == "Select Workshop") {
						error = 1;
						$('errorbox').update("<b>Error:</b> You must select a workshop for student 4");

				}
				if ($('student5').getValue() != "" && $('workshop5').getValue() == "Select Workshop") {
						error = 1;
						$('errorbox').update("<b>Error:</b> You must select a workshop for student 5");

				}
				if ($('student6').getValue() != "" && $('workshop6').getValue() == "Select Workshop") {
						error = 1;
						$('errorbox').update("<b>Error:</b> You must select a workshop for student 6");

				}
		
		
				if (error != 1)
				{
					$('buttonrow').update("Please wait...");
					$('errorbox').update(" ");
					//do ajax
					
					$('atseform2').request({
 						 method: 'get',
 						
 						 onComplete: function(transport){ 
						 //alert(transport.responseText);
						 $('box_content').update("<h1>Event</H1><H2>Registration</H2><p id='afterTitle'></p><br>Thank you! Your registration for upcoming Extreme Science Experience has been successful.  Confirmation details will be emailed to the specified address shortly.<div id='bodyPageCount' style='display:none;'>1</div><div id='bodyPageCurrent' style='display:none;'>1</div>");
						
						 //setTimeout('openMenu(4,2)',5000);
						 }
					})
				
				}
			
		}
		
		function doform()
		{
			var formfields = Form.getElements('atseform');
			var error = 0;
			
			formfields.each(function(s) {
				if (!s.getValue()) {
						$('errorbox').update("<b>Error:</b> You must complete all above fields.");
						error = 1;
						
				}
			});
		
		
				if (error != 1)
				{
					$('buttonrow').update("Please wait...");
					$('errorbox').update(" ");
					//do ajax
					
					$('atseform').request({
 						 method: 'get',
 						
 						 onComplete: function(transport){ 
						// alert(transport.responseText);
						 //$('box_content').update("<h1>Event</H1><H2>Registration</H2><p id='afterTitle'></p><br>Thank you! Your registration of interest in the upcoming Extreme Science Experience has been successfully registered.<div id='bodyPageCount' style='display:none;'>1</div><div id='bodyPageCurrent' style='display:none;'>1</div>");
						if (transport.responseText != 0) {
							openMenu(-6,2,transport.responseText);
						}
						else {
							$('box_content').update("<h1>Event</H1><H2>Registration</H2><p id='afterTitle'></p><br>An error has occured.<div id='bodyPageCount' style='display:none;'>1</div><div id='bodyPageCurrent' style='display:none;'>1</div>");
						
						}
						// setTimeout('openMenu(4,2)',5000);
						 }
					})
				
				}
		}
		
			function doRegistrationForm()
			{
				var formfields = Form.getElements('atseform');
				var error = 0;

				formfields.each(function(s) {
					if (!s.getValue()) {
							$('errorbox').update("<b>Error:</b> You must complete all above fields.");
							error = 1;

					}
				});


					if (error != 1)
					{
						$('buttonrow').update("Please wait...");
						$('errorbox').update(" ");
						//do ajax

						$('atseform').request({
	 						 method: 'get',

	 						 onComplete: function(transport){ 
							// alert(transport.responseText);
							 //$('box_content').update("<h1>Event</H1><H2>Registration</H2><p id='afterTitle'></p><br>Thank you! Your registration of interest in the upcoming Extreme Science Experience has been successfully registered.<div id='bodyPageCount' style='display:none;'>1</div><div id='bodyPageCurrent' style='display:none;'>1</div>");
							if (transport.responseText != 0) {
								openMenu(-5,2,transport.responseText);
							}
							else {
								$('box_content').update("<h1>Event</H1><H2>Registration</H2><p id='afterTitle'></p><br>An error has occured.<div id='bodyPageCount' style='display:none;'>1</div><div id='bodyPageCurrent' style='display:none;'>1</div>");

							}
							// setTimeout('openMenu(4,2)',5000);
							 }
						})

					}
			}
			
			function doRegistrationForm2()
			{
				var formfields = Form.getElements('atseform2');
				var error = 0;

					if ($('student1').getValue() != "" && $('workshop1').getValue() == "Select Workshop") {
						error = 1;
						$('errorbox').update("<b>Error:</b> You must select a workshop for student 1");

					}

					if ($('student2').getValue() != "" && $('workshop2').getValue() == "Select Workshop") {
						error = 1;
						$('errorbox').update("<b>Error:</b> You must select a workshop for student 2");

					}
					if ($('student3').getValue() != "" && $('workshop3').getValue() == "Select Workshop") {
							error = 1;
							$('errorbox').update("<b>Error:</b> You must select a workshop for student 3");

					}
					if ($('student4').getValue() != "" && $('workshop4').getValue() == "Select Workshop") {
							error = 1;
							$('errorbox').update("<b>Error:</b> You must select a workshop for student 4");

					}
					if ($('student5').getValue() != "" && $('workshop5').getValue() == "Select Workshop") {
							error = 1;
							$('errorbox').update("<b>Error:</b> You must select a workshop for student 5");

					}
					if ($('student6').getValue() != "" && $('workshop6').getValue() == "Select Workshop") {
							error = 1;
							$('errorbox').update("<b>Error:</b> You must select a workshop for student 6");

					}


					if (error != 1)
					{
						$('buttonrow').update("Please wait...");
						$('errorbox').update(" ");
						//do ajax

						$('atseform2').request({
	 						 method: 'get',

	 						 onComplete: function(transport){ 
							 //alert(transport.responseText);
							 //$('box_content').update("<h1>Event</H1><H2>Registration</H2><p id='afterTitle'></p><br>Thank you! Your registration for upcoming Extreme Science Experience has been successful.  Confirmation details will be emailed to the specified address shortly.<div id='bodyPageCount' style='display:none;'>1</div><div id='bodyPageCurrent' style='display:none;'>1</div>");
							openMenu(123,2);
							 //setTimeout('openMenu(4,2)',5000);
							 }
						})

					}

			}
			
		
		
		
		function testForEnter() 
			{    
				if (event.keyCode == 13) 
				{        
					event.cancelBubble = true;
					event.returnValue = false;
					doform();
     			}
			} 
			
		function checkCommentsBox() {
			//alert($F('comments'));
			if ($F('comments') == "") $('comments').replace('<textarea name="comments" id="comments" cols="5" rows="5" onkeyDown="testForEnter();" onblur="checkCommentsBox();">Any additional comments? (Optional)</textarea>');
		
		}
		
		function doClear(id) {
			
			$(id).setValue("");
			
		}
		
		function setField(id, newtext) {
			
			if ($(id).getValue() == "") {
			$(id).setValue(newtext);
		}
			
		}
		
		function loadVideo() {
			var so = new SWFObject("video/flvplayer.swf", "mymovie", "308", "200", "8", "#336699");
			so.addParam("allowfullscreen","false");
			so.addParam("shownavigation","false");
			so.addVariable("file","http://www.extremescience.com.au/video/ExtremeScience.flv");
			so.addVariable("image","");
			so.write("videoBox");
			
		}
		
		Event.observe(window, 'load', function() {
		  setTimeout('doRotator()',5000);
			//loadVideo();
		});
		
