if(typeof(jQuery)!='undefined') jQuery.noConflict();

if(jQuery.jqwidont) {
	jQuery.jqwidont.auto(false);	
}

(function($){

		$(function(){

		/*
		* Get rid of typographic orphans
		*/
		if($.jqwidont) {
			$('p, #marker_list .p, #marker_list .h3, h2, .h2').not('#marker_list .h3').widont();
		}
		
		//$('.columnize').wrapInner('<div class="padding"></div>');
		$('.columnize>*> :header').addClass('dontend');
		$('.columnize li, .columnize .large_image_wrapper, .columnize .image_list_wrapper, .columnize .footnotes, .columnize .detailbox').addClass("dontsplit"); 

		injectImages = function(el) {
		
			$(el).height(null);
			
			var els = $('.column .large_image_wrapper, .column .image_list_wrapper', el).remove();
			
			if(!$('.column.last .padding').length) $('.column.last').append('<div class="padding"></div>');
			$('.column.last .padding', el).prepend(els);// .column.last .padding?

			$('.column .padding>:last-child', el).css('margin-bottom',0);

			$(el).filter('.grid').find('.padding .strip:last-child').css('border-bottom',0).css('padding-bottom',0);			
			$(el).filter('.grid').find('.padding .strip:first-child .h2').css('font-size','200%')
			
			var columnsHeight = $(el).height();
			
			$('.column:not(.last)', el).each(function(){
				var imgHeights = 0;
										$(this).find('img').each(function(){
											//console.log('Adding image height: '+$(this).height());
											imgHeights += $(this).height();
										});
				//console.log('Combined image heights: '+imgHeights);
				var ratio = (columnsHeight-imgHeights)/($(this).height()-imgHeights);
				if(ratio<1.5) $(this).find('p').css('lineHeight',1.5*ratio+'em');
			});

			columnsHeight = $(el).height();
			$('.column', el).css('min-height',columnsHeight);//was .height()
			
			
		}
		
		if($.fn.columnize){
		setTimeout(function(){
			$('.columnize').each(function(){
				var el = this;
				if($(el).find('.padding').children().not('.dontend').length>1 && ($(el).closest('.grid').length != 0 || ($('.image_list_wrapper li',el).length<4 && ($('p, li, :header',el).text().length>(420*$('.image_list_wrapper li:visible',el).length))))) {
				$(el).columnize({
					strictWidth:true, 
					accuracy:100, 
					columns:2,
					target:el,//even though this is the same as the source, it seems to FORCE a columnize every time -> :)
					//width:400,
					lastNeverTallest : $(el).is('.grid'),
					//overflow:{height:300}, 
					doneFunc:function(){injectImages(el);},
					ignoreImageLoading:false
					});
				}
			});
		},0);
		}
		
		if($.browser.msie) {
			$('.buttons li a').corners('3px 3px');
		} else {
			$('.buttons li a').corners();
		}
		//$('.buttons li a').removeClass('rounded_by_jQuery_corners');
		//$('.buttons li a').corners('1px 1px top-left');
		
		
		/*
		* Remove left margin from the first thumb on every row
		*/
		/*
		$(".image_list_wrapper ul").fitFloats({childSelector:'li:visible'})
								   .bind('mouseup',function(){
								   		if($(this).find('span.draghandle').length) {
									   		$(this).find('>li:visible').css('marginRight',null)
													   		   .end()
													   		   .fitFloats({childSelector:'li'});
										}
								   		});
		*/
		
		
		$(":text").labelify({ labelledClass: "labelled" });

		});
})(jQuery);
