function adjustSize(){
	ch = $('#content').height() + 160;
	wh = $(window).height();
	
	mode = $('#bg-images').attr('class');
	
	if ((mode == "sequence") || (ch < wh)) {
		$('#background').height(wh);
		$('#background IMG').width('100%');
		$('#background IMG').height('auto');
		if ($('#background IMG').height() < wh) {
			$('#background IMG').width('auto');
			$('#background IMG').height('100%');
		}
	}
	else {
		$('#background').height(ch + 160);
		$('#background IMG').width('auto');
		$('#background IMG').height('100%');
	}
}

function bgimagesHandle(item){
	// To prevent some IE6 bothers
	$('#bg-images IMG').removeAttr('alt');
	$('#bg-images IMG').attr('galleryimg','no');
	
	$('#background').addClass('loading');
	
	mode = $('#bg-images').attr('class');
	
	if (mode == "sequence") {
		//sequence images
		link = $('#bg-images LI:eq('+ item +')').html();
		$('#background IMG').remove();
		
		img = new Image();
		$(img).load(function () {
			$(this).css('display', 'none');
			$('#background').removeClass('loading').append(this);
			adjustSize();
			$(this).fadeIn(1000);
		}).attr('src', link);
		
		item++;
		
		if (item >= $('#bg-images LI').size()) item = 0;
		
		window.setTimeout('bgimagesHandle('+ item +')', 10000);
	}
	else {
		// random image
		num = $('#bg-images LI').size();
		item = Math.floor(Math.random()*num);
		
		link = $('#bg-images LI:eq('+ item +')').html();
		
		img = new Image();
		$(img).load(function () {
			$(this).css('display', 'none');
			$('#background').removeClass('loading').append(this);
			adjustSize();
			$(this).fadeIn(10).fadeTo(500, 0.5);
		}).attr('src', link);
	}
}

function headerHandle(){
	$('#header').animate({top : '-280px'}, 1000, function(){
		$('#header').hover(function(){
			$(this).animate({top : '0px'}, 1000);
		}, function(){
			$(this).animate({top : '-280px'}, 800);
		});
	});
}

$(function() {
	// init
	$('#header, #content').fadeTo(200, 0.8);
	$(window).resize(function(){ adjustSize(); });
	
	// shadow
	tmp = $('#content.intro').html();
	$('#content.intro').prepend('<div class="shadow">'+ tmp +'</div>');
	
	// bg images
	$('BODY').prepend('<div id="background"></div>');
	bgimagesHandle(0);
	
	// header
	headerHandle();
});
