$(document).ready(function() {

    //init homepage bg
	if($('div#wrap').text() != null && $('div#wrap').text() != ""){
		initBg();
	}
	$(window).resize(function() {
		resizeBgImg();
	});
	
});

function initBg(){
	$('div#wrap').css('background','none');
	//var th = $('div#wrap').height();
	//var th = $(document).height();
	var th = $('div#header').height()+ 1 + $('div#nav').height() + $('div#main').height();
	// alert("th "+th);
	bgh = th; //- 130;
	var img = $('div#image-holder img').attr("src");
	resizeBgImg();
}

function resizeBgImg() {
	var win_width = $(window).width();
	//if (win_width < $(window).width()) {
	//	win_width = $(window).width()+25;
	//}
	//var win_height = $(document).height();
	var dh = $(document).height();
	
	var win_height = $('div#header').height()+ 1 + $('div#nav').height() + $('div#main').height();
	
	var soffset = $(document).scrollTop();
	if (win_height < $(window).height()+soffset) {
		win_height = $(window).height()+soffset;
	}
	if (win_height < dh) {
		win_height = dh;
	}
	bgh = win_height;
	debug(bgh);
	// tom changed this bgh = $(window).height()+25;
	
	//console.log(win_height);
	var win_aspect_ratio = win_width / win_height;
	var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
	current_width = parseInt($('#image-holder img').attr('width'));
	//alert(current_width);
	if (current_width > 1) {
		pic_width = current_width;
	}
	current_height = parseInt($('#image-holder img').attr('height'));
	
	if (current_height > 1) {
		pic_height = current_height;
	}
	pic_aspect_ratio = pic_width / pic_height;
	if (win_aspect_ratio > pic_aspect_ratio) {
		// scale pic to fit window width
			
		var new_width = win_width;
		var new_height = (win_width / pic_width) * pic_height;
	} else {
		// scale pic to fit window height		
		var new_height = win_height;
		var new_width = (win_height / pic_height) * pic_width;
	}
	
	// center image
	new_top = 0 - ((new_height - win_height) / 2);
	new_left =  0 - ((new_width - win_width) / 2);
	$('#image-holder img').css({top: new_top, left: new_left});
	if (isIE6){
		$('#image-holder img').css({height: new_height+"px", width: new_width+"px"});
		$('#image-holder').css({height: bgh+"px", width: win_width+"px"});
	} else {
		$('#image-holder img').css({height: new_height, width: new_width});
		$('#image-holder').css({height: bgh, width: win_width});
	}
	
	$('#image-holder').css({visibility:"visible", display:"block", left:"0"});
	
	
	//alert($('#image_holder').height());
}



