
	var currentPhoto, intOriginalLeft, intOriginalTop;
	var booShowing = false;
	
	window.addEvent('domready',galleryInitialise);
	
/*************************************************************************************************************************/

	function galleryInitialise()
	{
		var objBody = $E('body');

		var divGalleryShade = new Element('div');
		divGalleryShade.id = 'galleryShade';
		
		var divLightbox = new Element('div');
		divLightbox.id = 'galleryLightbox';
		//var divLightboxPhoto = new Element('div');
		//divLightboxPhoto.id = 'galleryLightboxPhoto';
		//divLightboxPhoto.injectInside(divLightbox);
		var divLightboxPanel = new Element('div');
		divLightboxPanel.id = 'galleryLightboxPanel';
		var lnkLightboxClose = new Element('a');
		lnkLightboxClose.id = 'galleryLightboxCloseLink';
		lnkLightboxClose.setStyle('padding-right',10);
		//lnkLightboxClose.innerHTML = 'close X';
		lnkLightboxClose.href = 'javascript:galleryHideLightbox();';
		lnkLightboxClose.injectInside(divLightbox);
		var divLightboxTitle = new Element('div');
		divLightboxTitle.id = 'galleryLightboxTitle';
		divLightboxTitle.injectInside(divLightbox);
		var divLightboxDescription = new Element('div');
		divLightboxDescription.id = 'galleryLightboxDescription';
		divLightboxDescription.injectInside(divLightbox);
		
		divLightboxPanel.injectTop(objBody);
		divLightbox.injectTop(objBody);
		divGalleryShade.injectTop(objBody);
		
		$('galleryEditor').injectInside(objBody);
		galleryLoadPhotos();
	}
		
/*************************************************************************************************************************/

	function galleryLoadPhoto(filename, target, title, title2, description, cvlink)
	{
		var intWidth = $(target).getSize().size.x;
		var intHeight = $(target).getSize().size.y;

		if(filename=='gallery/photos/')
		{
			new Asset.image('images/galleryPhoto.jpg', {onload:function(){
				this.setStyle('opacity',0);
				this.injectInside($(target));
				this.addEvent('click',function(){galleryShowLightbox(filename, title, title2, description, cvlink);});
				new Fx.Styles(this, {duration:250, transition:Fx.Transitions.Sine.easeOut, onComplete:function(){
					if(booLoggedIn) 
					{
						$(target).getParent().addEvent('mouseenter',function(){galleryShowTools($(target).getParent())}); 
						$(target).getParent().addEvent('mouseleave',function(){galleryHideTools()});
					}
				}}).start({'opacity':1});
			}});
			
		}
		else
		{
			new Asset.image('gallery/galleryimage.ashx?name=../'+filename+'&pad=true&back=ffffff&width=' + intWidth + '&height=' + intHeight, {onload:function(){
				this.setStyle('opacity',0);
				this.injectInside($(target));
				this.addEvent('click',function(){galleryShowLightbox(filename, title, title2, description, cvlink);});
				new Fx.Styles(this, {duration:250, transition:Fx.Transitions.Sine.easeOut, onComplete:function(){
					if(booLoggedIn) 
					{
						$(target).getParent().addEvent('mouseenter',function(){galleryShowTools($(target).getParent())}); 
						$(target).getParent().addEvent('mouseleave',function(){galleryHideTools()});
					}
				}}).start({'opacity':1});
			}});
			
		}
	}

