// Number of photos in this gallery
var photoNum = photoArray.length;
// initial counter
var counter = 0;
// current height of image
var currentheight = photoArray[counter][2];
// current height of image
var currentwidth = photoArray[counter][1];
// current caption
var currentcaption = photoArray[counter][3];
// transition speed
var speed = 250;
//	var to check if busy doing transition
var transition_busy = 0;

function prevPhoto($number) {
	// only if not busy doing a transition
	if (transition_busy == 0) {
		// decrease counter if there is not a direct $number
		if ($number == null) {counter --} else {counter = ($number-1)};
		// loop to end if we hit below 0
		if (counter < 0) { counter = photoNum-1 };
		//$('gcaption').innerHTML = counter;
		// now resize image container
		loadPhoto(counter);
	}
}

function nextPhoto($number) {
	// only if not busy doing a transition
	if (transition_busy == 0) {
		// decrease counter if there is not a direct $number
		if ($number == null) {counter ++} else {counter = ($number-1)};
		// loop to end if we hit below 0
		if (counter == photoNum) { counter = 0 };
		// now resize image container
		loadPhoto(counter);
	}
}

// loads new photo
function loadPhoto() {
	// update navigation if navigation on
	if($('gnav')){navigation();}
	// hide overlay nav and caption
	hide_elements();
	// show laoding animation
	$('gloading').style.display = 'inline';
	// load in new image
	imgPreloader = new Image();
	// if image is preloaded
	imgPreloader.onload=function(){
		// when loaded
		// hide laoding animation
		$('gloading').style.display = 'none';
		// hide current photo
		$('gallery_img').style.opacity = 0;
		// resize container to new size
		resize();
	}
	// preloader src
	imgPreloader.src = photoArray[counter][0];
}

// resize image container
function resize() {
	// get current photo width and height
	currentwidth = $('gallery_img').width;
	currentheight = $('gallery_img').height;
	// call moo.fx effect
	resizeContatinerHeight.custom(currentheight,photoArray[counter][2]);
	resizeContatinerWidth.custom(currentwidth,photoArray[counter][1]);
}



// hide various mooShow elements
function hide_elements() {
	// clear cpation. fill with space
	if ($('gcaption')) {
    	$('gcaption').innerHTML = '&nbsp;';
    }
	// overlay navigation
	$('gprev').style.display = 'none';
	$('gnext').style.display = 'none';
}

function switch_photo() {
	$('gallery_img').src=photoArray[counter][0];
	$('gallery_img').alt=photoArray[counter][2];
	$('gallery_img').width=photoArray[counter][1];
	$('gallery_img').height=photoArray[counter][2];
	$('gallery_img').name=counter;
	// update caption  if it exists
	if($('gcaption')){caption();}

	// show new image
	show_elements();
}



// show various mooShow elements
function show_elements() {
	// fade in photo
	fade_in('gallery_img',speed*2);
	// fade in caption if captions on
	if($('gcaption')){fade_in('gcaption',speed);}
}


// show nav elements
function show_nav_overlay() {
	// overlay navigation
	$('gprev').style.display = 'inline';
	$('gnext').style.display = 'inline';
}


// build navigation
function navigation() {
    if (1 < photoNum) {
        // clear current links
        $('gnav').innerHTML = '';
        var i=0
        for (i=0;i< photoNum;i++) {
            // get current content
            var content = $('gnav').innerHTML;
            // write links
            if(i==counter){
                $('gnav').innerHTML = content + ' '+(i+1)+' ';}
                else {
                $('gnav').innerHTML = content + ' <a href=\'#' +(i+1)+ '\' onclick=\"nextPhoto('+(i+1)+');\">'+(i+1)+'</a> ';}
            // add | between links
            if(i < photoNum-1) {
                var content = $('gnav').innerHTML; $('gnav').innerHTML = content + ' |  ';}
        }
    } else {
        $('gnav').style.display = 'none';
    }
    //update next / prev links
    if (counter == photoNum-1) {$('nextphoto').href = '#1'} else {$('nextphoto').href = '#' + (counter+2)};
    if (counter == 0) {$('prevphoto').href = '#'+photoNum} else {$('prevphoto').href = '#' + (counter)};
}

function caption(){
	// update caption
	var caption;
	if ('' == photoArray[counter][3]) {
        caption = '&nbsp;';
        $('gcaption').style.display = 'none';
    } else {
        caption=photoArray[counter][3];
        $('gcaption').style.display = '';

    }
    $('gcaption').innerHTML = caption;
}


// moo.fx
// resize
window.onload = function() {
	transition_busy = 1;
	resizeContatinerHeight = new fx.Height('gallery_img_container', {duration: speed, onComplete:function() {
    	// display new image etc.
	    switch_photo();
    	transition_busy = 0;
	}});
	resizeContatinerWidth = new fx.Width('gallery', {duration: speed});

	// load first photo
	transition_busy = 0;
	nextPhoto(1);
}

// fader effect
function fade_in($element,$speed) {
    var fader = new fx.Opacity($element, {duration: $speed, onComplete:function() {
    // overlay navigation
    show_nav_overlay();
    }});
    fader.hide();
    fader.toggle();
}
