var current = 1;

function adjustIndicator() {
	document.getElementById('img_ind_1').innerHTML = "<img src='images/un_selected.png'>";
	document.getElementById('img_ind_2').innerHTML = "<img src='images/un_selected.png'>";
	document.getElementById('img_ind_3').innerHTML = "<img src='images/un_selected.png'>";
	document.getElementById('img_ind_4').innerHTML = "<img src='images/un_selected.png'>";
	
	document.getElementById('img_ind_' + current ).innerHTML = "<img src='images/selected.png'>";
}

function nextImage( killit ) {
	var next = current + 1;
	if( next == 5 ) 
		next = 1;
	
	$("#img_" + current ).fadeOut( 400, function() { 
		current = next; 
		adjustIndicator();
		$("#img_" + next).fadeIn( 1000, null ); 
	} );
	
	if( killit )
		kill = true;
}

var delay = 4000;
var kill = false;
var firsttime = true;
function autoNext() {

	if( !kill && !firsttime) {
		nextImage();
		window.setTimeout(autoNext, delay);
	}
	if( firsttime ) {
		firsttime = false;
		window.setTimeout(autoNext, delay);
	}
}

 $(document).ready(function() {
	$("#img_ind_1").click(function(){
		var cur = "#img_" + current;
		$( cur ).fadeOut( 400, function() { $("#img_1").fadeIn( 1000, null ); } );
		current = 1;
		adjustIndicator( current );
		kill = true;
	});

	$("#img_ind_2").click(function(){
		var cur = "#img_" + current;
		$( cur ).fadeOut( 400, function() { $("#img_2").fadeIn( 1000, null ); } );
		current = 2;
		adjustIndicator( current );
		kill = true;
	});

	$("#img_ind_3").click(function(){
		var cur = "#img_" + current;
		$( cur ).fadeOut( 400, function() { $("#img_3").fadeIn( 1000, null ); } );
		current = 3;
		adjustIndicator( current );
		kill = true;
	});

	$("#img_ind_4").click(function(){
		var cur = "#img_" + current;
		$( cur ).fadeOut( 400, function() { $("#img_4").fadeIn( 1000, null ); } );
		current = 4;
		adjustIndicator( current );
		kill = true;
	});
	
	controls = $('div[class=\'album\'] ');
	$.each( controls, function( i, control ){
		control.onclick = function() { 
			showAlbum( this.id );
		};
	});
	
	window.setTimeout(autoNext, delay);
 });

var open = null;
		
function showAlbum( id ) {
	var album = id + "_contents";

	/*var options = {};
	if( open ) {
		$("#" + open ).hide( "blind", options, 500, function() {
			$("#" + album).show( "blind", options, 500, function() {
				fleXenv.fleXcrollMain("gal_left");
			} );
		});
	}
	else
		$("#" + album).show( "blind", options, 500, function() {
				fleXenv.fleXcrollMain("gal_left");
			} );
	
	open = album;*/
	
	$("#gallery_pic").fadeOut( 500, function() {
		if( open )
			document.getElementById( open ).style.display="none";
		else
			document.getElementById( "instruct" ).style.display="none";
			
		document.getElementById( album ).style.display="block";
		//document.getElementById("gallery_pic").innerHTML = document.getElementById( album ).innerHTML;
		open = album;
		$("#gallery_pic").fadeIn( 500, null );
	});
	

}

function showPicture( path ) {
	$("#gallery_pic").fadeOut( 500, function() {
		document.getElementById("gallery_pic").innerHTML = "<img src='" + path + "' class='gallery_pic' />";
		$("#gallery_pic").fadeIn( 500, null );
	});
}

var nav_current = "home";
var mini_hidden = false;
var inShowPage = false;
var cache = '';

function setInShowPage( value ) {
	inShowPage = value;
	
	if( !value && cache != '' ) {
		showPage( cache );
		cache = '';
	}
	
}

function showPage( name ) {
	if( !inShowPage ) {
		setInShowPage( true );
		var options = {};
		
		if( name == "gallery" ) {
			$("#" + nav_current ).fadeOut( 500, null );
			$("#thomas_mini_gallery" ).fadeOut( 500, function() {
				$("#" + name ).fadeIn( 500, function() {
					fleXenv.fleXcrollMain("gal_left");
					setInShowPage( false );
				});
				mini_hidden = true;
			});
			
		}
		else if( name == "mixes" ) {
			$("#" + nav_current ).fadeOut( 500, function() {
				if( mini_hidden ) {
					$("#thomas_mini_gallery" ).fadeIn( 500, null );
					mini_hidden = false;
				}
				$("#" + name ).fadeIn( 500, function() {
					fleXenv.fleXcrollMain("mixes_middle");
					setInShowPage( false );
				});
			});
		}
		else if( name == "bio" ) {
			$("#" + nav_current ).fadeOut( 500, function() {
				if( mini_hidden ) {
					$("#thomas_mini_gallery" ).fadeIn( 500, null );
					mini_hidden = false;
				}
				$("#" + name ).fadeIn( 500, function() {
					fleXenv.fleXcrollMain("bio_middle");
					setInShowPage( false );
				});
			});
		}
		else {
			$("#" + nav_current ).fadeOut( 500, function() {
				if( mini_hidden ) {
					$("#thomas_mini_gallery" ).fadeIn( 500, function() {
						setInShowPage( false );
					} );
					mini_hidden = false;
				}
				$("#" + name ).fadeIn( 500, function() {
					setInShowPage( false );
				} );
			});
		}
		
		$("#nav_" + nav_current ).removeClass( "selected" );
		$("#nav_" + nav_current ).addClass( "unselected" );
		$("#nav_" + name ).removeClass( "unselected" );
		$("#nav_" + name ).addClass( "selected" );
		
		nav_current = name;
	}
	else {
		cache = name;
	}
}
