$(document).ready(function(){
	
	//show up all hidden elements
	$('.js-show').removeClass('js-show');
	$('.js-hide').css('display', 'none');

	bgRatio = parseInt($('#background img').attr('width')) / parseInt($('#background img').attr('height'));

	initBackgroundScaling();
	initScrolling();
	initProductList();
	initGallery();
	setTableStyle();
});

/**
 * initialize scrolling functionality
 *
 */
function initScrolling(){
	$('.scrollable').jScrollPane();
}


/**
 * initialize product-list event handling
 *
 */
function initProductList(){
	$('#product-list-menu').slideUp( 0, $('#product-list-menu').css('visibility', 'visible') );
	$('.product-list-start').css('display', 'block');

	$('.product-list-start').click( function(){
		$('#product-list-menu').slideDown();
		return false;
	});
	
	$('#product-list').hover( function(){
			//$('#product-list-menu').slideDown();
		}, function(){
			$('#product-list-menu').slideUp();
		}
	);
}


/**
* initGalleryThumbHover
*
*/
function initGallery(){
	$('#gallery.smallGallery').fenoGalleryThumbHover( { offsetY:177 } );
	$('#gallery.slide-show').fenoGalleryThumbHover( { offsetY:432 } );
	$('.smallGallery').fenoGallery( { visibleThumbs: 16, isLightbox:true } );
	$('.slide-show').fenoGallery( { visibleThumbs: 44, thumbsWidthOffset:-8, isSlideshow:true } );
}


/**
 * initialize background image scalling
 *
 */
function initBackgroundScaling(){
	$(window).resize(
		function(){
			var height = $(window).height();
			var width = $(window).width();
			var bodyRatio = width/height;
									
			if(bgRatio > 1){
				if( bodyRatio > bgRatio ){
					$('#background img').attr('width', width+20);
					$('#background img').attr('height', (width+20)/bgRatio);
				
				}else{
					$('#background img').attr('height', height);
					$('#background img').attr('width', (height)*bgRatio);				
				}
			}
			if( $(window).height() < $(document).height()) {
				var pos = $('#footer').position();
				$('#background').css('height', (parseInt($('body').height())));
			}else{
				$('#background').css('height', '100%');
			}
		}
	);
	$(window).resize();
}

/* setTableStyle
 *
 */
function setTableStyle() {
	$( "table.contenttable tr" ).each( function() {
        $( "td:last", this ).addClass( "td-last" );
        $( "th:last", this ).addClass( "td-last" );
	});
}