/*************************************************************************************************************************/

	function galleryShowLightbox(filename, title, title2, description, cvlink)
	{
		if(!booShowing)
		{
			var intTop = $$('.centre')[0].getTop()+30;
			var intLeft = $$('.centre')[0].getLeft()+250;
			var intPad = 20;
			var intWidth = 200;
			var intHeight = 400;
			booShowing = true;
			$('galleryShade').setOpacity(0);
			$('galleryShade').setStyles({'width':'100%', 'top': window.getScrollTop(), 'height': window.getHeight()});
			new Fx.Styles($('galleryShade'),{duration:250, onComplete:function(){
				$('galleryLightbox').setOpacity(0);
				$('galleryLightbox').setStyles({left:intLeft, top:intTop, width:50, height:50});
				new Fx.Styles($('galleryLightbox'),{duration:250, onComplete:function(){
					new Fx.Styles($('galleryLightbox'),{duration:500, onComplete:function(){
						/*
						new Asset.image('gallery/galleryimage.ashx?name=../'+filename+'&pad=true&back=ffffff&width=' + intWidth + '&height='+intHeight, {onload:function(){
							this.setOpacity(0);
							this.injectInside($('galleryLightboxPhoto'));
							new Fx.Styles(this, {duration:250, transition:Fx.Transitions.Sine.easeOut, onComplete:function(){
								$('galleryLightboxPanel').setStyles({left:$('galleryLightbox').getLeft(), width:intWidth, top:intTop});
								$('galleryLightboxPanel').setOpacity(1);
								*/
								$('galleryLightboxTitle').innerHTML = title + ' ' + title2;
								$('galleryLightboxDescription').innerHTML = description.replace(/~/g,'<br>');
								$('galleryLightboxDescription').innerHTML+='<br>'+cvlink;
								/*
								$('galleryLightboxPanel').setStyles({top:intTop, height:intHeight});
								new Fx.Styles($('galleryLightboxPanel'), {duration:500, transition:Fx.Transitions.Sine.easeOut}).start({left:$('galleryLightboxPanel').getLeft()+$('galleryLightboxPanel').getSize().size.x+intPad});
						/*
							}}).start({'opacity':1});
						}});
						*/
					}}).start({width:(intWidth*2+intPad), height:(intHeight+intPad)});
				}}).start({opacity:1});
			}}).start({opacity:.5});		
		}
	}

/*************************************************************************************************************************/

	function galleryHideLightbox()
	{
		//new Fx.Styles($('galleryLightboxPanel'), {duration:500, transition:Fx.Transitions.Sine.easeOut, onComplete:function(){
			//$('galleryLightboxPanel').setOpacity(0);
			
			new Fx.Styles($('galleryLightbox'),{duration:500, onComplete:function(){
				new Fx.Styles($('galleryShade'),{duration:250, onComplete:function(){
					booShowing = false;
				}}).start({opacity:0});
			}}).start({opacity:0});
			
			
		//}}).start({left:$('galleryLightbox').getLeft()});		
	}
	
/*************************************************************************************************************************/

	function galleryShowTools(target)
	{
		//alert(target.getParent().getTop());
		if (target!=currentPhoto)
		{
			if(window.ie)
			{
				var intLeft = target.getLeft()+15-target.getParent().getLeft();
				var intTop= target.getTop()+15-target.getParent().getTop();
			}
			else
			{
				var intLeft = target.getLeft()+15-target.getParent().getLeft();
				var intTop= target.getTop()+15-target.getParent().getTop();
			}
			var lnkEditPhoto = new Element('a');
			lnkEditPhoto.id = 'galleryEditLink';
			lnkEditPhoto.innerHTML = 'Edit';
			lnkEditPhoto.href = 'javascript:galleryEditPhoto();';
			lnkEditPhoto.setStyles({left:intLeft, top:intTop, opacity:0});
			lnkEditPhoto.injectTop(target);
			var lnkDeletePhoto = new Element('a');
			lnkDeletePhoto.id = 'galleryDeleteLink';
			lnkDeletePhoto.innerHTML = 'Delete';
			lnkDeletePhoto.href = 'javascript:galleryDeletePhoto();';
			lnkDeletePhoto.setStyles({left:intLeft, top:intTop+25, opacity:0});
			lnkDeletePhoto.injectTop(target);
			new Fx.Styles($('galleryEditLink'), {duration:250}).start({opacity:1});			
			new Fx.Styles($('galleryDeleteLink'), {duration:250}).start({opacity:1});			
			currentPhoto = target;
		}		
	}
	
/*************************************************************************************************************************/

	function galleryHideTools()
	{
		if($('galleryEditLink')!=null) $('galleryEditLink').remove();
		if($('galleryDeleteLink')!=null) $('galleryDeleteLink').remove();
		currentPhoto = null;
	}
	
