/*	Clip a column if it exceeds a specific height
		
	Requires mootools.js, v1.0
	
	Alex Dunae (multi-up.ca) - March, 2007 */

//	Config
var clip_height = 337;
var link_padding_top = 10;
var unclip_duration = 1000;
//	End of config

var clip_actual_height = new Array();
var clip_padding_bottom = new Array();

function showClipped(id) {
	// hide the 'show more' link and slide to clipped area to full height
	$('unclip_' + id).setStyle('visibility', 'hidden');
	var exampleFx = new Fx.Styles(id, {duration: unclip_duration});
	exampleFx.start({'height':[clip_height + 'px',clip_actual_height[id] + 'px']});
	// re-set the original padding
	$(id).setStyle('padding-bottom', clip_padding_bottom[id] + 'px');
}

function init(){
	$$('.clipable').each(function(tag) {
		// save target height and defined padding-bottom
		clip_actual_height[tag.id] = parseInt($(tag.id).getStyle('height'));
		clip_padding_bottom[tag.id] = parseInt($(tag.id).getStyle('padding-bottom'));
		
		if(clip_actual_height[tag.id] > clip_height) {
			$(tag.id).setStyles({'height': clip_height + 'px', 'overflow': 'hidden', 'padding-bottom': '0px'});

			// prepend the 'show more' link
			var more_link = '<a onclick="showClipped(\'' + tag.id + '\');" class="unclip" id="unclip_' + tag.id + '">More</a>';
			$(tag.id).innerHTML = more_link + $(tag.id).innerHTML;
			var link_y = (get_y(tag) + parseInt(tag.offsetHeight) + link_padding_top) + 'px';
			
			$('unclip_' + tag.id).setStyles({'display': 'block', 'position': 'absolute', 'top': link_y, 'cursor': 'pointer', 'z-index': 9999});
		}
	})
}

// from http://blog.firetree.net/2005/07/04/javascript-find-position/
function get_y(obj) {
	var log = '';
	var curtop = 0;
	if(obj.offsetParent) {
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent) break;
			obj = obj.offsetParent;
		}
	} else if(obj.y) curtop += obj.y;
	return curtop;
	
}

window.addEvent('domready', init);