$('document').ready(function() {

	if ($('#grid').length > 0) {
		$('.cell').hide();
		$('#footer').hide();		
		
		// Get num of cells
		var row = Math.floor($(window).height() / 190);
		var cols = Math.floor($('#grid').width() / 190); 
		var cells = (row*cols) - 2; // descomptem buscador i paginador

		// Load slidegrid contents
		$('#grid').each(function () {
			
			// if AJAX mode
			if ($(this).attr('rel')) {
				$response = $(this);
				$.get($(this).attr('rel')+'/'+(cols)+'/'+cells+'/',{},function(response){ 
		      $response.html(response);
		      fadeGrid($('.slidegrid:first'));
	    	})

				// Pagination
				$('.cell .pagination li a').live('click', function() {
					$('#grid').load(this.href, function() {
						fadeGrid($('.slidegrid:first'), function() {
							$('#footer').fadeIn();
						});
					});	
					return false;
				})

			} else {
				fadeGrid($('.slidegrid:first'), function() {
					$('#footer').fadeIn();
				});				
			}
		});
	}	
	
	// Cell actions
	// Link overlay
	$('.cell a.overlay').colorbox({
		iframe: true,
		width: 760,
		height: 650,
		onOpen: function () {
			var left = '10px';
			// var top = '10px';
			var top = $('#header').height()+8;
			$('#colorbox').css({left: left, top: top, backgroundColor: '#E4E2DE'});
		}
	});
	
	// External links to _blank
	$("a[href^='http:']:not([href*='" + window.location.host + "'][target='_blank'])").live('click', function(){
		$(this).attr('target','_blank');
	});	
	
	// Summary
	$('.summary').summary();
});

function fadeGrid($grid, callback) {
	$grid.masonry({columnWidth: 190, itemSelector: '.cell'}, function() {
		$(this).parent().children('.cell:first').each(function () {
			fadeCell($(this));
		});
	});

	if ($grid.next('.slidegrid') && $grid.children().length > 0) {
		if ($grid.children().length == 1) {
			fadeGrid($grid.next('.slidegrid'), callback);		
		} else {
			$grid.children('.cell:last').focus(function () {
				fadeGrid($grid.next('.slidegrid'), callback);
			});
		}
	} else {
		if (typeof callback == 'function') {
			callback.call();
			return false;
		}
	}
}

function fadeCell($cell) {
	$cell.fadeIn(300, function() {
		if ($(this).next('.cell').length > 0) {
			$(this).next('.cell').focus();
			fadeCell($(this).next('.cell'));
		}
	});
}

/*
truncates elements that pass a certain height.
adds a "view more" link to display the rest of the content.

a different approach than standard truncation which relies on character counting.
character counting may not be desireable when elements have short words, but a 
number of line breaks.

usage:
  //using defaults
  $('css_expression').summary();
  
  //overriding options
  $('css_expression').summary({maxHeight: 200, className: 'view-more'});
*/
(function($) {
  $.fn.summary = function(options) {
	  return this.each(function() {
	    new $.Summary(this, options);
		});
	};

	$.Summary = function(e, o) {
	  var element = $(e);
	  var text = element.find('p');
	  var options = o || {};
    var maxHeight = element.height();
    var textHeight = text.height();
    
    if (textHeight > maxHeight) {
    	var StrippedString = text.html().replace(/(<([^>]+)>)/ig,"");
	    var aText = StrippedString.split(' ');
	    
	    while (text.height() > maxHeight) {
	    	aText.pop();
	    	text.html(aText.join(' '));
	    }
	
	    aText.pop();
			text.html(aText.join(' ')+' ...');
    }
	};
})(jQuery);
