//jQuery closure
(function($) {
	//document ready
	$(document).ready(function() {
		//init mainnav hover
		$('#main-nav>ul>li').hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
		
		//logo hover
		$('#logo img').hover(function(){
			$(this).attr('src',basePath + '/img/schriftzug_hover.png');
		},function(){
			$(this).attr('src',basePath + '/img/schriftzug.png');
		});
		
		//set bg height
		var refreshRate = 1000;
		var $bg = $('#content-bg');
		var $content = $('#content-container');
		var bgTimer;
		
		$bg.bind('set-height',function(){
			//has imgs
			if ($bg.find('img').size() > 0) {
				var height = 0;
				$bg.find('img').each(function(){
					var origWidth = $(this).attr('width');
					var origHeight = $(this).attr('height');
					var newWidth = $('#content-container').width();
					var newHeight = getHeight(origWidth,origHeight,newWidth);
					//set img height
					$(this).attr('width',newWidth);
					$(this).attr('height',newHeight);
					if (newHeight > height) height = newHeight;
					//set bg height
					$bg.animate({height: height},300);
					if ($content.height() < height) $content.animate({height: height},300);
				});
			}
			else {
				//set bg height
				$bg.animate({height: $content.height()},300);
			}
		});
		
		setBg();
		function setBg() {
			$bg.trigger('set-height');
			bgTimer = setTimeout(function(){setBg();},refreshRate);
		}
		
		function getHeight(imgWidth,imgHeight,newWidth) {
			return Math.floor((newWidth*imgHeight)/imgWidth);
		}
		
		//init slider
		$(window).load(function() {
			if ($bg.find('img').size() > 1) {
				$bg.find('.nivoSlider').nivoSlider({
					effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
					slices: 15, // For slice animations
					boxCols: 8, // For box animations
					boxRows: 4, // For box animations
					animSpeed: 1000, // Slide transition speed
					pauseTime: 6000, // How long each slide will show
					startSlide: 0, // Set starting Slide (0 index)
					directionNav: false, // Next & Prev navigation
					directionNavHide: true, // Only show on hover
					controlNav: false, // 1,2,3... navigation
					controlNavThumbs: false, // Use thumbnails for Control Nav
					controlNavThumbsFromRel: false, // Use image rel for thumbs
					controlNavThumbsSearch: '.jpg', // Replace this with...
					controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
					keyboardNav: false, // Use left & right arrows
					pauseOnHover: false, // Stop animation while hovering
					manualAdvance: false, // Force manual transitions
					captionOpacity: 0.8, // Universal caption opacity
					prevText: '', // Prev directionNav text
					nextText: '', // Next directionNav text
					beforeChange: function(){}, // Triggers before a slide transition
					afterChange: function(){}, // Triggers after a slide transition
					slideshowEnd: function(){}, // Triggers after all slides have been shown
					lastSlide: function(){}, // Triggers when last slide is shown
					afterLoad: function(){} // Triggers when slider has loaded
				});
			}
			else if ($bg.find('img').size() == 1) {
				$bg.find('img').show();
			}
		});
		
		//init gallery
		var $tiles = $('#content-container>.photos>.tile:not(.filter)');
		$tiles.each(function(){
			var $tile = $(this);
			var $description = $tile.find('p.description');
			var descrTop = 265;
			//position images
			$tile.find('img').each(function(){
				var offsetX = 0;
				var offsetY = 0;
				var imgWidth = $(this).attr('width');
				var imgHeight = $(this).attr('height');
				
				offsetX = Math.round((260 - imgWidth) / 2);
				offsetY = Math.round((260 - imgHeight) / 2);
				
				$(this).css('left',offsetX);
				$(this).css('top',offsetY);
				
				$description.css('top',descrTop - offsetY);
			});
			
			//hover
			$tile.hover(function(){
				$(this).find('img.color').fadeIn(300);
			},function(){
				$(this).find('img.color').fadeOut(300);
			});
		});
		
		//gallery filter
		$('#content-container>.photos #photo-filter a').click(function(e){
			e.preventDefault();
			
			var tag = $(this).text();
			$tiles.filter(':not(.' + tag + ')').animate({width: 0, marginRight: 0, opacity: 0},300);
			$tiles.filter('.' + tag).animate({width: 260, marginRight: 45, opacity: 1},300);
			
			$(this).closest('ul').find('li').removeClass('active');
			$(this).closest('li').addClass('active');
			
			$bg.trigger('set-height');
		});
		
		
	});
})(jQuery);
