var s_saver;

$(document).ready(function() {
	
	//** Cookie tells us in which direction to slide -----------------------------------
	cookieName = "avdeffects";
	cookieVal = getCookie(cookieName);
	
	skipThrough = false;
	
	slideSpeed = 400;
	fadeSpeed = 750;
	
	//** Slide in content div if no images are set instead of SuperBGImage -------------
	if(!$('body').hasClass('project')) {
		
		if(cookieVal == "prev") {
			
			$('#content').css('left', -$('body').width());
			$('#content').animate({left: 0}, slideSpeed);
			
		} else if(cookieVal == "next") {
			
			$('#content').css('left', $('body').width());
			$('#content').animate({left: 0}, slideSpeed);
			
		} else {
			$('#content').hide().fadeIn(fadeSpeed);
		}
	}
	
	//** Configure Background Image options for SuperBGImage ---------------------------
	firstImageToShow = 1;
	initialSlideDirection = 0;
	initialTransition = 90;
	initialOnShow = null;
	
	if(cookieVal == "prev") {
		// Start with last image
		firstImageToShow = $('#projectimages a').length || 1;
		initialSlideDirection = 1;
		
	} else if(cookieVal != "next") {
		// No cookie set (yet): fadein instead of slide
		initialTransition = 1;
		initialOnShow = function() {
			$.fn.superbgimage.options = { transition: 90, onShow: null };
		}
	}
	
	$.fn.superbgimage.options = { transition: initialTransition, speed: slideSpeed, showimage: firstImageToShow, initial_direction: initialSlideDirection, onShow: initialOnShow };
	$('#projectimages').superbgimage();
	
	if(cookieVal == "prev" && $('#projectimages a').length > 1) {
		// skip backwards to first image
		activateSkipThrough(1, "prev", false);
		$('#projectimages').prevSlide();
	}
	
	//** Slide down Navigation tab -----------------------------------------------------
	$('#navtab').mouseenter(function() {
		$('#navcontainer').addClass('over');
		$('#nav').slideDown();
	});
	$('#navcontainer').mouseleave(function(e) {
		
		if(e.relatedTarget != null) {
			$('#nav').slideUp(200, function() {
				$('#navcontainer').removeClass('over');
			});
		}
	});
	
	//** Reset Animation Cookie when Navigation is clicked -----------------------------
	$('#nav a').click(function() {
		setCookie({name: cookieName, value: ""});
	})
	
	//** Slide up Project details tab --------------------------------------------------
	$('#projectdetailstab').mouseenter(function() {
		$('#projectdetailscontainer').addClass('over');
		$('#projectdetails').slideDown();
		
	});
	$('#projectdetailscontainer').mouseleave(function(e) {
		
		if(e.relatedTarget != null) {
			$('#projectdetails').slideUp(200, function() {
				$('#projectdetailscontainer').removeClass('over');
			});
		}
	});
	
	//** Next page ----------------------------------------------------------------------
	$('#next').click(function(e) {
		if(skipThrough) { // button inactive while skipThrough in progress
			return false;
		} else {
			setCookie({name: cookieName, value: "next"});
			if($('#projectimages').length) { // slide project images
				activateSkipThrough($.superbg_imgIndex, "next", e.target.href);
				// avoid "wraparound"
				if($.superbg_imgActual != $.superbg_imgIndex) {
					$('#projectimages').nextSlide();
					return false;
				}
			}
		}
	});
	
	//** Previous page ----------------------------------------------------------------------
	$('#prev').click(function(e) {
		if(skipThrough) { // button inactive while skipThrough in progress
			return false;
		} else {
			setCookie({name: cookieName, value: "prev"});
			if($('#projectimages').length) { // slide project images
				activateSkipThrough(1, "prev", e.target.href);
				// avoid "wraparound"
				if($.superbg_imgActual != 1) {
					$('#projectimages').prevSlide();
					return false;
				}
			}
		}
	});
	
	//** Screensaver ----------------------------------------------------------------------
	// Set timeout directly on document.ready. Otherwise if mouse isn't moved no screensaver starts
	s_saver = setTimeout(function() {
		$('#schweinchen').fadeIn(900);
	}, 20000);
	
	// FadeOut Screensaver and set new timeout after each mousemove or click event
	$('body').mousemove(function() {
		hideSchweinchen();
	});
	$('#schweinchen').click(function() {
		hideSchweinchen();
	});
});

function hideSchweinchen() {
	clearTimeout(s_saver);
	s_saver = setTimeout(function() {
		$('#schweinchen').fadeIn(900);
	}, 20000);
	$('#schweinchen').fadeOut(100);
}

function activateSkipThrough(target, direction, url) {	
	skipThrough = true;
	if(direction == "next") {
		$.fn.superbgimage.options = { onShow: function(imgActual) {
			if(skipThrough && imgActual < target) {
				$('#projectimages').nextSlide();
			} else {
				// Reached target, deactivate!
				skipThrough = false;
				// Load next page
				if(url) {
					window.location.href = url;
				}
			}
		}};
	} else {
		$.fn.superbgimage.options = { onShow: function(imgActual) {
			if(skipThrough && imgActual > target) {
				$('#projectimages').prevSlide();
			} else {
				// Reached target, deactivate!
				skipThrough = false;
				// Load next page
				if(url) {
					window.location.href = url;
				}
			}
		}};
	}
}

function getCookie( cookieName ) {
	var cookies = document.cookie.split(";");
	var cookie;
	for( var i=0; i < cookies.length; i++ ) {
		cookie = cookies[i].split("=");
		try {
			if( cookie[0].trim() == cookieName ) return cookie[1].trim();
		} catch ( e ) { }
	}
}
	
function setCookie( options ) {
	if( !options.name && !options.value ) return false;
	
	var cookieString = options.name + "=" + options.value;
	if( options.expires ) { 
		cookieString += ";expires=" + options.expires.toGMTString();
	}
  	cookieString += ";path=/";
	
	document.cookie = cookieString;
	return true;
}
