/** Open Link in new window **/
function popUp( url, name, winargs ) {

	if( typeof( url ) !== 'undefined' && url != "" ) {

		if( typeof( name ) !== 'undefined' || name == "" ) {

			name = "_blank";
		}

		if( typeof( winargs ) === 'undefined' || winargs === "" ) {

			newwindow = window.open( url, name );
		} else {

			newwindow = window.open( url, name, winargs );
		}

		if( window.focus ) {

			newwindow.focus();
		}
	}

	return false;
}

/** Load Gallery **/
document.observe( "dom:loaded", setupSlideShows );

function setupSlideShows() {

	try {

		startSlideShow('image-gallery');
	} catch( err ) {

		//alert( err );
	}
}

var trans_time_ms = 4000;//Must be greater than 1500 for decent transition time

var img_array = [];
var current_index = 0;

function startSlideShow( elem ) {

	img_array = Element.childElements( $( elem ) );

	for( var i = 1; i < img_array.length; i++ ) {

		Element.setStyle( img_array[i], { "display":"none", "zIndex": "100"} );
	}

	setTimeout( 'show_next()', trans_time_ms );
}

function show_next() {

	var thisIndex = current_index + 1;

	if( current_index === -1 ) {

		Element.setStyle( img_array[img_array.length - 1], { "zIndex": "100" } );
	} else {

		Element.setStyle( img_array[current_index], { "zIndex": "100" } );
	}

	Element.setStyle( img_array[thisIndex], { "zIndex": "200"} );
	Effect.Appear( img_array[thisIndex], { duration: 1.0 } );

	setTimeout( 'hide_current()', 1200 );
}

function hide_current() {//last to 0 is clunky

	if( current_index === -1 ) {

		Element.setStyle( img_array[img_array.length - 1], { "display":"none" } );
	} else {

		Element.setStyle( img_array[current_index], { "display":"none" } );
	}

	current_index++;

	if( img_array.length <= ( current_index + 1 ) ) {

		current_index = -1;
	}

	setTimeout( 'show_next()', ( trans_time_ms - 1200 ) );
}