var currPage = 'title_page';
var time = new Date();
var start = time.getHours() + ''+ time.getMinutes() + '' + time.getSeconds();
var seconds = start;

function scroll( element, duration, threshold ) {
	threshold = threshold || 0;
	var scrollDistance = Math.abs( $( window ).scrollTop() - $( element ).offset().top );
	
	if ( scrollDistance >= threshold ) {
		$.scrollTo( element, duration );
	}
}

function scrollToSub( subtype ) {
	currPage = subtype;
	scroll( subtype == 'title_page' ? 'body' : ( '#page_' + subtype ), 1500, 10 );
};

function updateTime() {
	var t = new Date();
	seconds = t.getHours() + ''+ t.getMinutes() + '' + t.getSeconds();
	setTimeout( "updateTime()", 1000 );
};

function scrollToResize( curr ) {
	if ( ( seconds - start ) >= 1 ) {
		scroll( '#page_' + curr, 1500 );
		start = seconds;
	}
};

updateTime();

function changeSellBlock( show, hide ) {
	$( '#sell_form_' + hide ).hide();
	$( '#sell_form_' + show ).show();
	scrollToSub( 'jets_sell' );
	resizeBlock( 'jets_sell' );
};

function changeSubBlock( show, hide, block ) {
	$( '#' + hide ).hide();
	$( '#' + show ).show();
	scrollToSub( block );
	resizeBlock( block );
};

function loadBlock( url, container ) {
	if ( !url || !container ) return;
	$( '#' + container ).load( url, function() {
		resizePage();
	});
};

function submitForm( form_id, response_container ) {
	action = $( '#' + form_id ).attr( 'action' );
	fields = $( '#' + form_id ).find( 'input[checked], input[type="text"], input[type="hidden"], input[type="submit"], option[selected], textarea');
	$( '#' + response_container ).load( action, fields, function() {
		resizePage();
		if ( response_container == 'consult_us_container' ) {
			scroll( '#page_consult_us', 0 );
		}
	});
};

function blurInput( id, str ) {
	var el = $( '#' + id );
	if ( el.attr( 'value' ) == '' ) {
		el.attr( 'value', str );
	} else if ( el.attr( 'value' ) == str ) {
		el.attr( 'value', '' );
	}
	return false;
};

function resizePage() {
	//resizeBlock( 'title_page' );
	resizeBlock( 'jets_list' );
	resizeBlock( 'jets_sell' );
	resizeBlock( 'jets_request' );
	resizeBlock( 'consult_us' );
};

function resizeBlock( st ) {
	var h = $( window ).height();
	$( '#page_' + st ).css( 'height', 'auto' );
	height = $( '#page_' + st ).height();
	if ( height > h )  return;
	if ( st == 'consult_us' ) h = h-100;
	$( '#page_' + st ).css( 'height', h );
};

//$.fx.off = true;