var backAnimation;

$(function(){
	fadeInBack()
})

function startBackSwitching(){
	if (BackArray.length>0){
		if(backAnimation!=null){
			//normalBackSwitch()
		}else{
			backAnimation	=	setInterval( "normalBackSwitch()", 15000 ); //15000
			
			if(slideInterval!=null){
				normalBackSwitch()
			}	
		}
	}
}

function resetBackSwitching(){
	clearInterval(backAnimation);
	backAnimation	=	null;
}

function allowBackSwitching(){
	if(backAnimation==null){
		backAnimation	=	setInterval( "normalBackSwitch()", 15000 );
	}
}


function fadeInBack(){
	$('.img_Back:last').each(function () {
		if ( this.complete || this.readyState === 4 ) {
			fullSizeImage($(this))
			$(this).css({'opacity':0}).animate({'opacity':1},2000,removeLastImage)
		}else{
			$(this).load(function(){
				fullSizeImage($(this))
				$(this).css({'opacity':0}).animate({'opacity':1},2000,removeLastImage)
			})
		}
	})
}

function normalBackSwitch(){
	if($('.img_Back').length < 2){
		var newBack		=	BackArray.pop();
		showSomeBackGround(newBack)
		BackArray.unshift(newBack)
	}
}

function showSomeBackGround(image){
	$('#background').append('<img class="img_Back" src="'+image+'" />');
	fadeInBack()
}

function removeLastImage(){
	if(	$('.img_Back').length >1){
		$('.img_Back:not(.img_Back:last)').remove();
	}	 
}
