/* Controls the speed of fading in and out */
var speed = '.75';

function changeImage(div, caption) {

	// Fade out the current image
	$$('.current').each(function(elem) {
       elem.removeClassName('current');
       Effect.Fade(elem, {duration:speed});
   });
   
   // Fade in the new image
   Effect.Appear(div, {duration:speed});
   
   // Give the new image the 'current' class
   div.addClassName('current'); 
   
   // Update caption
   $('gallery-caption').innerHTML = caption; 

	// Update nav
	$('gallery-nav-prev').innerHTML = '';
	$('gallery-nav-next').innerHTML = '';
	
	var prevDiv = $$('div.current')[0].previous();
	var nextDiv = $$('div.current')[0].next();

	if (prevDiv && !(nextDiv)) {
		$('gallery-nav-prev').innerHTML = '<a href="#" onclick="previousImage(); return false">&lt; <strong>Previo</strong></a>';
	} else if (!(prevDiv) && nextDiv) {
		$('gallery-nav-next').innerHTML = '<a href="#" onclick="nextImage(); return false"><strong>Pr&oacute;ximo</strong> &gt;</a>';
	} else if (prevDiv && nextDiv) {
		$('gallery-nav-prev').innerHTML = '<a href="#" onclick="previousImage(); return false">&lt; <strong>Previo</strong></a>';
		$('gallery-nav-next').innerHTML = '<a href="#" onclick="nextImage(); return false"><strong>Pr&oacute;ximo</strong> &gt;</a>' 
	}  else {
		$('gallery-nav-next').innerHTML = '';
		$('gallery-nav-prev').innerHTML = '';
	}
}

function rotateImage(imgDiv, caption, hires) {
    
    div = $('gallery-banner-' + imgDiv);
    
    /* Ensure we're not working on the current image so it doesn't break when trying to swap with itself */
    if (!div.hasClassName('current')) {

        changeImage(div, caption);
        
        /* Check if hi-res image is set */
        if  (hires != '') {
            /* Update hi-res link */
            $('gallery-hires-link').href = hires;
            $('gallery-hires-link').show();
        
        /* If not, hide the link */    
        } else {
            if (hires != '') {
				$('gallery-hires-link').hide();
				};
        }
    
    }
    
}

function previousImage() {

	// get previous image div
	var prevDiv = $$('div.current')[0].previous();
	var prev = prevDiv.identify();
	
	// make sure previous div is an image div we want to display, then do it.
	if (prev) {
		
		//get "title" attribute text from image for caption
		var caption = prevDiv.readAttribute('title');
		
		//change the image
		changeImage(prevDiv, caption);
	};
}

function nextImage() {

	// get previous image div
	var nextDiv = $$('div.current')[0].next();
	var next = nextDiv.identify();
	
	// make sure previous div is an image div we want to display, then do it.
	if (next) {
		
		//get "title" attribute text from image for caption
		var caption = nextDiv.readAttribute('title');
		
		//change the image
		changeImage(nextDiv, caption);
	};
}


