(function() {

	var backgroundImageNativeWidth = 0;
	var backgroundImageNativeHeight = 0;
	
	function onBackgroundImageLoad() {
		backgroundImageNativeWidth = $(".background-image").width();
		backgroundImageNativeHeight = $(".background-image").height();

		//console.log("backgroundImageNativeWidth: " + backgroundImageNativeWidth + ", backgroundImageNativeHeight: " + backgroundImageNativeHeight);
		scaleBackgroundImage();
	}
	
	function scaleBackgroundImage() {

		if(backgroundImageNativeWidth == 0)
			return;
		
		var coffset = $("#content-bubble-container").offset();
		var cw = $("#content-bubble-container").width() + coffset.left;
		var ch = $("#content-bubble-container").height() + coffset.top;
		//console.log("content dimensions: " + cw + "x" + ch);

		var ww = $(window).width();
		var wh = $(window).height();
		//console.log("window dimensions: " + ww + "x" + wh);

		var dw = (cw > ww) ? cw : ww;
		var dh = (ch > wh) ? ch : wh;
		
		var nw = dw;
		var nh = (dw / backgroundImageNativeWidth) * backgroundImageNativeHeight;

		if(nh < dh)
		{
			nh = dh;
			nw = (dh / backgroundImageNativeHeight) * backgroundImageNativeWidth;
		}
		
		if(cw < ww && ch < wh)
		{ 
			$("#background-image-container").width("100%");
			$("#background-image-container").height("100%");
		} else {
			$("#background-image-container").width((cw > ww ? cw : ww) + "px");
			$("#background-image-container").height((ch > wh ? ch : wh) + "px");
		}
		
		$(".background-image").width(nw + "px");
		$(".background-image").height(nh + "px");
		
	}

	var needsFadeInTimeoutID;
	var needsFadeIn = false;
	
	function loadBackgroundImage() {
		var url = $(".background-image-url").text();
		
		needsFadeInTimeoutID = setTimeout(function() { needsFadeIn = true; }, 2000);
		
		var img = $("<img>");
		img.addClass("background-image");
		img.bind("load",function(){ 
			clearTimeout(needsFadeInTimeoutID);
			scaleBackgroundImage(); 
			if(needsFadeIn)
				$('.background-image').fadeIn(2000); 
			else
				$('.background-image').show(); 
		});
		$("#background-image-container").append(img);
		img.hide();		
		
		img.attr("src", url);
	}
	
	$(document).ready(function() { 
		loadBackgroundImage();
		$(".background-image").load(onBackgroundImageLoad);
		scaleBackgroundImage();
	
		$(window).resize(function() { 
			scaleBackgroundImage();
		});
		
		// Make links in like section open in a new window.
		$('#content-bubble-container .like a').click(function() { 
			window.open(this.href); return false;
		});
		
		// Make entry content box and bg image clickable on intro page.
		$('#content-bubble-container.intro').click(function() {
			window.location = "home"; return false;
		});
		$('#background-image-container.intro').click(function() {
			window.location = "home"; return false;
		});

        // Hide the content bubble if there is no content.
        if($('#content-bubble .text').length > 0 &&
        	$.trim($('#content-bubble .text').text()) == "")
            $('#content-bubble-container').hide();

		$('#content-bubble').supersleight();

		// Firefox specific hack for the Facebook page...
		// See below. Hide the text until we apply the fix.
		if($.browser.mozilla || $.browser.webkit) 
			$('#content-bubble .like .facebook ul li').hide();
		
	});
	
	$(window).load(function() {
		// Firefox specific hack for the Facebook page... (actually now also for webkit browsers)
		// Long links get broken up and the remainder of the line ends up underneath
		// the image. By setting a left margin on the li that contains the text,
		// it gets correctly left-aligned.
		if($.browser.mozilla || $.browser.webkit) {
			$('#content-bubble .like .facebook ul li').css('margin-left', 
				($('#content-bubble .like .facebook .image').width() + 15) + "px");	
			$('#content-bubble .like .facebook ul li').show();
		}
		
	});
})();