/*************************************************************************************************************************/

	function galleryEditPhoto()
	{
		if(window.ie)
		{
			var intPadX = -8;
			var intPadY = -10;
		}
		else
		{
			var intPadX = -10;
			var intPadY = -10;
		}
		var divContainer = $('galleryEditLink').getParent();
		$('galleryEditLink').remove();
		$('galleryDeleteLink').remove();
		currentPhoto = null;
		var arrElements = divContainer.getChildren();
		var strID = arrElements[0].id.substring(12,arrElements[0].id.length);
		var strTitle  = arrElements[1].innerHTML;
		var strTitle2 = arrElements[2].innerHTML;
		var strDescription = arrElements[3].innerHTML;
		strDescription = strDescription.replace(/~/g,'\r\n');
		var strFilename = arrElements[4].innerHTML;
		var strSection = arrElements[5].innerHTML;
		var intEditorWidth = 400;
		var intEditorHeight = 240;
		var intTargetLeft = (window.getWidth()-intEditorWidth)/2;
		var intTargetTop = (window.getHeight()-intEditorHeight)/2;
		$('galleryShade').setOpacity(0);
		$('galleryShade').setStyles({'width':'100%', 'top': window.getScrollTop(), 'height': window.getHeight()});
		new Fx.Styles($('galleryShade'),{duration:250, onComplete:function(){
			//$('galleryFileLabel').setStyle('display','none');														  
			//$('galleryFileInput').setStyle('display','none');														  
			$('galleryEditor').setOpacity(0);
			$('galleryEditor').setStyles({left:intTargetLeft, top:intTargetTop, width:intEditorWidth, height:intEditorHeight, display:'block', zIndex:2});
			$('galleryIDInput').value = strID;
			$('galleryActionInput').value= 'edit';
			$('galleryTitleInput').value = strTitle;
			$('galleryTitle2Input').value = strTitle2;
			$('galleryDescriptionInput').value = strDescription;
			$('gallerySectionInput').value = intSection;
			new Fx.Styles($('galleryEditor'),{duration:500}).start({opacity:1});		
		}}).start({opacity:.8});		
	}
	
/*************************************************************************************************************************/

	function galleryAddNewPhoto()
	{
		if(window.ie)
		{
			var intPadX = 0;
			var intPadY = 5;
		}
		else
		{
			var intPadX = -10;
			var intPadY = -10;
		}
		var intEditorWidth = 400;
		var intEditorHeight = 240;
		var intEditorLeft = ((window.getWidth()-intEditorWidth)/2);
		var intEditorTop = (window.getHeight()-intEditorHeight)/2;
		$('galleryShade').setOpacity(0);
		$('galleryShade').setStyles({'width':'100%', 'top': window.getScrollTop(), 'height': window.getHeight()});
		new Fx.Styles($('galleryShade'),{duration:250, onComplete:function(){
			$('galleryFileLabel').setStyle('display','inline');	
			$('galleryFileInput').setStyle('display','inline');	
			$('galleryEditor').setOpacity(0);
			$('galleryEditor').setStyles({left:intEditorLeft, top:intEditorTop, width:intEditorWidth, height:intEditorHeight, display:'block', zIndex:2});
			$('galleryIDInput').value = '';
			$('galleryActionInput').value= 'new';
			$('galleryTitleInput').value = '';
			$('galleryTitle2Input').value = '';
			$('galleryDescriptionInput').value = '';
			$('gallerySectionInput').value = intSection;
			new Fx.Styles($('galleryEditor'),{duration:500}).start({opacity:1});		
		}}).start({opacity:.8});		
	}

/*************************************************************************************************************************/

	function galleryCancelPhoto()
	{
		new Fx.Styles($('galleryEditor'),{duration:500, onComplete:function(){
			new Fx.Styles($('galleryShade'),{duration:500}).start({opacity:0});		
		}}).start({opacity:0});		
	}

/*************************************************************************************************************************/
	
	function gallerySavePhoto()
	{
		new Fx.Styles($('galleryEditor'),{duration:500, onComplete:function(){
			if($('galleryForm').getChildren()[0].id=='galleryFileLabel')
			{
				$('galleryForm').submit();
			}
			else
			{
				new Fx.Styles($('galleryEditPhotoWindow'),{duration:500, onComplete:function(){
					$('galleryForm').submit();
				}}).start({left:intOriginalLeft, top:intOriginalTop});		
			}
		}}).start({opacity:0});		
	}

/*************************************************************************************************************************/

	function galleryDeletePhoto()
	{
		var divContainer = $('galleryDeleteLink').getParent();
		$('galleryEditLink').remove();
		$('galleryDeleteLink').remove();
		currentPhoto = null;
		var arrElements = divContainer.getChildren();
		var strID = arrElements[0].id.substring(12,arrElements[0].id.length);	
		if(confirm('Are you sure you want to delete this photo ?')) 
		{
			$('galleryIDInput').value = strID;
			$('galleryActionInput').value = 'delete';
			$('galleryForm').submit();
		}		
	}
	
/*************************************************************************************************************************/	
