var countDiv = 0;
$(window).load(function(){
	//Imagenes de fondo
	if(template == 'module') {
		for(var i = 0; i <= totalImages-1; i++) {
			var widthDiv = $(window).width();
			var bgDiv = $('<div id="bg_body_' + i + '" class="bg_body" style="display:none; width: ' + widthDiv + 'px; height:100%;"></div>');
			if(i == 0) {
				$('#bg_loading').after(bgDiv);
			} else {
				$('#bg_body_' + parseInt(i-1)).after(bgDiv);
			}
		}
		$('#bg_body_0').css('background-image','url(' + urlPictures + picturesBody[0] +')');
		$('#bg_body_0').css('background-attachment','fixed');
		$('#bg_body_0').css('display','block');

		$('.before_header').css('width',$(window).width());
		setInterval(function () {
			changePictureBody();
		},6000);
	} else {
		var widthDiv = $(window).width();
		var bgDiv = $('<div id="bg_body_0" class="bg_body" style="display:none; width: ' + widthDiv + 'px; height:100%;"></div>');
		$('#bg_loading').after(bgDiv);
		$('#bg_body_0').css('background-image','url(' + urlPictures + picturesBody[0] +')');
		$('#bg_body_0').css('background-attachment','fixed');
		$('#bg_body_0').css('display','block');
	}

	//Slider del footer
	$('#slider').cycle({
		fx		:'scrollHorz', 
		timeout	: 5000,
		height	: 80,
		delay	: -800,
		next	: '#next_slider',
		prev	: '#prev_slider',
		pause	: 0,
		fit		: 1,
		cleartypeNoBg : true,
		pause: true
	});

	setHeightContent(true);

	var windowResizeTimeout;
	var winWidth = $(window).width();

	function DelayedResize() {
		var winNewWidth = $(window).width();
		if ((winWidth != winNewWidth) && !isNaN(winWidth)) {
			UpdateAllSizes();
			winWidth = winNewWidth;
		}
	}

	function checkResize() {
		window.clearTimeout(windowResizeTimeout);
		windowResizeTimeout = window.setTimeout(DelayedResize, 250);
	}

	$(window).wresize(checkResize);
});

function UpdateAllSizes() {
	showLoading();
	$('#l_contents').jScrollPaneRemove();
	setHeightContent(true);
	var rescaleWidth = 0;
	if ($(window).width()>$('#footer').width()){
		rescaleWidth = $(window).width();
	} else {
		//rescaleWidth = $(window).width();
		rescaleWidth = $('#footer').width();
	}
	$('.bg_body').each(function(){$(this).css('width',rescaleWidth)});
	$('.before_header').css('width',rescaleWidth);
}

function setHeightContent(onLoad) {
	//Alto del contenedor de secciones
	var heightTotal			= $(window).height();
	var heightHeader		= $(".before_header").height();
	var heightFooter		= $("#footer").height();
	var marginLContentTop	= $("#l_contents").css('margin-top');
	var marginLContentBottom= $("#l_contents").css('margin-bottom');
	var margins				= parseInt(marginLContentTop) + parseInt(marginLContentBottom);
	var margins				= 20;
	var heightLContent		= heightTotal - (parseInt(heightHeader) + parseInt(heightFooter + margins));
	$('#l_contents').animate({height : heightLContent + 'px', 'min-height': heightLContent},{
		duration: 1000,
		complete:function(){
			if(onLoad) {
				showScroll();
				hideLoading();
			}
		}
	});
}

function showScroll() {
	$('#l_contents').jScrollPane({showArrows:true, scrollbarWidth: 18, topCapHeight: 0, dragMinHeight: 0, bottomCapHeight: 0});
}
function hideLoading() {
	$('#bg_loading').fadeOut(800);
}

function showLoading() {
	$('#bg_loading').fadeIn(300);
}

function changePictureBody() {
	if(countDiv == parseInt(picturesBody.length)) {
		countDiv = 0;
	}
	$('#bg_body_' + countDiv).animate({'opacity':'0'},{duration:2000, queue:true, complete:function(){$(this).css('display','none')}});

	var nextBg = parseInt(countDiv)+1;
	if(nextBg == parseInt(picturesBody.length)) {
		nextBg = 0;
	}
	$('#bg_body_' + nextBg).css('background-image','url(' + urlPictures + picturesBody[nextBg] +')');
	$('#bg_body_' + nextBg).css('background-attachment','fixed');
	$('#bg_body_' + nextBg).css('display','block').css('opacity','0').animate({'opacity':'1'},{duration:3000, queue:true});
	countDiv++;
}
