/*	Clip a column if it exceeds a specific height
	and add a "more" link to expand the column to its
	full height.
	
	To use, add 'clipable' the class attr of one or more elements.
	
	The more link will not display if the clipable container has position: relative
	
	Requires mootools.js, v1.0
	
	Alex Dunae (multi-up.ca) - March, 2007 */


var clip_height = 340;
var link_padding_top = 10;
var unclip_duration = 1000;


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


var archive_slider;

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>';
			var coltext = $(tag.id).innerHTML;
			$(tag.id).innerHTML = '';
			$(tag.id).innerHTML = more_link + coltext;
			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});
		}
	})


	if(typeof sIFR == "function") sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:"/flash/cracked.swf", sColor:"#961b1e",sWmode:"transparent",nPaddingTop:2, nPaddingRight:0, nPaddingBottom:2, nPaddingLeft:0,sFlashVars:"textalign=left"}));
	archive_slider = new Fx.Slide('archive-links', {duration: 500});
	archive_slider.hide();

}



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');
}


// 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);