/* ******************************************************************************
  JavaScript Gallery Transition controller
	
 	Copyright (c) 2008 David Turner (davidturner.info)
 	Date: 30/07/2008	 
		 
****************************************************************************** */

document.observe('dom:loaded', function()
{	
	var TimeDelay = 7;
	var PeriodUpdate = new PeriodicalExecuter(fnNextImage, TimeDelay);
	// find all link anchors withing galery Nav
		$$('div#GalleryPagination a.Image').each(function(element){
			// if image link is clicked
			
			Event.observe(element, 'click', function(event){	
				PeriodUpdate.stop();
				fnLoadImage(element.readAttribute('title'))
				
				// remove the dotted line from the link
				element.blur();

				PeriodUpdate = new PeriodicalExecuter(fnNextImage, TimeDelay);
				Event.stop(event); 
			}); 

		
	$('image').title = "Click to view next image";
	$('image').addClassName("next-link");
	
	PlayPause = new Element('a',{	'class':'Pause',
																'title':'Click to pause slideshow'
																}).update('Pause');
	});
	$('GalleryHome').insert({'after':PlayPause});
		
	Event.observe(PlayPause, 'click', function(event){	
		if(PlayPause.hasClassName('Pause')){
			PlayPause.update('Play');
			PlayPause.removeClassName('Pause');
			PlayPause.addClassName('Play');
			PeriodUpdate.stop();
			
		}else{
			PlayPause.update('Pause');
			PlayPause.removeClassName('Play');
			PlayPause.addClassName('Pause');
			fnNextImage();
			PeriodUpdate = new PeriodicalExecuter(fnNextImage, TimeDelay);
		}
	});
	
	Event.observe($('image'), 'click', function(event){	
		PeriodUpdate.stop();
		fnNextImage();	
		PeriodUpdate = new PeriodicalExecuter(fnNextImage, TimeDelay);
		Event.stop(event);
	}); 
	
	
})

function fnLoadImage(imageLoadSrc){
					

			aryImages = $('image_container').descendants();
			obImage = aryImages[0];
			obImageId = obImage.identify();
			

			// get image id from url
			var currSrc = $(obImageId).readAttribute('src');			
			
			// regular expression matches for image IDs
			re_GetImageId = "[a-zA-Z0-9-_]+$";
			re_CurImageId = "([a-zA-Z0-9-_]+).jpg$";

			var currImage = currSrc.match(re_CurImageId);
			//var newImage= element.href.match(re_GetImageId);
			currImage = currImage[1];

			
			var newImageOb = new Element('img',{src:imageLoadSrc,'title':"Click to view next image"});
	
				
			
			//Event.observe(newImageOb,'load',function(){
																					 
				obImage.fade({ 	duration: 0.6,
												afterFinish: function(){
													
													$$('#image_container img').each(function(element){
														element.remove();
													});
													
													newImageOb.setOpacity(0);
													$('image_container').insert(newImageOb);
													
													// remove selected class from all elements
													$$('div#GalleryPagination a.Image').each(function(element){
														element.removeClassName('selected');
													});
													
													
													$$('div#GalleryPagination a.Image').each(function(element){
														if (element.readAttribute('title') == imageLoadSrc){
															element.addClassName('selected');
														}
													});
													
													// add selected class to current linked
													//$('img_'+newImage).addClassName('selected');
													
													
													newImageOb.appear({duration: 0.6,
													afterFinish: function(){
															newImageOb.addClassName('next-link');
															Event.observe(newImageOb.identify(), 'click', function(event){	
																fnNextImage();	
																Event.stop(event);
															});
														}
													});
												}
				});
				
			//});
}

function fnNextImage (){
	$$('div#GalleryPagination a.selected').each(function(current){
			
			if (current.next('div#GalleryPagination a.Image') != undefined){
				nextLink = current.next('div#GalleryPagination a.Image');
			} else {
				count=0;
				$$('div#GalleryPagination a.Image').each(function(findFirst){
					if(count < 1){
						nextLink = findFirst;
					}
					count++;
				});
			}
		});
	fnLoadImage(nextLink.readAttribute('title'));
}

function fnPreviousImage(){
	$$('a.image_link.selected').each(function(current){
		
		if (current.previous('a.image_link') != undefined){
			prevLink = current.previous('a.image_link');
		} else {
			$$('div.gallery_nav a.image_link').each(function(findFirst){
				prevLink = findFirst;
			});
		}
		fnLoadImage(prevLink.identify());
	});
}