// JavaScript Document

(function($) {

	jQuery.fn.dropit = function(options) {

		var croppedHeight = null;

		$('.viewMoreBar').click(function() {
			
			var viewBar = $(this);
			viewBar.find("a").toggleClass("open");
			var tabletsWrapper = $(this).closest('.viewAllWrapper').find('.tabletsWrapper').filter(":first");
			
			if(tabletsWrapper.hasClass("cropped") === true) {
				
				if(croppedHeight === null) {
					croppedHeight = tabletsWrapper.height();
				}
				
				var childTablets = tabletsWrapper.find(".tablet");
				var columns = 0;
				var fullHeight = 0;
				
				childTablets.each(function() {
					columns ++;
					if($(this).hasClass("last") === true) {	
						return false;
					}
				});	
				
				var tableHeight = parseInt(childTablets.filter(":first").height() + 10);
				var rows = Math.ceil(childTablets.length / columns);
				var fullHeight = tableHeight * rows;				
				
				tabletsWrapper.animate({height: fullHeight + "px"}, 500, "easeOutBounce", function() {
					tabletsWrapper.removeClass("cropped");
					
				});
				
			}
			else if(croppedHeight !== null) {
			
				tabletsWrapper.animate({height: croppedHeight}, 500, "easeInOutCirc", function() { tabletsWrapper.addClass("cropped"); });
				
			}
			
			return false;
			
										 
		});
							   
	}
	
})(jQuery);
