// BELOW HERE, THIS COMMENTED SECTION, IS WILLEMS ORIGINAL CODE FROM www.jangalaretreat.com
//		var is_ie = document.all && (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
//		function window_resize_handler () {
//		var doc_height = $(window).height();
//		if (is_ie) {
//		$("#container").css("position", "absolute");
//		$("#container").css("bottom", "-10px");
//		$("#wrapper").css("position", "relative");
//		$("#wrapper").css("height", String(doc_height)+"px");
//		}
//		var doc_width = $(window).width();
//		if(doc_width>1808) {
//		$("#bg img").css("width", String(doc_width)+"px");
//		}
//		$("#bg img").css("left",String((doc_width/2)-($("#bg img").width()/2))+"px");
//		if (is_ie) {
//		$("#bg img").css("top",String(16+(doc_height)-($("#bg img").height()))+"px");
//		} else {
//		$("#bg img").css("top",String((doc_height)-($("#bg img").height()))+"px");
//		}
//		}
//		$(document).ready(function(){
//		$(window).bind("resize", window_resize_handler);
//		window_resize_handler();
//		});
// ABOVE HERE, THIS COMMENTED SECTION, IS WILLEMS ORIGINAL CODE FROM www.jangalaretreat.com

		//var is_ie = document.all && (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
function getName(s) {
    var d = s.lastIndexOf('.');
    return s.substring(s.lastIndexOf('/') + 1, d < 0 ? s.length : d);
}

function window_resize_handler () {
	var doc_height = $(window).height();
	var doc_width = $(window).width();
	var image_ratio = 2000/1200;
	var bgImg = document.getElementById('bg').getElementsByTagName('img')[0].src;
	var imageName = getName(bgImg);
	if (imageName == 'home') {
	    image_ratio = 2297/1200;
	} else if (imageName == 'retreats') {
	    image_ratio = 1920/1200;
	} else if (imageName == 'instructors') {
	    image_ratio = 2273/1200;
	} else if (imageName == 'contact') {
	    image_ratio = 2222/1200;
	} else if (imageName == 'blog') {
	    image_ratio = 2128/1200;
	} else {
	    image_ratio = 2000/1200;
	}
//	if($("#bg img").width()>0) {
//		image_ratio =  ($("#bg img").width()/$("#bg img").height());
//	} else {
//		image_ratio = 2000/1200; // For a bug in safari, where the width is zero when the page first loads.
//	}

	if((doc_width/doc_height)<(image_ratio)) {
		//alert ('Resizing by Height');
		$("#bg img").css("height", String(doc_height)+"px");
		$("#bg img").css("width","auto");
	} else {
		// Resize by width
		//alert ('Resizing by Width: '+String(doc_width));
		$("#bg img").css("width", String(doc_width)+"px");
		$("#bg img").css("height","auto");
	}

//			$("#bg img").css("left",String((doc_width/2)-($("#bg img").width()/2))+"px");
//			if($("#bg img").width()==0) {
//				$("#bg img").css("left","0px"); // For a bug in safari, where the width is zero when the page first loads.
//			}
}

$(document).ready(function(){
	$(window).bind("resize", window_resize_handler);
	window_resize_handler(); // This is also on the onload event of the img element, cos otherwise it didn't work in Safari and Chrome.
});