(function ($) {
    $.fn.jTruncate = function (h) {
        var i = {
            length: 300,
            minTrail: 20,
            moreText: "more",
            lessText: "less",
            ellipsisText: "...",
            moreAni: "",
            lessAni: ""
        };
        var h = $.extend(i, h);
        return this.each(function () {
            obj = $(this);
            var a = obj.html();
            if (a.length > h.length + h.minTrail) {
                var b = a.indexOf('.', h.length);  // modified, original was : a.indexOf(' ', h.length);
                if (b != -1) {
                    var b = a.indexOf('.', h.length) + 1; // modified, original was : a.indexOf(' ', h.length);
                    var c = a.substring(0, b);
                    var d = a.substring(b, a.length - 1);
                    obj.html(c + '<span class="truncate_ellipsis">' + h.ellipsisText + '</span>' + '<span class="truncate_more">' + d + '</span>');
                    obj.find('.truncate_more').css("display", "none");
                    obj.append('<div class="clearboth">' + '<a href="#" class="truncate_more_link">' + h.moreText + '</a>' + '</div>');
                    var e = $('.truncate_more_link', obj);
                    var f = $('.truncate_more', obj);
                    var g = $('.truncate_ellipsis', obj);
                    e.click(function () {
                        if (e.text() == h.moreText) {
                            f.show(h.moreAni);
                            e.text(h.lessText);
                            g.css("display", "none")
                        } else {
                            f.hide(h.lessAni);
                            e.text(h.moreText);
                            g.css("display", "inline")
                        }
                        return false
                    })
                }
            }
        })
    }
})(jQuery);
jQuery(document).ready(function(){
	/*
	jQuery(".group-text").each(function(){
		var title = jQuery(this).find(".title");
		var details = jQuery(this).find(".title");
	});
	*/
	jQuery(".group-text .details").jTruncate({
        length: 100,
        minTrail: 0,
        moreText: "Read More",
        lessText: "Read Less",
        ellipsisText: ""
    });
	
	jQuery(".truncate_more_link").toggle(
		function(){
			jQuery(this).parent().parent().parent().css('margin-bottom', '45px');
		},
		function(){
			jQuery(this).parent().parent().parent().css('margin-bottom', '20px');
		}
	);
	
	//jQuery(".view-display-id-page_1").before('<img width="156" height="277" alt="" src="/sites/default/files/feed_image.png" style="float:left">');
	//jQuery(".view-display-id-page_1").css('margin-left', '15px').css('float', 'left').css('width', '458px');
	//jQuery(".view-display-id-page_1").after('<div class="clear"></div>');
	
	
	jQuery('.news-page-container .node-teaser > p').each(function(){
		if( jQuery(this).index() == 0 ){
			console.log(jQuery(this).index());
			console.log(jQuery(this));
			console.log('<h2>'+ jQuery(this).html() +'<h2>');
			jQuery(this).before( '<h2>'+ jQuery(this).html() +'</h2>' ).remove();
		}
	});	
	
	
	if(jQuery.browser.webkit){
		jQuery('.featured.green').css('margin-bottom', 35);
	}
	
	if(jQuery.browser.msie){
		//jQuery('.news-page ul li.news-teaser > div').css('margin-top', -18);
	}
	
	jQuery('.content .field-name-body a[rel="lightbox"]').fancybox();
	//view-biopages
	jQuery('span.bio-summary').each(function(){
		jQuery(this).parent().before(jQuery(this).clone());
		jQuery(this).parent().remove();
	});
	
	jQuery('span.bio-details').each(function(){
		jQuery(this).parent().before(jQuery(this).clone());
		jQuery(this).parent().remove();
	});
	jQuery('span.bio-details').hide();
	jQuery('span.bio-details').after('<div class="bio-more-container"><a href="#" class="bio-more">Read More</a></div>');
	jQuery('.bio-more').toggle(
	function(){
		jQuery(this).parent().prev().show();
		jQuery(this).text('Read less');
	},
	function(){
		jQuery(this).parent().prev().hide();
		jQuery(this).text('Read more');
	});
});
