/*global $, document, window, console */

$(document).ready(function() {
		
		var slides = $('.pic');
		var current_slide = 0;
		var int;
		var desc = $('#loader_desc');
		var loader = $('#loader');
		
		function goto_slide(slide) {
			$(slides[slide]).trigger('click');
			if (slide === 5) {
				slide = -1;
			}
			current_slide = slide;
		}

		function rotate() {
			int = setInterval(function () {
				goto_slide(current_slide + 1);
			}, 6000);
		}
		
		rotate();
		
		$('.pic').click(function(e) {

			e.preventDefault();
			clearInterval(int);
			
			$('#schaufenster_nav').find('li').removeClass('current');
			$(this).parent().addClass('current');
			desc.hide();
			var new_desc = $(this).attr("alt");
			var img_source = $(this).attr("href");
			loader.find('img').remove();
			loader.addClass("loading");
			
			var img = new Image();
		    $(img).load(function () {
	            $(this).hide();
				loader.removeClass("loading").append(this);
	            $(this).fadeIn();
				// change description
				setTimeout( function() {
					desc.text(new_desc).fadeIn();
				}, 400);
			}).error(function () {
	            // notify the user that the image could not be loaded
	        }).attr("src", img_source);
			
			rotate();
		});
				
		
		
		
		if ( $("a.zoom").length ) {
			$("a.zoom").fancybox({
				'overlayShow'	: false,
				'zoomSpeedIn'	: 600,
				'zoomSpeedOut'	: 500,
				'easingIn'		: 'easeOutBack',
				'easingOut'		: 'easeInBack'
			});
		}
	
});

