$(document).ready( function() {

    var fullScreen	= $('.no-bg-slider #background img.single, .no-bg-slider .only-one .content-bg img');
    var contactBg   = $('.contact.no-bg-slider #background');
    var aspectRatio = 1280/720;
    
	resizeFullScreen();
	if ( $('#slide-1').hasClass('only-one') ) {
		loadThumbs();
	}
	
	window.onorientationchange = function() {
		if ( isMobil() ) {
			resizeFullScreen();
		}
	}	

	$(window).resize(function() {
		if ( !isMobil() ) {
			resizeFullScreen();
		}	
	}); 
	
	function isMobil() {
		if( $('body').hasClass('ipad') || $('body').hasClass('mobile') ) {
			return true;
		} else {
			return false;
		} 
	}
	
	function loadThumbs() {
		var currentThumbs = $('.ngg-gallery-thumbnail-box img');
		var currentThumbsSrc = $('.ngg-gallery-thumbnail-box img').attr('ajax');
		
		if (currentThumbsSrc != '') {
			currentThumbs.each( function(index) {						
				currentThumbsSrc = $(this).attr('ajax');
			
				$(this)
                .load(function() {
                	$(this).fadeTo(500, 1.0);
                })
                .attr('src', currentThumbsSrc);
                
                $(this).attr('ajax', '');  
			});
		}	
	}
	
	function resizeFullScreen() {
	    if( ( $(window).width() / $(window).height() ) < aspectRatio ) {
	        fullScreen.removeClass('bgwidth').addClass('bgheight');
	    } else {
	        fullScreen.removeClass('bgheight').addClass('bgwidth');
	    }
	    
	    var bodyHeight = parseInt($('body').height());
	    contactBg.height(bodyHeight - 31 - 132);

		if ( !isMobil() ) {
		    var bodyWidth = parseInt($('body').width());
		    
		    if  (bodyHeight < bodyWidth / aspectRatio) {
				var newWidth = bodyWidth;
				var newHeight = bodyWidth / aspectRatio;
				var offsetLeft = 0;
				var offsetTop = Math.round((bodyHeight-newHeight) / 2);
			} else { 
				var newWidth = bodyHeight * aspectRatio;
				var newHeight = bodyHeight;
				var offsetLeft = Math.round((bodyWidth-newWidth) / 2);
				var offsetTop = 0;				
			}
			fullScreen.width(newWidth);
			fullScreen.height(newHeight);
			/*
			if (offsetLeft < 0) {
				fullScreen.css({left : offsetLeft});
			} else {
				fullScreen.css({left : 0});					
			}
			if (offsetTop < 0) {
				fullScreen.css({top : offsetTop});
			} else {
				fullScreen.css({top : 0});					
			}
			*/
		}
	}   	
}); 
