/**
 * Grid-A-Licious(tm)
 * Copyright (c) 2008 Suprb - info(at)suprb(dot)com
 *
 * License Agreement: By downloading Grid-A-Licious(tm),
 * you agree to the following: The copyright information 
 * must remain intact in the product.
 *
 * The product may be used for personal use only, no 
 * commercial projects. You are not free to remove the 
 * copyright information (anywhere).
 * 
 * You are not free to use or copy any of the 
 * "grid-a-licious.js" (this file) code on your own products
 * without asking for permission.
 * 
 * Thanks for understanding.
 */

	var MIN_COLS = 2;
	var COL_WIDTH = 200;
	var GAP = 25;
	var boxes = $("#wrapper").children();

	
	var offx, offy = 0;
	maxy = new Array();
	
	// on site load (DOM READY)
	$(function() { 
		offy = $('#allposts').offset().top;
		offx = $('#allposts').offset().left;
		arrange(); 
			   });
	
	
	
	$(function() {
		$('.logogroup').click(function() {
		$('.logos').parent().animate( {opacity: 1.0 }, 500); 
		$('.signage').parent().animate( {opacity: 0.2 }, 500); 
		$('.print').parent().animate( {opacity: 0.2 }, 500);
		$('.interactive').parent().animate( {opacity: 0.2 }, 500);
		
		});
	});
	
	$(function() {
		$('.signagegroup').click(function() {
		$('.signage').parent().animate( {opacity: 1.0 }, 500); 
		$('.logos').parent().animate( {opacity: 0.2 }, 500); 
		$('.print').parent().animate( {opacity: 0.2 }, 500);
		$('.interactive').parent().animate( {opacity: 0.2 }, 500);

		});
	});

	$(function() {
		$('.printgroup').click(function() {
		$('.print').parent().animate( {opacity: 1.0 }, 500); 
		$('.signage').parent().animate( {opacity: 0.2 }, 500); 
		$('.logos').parent().animate( {opacity: 0.2 }, 500);
		$('.interactive').parent().animate( {opacity: 0.2 }, 500);

		});
	});

	$(function() {
		$('.interactivegroup').click(function() {
		$('.interactive').parent().animate( {opacity: 1.0 }, 500); 
		$('.signage').parent().animate( {opacity: 0.2 }, 500); 
		$('.print').parent().animate( {opacity: 0.2 }, 500);
		$('.logos').parent().animate( {opacity: 0.2 }, 500);

		});
	});

	$(function() {
		$('.showall').click(function() {
		$('.signage').parent().animate( {opacity: 1.0 }, 500); 
		$('.print').parent().animate( {opacity: 1.0 }, 500);
		$('.interactive').parent().animate( {opacity: 1.0 }, 500);
		$('.logos').parent().animate( {opacity: 1.0 }, 500);


		});
	});

	$(function() {
		$('.contact').click(function() {
		$('.eachpost').parent().fadeOut(500); 
		$('.hidden').addClass('unhide', 1000);

	$(function() {
		$('.close').click(function() {
		$('.eachpost').parent().fadeIn(500); 
		$('.hidden').fadeOut(500);
		
				 });
			});
		});
	});





	// on window resize, call again
	$(window).resize( function() { arrange(); } );
	
	arrange();
	
	function arrange() {
		
	
		// how many columns fits here?
		var columns = Math.max(MIN_COLS, parseInt($('body').innerWidth() / (COL_WIDTH+GAP)));
		$('.eachpost').css('width',COL_WIDTH  + 'px');
		$('.twocols').css('width', COL_WIDTH*2 + GAP  );
		$('.threecols').css('width', COL_WIDTH*3 + GAP*2);
		$('.fourcols').css('width', COL_WIDTH*4 + GAP*3);


		for (x=0; x < columns; x++) {
			maxy[x] = 0;
		}
		
		// lets iterate over all posts
		$('.eachpost').each(function(i) {

			var pos, cursor, w , altura= 0;
	
			w = (Math.floor($(this).outerWidth() / COL_WIDTH));
			cursor = 0;

			if (w>1) {
				for (x=0; x < columns-(w-1); x++) {
					cursor = maxy[x] < maxy[cursor] ? x : cursor;
				}
				pos = cursor;
				
				for (var x=0; x<w; x++) {
					altura = Math.max(altura, maxy[pos+x]);
				}
				for (var x=0; x<w; x++) 
					maxy[pos+x] = parseInt($(this).outerHeight()) + GAP + altura;
					
				$(this).css('left', pos*(COL_WIDTH+GAP) + offx).css('top',altura + offy);
			}
			else {
			
				for (x=0; x < columns; x++) {
					cursor = maxy[x] < maxy[cursor] ? x : cursor;
				}

				$(this).css('left', cursor*(COL_WIDTH+GAP) + offx).css('top',maxy[cursor] + offy);
				maxy[cursor] += $(this).outerHeight() + GAP;
			}
		});
		
		
	}
	
(function(jQuery) {
jQuery.fn.fadeInSequence = function(fadeInTime, timeBetween)
{
	//Default Values
	timeBetween = typeof(timeBetween) == 'undefined' ? 0 : timeBetween;
	 fadeInTime = typeof(fadeInTime) == 'undefined' ? 500 : fadeInTime;

	//The amount of remaining time until the animation is complete.
	//Initially set to the value of the entire animation duration.
	var remainingTime = jQuery(this).size() * (fadeInTime+timeBetween);

	var i=0; //Counter
	return jQuery(this).each(function()
	{
		//Wait until previous element has finished fading and timeBetween has elapsed
		jQuery(this).delay(i++*(fadeInTime+timeBetween));

		//Decrement remainingTime
		remainingTime -= (fadeInTime+timeBetween);

		if(jQuery(this).css('display') == 'none')
		{
			jQuery(this).fadeIn(fadeInTime);
		}
		else //If hidden by other means such as opacity: 0
		{
			jQuery(this).animate({'opacity' : 1}, fadeInTime);
		}

		//Delay until the animation is over to fill up the queue.
		jQuery(this).delay(remainingTime+timeBetween);

	});	

};

})(jQuery);
	
