/* Pagefunctions for www.avoidredarrows.de */
/* ben@lieblingsempire.org */

	 jQuery(document).ready(function(){
	 
		jQuery('body').removeClass("no-js");
		
		jQuery('#container').isotope({	
			// options
			transformsEnabled: false,
			itemSelector : '.arrange',
			easing: 'none',
			layoutMode : 'masonry',
			masonry: {
				columnWidth : 275
			},

			animationOptions: {
				duration: 0,
				easing: 'linear',
				queue: false
			}
		});
		
		jQuery(".fadeable").hover(
		  function () {
			jQuery(".fadeable").not(this).stop().fadeTo(300, 0.07);
			// no fade needed here, handle implicitely through fading item above or below
			
			var description = jQuery(this).children(".description");
			var imgheight = jQuery(this).height();
			
			gapHeightToWindowBottom = jQuery(window).height() - (jQuery(this).height() + (jQuery(this).offset().top - jQuery(window).scrollTop()));
			distanceToContainerBottom = (jQuery("#container").height() + jQuery("#container").offset().top) - (jQuery(this).height() + jQuery(this).offset().top);
			distanceBetweenContainerAndElementTopPos = (jQuery(this).offset().top - jQuery(window).scrollTop()) - (jQuery("#container").offset().top - jQuery(window).scrollTop());
			
			lastRow = (distanceToContainerBottom < 30);
			firstRow = (distanceBetweenContainerAndElementTopPos == 0);
			notEnoughSpaceBelow = (gapHeightToWindowBottom < (description.height()+25));

			if ( lastRow || (notEnoughSpaceBelow && (!firstRow) ) ){
				var cssoffset = "-" + (description.height() + 25).toString() + "px";
				description.addClass("above");				
				description.css("top",cssoffset);
				}
			else {
				var cssoffset = (imgheight + 25).toString() + "px";
				description.addClass("below");				
				description.css("top", cssoffset);
			}

		  }, 
		  function () {
			jQuery(".fadeable").not(this).stop().fadeTo(300, 1.0);
			var description = jQuery(this).children(".description");
			// be dirty and don't check for the assigned class prelimanry. works like a charm
			description.removeClass("below above");
			//remove offset
			description.css("top","");
		  }
		);		
		
		
	});

