function _show_background(t) {
	$('.custom-background').fadeIn(t, function() {
		$(".default-background").remove();
	});
}

function start_remote_operation() {
	$("#fondo").height($(document).height());
	$("#fondo,#loading_wrapper").show();
}

function end_remote_operation() {
	$("#fondo,#loading_wrapper").hide();
	
}

function _resize_window(w, h, t) {
	var cw = Math.ceil(w/100)*100;
	var ch = Math.ceil(h/100)*100;
	var mult_t = 1;
	
	start_remote_operation();
	if ($("#bloque_menu:visible").length) {
		mult_t++;
	}
	if ($("#bloque_contenidos:visible").length) {
		mult_t++;
	}
	$("#container_background").empty();
	if (typeof(default_background) != "undefined" && default_background != "") {
		$("#container_background").append("<img class='page-background default-background' src='/img.php?w="+w+"&h="+h+"&f="+default_background+"' alt='' />");
		$("#container_background img").load(function() {
			end_remote_operation();
		});
		//$("#container_background").append("<img class='page-background default-background' src='/"+default_background+"' height='"+h+"' />");
	}

	if (typeof(img_background) != "undefined" && img_background != "") {
		if ((typeof(default_background) != "undefined" && default_background != img_background) || typeof(default_background) == "undefined") {
			$("#container_background").append("<img class='page-background custom-background' src='/img.php?w="+w+"&h="+h+"&f="+img_background+"' alt='' />");
			$("#container_background img").load(function() {
				end_remote_operation();
				setTimeout("_show_background("+t+");", t*mult_t);
//				_show_background(t);
			});
		}
	}
}

function resize_window(first) {
	var w = $("#bloque_contenedor").width();
	var h = $("#bloque_contenedor").height();
	if (!first) {
		default_background = img_background;
	}
	if ($("#container_background img").length) {
		$("#container_background img").fadeOut(300, function() {
			_resize_window(w, h, 300);
		});
	}
	else {
		_resize_window(w, h, 300);
	}
}

$(function() {
	resize_window(0);
	$(window).resize(function() {
		//resize_window(1);
	});
});
