$(document).ready(function() {
	$('.body_image img.background').load(function() {
		$(window).trigger('resize');
	});
	
	$.getScript("http://twitter.com/statuses/user_timeline/kayemeff.json?callback=twitterCallback2&count=1");
	
	everytime();
	
	$('.header .navi a').click(function() {
		var url = $(this).attr('href');
		var id = $(this).attr('id');
		
		$('.header h1').addClass('loading');
		
		$('.header .navi li a.active').removeClass('active');
		$(this).addClass('active');
		
		$('body').get(0).className = '';
		$('body').addClass(id);
		$('.load-content').load(url + ' .load-content > *', function() {
			$('.header h1').removeClass('loading');
			
			everytime();
		});
		
		return false;
	});
	
	// soundmanager
	soundManager.debugMode = false;	// disable debug mode
	soundManager.url = '/sound/';
	
	soundManager.onload = function() {
		jQuery.each(tracks, function(n){
			soundManager.createSound('sound' + n, tracks[n]);
		});
		
		$('.controls.next').trigger('click');
	}
	
	$('.controls.pause').click(function() {
		$(this).toggleClass('pause');
		$(this).toggleClass('play');
		
		soundManager.togglePause('sound' + current_track);
	});
	
	$('.controls.next').click(function() {
		current_track++;
		if(current_track>=tracks.length){current_track=0;}
		
		soundManager.stopAll();
		soundManager.play('sound' + current_track);
		
		$('.mp3 .title').html( titles[current_track] );
	})
});

function everytime() {
	Cufon.replace( $('.header ul li a'), {hover:true,fontFamily:'Futura Std'} );
	Cufon.replace( $('.streetbrush'), {hover:true,fontFamily:'Streetbrush'} );
	$('a[rel="fancybox"]').fancybox();
	
	$('.submit').unbind('click').click(function() {
		$(this).parents('form').trigger('submit');
	});
	
	$(window).trigger('resize');
	
	$('.body_image .cycle').cycle({
		fx: 'fade',
		after: function() {
			$('.body_image img').css('width', '100%');
			$('.body_image').height( $(this).height() - 30 );
		}
	});
}

$(window).resize(function() {
	$('.body_image').height( $('.body_image img').height() - 30 );
	
	var difference = $(window).height() -  $('.header').height() - $('.footer').height() - $('.load-content').height();
	if( difference > 0 ) {
		$('.footer').css('marginTop', difference - 23 );
	} else {
		$('.footer').css('marginTop', 0);
	}
});