window.addEvent('domready', function() {
		if(($('full_content') || $('right_col')) && $('thumbnails')){
			current = $('thumbnails').getFirst('li').getChildren('a')[0];
			//alert(current.getProperty('id'));		
			
			if($('full_content')){
				image_container = $('full_content');
			} else {
			    image_container = $('full_size');
			}
			
			fx = new Fx.Morph(image_container, {
					link: 'chain', 
					duration: 500, 
					transition: Fx.Transitions.Sine.easeOut
					});
					
 			fx2 = new Fx.Morph(image_container, {
 					link: 'chain', 
 					duration: 500, 
 					transition: Fx.Transitions.Sine.easeOut});
 			
 			if($('full_image')){
 				image_internal = 'full_image';
			} else {
				image_internal = 'full_size';
			}
					
				image_height = new Fx.Morph(image_internal, {
 						link: 'chain', 
 						duration: 500, 
 						transition: Fx.Transitions.Quart.easeOut});

			/* When an item (<a>) is clicked, it inserts the image into the full view div as a background image*/
			$$('.item').each(function(item){ 
				item.addEvent('click', function(e) { 
					e = new Event(e).stop();
					//alert(item.id);
					showImage(item);
				});
			});
			
			if($('prev')){
				$('prev').addEvent('click', function(e){
					e = new Event(e).stop();
					var parent = current.getParent('li');
					var previousSibling = parent.getPrevious('li');	
					if(previousSibling){
						var link = previousSibling.getChildren('a')[0];
						//alert(link.getProperty('id'));
						showImage(link);
					} else {
						previousSibling = parent.getParent('ul').getChildren('li').getLast('li');
						var link = previousSibling.getChildren('a')[0];
						//alert(link.getProperty('id'));
						showImage(link);
					}
				});
				//var parent = myElement.getParent([match]);
			}
			
			if($('next')){
				$('next').addEvent('click', function(e){
					e = new Event(e).stop();
					var parent = current.getParent('li');
					//alert(parent.getProperty('id'));
					var nextSibling = parent.getNext('li');
					//alert(nextSibling.getProperty('id'));
					if(nextSibling){
						var link = nextSibling.getChildren('a')[0];
						//alert(link.getProperty('id'));
						showImage(link);
					} else {
						nextSibling = parent.getParent('ul').getChildren('li')[0];
						var link = nextSibling.getChildren('a')[0];
						//alert(link.getProperty('id'));
						showImage(link);
					}
				});
				//var parent = myElement.getParent([match]);
				//var nextSibling = myElement.getNext([match]);	
			}
		}
});

function showImage(item){
	first_brief = true;
	$$('.brief_section').each(function(item){
		if(first_brief!=true){
			item.dispose();
		} else {
			first_brief = false;
		}
	});
	
	first_image = true;
	$$('.full_image').each(function(item){
		if(first_image!=true){
			item.dispose();
		} else {
			first_image = false;
		}
	});  
	
	current = item;
	//alert(typeof(current));
	fx2.start({ 
			'opacity' : 0													
		}).chain(function(){
	
			//fx2.start({ 'opacity' : 1 });
			this.largeImage = new Image();

			/* When the large image is loaded, fade out, fade in with new image */
			this.largeImage.onload = function(){
						position = (item.id).split('_');
						offset_width = position[2];
						offset_height = position[3];
						switch(position[4]){
							case('x'):
								offset_unit = 'px';
								break;
							case('p'):
								offset_unit = '%';
								break;	
						}
						
				 	 	image_container.setStyle('background-image', 'url('+item.href+')');
						image_container.setStyle('background-position', offset_width+offset_unit+' '+offset_height+offset_unit);
						//alert(offset_width+offset_unit);
						//alert(offset_height+offset_unit);
						
						image_height.start({
						   		'height': this.height
							});
							
						if($('full_image')){
							$('full_spacer').setStyle('margin-top', this.height);	
						}
							
						if(item.getElement('span')){
							var image_ID = item.getElement('span').id.split('_')[1]; // see if there are disciplines
							h3_display = true;
							h4_display = true;
							
							var url = item.getElements('span')[1].getProperty('text');
							
							$('project_title').setProperty('text', '');
							if($('project_title').hasChild('a')){
								($('project_title').getElement('a')[0]).dispose();
							}
							if(item.getElements('span')[1].getProperty('text')!=''){
								var a = new Element('a');
								a.setProperty('text', item.getElements('span')[0].getProperty('text'));
								a.setProperty('href', url);
								$('project_title').adopt(a);
							} else if(item.getElements('span')[0].getProperty('text')!=''){
								$('project_title').setProperty('text', item.getElements('span')[0].getProperty('text'));
							} else {
								h3_display = false;
							}

							$('disciplines_title').setProperty('text', '');
							if($('disciplines_title').hasChild('a')){
								($('disciplines_title').getElement('a')[0]).dispose();
							}
							if(item.getElements('span')[1].getProperty('text')!=''){
								var a = new Element('a');
								$('disciplines_title').adopt(a);
								a.setProperty('text', portfolio_images[image_ID]);
								a.setProperty('href', url);
							} else if(portfolio_images[image_ID]!=''){
								$('disciplines_title').setProperty('text', portfolio_images[image_ID]);
							} else {
								h4_display = false;
							}
							
							if(h3_display==false&& h4_display==false){
								($('project_title').getParent()).setStyle('display','none');
							} else if(url!=''){
								($('project_title').getParent()).setStyle('display','block');
								($('project_title').getParent()).setProperty('class','disciplines');
							} else {
								($('project_title').getParent()).setStyle('display','block');
								($('project_title').getParent()).setProperty('class','disciplines_no_link');
							}
						}
					
					fx.start({'opacity': 1});	 // then bring opacity of elements back to visible				
			};
			
			this.largeImage.src = item.href;
		});
}