var frames_jq = null;
var sidebar_frames_jq = null;
var slideshow_count = 1;
var slideshow_going = true
current_frame = 1;
var speed = 10

function starter() {
	//console.log( 'starter' );
	current_frame = 1;
	html = '<a href="#" onclick="play_pause();return false;" class="play_pause">pause</a>';
	for( i = 0; i < frames.length-1; i++ ) {
		html += '<a href="javascript:pause();frames['+i+'](slideshow_count);" class="frame">'+(i+1)+'</a>';
	}
	html += '<div style="clear:both;"></div>';
	//console.log( html );
	$('#frame_links').append( html );

	frames[0](1);
}
function exterior(prev_count) {
	if( prev_count != slideshow_count ) {
		return;
	}
	hideLogo();
	//console.log( 'exterior' );
	$('#facade').css({'top':-391}).animate({'top':0}, 1450*speed);
	$('#main_frames,#sidebar_frames').animate({'top':0,'opacity':1}, 'slow');
	$('#info #gradient').animate( { 'left':120 }, 'slow' );
	advanceToFrame(frame_indexes['exterior']);
	if( slideshow_going ) {
		timeout_command = 'frames[current_frame]('+prev_count+')';
		setTimeout( timeout_command, 1800*speed );
	}
}
function interior(prev_count) {
	if( prev_count != slideshow_count ) {
		return;
	}
	hideLogo();
	//console.log( 'interior' );
	$('#interior').css({'left':0}).animate({'left':100}, 1000*speed);
	$('#info #gradient').animate( { 'left':80 }, 'slow' );
	advanceToFrame(frame_indexes['interior']);
	if( slideshow_going ) {
		timeout_command = 'frames[current_frame]('+prev_count+')';
		setTimeout( timeout_command, 1350*speed );
	}
}

function neighborhood(prev_count) {
	if( prev_count != slideshow_count ) {
		return;
	}
	$('#lions').css({'left':0}).animate({'left':-100}, 750*speed);
	$('#info #gradient').animate( { 'left':5 }, 'slow' );
	advanceToFrame(frame_indexes['neighborhood']);
	showLogo();
	if( slideshow_going ) {
		timeout_command = 'frames[current_frame]('+prev_count+')';
		setTimeout( timeout_command, 900*speed );
	}
}

function biker(prev_count) {
	if( prev_count != slideshow_count ) {
		return;
	}
	//console.log( 'biker' );
	$('#info #gradient').animate( { 'left':5 }, 'slow' );
	advanceToFrame(frame_indexes['biker']);
	showLogo();
	if( slideshow_going ) {
		timeout_command = 'frames[current_frame]('+prev_count+')';
		setTimeout( timeout_command, 750*speed );
	}
}

function layouts(prev_count) {
	if( prev_count != slideshow_count ) {
		return;
	}
	//console.log( 'layouts' );
	advanceToFrame(frame_indexes['layouts']);
	showLogo();
	$('#info #gradient').animate( { 'left':70 }, 'slow' );
	if( slideshow_going ) {
		timeout_command = 'frames[current_frame]('+prev_count+')';
		setTimeout( timeout_command, 750*speed );
	}
}

var frames = new Array( exterior, interior, neighborhood, biker, layouts, resetSlideshow );
var frame_indexes = { 'exterior':0, 'interior':1, 'neighborhood':2, 'biker':3, 'layouts':4 };

function resetSlideshow() {
	$('#main_frames,#sidebar_frames').animate({'opacity':0}, 'slow');
	setTimeout( 'resetPositions()', 100*speed );
}
function resetPositions() {
	$('#main_frames,#sidebar_frames').css({'top':0});
	$('#info .frame_0').css( { 'left':35 }, 'slow' );
	$('#main_frames,#sidebar_frames').animate({'opacity':1}, 'slow');
	hideLogo();
	frames[0](slideshow_count);
}

function advanceFrame() {
	current_frame++;
	advanceToFrame( current_frame );
}

function advanceToFrame( frame, stop ) {
	if( stop == true ) {
		slideshow_count = false;
	}
	frame++;
	if( frame >= frames.length || frame < 1 ) {
		frame = 1;
	}
	current_frame = frame;
	var next_main_frame = document.getElementById('main_frame_'+current_frame);
	var next_sidebar_frame = document.getElementById('sidebar_frame_'+current_frame);
	$('#main_frames').animate( {'top':next_main_frame.offsetTop*-1+'px'}, 'slow' );
	$('#sidebar_frames').animate( {'top':next_sidebar_frame.offsetTop*-1+'px'}, 'slow' );
	$('#frame_links a').animate( {'opacity':.2});
	$('#frame_links a.frame:eq('+(current_frame-1)+')').stop().animate({'opacity':1},100).animate({'opacity':.75},'fast');
}
function showLogo() {
	$('#header h1').css( {'visibility':'visible' } );
	$('#header h1').animate({'opacity':'1'});
}
function hideLogo() {
	$('#header h1').animate({'opacity':0}, 'slow');
}
function pause() {
	slideshow_count++;
	slideshow_going = false;
	$('.play_pause').html('play');
	$('.play_pause').animate({'opacity':.75});
}
function play() {
	slideshow_count++;
	slideshow_going = true;
	frames[current_frame](slideshow_count);
	$('.play_pause').html('pause');
	$('.play_pause').animate({'opacity':.2});
}
function play_pause() {
	if( slideshow_going ) {
		pause();
	} else {
		play();
	}
}
