var tierli_w;
var tierli_h;
var is_animating = false;




function init() {
	var tierli = $('#tierli');
	$('body').mousemove( handleMouseMove );
	setInterval( handleTierliTimer , 500 );
	
	//$('.contact').mouseenter( showContactImage ).mouseleave( hideContactImage );
}






// TIERLI HANDLING

function animateTierli() {
	if ( is_animating ) return;
				
	var tierli = $('#tierli');
	is_animating = true;
	
	tierli.show();
	//tierli.load( setTierliWidth );
	tierli.attr( 'src' , '/assets/tierli/' + Math.floor( Math.random() * 10 ) + '.png' );
	tierli.removeAttr( 'width' );
	tierli_w = tierli.width();
	tierli_h = tierli.height();
	setTierliWidth( 0 );
}

function setTierliWidth( count ) {
	if (isNaN(count)) count = 0;
	
	var tierli = $('#tierli');

	if ( count > (1+Math.random()*3) ) {
		is_animating = false;
		tierli.hide();
		return;
	}
	
	//alert(count);

	tierli.css( {
		right: Math.round( -50 + Math.random() * 200 ) + 'px' ,
		top: Math.round( 50 + Math.random() * 200 ) + 'px'
	} );
	tierli.attr( 'width' , Math.round( tierli_w * ( Math.random() * 5 + 5 ) ) );
	
	setTimeout( 'setTierliWidth('+(count+1)+')' , Math.round( 1 + Math.random() * 200 ) );
}

var lastmouse;
var beastCountdown = 0;
//resetBeastCountdown();

function handleMouseMove( e ) {
	if ( lastmouse != null ) {
		var dx = e.clientX - lastmouse.x;
		var dy = e.clientX - lastmouse.y;
		tickleBeasts( Math.sqrt( dx * dx + dy * dy ) / 2 );
	} else {
		lastmouse = {};
	}
	lastmouse.x = e.clientX;
	lastmouse.y = e.clientX;
}

function handleTierliTimer() {
	tickleBeasts( 50 );
}

function tickleBeasts( amount ) {
	beastCountdown -= amount;
	if ( beastCountdown < 0 ) {
		animateTierli();
		resetBeastCountdown();
	}
	$('#debug').text( beastCountdown );
}
function resetBeastCountdown() {
	beastCountdown  = Math.random() * 6000 + 500;
}





// CONTACT IMAGES

var contactImageSrc1,contactImageSrc2;

function showContactImage( e ) {
	var target = e.currentTarget;
	var q = $(target);
	q.addClass( 'selected' );
	
	contactImageSrc1 = 'assets/contact/'+ q.attr('rel') +'_business.jpg';
	contactImageSrc2 = 'assets/contact/'+ q.attr('rel') +'_crazy.jpg';
	
	var img = $('#fullbg');
	if ( img.length ) {
		img.remove();
	}
	$('body').prepend( '<img id="fullbg"/>' );
	showContact1();
	$( window ).resize( handleContactImageResize );
	handleContactImageResize();
//	img.resize( handleContactImageResize );


}

function showContact1() {
	img = $('#fullbg');
	if ( img.length ) {
		img.attr( 'src' , contactImageSrc1 );
		setTimeout( 'showContact2()' , Math.round( Math.random() * 7500 ) );
	}
}
function showContact2() {
	img = $('#fullbg');
	if ( img.length ) {
		img.attr( 'src' , contactImageSrc2 );
		setTimeout( 'showContact1()' , Math.round( Math.random() * 200 ) );
	}
}

function hideContactImage( e ) {
	$( e.currentTarget ).removeClass( 'selected' );
	$('#fullbg').remove();
}

var resizeTimer = null;
function handleWindowResize( e ) {
    if (resizeTimer) clearTimeout(resizeTimer);
    resizeTimer = setTimeout(handleContactImageResize, 100);
}

function handleContactImageResize() {
	var img = $('#fullbg');
	var w = img.width();
	var wh = window.innerHeight;
	var ih = Math.round(w*1.5);
	img.height( ih );
	img.css( { top: '-' + Math.round( (ih-wh) / 2 ) +'px' } );
}




















$('body').ready( init );
