var fader_timeout = "5s"; /* 5 seconds */

function resize_background_image_now() {	
	var ratio = 12/8;
/*	$('#background_image').width($(window).width());
	$('#background_image').height($(window).width() / ratio);
	
	if ($('#background_image').height() < $(window).height()) {
		$('#background_image').height($(window).height());
		$('#background_image').width($(window).height() * ratio);
	} */
	$('.background_image').each(function(){
		$(this).width($(window).width());
		$(this).height($(window).width() / ratio);

		if ($(this).height() < $(window).height()) {
			$(this).height($(window).height());
			$(this).width($(window).height() * ratio);
		}
	});

}

function control_content_image(){
	var ratio = 960/820;
	var max_height = 450;
	var min_height = 200;
	var w = $("#content_image_wrapper").width();
	var h = $("#content_image_wrapper").height();
	
	if (w > 0 && h > 0){
	
		if (h >= max_height){
			$("#content_image").height(max_height);
			$("#content_image").width(max_height * ratio);
		}
		else if (h <= min_height){
			$("#content_image").height(min_height);
			$("#content_image").width(min_height * ratio);
		}
		else {
			$("#content_image").height(h);
			$("#content_image").width(h * ratio);
		}
	}
	else {
		setTimeout(control_content_image,100);
	}
}

/* function fix_content_wrapper(){
	var h_wrap = $("#content_wrapper").height();
	var h_cont = $("#content_container").height();
	var h_img = $("#content_image_wrapper").height();
	
	if (h_img > 0){
		var hmax = (h_cont > h_img) ? h_cont : h_img;
		if (h_wrap < hmax){
			$("#content_wrapper").height(h_cont);
		}
		else {
			/* $("#content_wrapper").height("100%"); */
			/*$("#content_wrapper").height($(window).height() - 315);
		}
		$("#content_wrapper").data("state","ready");
	}
	else {
		setTimeout(fix_content_wrapper,100);	
	}
} */
function fix_content_wrapper(){
	var container_height = $("#content_container").height();
	var wrapper_height = $("#content_wrapper").height();
	var window_height = $(window).height();
	
	var new_height = wrapper_height;
	if (container_height > wrapper_height){
		new_height = container_height;
	}

	
	if (new_height < (window_height - 315)){
		new_height = window_height - 315;
	}
	$("#content_wrapper").height(new_height);
}

function handle_window_resize(){
	fix_content_wrapper();
	resize_background_image_now();
/* 	control_content_image(); */
}

function show_content(){
	/* if ($("#content_wrapper").data("state") == "ready"){ */
		$("#content_wrapper").hide().css("visibility","visible").slideDown('slow','swing',fix_content_wrapper);
	/* }
	else {
		setTimeout(show_content,1000);	
	}*/
}

function hide_content(){
	$("#content_wrapper").slideUp('slow',function(){$("#content_wrapper").css("visibility","hidden").show()});
}

function init_background_images(){
	var zindex = 90;
	$(".background_image").each(function(){
		$(this).css("z-index",zindex);
		zindex -= 10;
		if ($(this).is(":hidden")){
			$(this).show();
		}
	});
}

function background_image_fader(){
	var current_index = $(".background_image:visible:first").index();
	var last_index = $(".background_image:last").index();
	if (current_index < last_index){
		$(".background_image").css("visibility","visible");
		$(".background_image:visible:first").fadeOut('slow');
	}
	else {
		$(".background_image:first").fadeIn('slow',function(){
			$(".background_image:hidden").show();
		});
	}
}

function start_fader(){
	$("#background").everyTime(fader_timeout,"banner-fader",background_image_fader);
}

function stop_fader(){
	$("#background").stopTime("banner-fader");
}

$(window).on('resize', handle_window_resize);

$(document).ready(function(){
	$("#content_wrapper").data("state","new");
	init_background_images();
	$(window).blur(stop_fader);
	$(window).focus(start_fader);
	$(window).trigger('resize');
	setTimeout("show_content()",500);
	
	$("#hide_content").click(hide_content);
	$("#hide_content").hover(function(){$(this).css("cursor","pointer");},function(){$(this).css("cursor","default");});

	start_fader();
	$("#fadeout").click(background_image_fader);
});

